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.Handler;
25import android.os.Message;
26import android.os.SystemProperties;
27import android.provider.Settings;
28import android.text.TextUtils;
29import android.util.Log;
30
31/*
32 * Handles OTA Start procedure at phone power up. At phone power up, if phone is not OTA
33 * provisioned (check MIN value of the Phone) and 'device_provisioned' is not set,
34 * OTA Activation screen is shown that helps user activate the phone
35 */
36public class OtaStartupReceiver extends BroadcastReceiver {
37    private static final String TAG = "OtaStartupReceiver";
38    private static final boolean DBG = (SystemProperties.getInt("ro.debuggable", 0) == 1);
39    private static final int MIN_READY = 10;
40    private Context mContext;
41
42    private Handler mHandler = new Handler() {
43        @Override
44        public void handleMessage(Message msg) {
45            switch (msg.what) {
46            case MIN_READY:
47                Log.v(TAG, "Attempting OtaActivation from handler");
48                OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
49            }
50        }
51    };
52
53    public void onReceive(Context context, Intent intent) {
54        mContext = context;
55
56        if (!OtaUtils.isCdmaPhone()) {
57            if (DBG) Log.d(TAG, "Not a CDMA phone, no need to process OTA");
58            return;
59        }
60
61        if (shouldPostpone(context)) {
62            if (DBG) Log.d(TAG, "Postponing CDMA provisioning until wizard runs");
63            return;
64        }
65
66        // The following depends on the phone process being persistent. Normally we can't
67        // expect a BroadcastReceiver to persist after returning from this function but it does
68        // because the phone activity is persistent.
69        OtaUtils.maybeDoOtaCall(mContext, mHandler, MIN_READY);
70    }
71
72    /**
73     * On devices that provide a phone initialization wizard (such as Google Setup Wizard), we
74     * allow delaying CDMA OTA setup so it can be done in a single wizard. The wizard is responsible
75     * for (1) disabling itself once it has been run and/or (2) setting the 'device_provisioned'
76     * flag to something non-zero and (3) calling the OTA Setup with the action below.
77     *
78     * NB: Typical phone initialization wizards will install themselves as the homescreen
79     * (category "android.intent.category.HOME") with a priority higher than the default.
80     * The wizard should set 'device_provisioned' when it completes, disable itself with the
81     * PackageManager.setComponentEnabledSetting() and then start home screen.
82     *
83     * @return true if setup will be handled by wizard, false if it should be done now.
84     */
85    private boolean shouldPostpone(Context context) {
86        Intent intent = new Intent("android.intent.action.DEVICE_INITIALIZATION_WIZARD");
87        ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
88                PackageManager.MATCH_DEFAULT_ONLY);
89        boolean provisioned = Settings.Secure.getInt(context.getContentResolver(),
90                Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
91        String mode = SystemProperties.get("ro.setupwizard.mode", "REQUIRED");
92        boolean runningSetupWizard = "REQUIRED".equals(mode) || "OPTIONAL".equals(mode);
93        if (DBG) {
94            Log.v(TAG, "resolvInfo = " + resolveInfo + ", provisioned = " + provisioned
95                    + ", runningSetupWizard = " + runningSetupWizard);
96        }
97        return resolveInfo != null && !provisioned && runningSetupWizard;
98    }
99}
100