summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/deeplink/DeepLinkIntegrationManager.java
blob: 971308ca3b52182d3592f3f318942348eaae04f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.deeplink;

import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;

import com.android.phone.common.ambient.AmbientConnection;
import com.cyanogen.ambient.analytics.AnalyticsServices;
import com.cyanogen.ambient.common.ConnectionResult;
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.ResultCallback;
import com.cyanogen.ambient.deeplink.DeepLink;
import com.cyanogen.ambient.deeplink.DeepLinkApi;
import com.cyanogen.ambient.deeplink.DeepLinkServices;
import com.cyanogen.ambient.deeplink.applicationtype.DeepLinkApplicationType;
import com.cyanogen.ambient.deeplink.linkcontent.DeepLinkContentType;
import com.cyanogen.ambient.deeplink.metrics.DeepLinkMetricsHelper;
import com.cyanogen.ambient.deeplink.metrics.DeepLinkMetricsHelper.Categories;
import com.cyanogen.ambient.deeplink.metrics.DeepLinkMetricsHelper.Events;
import com.cyanogen.ambient.deeplink.metrics.DeepLinkMetricsHelper.Parameters;

import java.util.HashMap;
import java.util.List;

public class DeepLinkIntegrationManager {

    public static DeepLinkIntegrationManager getInstance() {
        if (sInstance == null) {
            sInstance = new DeepLinkIntegrationManager();
        }
        return sInstance;
    }

    private String dummyNumber = "00000000";
    private long dummyTime = 0l;
    private static DeepLinkIntegrationManager sInstance;
    private AmbientApiClient mAmbientApiClient;
    private DeepLinkApi mApi;

    public void setUp(Context ctx) {
        if(ambientIsAvailable(ctx)) {
            mApi = (DeepLinkApi) DeepLinkServices.API;
            mAmbientApiClient = AmbientConnection.CLIENT.get(ctx);
        }
    }

    public PendingResult<DeepLink.DeepLinkResultList> getPreferredLinksFor(
            ResultCallback<DeepLink.DeepLinkResultList> callback, DeepLinkContentType category,
            Uri uri) {
        PendingResult<DeepLink.DeepLinkResultList> result = null;
        if (hasConnectedClient()) {
            result = mApi.getPreferredLinksForSingleItem(mAmbientApiClient,
                    DeepLinkApplicationType.NOTE, category, uri);
            result.setResultCallback(callback);
        }
        return result;
    }

    public PendingResult<DeepLink.DeepLinkResultList> getPreferredLinksForList(
            ResultCallback<DeepLink.DeepLinkResultList> callback, DeepLinkContentType category,
            List<Uri> uris) {
        PendingResult<DeepLink.DeepLinkResultList> result = null;
        if (hasConnectedClient()) {
            result = mApi.getPreferredLinksForList(mAmbientApiClient,
                    DeepLinkApplicationType.NOTE, category, uris);
            result.setResultCallback(callback);
        }
        return result;
    }

    public void getDefaultPlugin(ResultCallback<DeepLink.StringResultList> callback,
            DeepLinkContentType category) {
        PendingResult<DeepLink.StringResultList> result = null;
        if (hasConnectedClient()) {
            result = mApi.getDefaultProviderDisplayInformation(mAmbientApiClient,
                    DeepLinkApplicationType.NOTE, category,
                    DeepLinkIntegrationManager.generateCallUri(dummyNumber, dummyTime));
            result.setResultCallback(callback);
        }
    }

    /**
     * Generate a uri which will identify the call for a given number and timestamp
     *
     * @param number - the phone number dialed
     * @param time   - the time the call occured
     * @return Uri identifying the call.
     */
    public static Uri generateCallUri(String number, long time) {
        return Uri.parse(CallLog.AUTHORITY + "." + number + "." + time);
    }

    public boolean ambientIsAvailable(Context ctx) {
        return CyanogenAmbientUtil.isCyanogenAmbientAvailable(ctx) == CyanogenAmbientUtil.SUCCESS;
    }

    private boolean hasConnectedClient() {
        return mAmbientApiClient != null && mAmbientApiClient.isConnected();
    }

    public void sendEvent(Context ctx, Categories categories, Events event,
            HashMap<Parameters, Object> params) {
        if(hasConnectedClient()) {
            DeepLinkMetricsHelper.sendEvent(ctx, categories, event, params, mAmbientApiClient);
        }
    }

    private void sendEvent(Context ctx, DeepLink deepLink, ComponentName cn,
            Categories category, Events event) {

        HashMap<Parameters, Object>
                parameters = new HashMap<Parameters, Object>();

        parameters.put(Parameters.SOURCE, cn.flattenToString());
        parameters.put(Parameters.DESTINATION, deepLink.getPackageName());
        parameters.put(Parameters.CONTENT_TYPE,
                deepLink.getDeepLinkContentType());
        parameters.put(Parameters.DEST_APPLICATION_TYPE,
                deepLink.getApplicationType());
        parameters.put(Parameters.CONTENT_UID, deepLink.getUri().toString());
        sendEvent(ctx, category, event, parameters);
    }

    /**
     * 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.
     */
    public void viewNote(Context ctx, DeepLink deepLink, ComponentName cn) {
        if (deepLink != null && deepLink.getAlreadyHasContent()) {
            sendContentSentEvent(ctx, deepLink, cn);
            ctx.startActivity(deepLink.createViewIntent());
        }
    }

    public void sendContentSentEvent(Context ctx, DeepLink deepLink, ComponentName cn) {
        sendEvent(ctx, deepLink, cn, Categories.USER_ACTIONS, Events.CONTENT_SENT);
    }

    public void sendOpeningExistingEvent(Context ctx, DeepLink deepLink, ComponentName cn) {
        sendEvent(ctx, deepLink, cn, Categories.USER_ACTIONS, Events.OPENING_EXISTING_LINK);
    }


    public void openDeepLinkPreferences(DeepLinkApplicationType deepLinkApplicationType) {
        if (hasConnectedClient()) {
            mApi.openDeepLinkPreferences(mAmbientApiClient, deepLinkApplicationType);
        }
    }

    public void isApplicationTypeEnabled(DeepLinkApplicationType deepLinkApplicationType,
            ResultCallback<DeepLink.BooleanResult> callback) {
        if (hasConnectedClient()) {
            PendingResult<DeepLink.BooleanResult> result = mApi.isApplicationTypeEnabled(
                    mAmbientApiClient, deepLinkApplicationType);
            result.setResultCallback(callback);
        }
    }
}