示例#1
0
  public ArrayList<Pojo> getResults(String query) {
    query = StringNormalizer.normalize(query);

    ArrayList<Pojo> results = new ArrayList<>();

    for (AliasPojo entry : pojos) {
      if (entry.alias.startsWith(query)) {
        // Retrieve the AppPojo from AppProvider, being careful not to create any side effect
        // (default behavior is to alter displayName, which is not what we want)
        Pojo appPojo = appProvider.findById(entry.app, false);
        // Only add if default AppProvider is not already displaying it
        if (appPojo != null && !appPojo.nameNormalized.contains(query)) {
          appPojo.displayName =
              appPojo.name
                  + " <small>("
                  + entry.alias.replaceFirst("(?i)(" + Pattern.quote(query) + ")", "{$1}")
                  + ")</small>";
          appPojo.relevance = 10;
          results.add(appPojo);
        }
      }
    }

    return results;
  }
示例#2
0
  @Override
  protected ArrayList<ContactPojo> doInBackground(Void... params) {
    Pattern homePattern = Pattern.compile("^\\+33\\s?[1-5]");

    long start = System.nanoTime();

    // Run query
    Cursor cur =
        context
            .getContentResolver()
            .query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[] {
                  ContactsContract.Contacts.LOOKUP_KEY,
                  ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED,
                  ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                  ContactsContract.CommonDataKinds.Phone.NUMBER,
                  ContactsContract.CommonDataKinds.Phone.STARRED,
                  ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY,
                  ContactsContract.Contacts.PHOTO_ID
                },
                null,
                null,
                null);

    // Prevent duplicates by keeping in memory encountered phones.
    // The string key is "phone" + "|" + "name" (so if two contacts
    // with distinct name share same number, they both get displayed)
    HashMap<String, ArrayList<ContactPojo>> mapContacts = new HashMap<>();

    if (cur.getCount() > 0) {
      while (cur.moveToNext()) {
        ContactPojo contact = new ContactPojo();

        contact.lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
        contact.timesContacted =
            Integer.parseInt(
                cur.getString(
                    cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED)));
        contact.name =
            cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        contact.phone =
            cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        contact.homeNumber = homePattern.matcher(contact.phone).lookingAt();

        contact.starred =
            cur.getInt(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED)) != 0;
        contact.primary =
            cur.getInt(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY))
                != 0;
        String photoId = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
        if (photoId != null) {
          contact.icon =
              ContentUris.withAppendedId(
                  ContactsContract.Data.CONTENT_URI, Long.parseLong(photoId));
        }

        contact.id = pojoScheme + contact.lookupKey + contact.phone;

        if (contact.name != null) {
          contact.nameLowerCased = StringNormalizer.normalize(contact.name);

          if (mapContacts.containsKey(contact.lookupKey))
            mapContacts.get(contact.lookupKey).add(contact);
          else {
            ArrayList<ContactPojo> phones = new ArrayList<>();
            phones.add(contact);
            mapContacts.put(contact.lookupKey, phones);
          }
        }
      }
    }
    cur.close();
    ArrayList<ContactPojo> contacts = new ArrayList<>();
    for (ArrayList<ContactPojo> phones : mapContacts.values()) {
      // Find primary phone and add this one.
      Boolean hasPrimary = false;
      for (int j = 0; j < phones.size(); j++) {
        ContactPojo contact = phones.get(j);
        if (contact.primary) {
          contacts.add(contact);
          hasPrimary = true;
          break;
        }
      }

      // If not available, add all (excluding duplicates).
      if (!hasPrimary) {
        HashMap<String, Boolean> added = new HashMap<>();
        for (int j = 0; j < phones.size(); j++) {
          String uniqueKey = phones.get(j).phone.replaceAll("[ \\.\\(\\)]", "");
          uniqueKey = uniqueKey.replaceAll("^\\+33", "0");
          uniqueKey = uniqueKey.replaceAll("^\\+1", "0");
          if (!added.containsKey(uniqueKey)) {
            added.put(uniqueKey, true);
            contacts.add(phones.get(j));
          }
        }
      }
    }
    long end = System.nanoTime();
    Log.i("time", Long.toString((end - start) / 1000000) + " milliseconds to list contacts");
    return contacts;
  }