diff options
author | Rohit Yengisetty <rohit@cyngn.com> | 2016-01-06 21:58:38 -0800 |
---|---|---|
committer | Rohit Yengisetty <rohit@cyngn.com> | 2016-01-12 22:36:37 -0800 |
commit | bbf7e8276d72c2e524c13cf659c64e473385265e (patch) | |
tree | 73b3e0166526a1c43452d2e2bb2b01caa5ea5718 /src | |
parent | b5b16f7a9c3d298b6473c5056c40299dbc7235a7 (diff) | |
download | android_packages_apps_ContactsCommon-bbf7e8276d72c2e524c13cf659c64e473385265e.tar.gz android_packages_apps_ContactsCommon-bbf7e8276d72c2e524c13cf659c64e473385265e.tar.bz2 android_packages_apps_ContactsCommon-bbf7e8276d72c2e524c13cf659c64e473385265e.zip |
Refactor contact info lookup
Change-Id: Ie926475b31360455b7cdf2d653b58bec9579f736
Diffstat (limited to 'src')
4 files changed, 254 insertions, 0 deletions
diff --git a/src/com/cyanogen/lookup/phonenumber/contract/LookupProvider.java b/src/com/cyanogen/lookup/phonenumber/contract/LookupProvider.java new file mode 100644 index 00000000..860dadff --- /dev/null +++ b/src/com/cyanogen/lookup/phonenumber/contract/LookupProvider.java @@ -0,0 +1,61 @@ +package com.cyanogen.lookup.phonenumber.contract; + +import com.cyanogen.lookup.phonenumber.request.LookupRequest; + +/** + * Notion of a phone number lookup provider + */ +public interface LookupProvider { + + /** + * Explicit call to the provider to initialize itself. Decoupling it from provider construction + * to enable explicit setup and tear down based on resource constraints. + */ + boolean initialize(); + + /** + * Returns true if the provider is installed and enabled + */ + boolean isEnabled(); + + /** + * Request to lookup contact info asynchronously. The callback is embedded + * within {@link LookupRequest} + */ + void fetchInfo(LookupRequest request); + + /** + * Explicit call to disable provider and free resources + */ + void disable(); + + /** + * flag a phone number as spam + * + * @param phoneNumber {@link String} + */ + void markAsSpam(String phoneNumber); + + /** + * un-flag a phone number as spam + * + * @param phoneNumber {@link String} + */ + void unmarkAsSpam(String phoneNumber); + + /** + * Check if the current provider supports spam reporting + * + * @return {@link Boolean} <code>true</code> if available, <code>false</code> if not + */ + boolean supportsSpamReporting(); + + /** + * Returns the name of the current provider + * + * @return {@link String} + */ + String getDisplayName(); + +} + diff --git a/src/com/cyanogen/lookup/phonenumber/request/LookupRequest.java b/src/com/cyanogen/lookup/phonenumber/request/LookupRequest.java new file mode 100644 index 00000000..e160bbb6 --- /dev/null +++ b/src/com/cyanogen/lookup/phonenumber/request/LookupRequest.java @@ -0,0 +1,25 @@ +package com.cyanogen.lookup.phonenumber.request; + +import com.cyanogen.lookup.phonenumber.response.LookupResponse; + +/** + * Encapsulates the notion of a phone number lookup request + */ +public class LookupRequest { + public String mPhoneNumber; + public Callback mCallback; + + public LookupRequest(String phoneNumber, Callback callback) { + mPhoneNumber = phoneNumber; + mCallback = callback; + } + + @Override + public int hashCode() { + return mPhoneNumber.hashCode(); + } + + public interface Callback { + void onNewInfo(LookupRequest lookupRequest, LookupResponse response); + } +} diff --git a/src/com/cyanogen/lookup/phonenumber/response/LookupResponse.java b/src/com/cyanogen/lookup/phonenumber/response/LookupResponse.java new file mode 100644 index 00000000..1cb51c2b --- /dev/null +++ b/src/com/cyanogen/lookup/phonenumber/response/LookupResponse.java @@ -0,0 +1,32 @@ +package com.cyanogen.lookup.phonenumber.response; + +import android.graphics.drawable.Drawable; + +/** + * ADT to store the result of a phone number lookup + */ +public class LookupResponse { + public String mProviderName; + public String mName; + public String mNumber; + public String mCity; + public String mCountry; + public String mAddress; + public String mPhotoUrl; + public int mSpamCount; + + public Drawable mAttributionLogo; + + @Override + public String toString() { + return String.format("{ providerName = %s, name = %s, number = %s, city = %s, country = %s, address = %s, photo-url : %s, spam-count = %d}", + mProviderName != null ? mProviderName : "null" , + mName != null ? mName : "null" , + mNumber != null ? mNumber : "null" , + mCity != null ? mCity : "null" , + mCountry != null ? mCountry : "null" , + mAddress != null ? mAddress : "null" , + mPhotoUrl != null ? mPhotoUrl : "null" , + mSpamCount ); + } +}
\ No newline at end of file diff --git a/src/com/cyanogen/lookup/phonenumber/util/LookupHandlerThread.java b/src/com/cyanogen/lookup/phonenumber/util/LookupHandlerThread.java new file mode 100644 index 00000000..f8380569 --- /dev/null +++ b/src/com/cyanogen/lookup/phonenumber/util/LookupHandlerThread.java @@ -0,0 +1,136 @@ +package com.cyanogen.lookup.phonenumber.util; + +import android.content.Context; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Message; +import android.text.TextUtils; +import android.util.Log; +import com.cyanogen.lookup.phonenumber.contract.LookupProvider; +import com.cyanogen.lookup.phonenumber.request.LookupRequest; + +import java.util.HashSet; + +public class LookupHandlerThread extends HandlerThread implements Handler.Callback { + + private static final int MSG_FETCH_INFO = 0; + private static final int MSG_MARK_AS_SPAM = 1; + private static final String TAG = LookupHandlerThread.class.getSimpleName(); + + private Context mContext; + private Handler mHandler; + private LookupProvider mLookupProvider; + private HashSet<LookupRequest> mSubmittedRequests; + private boolean mInitialized = false; + + public LookupHandlerThread(String threadName, Context ctx, LookupProvider lookupProvider) { + super(threadName); + mContext = ctx; + mLookupProvider = lookupProvider; + } + + public LookupHandlerThread(String name, int priority, Context ctx, + LookupProvider lookupProvider) { + super(name, priority); + mContext = ctx; + mLookupProvider = lookupProvider; + } + + public boolean initialize() { + if (mInitialized) { + return true; + } + + mInitialized = mLookupProvider.initialize(); + if (mInitialized) { + mSubmittedRequests = new HashSet<>(); + start(); + mHandler = new Handler(getLooper(), this); + } else { + Log.w(TAG, "Failed to initialize!"); + } + + return mInitialized; + } + + public boolean isProviderEnabled() { + return mLookupProvider.isEnabled(); + } + + public void tearDown() { + if (mInitialized) { + quit(); + mLookupProvider.disable(); + mInitialized = false; + } + } + + public boolean fetchInfoForPhoneNumber(LookupRequest lookupRequest) { + if (!mSubmittedRequests.contains(lookupRequest)) { + Message msg = mHandler.obtainMessage(MSG_FETCH_INFO); + msg.obj = lookupRequest; + boolean requested = mHandler.sendMessage(msg); + if (requested) { + mSubmittedRequests.add(lookupRequest); + } + return requested; + } + + return false; + } + + /** + * Posts a message to {@link #mHandler} which later dispatches a request to the provider + * implementation that knows how to mark a phone number as spam + * + * @param phoneNumber {@link String} + */ + public void markAsSpam(String phoneNumber) { + if (TextUtils.isEmpty(phoneNumber)) { + return; + } + if (mHandler != null) { + Message msg = mHandler.obtainMessage(MSG_MARK_AS_SPAM); + msg.obj = phoneNumber; + mHandler.sendMessage(msg); + } else { + Log.w(TAG, "No handler!"); + } + } + + /** + * Check if the provider supports spam reporting + * + * @return {@link Boolean} + */ + public boolean isProviderInterestedInSpam() { + return mLookupProvider.supportsSpamReporting(); + } + + /** + * Get the display name of the provider + * + * @return {@link String} + */ + public String getProviderName() { + return mLookupProvider.getDisplayName(); + } + + @Override + public boolean handleMessage(Message msg) { + int what = msg.what; + switch (what) { + case MSG_FETCH_INFO : + if (mInitialized) { + mLookupProvider.fetchInfo((LookupRequest) msg.obj); + } + break; + case MSG_MARK_AS_SPAM : + if (mInitialized) { + mLookupProvider.markAsSpam((String) msg.obj); + } + break; + } + return true; + } +} |