1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 package com.android.phone.vvm.omtp;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager.NameNotFoundException;
20 import android.os.PersistableBundle;
21 import android.telephony.CarrierConfigManager;
22 import android.telephony.SmsManager;
23 import android.telephony.SubscriptionManager;
24 import android.telephony.TelephonyManager;
25 import android.text.TextUtils;
26 import android.util.Log;
27 
28 import com.android.phone.vvm.omtp.sms.OmtpCvvmMessageSender;
29 import com.android.phone.vvm.omtp.sms.OmtpMessageSender;
30 import com.android.phone.vvm.omtp.sms.OmtpStandardMessageSender;
31 
32 /**
33  * Handle activation and deactivation of a visual voicemail source. This class is necessary to
34  * retrieve carrier vvm configuration details before sending the appropriate texts.
35  */
36 public class OmtpVvmCarrierConfigHelper {
37 
38     private static final String TAG = "OmtpVvmCarrierCfgHlpr";
39     private Context mContext;
40     private int mSubId;
41     private PersistableBundle mCarrierConfig;
42     private String mVvmType;
43 
OmtpVvmCarrierConfigHelper(Context context, int subId)44     public OmtpVvmCarrierConfigHelper(Context context, int subId) {
45         mContext = context;
46         mSubId = subId;
47         mCarrierConfig = getCarrierConfig();
48         mVvmType = getVvmType();
49     }
50 
getVvmType()51     public String getVvmType() {
52         if (mCarrierConfig == null) {
53             return null;
54         }
55 
56         return mCarrierConfig.getString(
57                 CarrierConfigManager.KEY_VVM_TYPE_STRING, null);
58     }
59 
getCarrierVvmPackageName()60     public String getCarrierVvmPackageName() {
61         if (mCarrierConfig == null) {
62             return null;
63         }
64 
65         return mCarrierConfig.getString(
66                 CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING, null);
67     }
68 
isOmtpVvmType()69     public boolean isOmtpVvmType() {
70         return (TelephonyManager.VVM_TYPE_OMTP.equals(mVvmType) ||
71                 TelephonyManager.VVM_TYPE_CVVM.equals(mVvmType));
72     }
73 
74     /**
75      * For checking upon sim insertion whether visual voicemail should be enabled. This method does
76      * so by checking if the carrier's voicemail app is installed.
77      */
isEnabledByDefault()78     public boolean isEnabledByDefault() {
79         String packageName = mCarrierConfig.getString(
80                 CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING);
81         if (packageName == null) {
82             return true;
83         }
84         try {
85             mContext.getPackageManager().getPackageInfo(packageName, 0);
86             return false;
87         } catch (NameNotFoundException e) {
88             return true;
89         }
90     }
91 
isCellularDataRequired()92     public boolean isCellularDataRequired() {
93         if (mCarrierConfig == null) {
94             return false;
95         }
96         return mCarrierConfig
97                 .getBoolean(CarrierConfigManager.KEY_VVM_CELLULAR_DATA_REQUIRED_BOOL);
98     }
99 
isPrefetchEnabled()100     public boolean isPrefetchEnabled() {
101         if (mCarrierConfig == null) {
102             return false;
103         }
104         return mCarrierConfig
105                 .getBoolean(CarrierConfigManager.KEY_VVM_PREFETCH_BOOL);
106     }
107 
startActivation()108     public void startActivation() {
109         OmtpMessageSender messageSender = getMessageSender();
110         if (messageSender != null) {
111             Log.i(TAG, "Requesting VVM activation for subId: " + mSubId);
112             messageSender.requestVvmActivation(null);
113         }
114     }
115 
startDeactivation()116     public void startDeactivation() {
117         OmtpMessageSender messageSender = getMessageSender();
118         if (messageSender != null) {
119             Log.i(TAG, "Requesting VVM deactivation for subId: " + mSubId);
120             messageSender.requestVvmDeactivation(null);
121         }
122     }
123 
getCarrierConfig()124     private PersistableBundle getCarrierConfig() {
125         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
126             Log.w(TAG, "Invalid subscriptionId or subscriptionId not provided in intent.");
127             return null;
128         }
129 
130         CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
131                 mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
132         if (carrierConfigManager == null) {
133             Log.w(TAG, "No carrier config service found.");
134             return null;
135         }
136 
137         return carrierConfigManager.getConfigForSubId(mSubId);
138     }
139 
getMessageSender()140     private OmtpMessageSender getMessageSender() {
141         if (mCarrierConfig == null) {
142             Log.w(TAG, "Empty carrier config.");
143             return null;
144         }
145 
146         int applicationPort = mCarrierConfig.getInt(
147                 CarrierConfigManager.KEY_VVM_PORT_NUMBER_INT, 0);
148         String destinationNumber = mCarrierConfig.getString(
149                 CarrierConfigManager.KEY_VVM_DESTINATION_NUMBER_STRING);
150         if (TextUtils.isEmpty(destinationNumber)) {
151             Log.w(TAG, "No destination number for this carrier.");
152             return null;
153         }
154 
155         OmtpMessageSender messageSender = null;
156         SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(mSubId);
157         switch (mVvmType) {
158             case TelephonyManager.VVM_TYPE_OMTP:
159                 messageSender = new OmtpStandardMessageSender(smsManager, (short) applicationPort,
160                         destinationNumber, null, OmtpConstants.PROTOCOL_VERSION1_1, null);
161                 break;
162             case TelephonyManager.VVM_TYPE_CVVM:
163                 messageSender = new OmtpCvvmMessageSender(smsManager, (short) applicationPort,
164                         destinationNumber);
165                 break;
166             default:
167                 Log.w(TAG, "Unexpected visual voicemail type: " + mVvmType);
168         }
169 
170         return messageSender;
171     }
172 }