• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.IPackageManager;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.os.UserManager;
25 import android.telecom.PhoneAccountHandle;
26 import android.telephony.CarrierConfigManager;
27 import android.telephony.SubscriptionManager;
28 import android.telephony.TelephonyManager;
29 import com.android.internal.telephony.IccCardConstants;
30 import com.android.internal.telephony.PhoneConstants;
31 import com.android.internal.telephony.TelephonyIntents;
32 import com.android.phone.VoicemailStatus;
33 import com.android.phone.settings.VisualVoicemailSettingsUtil;
34 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
35 import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter;
36 
37 /**
38  * This class listens to the {@link CarrierConfigManager#ACTION_CARRIER_CONFIG_CHANGED} and {@link
39  * TelephonyIntents#ACTION_SIM_STATE_CHANGED} to determine when a SIM is added, replaced, or
40  * removed.
41  *
42  * When a SIM is added, send an activate SMS. When a SIM is removed, remove the sync accounts and
43  * change the status in the voicemail_status table.
44  */
45 public class SimChangeReceiver extends BroadcastReceiver {
46 
47     private static final String TAG = "VvmSimChangeReceiver";
48 
49     @Override
onReceive(Context context, Intent intent)50     public void onReceive(Context context, Intent intent) {
51         final String action = intent.getAction();
52         if (action == null) {
53             VvmLog.w(TAG, "Null action for intent.");
54             return;
55         }
56 
57         switch (action) {
58             case TelephonyIntents.ACTION_SIM_STATE_CHANGED:
59                 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(
60                         intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE))) {
61                     VvmLog.i(TAG, "Sim removed, removing inactive accounts");
62                     OmtpVvmSourceManager.getInstance(context).removeInactiveSources();
63                 }
64                 break;
65             case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED:
66                 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
67                         SubscriptionManager.INVALID_SUBSCRIPTION_ID);
68 
69                 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
70                     VvmLog.i(TAG, "Received SIM change for invalid subscription id.");
71                     return;
72                 }
73                 VvmLog.d(TAG, "Carrier config changed");
74                 if (UserManager.get(context).isUserUnlocked() && !isCryptKeeperMode()) {
75                     processSubId(context, subId);
76                 } else {
77                     VvmLog.d(TAG, "User locked, activation request delayed until unlock");
78                     // After the device is unlocked, VvmBootCompletedReceiver will iterate through
79                     // all call capable subIds, nothing need to be done here.
80                 }
81                 break;
82         }
83     }
84 
processSubId(Context context, int subId)85     public static void processSubId(Context context, int subId) {
86         PhoneAccountHandle phoneAccount = PhoneAccountHandleConverter.fromSubId(subId);
87         if (phoneAccount == null) {
88             // This should never happen
89             VvmLog.e(TAG, "unable to convert subId " + subId + " to PhoneAccountHandle");
90             return;
91         }
92 
93         OmtpVvmCarrierConfigHelper carrierConfigHelper =
94                 new OmtpVvmCarrierConfigHelper(context, subId);
95         if (carrierConfigHelper.isValid()) {
96             if (VisualVoicemailSettingsUtil.isEnabled(context, phoneAccount)) {
97                 VvmLog.i(TAG, "Sim state or carrier config changed for " + subId);
98                 // Add a phone state listener so that changes to the communication channels
99                 // can be recorded.
100                 OmtpVvmSourceManager.getInstance(context).addPhoneStateListener(
101                         phoneAccount);
102                 carrierConfigHelper.startActivation();
103             } else {
104                 if (carrierConfigHelper.isLegacyModeEnabled()) {
105                     // SMS still need to be filtered under legacy mode.
106                     VvmLog.i(TAG, "activating SMS filter for legacy mode");
107                     carrierConfigHelper.activateSmsFilter();
108                 }
109                 // It may be that the source was not registered to begin with but we want
110                 // to run through the steps to remove the source just in case.
111                 OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount);
112                 VvmLog.v(TAG, "Sim change for disabled account.");
113             }
114         } else {
115             String mccMnc = context.getSystemService(TelephonyManager.class).getSimOperator(subId);
116             VvmLog.d(TAG,
117                     "visual voicemail not supported for carrier " + mccMnc + " on subId " + subId);
118             VoicemailStatus.disable(context, phoneAccount);
119         }
120     }
121 
122     /**
123      * CryptKeeper mode is the pre-file based encryption locked state, when the user has selected
124      * "Require password to boot" and the device hasn't been unlocked yet during a reboot. {@link
125      * UserManager#isUserUnlocked()} will still return true in this mode, but storage in /data and
126      * all content providers will not be available(including SharedPreference).
127      */
isCryptKeeperMode()128     private static boolean isCryptKeeperMode() {
129         try {
130             return IPackageManager.Stub.asInterface(ServiceManager.getService("package")).
131                     isOnlyCoreApps();
132         } catch (RemoteException e) {
133         }
134         return false;
135     }
136 }