blob: e3c93450d0038f19000648d5a55ac317b955f555 [file] [log] [blame]
Santos Cordon83570472013-09-06 15:45:10 -07001/*
2 * Copyright (C) 2013 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.app.Activity;
20import android.app.AlertDialog;
21import android.app.PendingIntent;
22import android.app.PendingIntent.CanceledException;
Santos Cordon83570472013-09-06 15:45:10 -070023import android.content.DialogInterface;
24import android.content.Intent;
Santos Cordon83570472013-09-06 15:45:10 -070025import android.os.Bundle;
Santos Cordon83570472013-09-06 15:45:10 -070026import android.util.Log;
27
Santos Cordon83570472013-09-06 15:45:10 -070028/**
29 * Starts and displays status for Hands Free Activation (HFA).
30 *
Santos Cordon00d7a432013-09-17 14:07:14 -070031 * This class operates with Hands Free Activation apps. It comes up during activation
32 * requests that occur outside of setup wizard and so provides its own UI.
33 * It uses {@link HfaLogic} to perform the actual activation and during the process
34 * displays a "performing activation..." dialog. This will remain up until the user
35 * chooses to skip the activation (still happens in the background) or the activation
36 * is successful. Upon failure, the dialog also goes away but a subsequent dialog will
37 * ask the user if they would like to try again or cancel.
Santos Cordon83570472013-09-06 15:45:10 -070038 */
39public class HfaActivity extends Activity {
40 private static final String TAG = HfaActivity.class.getSimpleName();
41
42 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
43
Santos Cordon83570472013-09-06 15:45:10 -070044 public static final int OTASP_UNKNOWN = 0;
45 public static final int OTASP_USER_SKIPPED = 1;
46 public static final int OTASP_SUCCESS = 2;
47 public static final int OTASP_FAILURE = 3;
48
Santos Cordon83570472013-09-06 15:45:10 -070049 private boolean mCanSkip;
50 private AlertDialog mDialog;
Santos Cordon00d7a432013-09-17 14:07:14 -070051 private HfaLogic mHfaLogic;
Santos Cordon83570472013-09-06 15:45:10 -070052
53 @Override
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56
57 if (VERBOSE) Log.v(TAG, "onCreate");
58
Santos Cordon00d7a432013-09-17 14:07:14 -070059 mHfaLogic = new HfaLogic(this.getApplicationContext(), new HfaLogic.HfaLogicCallback() {
60 @Override
61 public void onSuccess() {
62 onHfaSuccess();
63 }
64
65 @Override
66 public void onError(String error) {
67 onHfaError(error);
68 }
69 });
70
Santos Cordon83570472013-09-06 15:45:10 -070071 startProvisioning();
72 }
73
74 @Override
75 protected void onDestroy() {
76 super.onDestroy();
77
78 if (VERBOSE) Log.v(TAG, "onDestroy");
79
80 if (mDialog != null && mDialog.isShowing()) {
81 mDialog.dismiss();
82 mDialog = null;
83 }
Santos Cordon83570472013-09-06 15:45:10 -070084 }
85
86 private void startProvisioning() {
87 buildAndShowDialog();
Santos Cordon00d7a432013-09-17 14:07:14 -070088 mHfaLogic.start();
Santos Cordon83570472013-09-06 15:45:10 -070089 }
90
91 private void buildAndShowDialog() {
92 mCanSkip = true;
93
94 mDialog = new AlertDialog.Builder(this)
95 .setTitle(R.string.ota_hfa_activation_title)
96 .setMessage(R.string.ota_hfa_activation_dialog_message)
97 .setPositiveButton(R.string.ota_skip_activation_dialog_skip_label,
98 new DialogInterface.OnClickListener() {
99 @Override
100 public void onClick(DialogInterface di, int which) {
101 if (mCanSkip) {
Santos Cordon00d7a432013-09-17 14:07:14 -0700102 sendFinalResponse(OTASP_USER_SKIPPED);
Santos Cordon83570472013-09-06 15:45:10 -0700103 }
104 }})
105 /*.setOnCancelListener(new DialogInterface.OnCancelListener() {
106 @Override
107 public void onCancel(DialogInterface di) {
Santos Cordon00d7a432013-09-17 14:07:14 -0700108 sendFinalResponse(OTASP_USER_SKIPPED);
Santos Cordon83570472013-09-06 15:45:10 -0700109 }})*/
110 .create();
111
Santos Cordon00d7a432013-09-17 14:07:14 -0700112 // Do not allow user to dismiss dialog unless they are clicking "skip"
113 mDialog.setCanceledOnTouchOutside(false);
114 mDialog.setCancelable(false);
115
Santos Cordon83570472013-09-06 15:45:10 -0700116 if (VERBOSE) Log.v(TAG, "showing dialog");
117 mDialog.show();
118 }
119
Santos Cordon83570472013-09-06 15:45:10 -0700120 private void onHfaError(String errorMsg) {
121 mDialog.dismiss();
122
123 AlertDialog errorDialog = new AlertDialog.Builder(this)
124 .setMessage(errorMsg)
125 .setPositiveButton(R.string.ota_skip_activation_dialog_skip_label,
126 new DialogInterface.OnClickListener() {
127 @Override
128 public void onClick(DialogInterface di, int which) {
129 di.dismiss();
Santos Cordon00d7a432013-09-17 14:07:14 -0700130 sendFinalResponse(OTASP_USER_SKIPPED);
Santos Cordon83570472013-09-06 15:45:10 -0700131 }
132 })
133 .setNegativeButton(R.string.ota_try_again,
134 new DialogInterface.OnClickListener() {
135 @Override
136 public void onClick(DialogInterface di, int which) {
137 di.dismiss();
138 startProvisioning();
139 }
140 })
141 .create();
142
143 errorDialog.show();
144 }
145
146 private void onHfaSuccess() {
147 // User can no longer skip after success.
148 mCanSkip = false;
149
Santos Cordon00d7a432013-09-17 14:07:14 -0700150 sendFinalResponse(OTASP_SUCCESS);
Santos Cordon83570472013-09-06 15:45:10 -0700151 }
152
Santos Cordon00d7a432013-09-17 14:07:14 -0700153 private void sendFinalResponse(int responseCode) {
Santos Cordon83570472013-09-06 15:45:10 -0700154 final PendingIntent otaResponseIntent = getIntent().getParcelableExtra(
155 OtaUtils.EXTRA_OTASP_RESULT_CODE_PENDING_INTENT);
156
Russell Brennerafce7ad2013-09-13 15:29:21 -0700157 if (otaResponseIntent != null) {
158 final Intent extraStuff = new Intent();
159 extraStuff.putExtra(OtaUtils.EXTRA_OTASP_RESULT_CODE, responseCode);
Santos Cordon83570472013-09-06 15:45:10 -0700160
Russell Brennerafce7ad2013-09-13 15:29:21 -0700161 try {
162 if (VERBOSE) Log.v(TAG, "Sending OTASP confirmation with result code: "
163 + responseCode);
164 otaResponseIntent.send(this, 0 /* resultCode (not used) */, extraStuff);
165 } catch (CanceledException e) {
166 Log.e(TAG, "Pending Intent canceled");
167 }
Santos Cordon83570472013-09-06 15:45:10 -0700168 }
169
170 finish();
171 }
Santos Cordon83570472013-09-06 15:45:10 -0700172}