Home | History | Annotate | Download | only in omtp
      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     private static final String TAG = "OmtpVvmCarrierConfigHelper";
     38     private Context mContext;
     39     private int mSubId;
     40     private PersistableBundle mCarrierConfig;
     41     private String mVvmType;
     42 
     43     public OmtpVvmCarrierConfigHelper(Context context, int subId) {
     44         mContext = context;
     45         mSubId = subId;
     46         mCarrierConfig = getCarrierConfig();
     47         mVvmType = getVvmType();
     48     }
     49 
     50     public String getVvmType() {
     51         if (mCarrierConfig == null) {
     52             return null;
     53         }
     54 
     55         return mCarrierConfig.getString(
     56                 CarrierConfigManager.KEY_VVM_TYPE_STRING, null);
     57     }
     58 
     59     public String getCarrierVvmPackageName() {
     60         if (mCarrierConfig == null) {
     61             return null;
     62         }
     63 
     64         return mCarrierConfig.getString(
     65                 CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING, null);
     66     }
     67 
     68     public boolean isOmtpVvmType() {
     69         return (TelephonyManager.VVM_TYPE_OMTP.equals(mVvmType) ||
     70                 TelephonyManager.VVM_TYPE_CVVM.equals(mVvmType));
     71     }
     72 
     73     /**
     74      * For checking upon sim insertion whether visual voicemail should be enabled. This method does
     75      * so by checking if the carrier's voicemail app is installed.
     76      */
     77     public boolean isEnabledByDefault() {
     78         String packageName = mCarrierConfig.getString(
     79                 CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING);
     80         if (packageName == null) {
     81             return true;
     82         }
     83         try {
     84             mContext.getPackageManager().getPackageInfo(packageName, 0);
     85             return false;
     86         } catch (NameNotFoundException e) {
     87             return true;
     88         }
     89     }
     90 
     91     public void startActivation() {
     92         OmtpMessageSender messageSender = getMessageSender();
     93         if (messageSender != null) {
     94             Log.i(TAG, "Requesting VVM activation for subId: " + mSubId);
     95             messageSender.requestVvmActivation(null);
     96         }
     97     }
     98 
     99     public void startDeactivation() {
    100         OmtpMessageSender messageSender = getMessageSender();
    101         if (messageSender != null) {
    102             Log.i(TAG, "Requesting VVM deactivation for subId: " + mSubId);
    103             messageSender.requestVvmDeactivation(null);
    104         }
    105     }
    106 
    107     private PersistableBundle getCarrierConfig() {
    108         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
    109             Log.w(TAG, "Invalid subscriptionId or subscriptionId not provided in intent.");
    110             return null;
    111         }
    112 
    113         CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
    114                 mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
    115         if (carrierConfigManager == null) {
    116             Log.w(TAG, "No carrier config service found.");
    117             return null;
    118         }
    119 
    120         return carrierConfigManager.getConfigForSubId(mSubId);
    121     }
    122 
    123     private OmtpMessageSender getMessageSender() {
    124         if (mCarrierConfig == null) {
    125             Log.w(TAG, "Empty carrier config.");
    126             return null;
    127         }
    128 
    129         int applicationPort = mCarrierConfig.getInt(
    130                 CarrierConfigManager.KEY_VVM_PORT_NUMBER_INT, 0);
    131         String destinationNumber = mCarrierConfig.getString(
    132                 CarrierConfigManager.KEY_VVM_DESTINATION_NUMBER_STRING);
    133         if (TextUtils.isEmpty(destinationNumber)) {
    134             Log.w(TAG, "No destination number for this carrier.");
    135             return null;
    136         }
    137 
    138         OmtpMessageSender messageSender = null;
    139         SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(mSubId);
    140         switch (mVvmType) {
    141             case TelephonyManager.VVM_TYPE_OMTP:
    142                 messageSender = new OmtpStandardMessageSender(smsManager, (short) applicationPort,
    143                         destinationNumber, null, OmtpConstants.PROTOCOL_VERSION1_1, null);
    144                 break;
    145             case TelephonyManager.VVM_TYPE_CVVM:
    146                 messageSender = new OmtpCvvmMessageSender(smsManager, (short) applicationPort,
    147                         destinationNumber);
    148                 break;
    149             default:
    150                 Log.w(TAG, "Unexpected visual voicemail type: " + mVvmType);
    151         }
    152 
    153         return messageSender;
    154     }
    155 }