1/*
2 * Copyright (C) 2009 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
17package com.android.phone;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.os.AsyncResult;
25import android.os.Handler;
26import android.os.Message;
27import android.os.SystemProperties;
28import android.provider.Settings;
29import android.telephony.PhoneStateListener;
30import android.telephony.ServiceState;
31import android.telephony.TelephonyManager;
32
33import com.android.internal.telephony.Phone;
34import com.android.internal.telephony.TelephonyCapabilities;
35
36import android.util.Log;
37
38/*
39 * Handles OTA Start procedure at phone power up. At phone power up, if phone is not OTA
40 * provisioned (check MIN value of the Phone) and 'device_provisioned' is not set,
41 * OTA Activation screen is shown that helps user activate the phone
42 */
43public class OtaStartupReceiver extends BroadcastReceiver {
44    private static final String TAG = "OtaStartupReceiver";
45    private static final boolean DBG = false;
46    private static final int MIN_READY = 10;
47    private static final int SERVICE_STATE_CHANGED = 11;
48    private Context mContext;
49
50    /**
51     * For debug purposes we're listening for otaspChanged events as
52     * this may be be used in the future for deciding if OTASP is
53     * necessary.
54     */
55    private int mOtaspMode = -1;
56    private boolean mPhoneStateListenerRegistered = false;
57    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
58        @Override
59        public void onOtaspChanged(int otaspMode) {
60            mOtaspMode = otaspMode;
61            Log.v(TAG, "onOtaspChanged: mOtaspMode=" + mOtaspMode);
62        }
63    };
64
65
66    private Handler mHandler = new Handler() {
67        @Override
68        public void handleMessage(Message msg) {
69            switch (msg.what) {
70                case MIN_READY:
71                    Log.v(TAG, "Attempting OtaActivation from handler, mOtaspMode=" + mOtaspMode);
72                    OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
73                    break;
74                case SERVICE_STATE_CHANGED: {
75                    ServiceState state = (ServiceState) ((AsyncResult) msg.obj).result;
76                    if (DBG) Log.d(TAG, "onServiceStateChanged()...  new state = " + state);
77
78                    // Possible service states:
79                    // - STATE_IN_SERVICE        // Normal operation
80                    // - STATE_OUT_OF_SERVICE    // Still searching for an operator to register to,
81                    //                           // or no radio signal
82                    // - STATE_EMERGENCY_ONLY    // Phone is locked; only emergency numbers are allowed
83                    // - STATE_POWER_OFF         // Radio is explicitly powered off (airplane mode)
84
85                    // Once we reach STATE_IN_SERVICE
86                    // it's finally OK to start OTA provisioning
87                    if (state.getState() == ServiceState.STATE_IN_SERVICE) {
88                        if (DBG) Log.d(TAG, "call OtaUtils.maybeDoOtaCall after network is available");
89                        Phone phone = PhoneGlobals.getPhone();
90                        phone.unregisterForServiceStateChanged(this);
91                        OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
92                    }
93                    break;
94                }
95            }
96
97        }
98    };
99
100    @Override
101    public void onReceive(Context context, Intent intent) {
102        mContext = context;
103        if (DBG) {
104            Log.v(TAG, "onReceive: intent action=" + intent.getAction() +
105                    "  mOtaspMode=" + mOtaspMode);
106        }
107
108        PhoneGlobals globals = PhoneGlobals.getInstanceIfPrimary();
109        if (globals == null) {
110            if (DBG) Log.d(TAG, "Not primary user, nothing to do.");
111            return;
112        }
113
114        if (!TelephonyCapabilities.supportsOtasp(PhoneGlobals.getPhone())) {
115            if (DBG) Log.d(TAG, "OTASP not supported, nothing to do.");
116            return;
117        }
118
119        if (mPhoneStateListenerRegistered == false) {
120            if (DBG) Log.d(TAG, "Register our PhoneStateListener");
121            TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(
122                    Context.TELEPHONY_SERVICE);
123            telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_OTASP_CHANGED);
124            mPhoneStateListenerRegistered = true;
125        } else {
126            if (DBG) Log.d(TAG, "PhoneStateListener already registered");
127        }
128
129        if (shouldPostpone(context)) {
130            if (DBG) Log.d(TAG, "Postponing OTASP until wizard runs");
131            return;
132        }
133
134        // Delay OTA provisioning if network is not available yet
135        PhoneGlobals app = PhoneGlobals.getInstance();
136        Phone phone = PhoneGlobals.getPhone();
137        if (app.mCM.getServiceState() != ServiceState.STATE_IN_SERVICE) {
138            if (DBG) Log.w(TAG, "Network is not ready. Registering to receive notification.");
139            phone.registerForServiceStateChanged(mHandler, SERVICE_STATE_CHANGED, null);
140            return;
141        }
142
143        // The following depends on the phone process being persistent. Normally we can't
144        // expect a BroadcastReceiver to persist after returning from this function but it does
145        // because the phone activity is persistent.
146        if (DBG) Log.d(TAG, "call OtaUtils.maybeDoOtaCall");
147        OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
148    }
149
150    /**
151     * On devices that provide a phone initialization wizard (such as Google Setup Wizard), we
152     * allow delaying CDMA OTA setup so it can be done in a single wizard. The wizard is responsible
153     * for (1) disabling itself once it has been run and/or (2) setting the 'device_provisioned'
154     * flag to something non-zero and (3) calling the OTA Setup with the action below.
155     *
156     * NB: Typical phone initialization wizards will install themselves as the homescreen
157     * (category "android.intent.category.HOME") with a priority higher than the default.
158     * The wizard should set 'device_provisioned' when it completes, disable itself with the
159     * PackageManager.setComponentEnabledSetting() and then start home screen.
160     *
161     * @return true if setup will be handled by wizard, false if it should be done now.
162     */
163    private boolean shouldPostpone(Context context) {
164        Intent intent = new Intent("android.intent.action.DEVICE_INITIALIZATION_WIZARD");
165        ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
166                PackageManager.MATCH_DEFAULT_ONLY);
167        boolean provisioned = Settings.Global.getInt(context.getContentResolver(),
168                Settings.Global.DEVICE_PROVISIONED, 0) != 0;
169        String mode = SystemProperties.get("ro.setupwizard.mode", "REQUIRED");
170        boolean runningSetupWizard = "REQUIRED".equals(mode) || "OPTIONAL".equals(mode);
171        if (DBG) {
172            Log.v(TAG, "resolvInfo = " + resolveInfo + ", provisioned = " + provisioned
173                    + ", runningSetupWizard = " + runningSetupWizard);
174        }
175        return resolveInfo != null && !provisioned && runningSetupWizard;
176    }
177}
178