summaryrefslogtreecommitdiffstats
path: root/src-ambient/incall/CallMethodInfo.java
blob: 7c6ec963102b26467b70ac9baec2eced8052c80a (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (C) 2015 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.phone.common.incall;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.provider.ContactsContract;
import android.telecom.PhoneAccountHandle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
import android.util.Log;
import com.android.phone.common.ambient.AmbientConnection;
import com.android.phone.common.incall.CallMethodHelper.InCallCallListener;
import com.android.phone.common.R;
import com.android.phone.common.util.StartInCallCallReceiver;
import com.cyanogen.ambient.incall.InCallServices;
import com.cyanogen.ambient.incall.extension.CreditBalance;
import com.cyanogen.ambient.incall.extension.CreditInfo;
import com.cyanogen.ambient.incall.extension.StartCallRequest;
import com.cyanogen.ambient.incall.extension.SubscriptionInfo;
import com.google.common.base.Objects;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;

public class CallMethodInfo {

    public String mId;
    public UserHandle mUserHandle;
    public ComponentName mComponent;
    public String mName;
    public String mSummary;
    public int mSlotId;
    public int mSubId;
    public int mColor;
    public int mStatus;
    public boolean mIsAuthenticated;
    public String mMimeType;
    public String mVideoCallableMimeType;
    public String mImMimeType;
    public String mSubscriptionButtonText;
    public String mCreditButtonText;
    public String mT9HintDescriptionNoCreditOrSub;
    public String mT9HintDescriptionHasCreditOrSub;
    public PendingIntent mSettingsIntent;
    /* Plugin's simple brand icon (24dp x 24dp)
       Expected format: Vector Drawable (.xml)
       2 colors allowed. */
    public Drawable mBrandIcon;
    /* Plugin's single color simple brand icon (24dp x 24dp)
       Same as to pluginBrandIcon, but only a single color.
       Please keep image single color, it will be tinted in the client application.
       Expected format: Vector Drawable (.xml)
       1 color allowed. */
    public Drawable mSingleColorBrandIcon;
    /* Plugin's attribution badge (17dp x 17dp)
       Similar to pluginBrandIcon.
       To make badge pop against (on top of) images, please add a white outline.
       Expected format: Vector Drawable (.xml)
       2 colors allowed. */
    public Drawable mBadgeIcon;
    /* Plugin's full brand icon (any width (max 360dp) x 60dp height)
       May contain full brand name or icon image.
       Please keep image single color, it will be tinted in the client application.
       Expected format: Vector Drawable (.xml)
       1 color allowed. */
    public Drawable mLoginIcon;
    /* Plugin's first action icon (24dp x 24dp)
       This icon is used in conjunction with pluginActionOneTitle.
       Expected format: Vector Drawable (.xml)
       2 colors allowed. */
    public Drawable mActionOneIcon;
    /* Plugin's second action icon (24dp x 24dp)
       This icon is used in conjunction with pluginActionTwoTitle.
       Expected format: Vector Drawable (.xml)
       2 colors allowed. */
    public Drawable mActionTwoIcon;
    public String mActionOneText;
    public String mActionTwoText;
    public boolean mIsInCallProvider;
    public PendingIntent mManageCreditIntent;
    public CreditInfo mProviderCreditInfo;
    public float mCreditWarn = 0.0f;
    public PendingIntent mLoginIntent;
    public PendingIntent mDefaultDirectorySearchIntent; // empty contact Uri
    public PendingIntent mDirectorySearchIntent;
    public PendingIntent mInviteIntent;
    public String mAccountType;
    private int mCurrencyAmount;
    public String mAccountHandle;
    public int mBrandIconId; // resource ID
    public int mLoginIconId;

    public ComponentName mNudgeComponent;
    public String mLoginSubtitle;
    // Contact login nudge
    public boolean mLoginNudgeEnable;
    public String mLoginNudgeTitle;
    public String mLoginNudgeSubtitle;
    public String mLoginNudgeActionText;
    // Contact install nudge
    public boolean mInstallNudgeEnable;
    public String mInstallNudgeTitle;
    public String mInstallNudgeSubtitle;
    public String mInstallNudgeActionText;
    public String mDependentPackage;
    /* Plugin's IM action icon (24dp x 24dp)
       Expected format: Vector Drawable (.xml)
       1 color allowed. */
    public Drawable mImIcon;
    /* Plugin's video call action icon (24dp x 24dp)
       Expected format: Vector Drawable (.xml)
       1 color allowed. */
    public Drawable mVideoIcon;
    /* Plugin's voice call action icon (24dp x 24dp)
       Expected format: Vector Drawable (.xml)
       1 color allowed. */
    public Drawable mVoiceIcon;
    private static CallMethodInfo sEmergencyCallMethod;

    @Override
    public int hashCode() {
        return Objects.hashCode(mId, mComponent, mName, mSlotId, mSubId);
    }

    public static final String TAG = "CallMethodInfo";

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof CallMethodInfo) {
            final CallMethodInfo info = (CallMethodInfo) object;
            return Objects.equal(this.mId, info.mId)
                    && Objects.equal(this.mComponent, info.mComponent)
                    && Objects.equal(this.mName, info.mName)
                    && Objects.equal(this.mSlotId, info.mSlotId)
                    && Objects.equal(this.mSubId, info.mSubId);
        }
        return false;
    }

    /**
     * return empty mock call method that represents emergency call only mode
     */
    public static CallMethodInfo getEmergencyCallMethod(Context ctx) {
        if (sEmergencyCallMethod == null) {
            sEmergencyCallMethod = new CallMethodInfo();
            sEmergencyCallMethod.mName =
                    ctx.getString(R.string.call_method_spinner_item_emergency_call);
            sEmergencyCallMethod.mColor =
                    ctx.getResources().getColor(R.color.emergency_call_icon_color);
            sEmergencyCallMethod.mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
            sEmergencyCallMethod.mSlotId = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
            sEmergencyCallMethod.mIsInCallProvider = false;
        }

        return sEmergencyCallMethod;
    }

    public static PhoneAccountHandle getPhoneAccountHandleFromCallMethodInfo(Context ctx,
            CallMethodInfo callMethodInfo, String number) {
        CallMethodInfo emergencyCallMethod = getEmergencyCallMethod(ctx);
        // If no sim is selected, or emergency callmethod selected, or number is
        // an emergency number, phone account handle should be null, and will use the
        // default account.
        // Else, create PhoneAccountHandle from selected callmethod components and
        // initial call using that account.
        PhoneAccountHandle handle = null;
        if (callMethodInfo != null && !callMethodInfo.mIsInCallProvider &&
                !callMethodInfo.equals(emergencyCallMethod) &&
                (number == null || !PhoneNumberUtils.isEmergencyNumber(number))) {
            handle = new PhoneAccountHandle(callMethodInfo.mComponent,
                    callMethodInfo.mId,
                    callMethodInfo.mUserHandle);
        }
        return handle;
    }

    public void placeCall(String origin, String number, Context c) {
        placeCall(origin, number, c, false);
    }

    public void placeCall(String origin, String number, Context c, boolean isVideoCall) {
        placeCall(origin, number, c, isVideoCall, false, null, null);
    }

    public void placeCall(String origin, String number, Context c, boolean isVideoCall, boolean
            forcePSTN) {
        placeCall(origin, number, c, isVideoCall, forcePSTN, null, null);
    }

    public void placeCall(String origin, String number, Context c, boolean isVideoCall, boolean
            forcePSTN, InCallCallListener listener) {
        placeCall(origin, number, c, isVideoCall, forcePSTN, null, listener);
    }

    public void placeCall(String origin, String number, Context c, boolean isVideoCall,
                          boolean forcePSTN, String numberMimeType) {
        placeCall(origin, number, c, isVideoCall, forcePSTN, numberMimeType, null);
    }

    public void placeCall(String origin, String number, Context c, boolean isVideoCall,
            boolean forcePSTN, String numberMimeType, InCallCallListener listener) {

        StartInCallCallReceiver svcrr
                = CallMethodHelper.getVoIPResultReceiver(this, origin, listener);
        StartCallRequest request = new StartCallRequest(number, origin, 0, svcrr);

        if (isVideoCall) {
            InCallServices.getInstance().startVideoCall(
                    AmbientConnection.CLIENT.get(c), this.mComponent, request);
        } else {
            if (forcePSTN || (numberMimeType != null && numberMimeType.equals(ContactsContract
                    .CommonDataKinds.Phone.CONTENT_ITEM_TYPE))) {
                InCallServices.getInstance().startOutCall(
                        AmbientConnection.CLIENT.get(c), this.mComponent, request);
            } else {
                InCallServices.getInstance().startVoiceCall(
                        AmbientConnection.CLIENT.get(c), this.mComponent, request);
            }
        }
    }

    public String getCreditsDescriptionText(Resources r) {
        String ret = null;
        CreditInfo ci =  this.mProviderCreditInfo;

        List<SubscriptionInfo> subscriptionInfos = ci.subscriptions;

        if (showSubscriptions()) {
            int size = subscriptionInfos.size();
            return r.getQuantityString(R.plurals.number_of_incall_subscriptions, size, size);
        } else {
            CreditBalance balance = ci.balance;
            if (balance != null) {
                mCurrencyAmount = (int) balance.balance;
                try {
                    if (balance.currencyCode != null) {
                        Currency currencyCode = Currency.getInstance(balance.currencyCode);
                        BigDecimal availableCredit = BigDecimal.valueOf(mCurrencyAmount,
                                currencyCode.getDefaultFractionDigits());


                         return currencyCode.getSymbol(r.getConfiguration().locale)
                                + availableCredit.toString();
                    } else {
                        throw new IllegalArgumentException();
                    }
                } catch (IllegalArgumentException e) {
                    Log.w(TAG, "Unable to retrieve currency code for plugin: " +
                            this.mComponent);

                    return null;
                }
            } else {
                Log.e(TAG, "Plugin has credit component but the balance and subscriptions are" +
                        " null. This should never happen. Not showing credit banner due to " +
                        "failures.");

                return null;
            }
        }
    }

    public boolean hasAvailableCredit() {
        boolean hasAvailableCredit = false;
        CreditInfo ci =  this.mProviderCreditInfo;
        if (ci != null) {
            CreditBalance balance = ci.balance;
            if (balance != null) {
                mCurrencyAmount = (int) balance.balance;
                hasAvailableCredit = mCurrencyAmount > 0;
            }
        }
        return hasAvailableCredit;
    }

    public boolean hasSubscription() {
        boolean hasSubscription = false;
        CreditInfo ci =  this.mProviderCreditInfo;
        if (ci != null) {
            List<SubscriptionInfo> subscriptionInfos = ci.subscriptions;
            hasSubscription = subscriptionInfos != null && !subscriptionInfos.isEmpty();
        }
        return hasSubscription;
    }

    public boolean showSubscriptions() {
        // If the user has > 0 credits, we don't want to show the subscription.
        boolean hasCredits = hasAvailableCredit();
        boolean hasSubscriptions = hasSubscription();

        return !hasCredits && hasSubscriptions;
    }

    public int getCurrencyAmount() {
        return mCurrencyAmount;
    }

    /**
     * Returns hint text based on credit and subscription availability.
     * If either is null or empty, use the non-null/non-empty string.
     * Null is returned if both are null or empty.
     * @return hintText String hint text for the current situation
     */
    public String getHintText() {
        String hintText = null;
        boolean hasAvailableCredit = hasAvailableCredit();
        boolean usesSubscriptions = hasSubscription();
        if (!hasAvailableCredit && !usesSubscriptions) {
            // No credits and no subscription
            if (!TextUtils.isEmpty(mT9HintDescriptionNoCreditOrSub)) {
                // Use no credits or subscription hint
                hintText = mT9HintDescriptionNoCreditOrSub;
            } else if (!TextUtils.isEmpty(mT9HintDescriptionHasCreditOrSub)) {
                // If no credit/sub hint empty, but has credit/sub hint valid, use it
                hintText = mT9HintDescriptionHasCreditOrSub;
            }
        } else {
            // Has credits or subscription
            if (!TextUtils.isEmpty(mT9HintDescriptionHasCreditOrSub)) {
                // Use has credits/sub hint text
                hintText = mT9HintDescriptionHasCreditOrSub;
            } else if (!TextUtils.isEmpty(mT9HintDescriptionNoCreditOrSub)) {
                // If has credit/sub hint empty, but no credit/sub hint valid, use it
                hintText = mT9HintDescriptionNoCreditOrSub;
            }
        }
        return hintText;
    }
}