• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.phone;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.AsyncResult;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.os.SystemProperties;
28 import android.provider.Settings;
29 import android.telephony.PhoneStateListener;
30 import android.telephony.ServiceState;
31 import android.telephony.TelephonyManager;
32 
33 import com.android.internal.telephony.Phone;
34 import com.android.internal.telephony.TelephonyCapabilities;
35 
36 import 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  */
43 public 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 = PhoneApp.getPhone();
90                         phone.unregisterForServiceStateChanged(this);
91                         OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
92                     }
93                     break;
94                 }
95             }
96 
97         }
98     };
99 
100     @Override
onReceive(Context context, Intent intent)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         if (!TelephonyCapabilities.supportsOtasp(PhoneApp.getPhone())) {
109             if (DBG) Log.d(TAG, "OTASP not supported, nothing to do.");
110             return;
111         }
112 
113         if (mPhoneStateListenerRegistered == false) {
114             if (DBG) Log.d(TAG, "Register our PhoneStateListener");
115             TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(
116                     Context.TELEPHONY_SERVICE);
117             telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_OTASP_CHANGED);
118             mPhoneStateListenerRegistered = true;
119         } else {
120             if (DBG) Log.d(TAG, "PhoneStateListener already registered");
121         }
122 
123         if (shouldPostpone(context)) {
124             if (DBG) Log.d(TAG, "Postponing OTASP until wizard runs");
125             return;
126         }
127 
128         // Delay OTA provisioning if network is not available yet
129         PhoneApp app = PhoneApp.getInstance();
130         Phone phone = PhoneApp.getPhone();
131         if (app.mCM.getServiceState() != ServiceState.STATE_IN_SERVICE) {
132             if (DBG) Log.w(TAG, "Network is not ready. Registering to receive notification.");
133             phone.registerForServiceStateChanged(mHandler, SERVICE_STATE_CHANGED, null);
134             return;
135         }
136 
137         // The following depends on the phone process being persistent. Normally we can't
138         // expect a BroadcastReceiver to persist after returning from this function but it does
139         // because the phone activity is persistent.
140         if (DBG) Log.d(TAG, "call OtaUtils.maybeDoOtaCall");
141         OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
142     }
143 
144     /**
145      * On devices that provide a phone initialization wizard (such as Google Setup Wizard), we
146      * allow delaying CDMA OTA setup so it can be done in a single wizard. The wizard is responsible
147      * for (1) disabling itself once it has been run and/or (2) setting the 'device_provisioned'
148      * flag to something non-zero and (3) calling the OTA Setup with the action below.
149      *
150      * NB: Typical phone initialization wizards will install themselves as the homescreen
151      * (category "android.intent.category.HOME") with a priority higher than the default.
152      * The wizard should set 'device_provisioned' when it completes, disable itself with the
153      * PackageManager.setComponentEnabledSetting() and then start home screen.
154      *
155      * @return true if setup will be handled by wizard, false if it should be done now.
156      */
shouldPostpone(Context context)157     private boolean shouldPostpone(Context context) {
158         Intent intent = new Intent("android.intent.action.DEVICE_INITIALIZATION_WIZARD");
159         ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
160                 PackageManager.MATCH_DEFAULT_ONLY);
161         boolean provisioned = Settings.Secure.getInt(context.getContentResolver(),
162                 Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
163         String mode = SystemProperties.get("ro.setupwizard.mode", "REQUIRED");
164         boolean runningSetupWizard = "REQUIRED".equals(mode) || "OPTIONAL".equals(mode);
165         if (DBG) {
166             Log.v(TAG, "resolvInfo = " + resolveInfo + ", provisioned = " + provisioned
167                     + ", runningSetupWizard = " + runningSetupWizard);
168         }
169         return resolveInfo != null && !provisioned && runningSetupWizard;
170     }
171 }
172