diff options
author | Raj Yengisetty <raj@cyngn.com> | 2016-02-26 15:32:33 -0800 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyanogenmod.org> | 2016-03-07 07:32:49 -0800 |
commit | 4d67195ade2d7fddb59c07bdf7eae000848239e6 (patch) | |
tree | 52ef26da5e7690076eaab8ed5498f3aa482c70c5 /info_lookup | |
parent | 463be6a1088c8a3259d618ac67884a74ae8c2d8a (diff) | |
download | android_packages_apps_ContactsCommon-4d67195ade2d7fddb59c07bdf7eae000848239e6.tar.gz android_packages_apps_ContactsCommon-4d67195ade2d7fddb59c07bdf7eae000848239e6.tar.bz2 android_packages_apps_ContactsCommon-4d67195ade2d7fddb59c07bdf7eae000848239e6.zip |
Update info_lookup to use AmbientSDK directly
This was previously handled by an abstraction
Change-Id: Icf92583f391338875ad542704810837e22c22693
Diffstat (limited to 'info_lookup')
3 files changed, 265 insertions, 17 deletions
diff --git a/info_lookup/phonenumber_lookup_provider.mk b/info_lookup/phonenumber_lookup_provider.mk index 02c423a5..cbaf2c2f 100644 --- a/info_lookup/phonenumber_lookup_provider.mk +++ b/info_lookup/phonenumber_lookup_provider.mk @@ -1,2 +1,9 @@ CONTACTS_COMMON_PHONE_NUMBER_LOOKUP := ../../../packages/apps/ContactsCommon/info_lookup/src/com/cyanogen/lookup/phonenumber -LOCAL_SRC_FILES += $(call all-java-files-under, $(CONTACTS_COMMON_PHONE_NUMBER_LOOKUP))
\ No newline at end of file +LOCAL_SRC_FILES += $(call all-java-files-under, $(CONTACTS_COMMON_PHONE_NUMBER_LOOKUP)) + +LOCAL_STATIC_JAVA_AAR_LIBRARIES += \ + ambientsdk + +LOCAL_AAPT_FLAGS += \ + --auto-add-overlay \ + --extra-packages com.cyanogen.ambient diff --git a/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/AmbientConnectionManager.java b/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/AmbientConnectionManager.java new file mode 100644 index 00000000..af424e4a --- /dev/null +++ b/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/AmbientConnectionManager.java @@ -0,0 +1,92 @@ +package com.cyanogen.lookup.phonenumber.provider; + +import android.content.Context; +import android.os.Bundle; +import android.util.Log; + +import com.cyanogen.ambient.callerinfo.util.CallerInfoHelper; +import com.cyanogen.ambient.callerinfo.CallerInfoServices; +import com.cyanogen.ambient.common.CyanogenAmbientUtil; +import com.cyanogen.ambient.common.api.AmbientApiClient; +import com.cyanogen.ambient.common.api.AmbientApiClient.OnDisconnectionListener; +import com.cyanogen.ambient.common.api.AmbientApiClient.OnConnectionFailedListener; +import com.cyanogen.ambient.common.api.AmbientApiClient.ConnectionCallbacks; +import com.cyanogen.ambient.common.ConnectionResult; + +public class AmbientConnectionManager { + + private static final String TAG = "AmbientConnectionManager"; + private static AmbientApiClient sClient; + private static int mActiveSubscribers = 0; + + public static synchronized AmbientApiClient requestClient(Context context) { + if (sClient == null) { + sClient = + new AmbientApiClient.Builder(context.getApplicationContext()) + .addApi(CallerInfoServices.API).build(); + sClient.registerConnectionFailedListener( + new OnConnectionFailedListener() { + @Override + public void onConnectionFailed(ConnectionResult result) { + Log.w(TAG, "CallerInfo connection failed: " + result); + } + }); + sClient.registerDisconnectionListener( + new OnDisconnectionListener() { + @Override + public void onDisconnection() { + Log.d(TAG, "CallerInfo connection disconnected"); + } + }); + sClient.registerConnectionCallbacks( + new ConnectionCallbacks() { + @Override + public void onConnected(Bundle connectionHint) { + Log.d(TAG, "CallerInfo connection established"); + } + + @Override + public void onConnectionSuspended(int cause) { + Log.d(TAG, "CallerInfo connection suspended"); + } + }); + } + + if (!(sClient.isConnecting() || sClient.isConnected())) { + sClient.connect(); + } + + ++ mActiveSubscribers; + return sClient; + } + + /** + * Convenience method to access the AmbientApiClient instance after creating it + * via {@link #requestClient(Context)} + * + * This method should only be used *after* ensuring that {@link #requestClient(Context)} has + * been called.{@link #discardClient()} should be called in the appropriate place to manage + * the lifecycle of the AmbientApiClient according to the lifecycle of components + * making use of it. + */ + public static AmbientApiClient getClient() { + return sClient; + } + + public static synchronized void discardClient() { + -- mActiveSubscribers; + if (mActiveSubscribers == 0) { + // disconnect from AmbientClient when the last subscriber disconnects + sClient.disconnect(); + } + } + + public static boolean isAvailable(Context context) { + boolean ambientAvailable = CyanogenAmbientUtil.isCyanogenAmbientAvailable(context) == CyanogenAmbientUtil.SUCCESS; + boolean activeProvider = CallerInfoHelper.hasActiveProvider(context); + Log.d(TAG, "ambientAvailable=" + ambientAvailable + + ", activeProvider=" + activeProvider); + return ambientAvailable && activeProvider; + } + +}
\ No newline at end of file diff --git a/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/LookupProviderImpl.java b/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/LookupProviderImpl.java index 720132f3..ccd78fd3 100644 --- a/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/LookupProviderImpl.java +++ b/info_lookup/src/com/cyanogen/lookup/phonenumber/provider/LookupProviderImpl.java @@ -1,61 +1,210 @@ package com.cyanogen.lookup.phonenumber.provider; import android.content.Context; + +import android.database.ContentObserver; +import android.net.Uri; import android.os.Handler; +import android.os.Looper; +import android.provider.Settings; +import android.text.TextUtils; +import android.util.Log; +import com.cyanogen.ambient.callerinfo.CallerInfoServices; +import com.cyanogen.ambient.callerinfo.extension.CallerInfo; +import com.cyanogen.ambient.callerinfo.results.LookupByNumberResult; +import com.cyanogen.ambient.callerinfo.util.CallerInfoHelper; +import com.cyanogen.ambient.callerinfo.util.ProviderInfo; +import com.cyanogen.ambient.common.CyanogenAmbientUtil; +import com.cyanogen.ambient.common.api.AmbientApiClient; +import com.cyanogen.ambient.common.api.PendingResult; +import com.cyanogen.ambient.common.api.Result; +import com.cyanogen.ambient.common.api.ResultCallback; +import com.cyanogen.ambient.common.api.Status; import com.cyanogen.lookup.phonenumber.contract.LookupProvider; import com.cyanogen.lookup.phonenumber.request.LookupRequest; +import com.cyanogen.lookup.phonenumber.response.LookupResponse; /** - * Shell implementation of a phonenumber LookupProvider + * @author Rohit Yengisetty */ public class LookupProviderImpl implements LookupProvider { + private static final String PROVIDER_KEY = "ambient_callerinfo_provider_name"; + private static final String TAG = LookupProviderImpl.class.getSimpleName(); + + private Context mContext; + private AmbientApiClient mAmbientClient; + private ProviderInfo mProviderInfo; + private ContentObserver mProviderObserver; + private Handler mHandler; + private String mCurrentProviderName; + public LookupProviderImpl(Context context) { - /* NOT IMPLEMENTED */ + mContext = context; + mHandler = new Handler(Looper.getMainLooper()); } @Override public boolean initialize() { - /* NOT a valid implementation */ + if (isEnabled()) { + mAmbientClient = AmbientConnectionManager.requestClient(mContext); + mProviderInfo = CallerInfoHelper.getActiveProviderInfo(mContext); + mCurrentProviderName = Settings.Secure.getString(mContext.getContentResolver(), + PROVIDER_KEY); + + // update provider info on caller info provider changes + mProviderObserver = new ContentObserver(mHandler) { + @Override + public boolean deliverSelfNotifications() { + return false; + } + + @Override + public void onChange(boolean selfChange) { + onChange(selfChange, null); + } + + @Override + public void onChange(boolean selfChange, Uri uri) { + if (uri != null) { + mProviderInfo = CallerInfoHelper.getActiveProviderInfo(mContext); + } + } + }; + Uri providerInfoUri = Settings.Secure.getUriFor(PROVIDER_KEY); + mContext.getContentResolver().registerContentObserver(providerInfoUri, false, + mProviderObserver); + return true; + } + return false; } @Override - public void fetchInfo(final LookupRequest request) { - /* NOT IMPLEMENTED */ + public boolean isEnabled() { + return AmbientConnectionManager.isAvailable(mContext); } @Override - public void disable() { - /* NOT IMPLEMENTED */ + public void fetchInfo(final LookupRequest request) { + String number = request.mPhoneNumber; + if (!TextUtils.isEmpty(number) && mAmbientClient != null && + (mAmbientClient.isConnecting() || mAmbientClient.isConnected())) { + + com.cyanogen.ambient.callerinfo.extension.LookupRequest ambientRequest = + new com.cyanogen.ambient.callerinfo.extension.LookupRequest(number, + com.cyanogen.ambient.callerinfo.extension.LookupRequest.ORIGIN_CODE_HISTORY); + PendingResult<LookupByNumberResult> result = CallerInfoServices.CallerInfoApi. + lookupByNumber(mAmbientClient, ambientRequest); + + result.setResultCallback(new ResultCallback<LookupByNumberResult>() { + @Override + public void onResult(LookupByNumberResult lookupByNumberResult) { + + if (!lookupByNumberResult.getStatus().isSuccess()) { + return; + } + CallerInfo callerInfo = lookupByNumberResult.getCallerInfo(); + + if (!hasUsableInfo(callerInfo)) { + return; + } + + // map caller info to LookupResponse + LookupResponse lookupResponse = new LookupResponse(); + lookupResponse.mProviderName = mProviderInfo.getTitle(); + lookupResponse.mName = callerInfo.getName(); + lookupResponse.mNumber = callerInfo.getNumber(); + lookupResponse.mAddress = callerInfo.getAddress(); + lookupResponse.mPhotoUrl = callerInfo.getPhotoUrl(); + lookupResponse.mAttributionLogo = mProviderInfo.getBadgeLogo(); + lookupResponse.mSpamCount = callerInfo.getSpamCount(); + + request.mCallback.onNewInfo(request, lookupResponse); + } + }); + } + } + + private boolean hasUsableInfo(CallerInfo callerInfo) { + return (callerInfo != null && + (!TextUtils.isEmpty(callerInfo.getName()) || callerInfo.getSpamCount() > 0)); } @Override - public boolean isEnabled() { - /* NOT a valid implementation */ - return false; + public void disable() { + if(mAmbientClient != null) { + AmbientConnectionManager.discardClient(); + } + mContext.getContentResolver().unregisterContentObserver(mProviderObserver); } @Override public void markAsSpam(String phoneNumber) { - /* NOT IMPLEMENTED */ + if (TextUtils.isEmpty(phoneNumber)) { + return; + } + if (!supportsSpamReporting()) { + return; + } + if (mAmbientClient != null && + (mAmbientClient.isConnecting() || mAmbientClient.isConnected())) { + PendingResult<Result> result = + CallerInfoServices.CallerInfoApi.markAsSpam(mAmbientClient, phoneNumber); + result.setResultCallback(new ResultCallback<Result>() { + @Override + public void onResult(Result result) { + Status status = result.getStatus(); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(LookupProviderImpl.class.getSimpleName(), + "Status: " + status.getStatusMessage()); + } + } + }); + } } @Override public void unmarkAsSpam(String phoneNumber) { - /* NOT IMPLEMENTED */ + if (TextUtils.isEmpty(phoneNumber)) { + return; + } + if (!supportsSpamReporting()) { + return; + } + if (mAmbientClient != null && + (mAmbientClient.isConnecting() || mAmbientClient.isConnected())) { + PendingResult<Result> result = + CallerInfoServices.CallerInfoApi.unMarkAsSpam(mAmbientClient, phoneNumber); + result.setResultCallback(new ResultCallback<Result>() { + @Override + public void onResult(Result result) { + Status status = result.getStatus(); + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(LookupProviderImpl.class.getSimpleName(), + "Status: " + status.getStatusMessage()); + } + } + }); + } } @Override public boolean supportsSpamReporting() { - /* NOT a valid implementation */ - return false; + ProviderInfo providerInfo = CallerInfoHelper.getActiveProviderInfo(mContext); + return providerInfo != null && + providerInfo.hasProperty(ProviderInfo.PROPERTY_SUPPORTS_SPAM); } @Override public String getDisplayName() { - /* NOT a valid implementation */ - return null; + String provider = null; + ProviderInfo providerInfo = CallerInfoHelper.getActiveProviderInfo(mContext); + if (CyanogenAmbientUtil.isCyanogenAmbientAvailable(mContext) == CyanogenAmbientUtil + .SUCCESS && providerInfo != null) { + provider = providerInfo.getTitle(); + } + return provider; } } |