summaryrefslogtreecommitdiffstats
path: root/src-ambient/incall/CallMethodInfo.java
blob: b68e74d8f80e80027448812f5d076b4fee6344be (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
/*
 * 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.telephony.PhoneNumberUtils;
import android.telephony.SubscriptionManager;
import android.util.Log;
import com.android.phone.common.ambient.AmbientConnection;
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 mSubscriptionButtonText;
    public String mCreditButtonText;
    public String mT9HintDescription;
    public PendingIntent mSettingsIntent;
    public Drawable mBrandIcon;
    public Drawable mSingleColorBrandIcon;
    public Drawable mBadgeIcon;
    public Drawable mLoginIcon;
    public Drawable mActionOneIcon;
    public Drawable mActionTwoIcon;
    public Resources pluginResources;
    public String mActionOneText;
    public String mActionTwoText;
    public boolean mIsInCallProvider;
    public PendingIntent mManageCreditIntent;
    public CreditInfo mProviderCreditInfo;
    public float mCreditWarn = 0.0f;
    public PendingIntent mLoginIntent;

    private int mCurrencyAmount;

    private static CallMethodInfo sEmergencyCallMethod;

    @Override
    public int hashCode() {
        return Objects.hashCode(mId, mComponent, mName, mSummary, mSlotId, mSubId, mColor,
                mMimeType, mVideoCallableMimeType, mSubscriptionButtonText, mCreditButtonText,
                mT9HintDescription, mSettingsIntent, mBrandIcon, mSingleColorBrandIcon, mBadgeIcon,
                mLoginIcon, mActionOneIcon, mActionTwoIcon, pluginResources, mActionOneText,
                mActionTwoText, mIsInCallProvider, mLoginIntent);
    }

    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.mSummary, info.mSummary)
                    && Objects.equal(this.mSlotId, info.mSlotId)
                    && Objects.equal(this.mSubId, info.mSubId)
                    && Objects.equal(this.mColor, info.mColor)
                    && Objects.equal(this.mMimeType, info.mMimeType)
                    && Objects.equal(this.mVideoCallableMimeType, info.mVideoCallableMimeType)
                    && Objects.equal(this.mSubscriptionButtonText, info.mSubscriptionButtonText)
                    && Objects.equal(this.mCreditButtonText, info.mCreditButtonText)
                    && Objects.equal(this.mT9HintDescription, info.mT9HintDescription)
                    && Objects.equal(this.mSettingsIntent, info.mSettingsIntent)
                    && Objects.equal(this.mBrandIcon, info.mBrandIcon)
                    && Objects.equal(this.mSingleColorBrandIcon, info.mSingleColorBrandIcon)
                    && Objects.equal(this.mBadgeIcon, info.mBadgeIcon)
                    && Objects.equal(this.mLoginIcon, info.mLoginIcon)
                    && Objects.equal(this.mActionOneIcon, info.mActionOneIcon)
                    && Objects.equal(this.mActionTwoIcon, info.mActionTwoIcon)
                    && Objects.equal(this.pluginResources, info.pluginResources)
                    && Objects.equal(this.mActionOneText, info.mActionOneText)
                    && Objects.equal(this.mActionTwoText, info.mActionTwoText)
                    && Objects.equal(this.mIsInCallProvider, info.mIsInCallProvider)
                    && Objects.equal(this.mLoginIntent, info.mLoginIntent);
        }
        return false;
    }

    /**
     * return empty mock call method that represents emergency call only mode
     */
    public static CallMethodInfo getEmergencyCallMethod(Context context) {
        Context ctx = context.getApplicationContext();
        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 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);
    }

    public void placeCall(String origin, String number, Context c, boolean isVideoCall,
                          boolean forcePSTN) {
        StartInCallCallReceiver svcrr = CallMethodHelper.getVoIPResultReceiver(this, origin);
        StartCallRequest request = new StartCallRequest(number, origin, 0, svcrr);

        if (isVideoCall) {
            InCallServices.getInstance().startVideoCall(
                    AmbientConnection.CLIENT.get(c), this.mComponent, request);
        } else {
            if (PhoneNumberUtils.isGlobalPhoneNumber(number) || forcePSTN) {
                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 (subscriptionInfos != null && !subscriptionInfos.isEmpty()) {
            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 usesSubscriptions() {
        List<SubscriptionInfo> subscriptionInfos = this.mProviderCreditInfo.subscriptions;
        return subscriptionInfos != null && !subscriptionInfos.isEmpty();
    }

    public int getCurrencyAmount() {
        return mCurrencyAmount;
    }
}