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.Handler; 25 import android.os.Message; 26 import android.os.SystemProperties; 27 import android.provider.Settings; 28 import android.telephony.PhoneStateListener; 29 import android.telephony.TelephonyManager; 30 import android.util.Log; 31 32 /* 33 * Handles OTA Start procedure at phone power up. At phone power up, if phone is not OTA 34 * provisioned (check MIN value of the Phone) and 'device_provisioned' is not set, 35 * OTA Activation screen is shown that helps user activate the phone 36 */ 37 public class OtaStartupReceiver extends BroadcastReceiver { 38 private static final String TAG = "OtaStartupReceiver"; 39 private static final boolean DBG = true; 40 private static final int MIN_READY = 10; 41 private Context mContext; 42 43 /** 44 * For debug purposes we're listening for otaspChanged events as 45 * this may be be used in the future for deciding if OTASP is 46 * necessary. 47 */ 48 private int mOtaspMode = -1; 49 private boolean mPhoneStateListenerRegistered = false; 50 private PhoneStateListener mPhoneStateListener = new PhoneStateListener() { 51 @Override 52 public void onOtaspChanged(int otaspMode) { 53 mOtaspMode = otaspMode; 54 Log.v(TAG, "onOtaspChanged: mOtaspMode=" + mOtaspMode); 55 } 56 }; 57 58 59 private Handler mHandler = new Handler() { 60 @Override 61 public void handleMessage(Message msg) { 62 switch (msg.what) { 63 case MIN_READY: 64 Log.v(TAG, "Attempting OtaActivation from handler, mOtaspMode=" + mOtaspMode); 65 OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY); 66 } 67 } 68 }; 69 70 @Override 71 public void onReceive(Context context, Intent intent) { 72 mContext = context; 73 if (DBG) { 74 Log.v(TAG, "onReceive: intent action=" + intent.getAction() + 75 " mOtaspMode=" + mOtaspMode); 76 } 77 78 if (!TelephonyCapabilities.supportsOtasp(PhoneApp.getPhone())) { 79 if (DBG) Log.d(TAG, "OTASP not supported, nothing to do."); 80 return; 81 } 82 83 if (mPhoneStateListenerRegistered == false) { 84 if (DBG) Log.d(TAG, "Register our PhoneStateListener"); 85 TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService( 86 Context.TELEPHONY_SERVICE); 87 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_OTASP_CHANGED); 88 mPhoneStateListenerRegistered = true; 89 } else { 90 if (DBG) Log.d(TAG, "PhoneStateListener already registered"); 91 } 92 93 if (shouldPostpone(context)) { 94 if (DBG) Log.d(TAG, "Postponing OTASP until wizard runs"); 95 return; 96 } 97 98 // The following depends on the phone process being persistent. Normally we can't 99 // expect a BroadcastReceiver to persist after returning from this function but it does 100 // because the phone activity is persistent. 101 if (DBG) Log.d(TAG, "call OtaUtils.maybeDoOtaCall"); 102 OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY); 103 } 104 105 /** 106 * On devices that provide a phone initialization wizard (such as Google Setup Wizard), we 107 * allow delaying CDMA OTA setup so it can be done in a single wizard. The wizard is responsible 108 * for (1) disabling itself once it has been run and/or (2) setting the 'device_provisioned' 109 * flag to something non-zero and (3) calling the OTA Setup with the action below. 110 * 111 * NB: Typical phone initialization wizards will install themselves as the homescreen 112 * (category "android.intent.category.HOME") with a priority higher than the default. 113 * The wizard should set 'device_provisioned' when it completes, disable itself with the 114 * PackageManager.setComponentEnabledSetting() and then start home screen. 115 * 116 * @return true if setup will be handled by wizard, false if it should be done now. 117 */ 118 private boolean shouldPostpone(Context context) { 119 Intent intent = new Intent("android.intent.action.DEVICE_INITIALIZATION_WIZARD"); 120 ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, 121 PackageManager.MATCH_DEFAULT_ONLY); 122 boolean provisioned = Settings.Secure.getInt(context.getContentResolver(), 123 Settings.Secure.DEVICE_PROVISIONED, 0) != 0; 124 String mode = SystemProperties.get("ro.setupwizard.mode", "REQUIRED"); 125 boolean runningSetupWizard = "REQUIRED".equals(mode) || "OPTIONAL".equals(mode); 126 if (DBG) { 127 Log.v(TAG, "resolvInfo = " + resolveInfo + ", provisioned = " + provisioned 128 + ", runningSetupWizard = " + runningSetupWizard); 129 } 130 return resolveInfo != null && !provisioned && runningSetupWizard; 131 } 132 } 133