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 */
16package com.android.phone.otasp;
17
18import android.content.BroadcastReceiver;
19import android.content.Context;
20import android.content.Intent;
21import android.os.PersistableBundle;
22import android.telephony.CarrierConfigManager;
23import android.telephony.PhoneStateListener;
24import android.telephony.TelephonyManager;
25import android.util.Log;
26import com.android.internal.telephony.Phone;
27import com.android.phone.PhoneGlobals;
28
29public class OtaspSimStateReceiver extends BroadcastReceiver {
30    private static final String TAG = OtaspSimStateReceiver.class.getSimpleName();
31    private static final boolean DBG = true;
32    private Context mContext;
33
34    private PhoneStateListener mPhoneStateListener = new PhoneStateListener(){
35        @Override
36        public void onOtaspChanged(int otaspMode) {
37            logd("onOtaspChanged: otaspMode=" + otaspMode);
38            if (otaspMode == TelephonyManager.OTASP_NEEDED) {
39                logd("otasp activation required, start otaspActivationService");
40                mContext.startService(new Intent(mContext, OtaspActivationService.class));
41            } else if (otaspMode == TelephonyManager.OTASP_NOT_NEEDED) {
42                OtaspActivationService.updateActivationState(mContext, true);
43            }
44        }
45    };
46
47    /**
48     * check if OTA service provisioning activation is supported by the current carrier
49     * @return true if otasp activation is needed, false otherwise
50     */
51    private static boolean isCarrierSupported() {
52        final Phone phone = PhoneGlobals.getPhone();
53        final Context context = phone.getContext();
54        if (context != null) {
55            PersistableBundle b = null;
56            final CarrierConfigManager configManager = (CarrierConfigManager) context
57                    .getSystemService(Context.CARRIER_CONFIG_SERVICE);
58            if (configManager != null) {
59                b = configManager.getConfig();
60            }
61            if (b != null && b.getBoolean(
62                    CarrierConfigManager.KEY_USE_OTASP_FOR_PROVISIONING_BOOL)) {
63                return true;
64            }
65        }
66        logd("otasp activation not needed: no supported carrier");
67        return false;
68    }
69
70    @Override
71    public void onReceive(Context context, Intent intent) {
72        mContext = context;
73        if(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
74            if (DBG) logd("Received intent: " + intent.getAction());
75            if (PhoneGlobals.getPhone().getIccRecordsLoaded() && isCarrierSupported()) {
76                final TelephonyManager telephonyManager = TelephonyManager.from(context);
77                telephonyManager.listen(mPhoneStateListener,
78                        PhoneStateListener.LISTEN_OTASP_CHANGED);
79            }
80        }
81    }
82
83    private static void logd(String s) {
84        Log.d(TAG, s);
85    }
86}
87
88