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.BroadcastReceiver;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.UserHandle;
     22 import android.os.UserManager;
     23 import android.telecom.PhoneAccountHandle;
     24 import android.telephony.CarrierConfigManager;
     25 import android.telephony.SubscriptionManager;
     26 import android.util.Log;
     27 
     28 import com.android.internal.telephony.IccCardConstants;
     29 import com.android.internal.telephony.PhoneConstants;
     30 import com.android.internal.telephony.TelephonyIntents;
     31 import com.android.phone.PhoneUtils;
     32 import com.android.phone.R;
     33 import com.android.phone.settings.VisualVoicemailSettingsUtil;
     34 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
     35 
     36 /**
     37  * This class listens to the {@link CarrierConfigManager#ACTION_CARRIER_CONFIG_CHANGED} and {@link
     38  * TelephonyIntents#ACTION_SIM_STATE_CHANGED} to determine when a SIM is added, replaced, or
     39  * removed.
     40  *
     41  * When a SIM is added, send an activate SMS. When a SIM is removed, remove the sync accounts and
     42  * change the status in the voicemail_status table.
     43  */
     44 public class SimChangeReceiver extends BroadcastReceiver {
     45 
     46     private static final String TAG = "SimChangeReceiver";
     47 
     48     @Override
     49     public void onReceive(Context context, Intent intent) {
     50         if (UserHandle.myUserId() != UserHandle.USER_SYSTEM) {
     51             Log.v(TAG, "Received broadcast for user that is not system.");
     52             return;
     53         }
     54 
     55         final String action = intent.getAction();
     56         if (action == null) {
     57             Log.w(TAG, "Null action for intent.");
     58             return;
     59         }
     60 
     61         switch (action) {
     62             case TelephonyIntents.ACTION_SIM_STATE_CHANGED:
     63                 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(
     64                         intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE))) {
     65                     Log.i(TAG, "Sim removed, removing inactive accounts");
     66                     OmtpVvmSourceManager.getInstance(context).removeInactiveSources();
     67                 }
     68                 break;
     69             case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED:
     70                 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
     71                         SubscriptionManager.INVALID_SUBSCRIPTION_ID);
     72 
     73                 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
     74                     Log.i(TAG, "Received SIM change for invalid subscription id.");
     75                     return;
     76                 }
     77 
     78                 if (!UserManager.get(context).isUserUnlocked()) {
     79                     OmtpBootCompletedReceiver.addDeferredSubId(context, subId);
     80                 } else {
     81                     processSubId(context, subId);
     82                 }
     83                 break;
     84         }
     85     }
     86 
     87     public static void processSubId(Context context, int subId) {
     88         OmtpVvmCarrierConfigHelper carrierConfigHelper =
     89                 new OmtpVvmCarrierConfigHelper(context, subId);
     90         if (carrierConfigHelper.isOmtpVvmType()) {
     91             PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(
     92                     SubscriptionManager.getPhoneId(subId));
     93 
     94             boolean isUserSet = VisualVoicemailSettingsUtil.isVisualVoicemailUserSet(
     95                     context, phoneAccount);
     96             boolean isEnabledInSettings =
     97                     VisualVoicemailSettingsUtil.isVisualVoicemailEnabled(context,
     98                             phoneAccount);
     99             boolean isSupported =
    100                     context.getResources().getBoolean(R.bool.allow_visual_voicemail);
    101             boolean isEnabled = isSupported && (isUserSet ? isEnabledInSettings :
    102                     carrierConfigHelper.isEnabledByDefault());
    103 
    104             if (!isUserSet) {
    105                 // Preserve the previous setting for "isVisualVoicemailEnabled" if it is
    106                 // set by the user, otherwise, set this value for the first time.
    107                 VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(context, phoneAccount,
    108                         isEnabled, /** isUserSet */false);
    109             }
    110 
    111             if (isEnabled) {
    112                 LocalLogHelper.log(TAG, "Sim state or carrier config changed: requesting"
    113                         + " activation for " + phoneAccount.getId());
    114 
    115                 // Add a phone state listener so that changes to the communication channels
    116                 // can be recorded.
    117                 OmtpVvmSourceManager.getInstance(context).addPhoneStateListener(
    118                         phoneAccount);
    119                 carrierConfigHelper.startActivation();
    120             } else {
    121                 // It may be that the source was not registered to begin with but we want
    122                 // to run through the steps to remove the source just in case.
    123                 OmtpVvmSourceManager.getInstance(context).removeSource(phoneAccount);
    124                 Log.v(TAG, "Sim change for disabled account.");
    125             }
    126         }
    127     }
    128 }