diff options
Diffstat (limited to 'src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java')
-rw-r--r-- | src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java | 121 |
1 files changed, 102 insertions, 19 deletions
diff --git a/src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java b/src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java index 971308ca3..257d9ba59 100644 --- a/src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java +++ b/src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java @@ -21,6 +21,7 @@ import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.provider.CallLog; +import android.util.ArrayMap; import com.android.phone.common.ambient.AmbientConnection; import com.cyanogen.ambient.analytics.AnalyticsServices; @@ -44,6 +45,9 @@ import java.util.List; public class DeepLinkIntegrationManager { + ArrayMap<Object, PendingResult<DeepLink.BooleanResult>> mPendingResultArrayMap = + new ArrayMap<Object, PendingResult<DeepLink.BooleanResult>>(); + public static DeepLinkIntegrationManager getInstance() { if (sInstance == null) { sInstance = new DeepLinkIntegrationManager(); @@ -56,19 +60,32 @@ public class DeepLinkIntegrationManager { private static DeepLinkIntegrationManager sInstance; private AmbientApiClient mAmbientApiClient; private DeepLinkApi mApi; + private boolean mShouldQueueRequests = false; + /** + * Initialize the API. + * @param ctx Context to use for connecting to the API. + */ public void setUp(Context ctx) { - if(ambientIsAvailable(ctx)) { + if (ambientIsAvailable(ctx)) { + mShouldQueueRequests = true; mApi = (DeepLinkApi) DeepLinkServices.API; mAmbientApiClient = AmbientConnection.CLIENT.get(ctx); } } + /** + * Get the API's preferred Links for the given Uri. + * @param callback code to execute when the API has completed its work. + * @param category DeepLinkContentType to query for + * @param uri The content Uri to ask the API about. + * @return Pending result which represents the future completion of the request. + */ public PendingResult<DeepLink.DeepLinkResultList> getPreferredLinksFor( ResultCallback<DeepLink.DeepLinkResultList> callback, DeepLinkContentType category, Uri uri) { PendingResult<DeepLink.DeepLinkResultList> result = null; - if (hasConnectedClient()) { + if (mShouldQueueRequests) { result = mApi.getPreferredLinksForSingleItem(mAmbientApiClient, DeepLinkApplicationType.NOTE, category, uri); result.setResultCallback(callback); @@ -76,11 +93,20 @@ public class DeepLinkIntegrationManager { return result; } + /** + * For the given list of Uri's, find the preferred link for each system supported + * ApplicationType + * + * @param callback code to execute when the API has finished its work. + * @param category DeepLinKContentType to query for (calls, emails etc..) + * @param uris List of Uri's the search for a preferred set of links against. + * @return Pending result which represents the future completion of the request. + */ public PendingResult<DeepLink.DeepLinkResultList> getPreferredLinksForList( ResultCallback<DeepLink.DeepLinkResultList> callback, DeepLinkContentType category, List<Uri> uris) { PendingResult<DeepLink.DeepLinkResultList> result = null; - if (hasConnectedClient()) { + if (mShouldQueueRequests) { result = mApi.getPreferredLinksForList(mAmbientApiClient, DeepLinkApplicationType.NOTE, category, uris); result.setResultCallback(callback); @@ -88,10 +114,15 @@ public class DeepLinkIntegrationManager { return result; } + /** + * Get's the default plugin information for display. + * @param callback Code to execute upon completion. + * @param category DeepLinkContentType to query for API defaults. + */ public void getDefaultPlugin(ResultCallback<DeepLink.StringResultList> callback, DeepLinkContentType category) { PendingResult<DeepLink.StringResultList> result = null; - if (hasConnectedClient()) { + if (mShouldQueueRequests) { result = mApi.getDefaultProviderDisplayInformation(mAmbientApiClient, DeepLinkApplicationType.NOTE, category, DeepLinkIntegrationManager.generateCallUri(dummyNumber, dummyTime)); @@ -110,17 +141,26 @@ public class DeepLinkIntegrationManager { return Uri.parse(CallLog.AUTHORITY + "." + number + "." + time); } + /** + * Checks to ensure ambient is installed and working correctly. + * @param ctx context to query against + * @return true if ambient is available, false otherwise. + */ public boolean ambientIsAvailable(Context ctx) { return CyanogenAmbientUtil.isCyanogenAmbientAvailable(ctx) == CyanogenAmbientUtil.SUCCESS; } - private boolean hasConnectedClient() { - return mAmbientApiClient != null && mAmbientApiClient.isConnected(); - } - + /** + * Send a metrics event of your own design. + * @param ctx context to execute against + * @param categories Event Categories + * @param event Event to report + * @param params Specific parameters to include in this event, such as source and + * destination. + */ public void sendEvent(Context ctx, Categories categories, Events event, HashMap<Parameters, Object> params) { - if(hasConnectedClient()) { + if (mShouldQueueRequests) { DeepLinkMetricsHelper.sendEvent(ctx, categories, event, params, mAmbientApiClient); } } @@ -145,9 +185,9 @@ public class DeepLinkIntegrationManager { * View a given note in the application in which it was taken. Also logs metrics events for * viewing the note. * - * @param ctx Context to log metrics against and to start the activity against. - * @param deepLink The DeepLink for the content to view - * @param cn The ComponentName to log as the generator of the metrics event. + * @param ctx Context to log metrics against and to start the activity against. + * @param deepLink The DeepLink for the content to view + * @param cn The ComponentName to log as the generator of the metrics event. */ public void viewNote(Context ctx, DeepLink deepLink, ComponentName cn) { if (deepLink != null && deepLink.getAlreadyHasContent()) { @@ -156,27 +196,70 @@ public class DeepLinkIntegrationManager { } } + /** + * Report metrics events for creating a new DeepLink between applications. + * + * @param ctx Context to execute against + * @param deepLink DeepLink being created + * @param cn ComponentName of the package/class which the event originated within. + */ public void sendContentSentEvent(Context ctx, DeepLink deepLink, ComponentName cn) { sendEvent(ctx, deepLink, cn, Categories.USER_ACTIONS, Events.CONTENT_SENT); } + /** + * Report metrics events for opening a previously created DeepLink. + * + * @param ctx Context to execute against + * @param deepLink DeepLink being opened + * @param cn ComponentName of the package/class which the event originated within. + */ public void sendOpeningExistingEvent(Context ctx, DeepLink deepLink, ComponentName cn) { sendEvent(ctx, deepLink, cn, Categories.USER_ACTIONS, Events.OPENING_EXISTING_LINK); } - + /** + * Opens a preference activity for the DeepLinkApi to allow users to enable/disable + * functionality. + * + * @param deepLinkApplicationType DepeLinkApplicationType to open preferences for. + */ public void openDeepLinkPreferences(DeepLinkApplicationType deepLinkApplicationType) { - if (hasConnectedClient()) { + if (mShouldQueueRequests) { mApi.openDeepLinkPreferences(mAmbientApiClient, deepLinkApplicationType); } } + /** + * Returns true if the given DeepLinkApplicationType is enabled, false if not. + * + * @param deepLinkApplicationType DeepLinkApplicationType to check the status of + * @param callback Code to execute upon completion of the query. + */ public void isApplicationTypeEnabled(DeepLinkApplicationType deepLinkApplicationType, ResultCallback<DeepLink.BooleanResult> callback) { - if (hasConnectedClient()) { - PendingResult<DeepLink.BooleanResult> result = mApi.isApplicationTypeEnabled( - mAmbientApiClient, deepLinkApplicationType); - result.setResultCallback(callback); + if (mShouldQueueRequests) { + completeEnabledRequest(callback); + PendingResult<DeepLink.BooleanResult> request = + mApi.isApplicationTypeEnabled(mAmbientApiClient, deepLinkApplicationType); + request.setResultCallback(callback); + mPendingResultArrayMap.put(callback, request); + } + } + + /** + * Takes a ResultCallback<?>.toString() as an argument, cancels any pending requests which have + * the argument set as the callback method, removes them from the list of callbacks to track. + * + * The callbacks are tracked from the moment a query is executed against the API and must be + * cleaned up when they are completed, or when the application context becomes invalid. + * + * @param toCancel key to use for removing objects in PendingResultArrayMap. + */ + public void completeEnabledRequest(Object toCancel) { + if (mPendingResultArrayMap.containsKey(toCancel)) { + mPendingResultArrayMap.get(toCancel).cancel(); + mPendingResultArrayMap.remove(toCancel); } } -} +}
\ No newline at end of file |