1 /* 2 * Copyright (C) 2017 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.otasp; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.AsyncResult; 22 import android.os.Handler; 23 import android.os.Message; 24 import android.os.PersistableBundle; 25 import android.telephony.CarrierConfigManager; 26 import android.telephony.TelephonyManager; 27 import android.util.Log; 28 29 import com.android.internal.telephony.Phone; 30 import com.android.internal.telephony.flags.Flags; 31 import com.android.phone.PhoneGlobals; 32 33 public class OtaspSimStateReceiver extends BroadcastReceiver { 34 private static final String TAG = OtaspSimStateReceiver.class.getSimpleName(); 35 private static final boolean DBG = true; 36 private Context mContext; 37 38 private static final int EVENT_OTASP_CHANGED = 1; 39 40 private Handler mOtaspHandler = new Handler() { 41 @Override 42 public void handleMessage(Message msg) { 43 AsyncResult ar; 44 switch (msg.what) { 45 case EVENT_OTASP_CHANGED: 46 ar = (AsyncResult) msg.obj; 47 if (ar.exception == null && ar.result != null) { 48 int otaspMode = (Integer) ar.result; 49 logd("EVENT_OTASP_CHANGED: otaspMode=" + otaspMode); 50 if (otaspMode == TelephonyManager.OTASP_NEEDED) { 51 logd("otasp activation required, start otaspActivationService"); 52 mContext.startService( 53 new Intent(mContext, OtaspActivationService.class)); 54 } else if (otaspMode == TelephonyManager.OTASP_NOT_NEEDED) { 55 OtaspActivationService.updateActivationState(mContext, true); 56 } 57 } else { 58 logd("EVENT_OTASP_CHANGED: exception=" + ar.exception); 59 } 60 break; 61 default: 62 super.handleMessage(msg); 63 break; 64 } 65 } 66 }; 67 68 /** 69 * check if OTA service provisioning activation is supported by the current carrier 70 * @return true if otasp activation is needed, false otherwise 71 */ isCarrierSupported()72 private static boolean isCarrierSupported() { 73 final Phone phone = PhoneGlobals.getPhone(); 74 final Context context = phone.getContext(); 75 if (context != null) { 76 PersistableBundle b = null; 77 final CarrierConfigManager configManager = (CarrierConfigManager) context 78 .getSystemService(Context.CARRIER_CONFIG_SERVICE); 79 if (configManager != null) { 80 b = configManager.getConfig(); 81 } 82 if (b != null && b.getBoolean( 83 CarrierConfigManager.KEY_USE_OTASP_FOR_PROVISIONING_BOOL)) { 84 return true; 85 } 86 } 87 logd("otasp activation not needed: no supported carrier"); 88 return false; 89 } 90 91 @Override onReceive(Context context, Intent intent)92 public void onReceive(Context context, Intent intent) { 93 if (Flags.phoneTypeCleanup()) return; 94 mContext = context; 95 if(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) { 96 if (DBG) logd("Received intent: " + intent.getAction()); 97 // Allow the receiver to keep active after returning from onReceive(). 98 final PendingResult result = goAsync(); 99 // Do the actual work on another thread to prevent ANR. 100 new Thread(() -> { 101 if (DBG) logd("Start to process ACTION_CARRIER_CONFIG_CHANGED."); 102 if (PhoneGlobals.getPhone().getIccRecordsLoaded() && isCarrierSupported()) { 103 registerOtaspChangedHandler(); 104 } 105 result.finish(); 106 }).start(); 107 } 108 } 109 110 // It's fine to call multiple times, as the registrants are de-duped by Handler object. registerOtaspChangedHandler()111 private void registerOtaspChangedHandler() { 112 if (DBG) logd("registerOtaspChangedHandler"); 113 final Phone phone = PhoneGlobals.getPhone(); 114 phone.registerForOtaspChange(mOtaspHandler, EVENT_OTASP_CHANGED, null); 115 } 116 logd(String s)117 private static void logd(String s) { 118 Log.d(TAG, s); 119 } 120 } 121 122