Search in sources :

Example 16 with ContactInfo

use of com.android.dialer.phonenumbercache.ContactInfo in project android_packages_apps_Dialer by LineageOS.

the class ContactInfoCache method getValue.

public ContactInfo getValue(String number, String countryIso, ContactInfo callLogContactInfo, boolean remoteLookupIfNotFoundLocally) {
    NumberWithCountryIso numberCountryIso = new NumberWithCountryIso(number, countryIso);
    ExpirableCache.CachedValue<ContactInfo> cachedInfo = mCache.getCachedValue(numberCountryIso);
    ContactInfo info = cachedInfo == null ? null : cachedInfo.getValue();
    int requestType = remoteLookupIfNotFoundLocally ? ContactInfoRequest.TYPE_LOCAL_AND_REMOTE : ContactInfoRequest.TYPE_LOCAL;
    if (cachedInfo == null) {
        mCache.put(numberCountryIso, ContactInfo.EMPTY);
        // Use the cached contact info from the call log.
        info = callLogContactInfo;
        // The db request should happen on a non-UI thread.
        // Request the contact details immediately since they are currently missing.
        enqueueRequest(number, countryIso, callLogContactInfo, /* immediate */
        true, requestType);
    // We will format the phone number when we make the background request.
    } else {
        if (cachedInfo.isExpired()) {
            // The contact info is no longer up to date, we should request it. However, we
            // do not need to request them immediately.
            enqueueRequest(number, countryIso, callLogContactInfo, /* immediate */
            false, requestType);
        } else if (!callLogInfoMatches(callLogContactInfo, info)) {
            // The call log information does not match the one we have, look it up again.
            // We could simply update the call log directly, but that needs to be done in a
            // background thread, so it is easier to simply request a new lookup, which will, as
            // a side-effect, update the call log.
            enqueueRequest(number, countryIso, callLogContactInfo, /* immediate */
            false, requestType);
        }
        if (Objects.equals(info, ContactInfo.EMPTY)) {
            // Use the cached contact info from the call log.
            info = callLogContactInfo;
        }
    }
    return info;
}
Also used : ExpirableCache(com.android.dialer.util.ExpirableCache) ContactInfo(com.android.dialer.phonenumbercache.ContactInfo)

Example 17 with ContactInfo

use of com.android.dialer.phonenumbercache.ContactInfo in project android_packages_apps_Dialer by LineageOS.

the class RegularSearchListAdapter method getContactInfo.

public CachedContactInfo getContactInfo(CachedNumberLookupService lookupService, int position) {
    ContactInfo info = new ContactInfo();
    CachedContactInfo cacheInfo = lookupService.buildCachedContactInfo(info);
    final Cursor item = (Cursor) getItem(position);
    if (item != null) {
        final DirectoryPartition partition = (DirectoryPartition) getPartition(getPartitionForPosition(position));
        final long directoryId = partition.getDirectoryId();
        final boolean isExtendedDirectory = isExtendedDirectory(directoryId);
        info.name = item.getString(PhoneQuery.DISPLAY_NAME);
        info.type = item.getInt(PhoneQuery.PHONE_TYPE);
        info.label = item.getString(PhoneQuery.PHONE_LABEL);
        info.number = item.getString(PhoneQuery.PHONE_NUMBER);
        final String photoUriStr = item.getString(PhoneQuery.PHOTO_URI);
        info.photoUri = photoUriStr == null ? null : Uri.parse(photoUriStr);
        /*
       * An extended directory is custom directory in the app, but not a directory provided by
       * framework. So it can't be USER_TYPE_WORK.
       *
       * When a search result is selected, RegularSearchFragment calls getContactInfo and
       * cache the resulting @{link ContactInfo} into local db. Set usertype to USER_TYPE_WORK
       * only if it's NOT extended directory id and is enterprise directory.
       */
        info.userType = !isExtendedDirectory && DirectoryCompat.isEnterpriseDirectoryId(directoryId) ? ContactsUtils.USER_TYPE_WORK : ContactsUtils.USER_TYPE_CURRENT;
        cacheInfo.setLookupKey(item.getString(PhoneQuery.LOOKUP_KEY));
        final String sourceName = partition.getLabel();
        if (isExtendedDirectory) {
            cacheInfo.setExtendedSource(sourceName, directoryId);
        } else {
            cacheInfo.setDirectorySource(sourceName, directoryId);
        }
    }
    return cacheInfo;
}
Also used : CachedContactInfo(com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo) DirectoryPartition(com.android.contacts.common.list.DirectoryPartition) ContactInfo(com.android.dialer.phonenumbercache.ContactInfo) CachedContactInfo(com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo) Cursor(android.database.Cursor)

Example 18 with ContactInfo

use of com.android.dialer.phonenumbercache.ContactInfo in project android_packages_apps_Dialer by LineageOS.

the class ContactBuilder method build.

public ContactInfo build() {
    if (mName == null) {
        throw new IllegalStateException("Name has not been set");
    }
    if (mDirectoryType != FORWARD_LOOKUP && mDirectoryType != PEOPLE_LOOKUP && mDirectoryType != REVERSE_LOOKUP) {
        throw new IllegalStateException("Invalid directory type");
    }
    // number differently (eg. without the area code).
    if (mPhoneNumbers.size() == 0) {
        PhoneNumber pn = new PhoneNumber();
        // Use the formatted number where possible
        pn.number = mFormattedNumber != null ? mFormattedNumber : mNormalizedNumber;
        pn.type = Phone.TYPE_MAIN;
        addPhoneNumber(pn);
    }
    try {
        JSONObject contact = new JSONObject();
        // Insert the name
        contact.put(StructuredName.CONTENT_ITEM_TYPE, mName.getJsonObject());
        // Insert phone numbers
        JSONArray phoneNumbers = new JSONArray();
        for (int i = 0; i < mPhoneNumbers.size(); i++) {
            phoneNumbers.put(mPhoneNumbers.get(i).getJsonObject());
        }
        contact.put(Phone.CONTENT_ITEM_TYPE, phoneNumbers);
        // Insert addresses if there are any
        if (mAddresses.size() > 0) {
            JSONArray addresses = new JSONArray();
            for (int i = 0; i < mAddresses.size(); i++) {
                addresses.put(mAddresses.get(i).getJsonObject());
            }
            contact.put(StructuredPostal.CONTENT_ITEM_TYPE, addresses);
        }
        // Insert websites if there are any
        if (mWebsites.size() > 0) {
            JSONArray websites = new JSONArray();
            for (int i = 0; i < mWebsites.size(); i++) {
                websites.put(mWebsites.get(i).getJsonObject());
            }
            contact.put(Website.CONTENT_ITEM_TYPE, websites);
        }
        ContactInfo info = new ContactInfo();
        info.name = mName.displayName;
        info.normalizedNumber = mNormalizedNumber;
        info.number = mPhoneNumbers.get(0).number;
        info.type = mPhoneNumbers.get(0).type;
        info.label = mPhoneNumbers.get(0).label;
        info.photoUri = mPhotoUri != null ? mPhotoUri : null;
        String json = new JSONObject().put(Contacts.DISPLAY_NAME, mName.displayName).put(Contacts.DISPLAY_NAME_SOURCE, mDisplayNameSource).put(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_ANY_ACCOUNT).put(Contacts.CONTENT_ITEM_TYPE, contact).toString();
        if (json != null) {
            long directoryId = -1;
            switch(mDirectoryType) {
                case FORWARD_LOOKUP:
                    directoryId = DirectoryId.NEARBY;
                    break;
                case PEOPLE_LOOKUP:
                    directoryId = DirectoryId.PEOPLE;
                    break;
                case REVERSE_LOOKUP:
                    // use null directory to be backwards compatible with old code
                    directoryId = DirectoryId.NULL;
                    break;
            }
            info.lookupUri = Contacts.CONTENT_LOOKUP_URI.buildUpon().appendPath(Constants.LOOKUP_URI_ENCODED).appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)).encodedFragment(json).build();
        }
        return info;
    } catch (JSONException e) {
        Log.e(TAG, "Failed to build contact", e);
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ContactInfo(com.android.dialer.phonenumbercache.ContactInfo)

Example 19 with ContactInfo

use of com.android.dialer.phonenumbercache.ContactInfo in project android_packages_apps_Dialer by LineageOS.

the class LookupCache method getCachedContact.

public static ContactInfo getCachedContact(Context context, String number) {
    String normalizedNumber = formatE164(context, number);
    if (normalizedNumber == null) {
        return null;
    }
    File file = getFilePath(context, normalizedNumber);
    if (!file.exists()) {
        // Whatever is calling this should probably check anyway
        return null;
    }
    ContactInfo info = new ContactInfo();
    FileInputStream in = null;
    JsonReader reader = null;
    try {
        in = new FileInputStream(file);
        reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (NAME.equals(name)) {
                info.name = reader.nextString();
            } else if (TYPE.equals(name)) {
                info.type = reader.nextInt();
            } else if (LABEL.equals(name)) {
                info.label = reader.nextString();
            } else if (NUMBER.equals(name)) {
                info.number = reader.nextString();
            } else if (FORMATTED_NUMBER.equals(name)) {
                info.formattedNumber = reader.nextString();
            } else if (NORMALIZED_NUMBER.equals(name)) {
                info.normalizedNumber = reader.nextString();
            } else if (PHOTO_ID.equals(name)) {
                info.photoId = reader.nextInt();
            } else if (LOOKUP_URI.equals(name)) {
                Uri lookupUri = Uri.parse(reader.nextString());
                if (hasCachedImage(context, normalizedNumber)) {
                    // Insert cached photo URI
                    Uri image = Uri.withAppendedPath(LookupProvider.IMAGE_CACHE_URI, Uri.encode(normalizedNumber));
                    String json = lookupUri.getEncodedFragment();
                    if (json != null) {
                        try {
                            JSONObject jsonObj = new JSONObject(json);
                            jsonObj.putOpt(Contacts.PHOTO_URI, image.toString());
                            lookupUri = lookupUri.buildUpon().encodedFragment(jsonObj.toString()).build();
                        } catch (JSONException e) {
                            Log.e(TAG, "Failed to add image URI to json", e);
                        }
                    }
                    info.photoUri = image;
                }
                info.lookupUri = lookupUri;
            }
        }
        reader.endObject();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        DialerUtils.closeQuietly(reader);
        DialerUtils.closeQuietly(in);
    }
    return info;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) JsonReader(android.util.JsonReader) JSONException(org.json.JSONException) ContactInfo(com.android.dialer.phonenumbercache.ContactInfo) IOException(java.io.IOException) File(java.io.File) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream)

Aggregations

ContactInfo (com.android.dialer.phonenumbercache.ContactInfo)19 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)4 Notification (android.app.Notification)3 Cursor (android.database.Cursor)3 Bitmap (android.graphics.Bitmap)3 StatusBarNotification (android.service.notification.StatusBarNotification)3 WorkerThread (android.support.annotation.WorkerThread)3 ContactPhotoLoader (com.android.dialer.app.contactinfo.ContactPhotoLoader)3 ContactBuilder (com.android.dialer.lookup.ContactBuilder)3 CachedContactInfo (com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo)3 ArrayList (java.util.ArrayList)3 JSONArray (org.json.JSONArray)3 Builder (android.app.Notification.Builder)2 Uri (android.net.Uri)2 PhoneAccountHandle (android.telecom.PhoneAccountHandle)2 NewCall (com.android.dialer.app.calllog.CallLogNotificationsQueryHelper.NewCall)2 NotificationManager (android.app.NotificationManager)1 MatrixCursor (android.database.MatrixCursor)1 MainThread (android.support.annotation.MainThread)1