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.phone.PhoneGlobals;
31 
32 public class OtaspSimStateReceiver extends BroadcastReceiver {
33     private static final String TAG = OtaspSimStateReceiver.class.getSimpleName();
34     private static final boolean DBG = true;
35     private Context mContext;
36 
37     private static final int EVENT_OTASP_CHANGED = 1;
38 
39     private Handler mOtaspHandler = new Handler() {
40         @Override
41         public void handleMessage(Message msg) {
42             AsyncResult ar;
43             switch (msg.what) {
44                 case EVENT_OTASP_CHANGED:
45                     ar = (AsyncResult) msg.obj;
46                     if (ar.exception == null && ar.result != null) {
47                         int otaspMode = (Integer) ar.result;
48                         logd("EVENT_OTASP_CHANGED: otaspMode=" + otaspMode);
49                         if (otaspMode == TelephonyManager.OTASP_NEEDED) {
50                             logd("otasp activation required, start otaspActivationService");
51                             mContext.startService(
52                                     new Intent(mContext, OtaspActivationService.class));
53                         } else if (otaspMode == TelephonyManager.OTASP_NOT_NEEDED) {
54                             OtaspActivationService.updateActivationState(mContext, true);
55                         }
56                     } else {
57                         logd("EVENT_OTASP_CHANGED: exception=" + ar.exception);
58                     }
59                     break;
60                 default:
61                     super.handleMessage(msg);
62                     break;
63             }
64         }
65     };
66 
67     /**
68      * check if OTA service provisioning activation is supported by the current carrier
69      * @return true if otasp activation is needed, false otherwise
70      */
isCarrierSupported()71     private static boolean isCarrierSupported() {
72         final Phone phone = PhoneGlobals.getPhone();
73         final Context context = phone.getContext();
74         if (context != null) {
75             PersistableBundle b = null;
76             final CarrierConfigManager configManager = (CarrierConfigManager) context
77                     .getSystemService(Context.CARRIER_CONFIG_SERVICE);
78             if (configManager != null) {
79                 b = configManager.getConfig();
80             }
81             if (b != null && b.getBoolean(
82                     CarrierConfigManager.KEY_USE_OTASP_FOR_PROVISIONING_BOOL)) {
83                 return true;
84             }
85         }
86         logd("otasp activation not needed: no supported carrier");
87         return false;
88     }
89 
90     @Override
onReceive(Context context, Intent intent)91     public void onReceive(Context context, Intent intent) {
92         mContext = context;
93         if(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
94             if (DBG) logd("Received intent: " + intent.getAction());
95             if (PhoneGlobals.getPhone().getIccRecordsLoaded() && isCarrierSupported()) {
96                 registerOtaspChangedHandler();
97             }
98         }
99     }
100 
101     // It's fine to call mutiple times, as the registrants are de-duped by Handler object.
registerOtaspChangedHandler()102     private void registerOtaspChangedHandler() {
103         final Phone phone = PhoneGlobals.getPhone();
104         phone.registerForOtaspChange(mOtaspHandler, EVENT_OTASP_CHANGED, null);
105     }
106 
logd(String s)107     private static void logd(String s) {
108         Log.d(TAG, s);
109     }
110 }
111 
112