1/*
2 * Copyright (C) 2010 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.tests;
18
19import android.app.Activity;
20import android.app.PendingIntent;
21import android.content.ActivityNotFoundException;
22import android.content.Intent;
23import android.os.Bundle;
24import android.util.Log;
25import android.view.View;
26import android.widget.Button;
27import android.widget.ProgressBar;
28import android.widget.TextView;
29
30import com.android.phone.OtaUtils;
31
32/**
33 * Test activity that mimics the PERFORM_CDMA_PROVISIONING behavior of
34 * SetupWizard, useful for testing "non-interactive" OTASP.
35 * @see OtaUtils.startNonInteractiveOtasp
36 *
37 */
38public class OtaspTestActivity extends Activity implements View.OnClickListener {
39    private static final String LOG_TAG = "OtaspTestActivity";
40
41    // Request code used with startActivityForResult()
42    private static final int PERFORM_CDMA_PROVISIONING_REQUEST_CODE = 1;
43
44    // UI elements
45    private TextView mLabel;
46    private ProgressBar mProgressBar;
47    private TextView mResult;
48    private Button mButton;
49
50    @Override
51    protected void onCreate(Bundle savedInstanceState) {
52        Intent intent = getIntent();
53        Log.i(LOG_TAG, "##### onCreate: intent = " + intent);
54        Bundle extras = intent.getExtras();
55        if (extras != null) {
56            Log.i(LOG_TAG, "      - has extras: size = " + extras.size()); // forces an unparcel()
57            Log.i(LOG_TAG, "      - extras = " + extras);
58        }
59
60        // Construct our basic UI:
61        super.onCreate(savedInstanceState);
62        setContentView(R.layout.otasp_test_activity);
63
64        mLabel = (TextView) findViewById(R.id.label1);
65        mLabel.setText("OTA Test Activity");
66
67        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
68        mResult = (TextView) findViewById(R.id.result1);
69
70        mButton = (Button) findViewById(R.id.button1);
71        mButton.setText("Make test call");
72        mButton.setOnClickListener(this);
73
74
75        // We can be launched either:
76        //
77        // (1) Directly from the launcher, in which case the current intent
78        //     will simply be an ACTION_MAIN intent
79        //
80        // (2) Via the PendingIntent that we sent along (when we originally
81        //     fired off the ACTION_PERFORM_CDMA_PROVISIONING intent) that
82        //     allows the phone app to send us back a result code.
83        //     We can identify this case by the presence of the
84        //     EXTRA_OTASP_RESULT_CODE extra.
85
86        if (intent.hasExtra(OtaUtils.EXTRA_OTASP_RESULT_CODE)) {
87            // Got a result from the OTASP call!
88            Log.i(LOG_TAG, "==> onCreate: got a result from the OTASP call!");
89
90            int resultCode = intent.getIntExtra(OtaUtils.EXTRA_OTASP_RESULT_CODE,
91                                                OtaUtils.OTASP_UNKNOWN);
92            Log.i(LOG_TAG, "    - resultCode = " + resultCode);
93
94            String resultString;
95            switch (resultCode) {
96                case OtaUtils.OTASP_USER_SKIPPED:
97                    resultString = "User skipped!";
98                    break;
99                case OtaUtils.OTASP_SUCCESS:
100                    resultString = "Success!";
101                    break;
102                case OtaUtils.OTASP_FAILURE:
103                    resultString = "FAILURE";
104                    break;
105                default:
106                    resultString = "Unexpected code: " + resultCode;
107                    break;
108            }
109            Log.i(LOG_TAG, "    - result: " + resultString);
110            mResult.setText(resultString);
111            mResult.setVisibility(View.VISIBLE);
112            mProgressBar.setVisibility(View.INVISIBLE);
113
114        } else {
115            // We must have gotten here directly from the launcher.
116            // Leave the UI in its initial state.
117            Log.i(LOG_TAG, "==> onCreate: entered from the launcher.");
118        }
119    }
120
121    @Override
122    protected void onNewIntent(Intent intent) {
123        Log.i(LOG_TAG, "onNewIntent: intent=" + intent);
124        Bundle extras = intent.getExtras();
125        if (extras != null) Log.i(LOG_TAG, "      - intent extras = " + extras);
126
127        // This method isn't actually used since this test activity is not
128        // launched in "singleTop" mode.
129
130        // Activities that *are* launched in singleTop mode, like the SetupWizard,
131        // would have to handle the various PendingIntents here just like
132        // we do above in onCreate().
133    }
134
135    @Override
136    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
137        Log.i(LOG_TAG, "onActivityResult: request " + requestCode
138              + " result " + resultCode + " data " + data);
139
140        // Note we receive this call immediately before onResume(), when
141        // we get re-started after launching the PERFORM_CDMA_PROVISIONING
142        // intent.
143
144        if (requestCode == PERFORM_CDMA_PROVISIONING_REQUEST_CODE) {
145            // The InCallScreenShowActivation activity can set the following
146            // result codes:
147            //
148            //   RESULT_INTERACTIVE_OTASP_STARTED
149            //   RESULT_NONINTERACTIVE_OTASP_STARTED
150            //   RESULT_NONINTERACTIVE_OTASP_FAILED
151            //
152            // but note that in practice we won't ever *get* the
153            // RESULT_INTERACTIVE_OTASP_STARTED result code, since the
154            // "interactive" OTASP sequence never actually finish()es;
155            // it ends by directly launching the Home activity.
156            //
157            // However, in non-interactive OTASP, the
158            // InCallScreenShowActivation activity will set one of the
159            // RESULT_NONINTERACTIVE_* codes and immediately
160            // finish(), so we *will* see that result here.
161            //
162            // Also, resultCode will be RESULT_CANCELED (= 0) if the
163            // InCallScreenShowActivation activity didn't return any
164            // result, or crashed.
165
166            switch (resultCode) {
167                case OtaUtils.RESULT_INTERACTIVE_OTASP_STARTED:
168                    Log.i(LOG_TAG, "==> onActivityResult: INTERACTIVE_OTASP_STARTED");
169                    break;
170                case OtaUtils.RESULT_NONINTERACTIVE_OTASP_STARTED:
171                    Log.i(LOG_TAG, "==> onActivityResult: NONINTERACTIVE_OTASP_STARTED");
172                    break;
173                case OtaUtils.RESULT_NONINTERACTIVE_OTASP_FAILED:
174                    Log.w(LOG_TAG, "==> onActivityResult: NONINTERACTIVE_OTASP_FAILED");
175                    // This means we couldn't even *initiate* an outgoing call
176                    // to start the OTASP process.  Not sure what could cause this.
177                    // TODO: Update UI to indicate the error.
178                    break;
179                case RESULT_CANCELED:
180                    Log.i(LOG_TAG, "==> onActivityResult: CANCELED");
181                    break;
182                default:
183                    Log.i(LOG_TAG, "==> onActivityResult: unknown result: " + resultCode);
184                    break;
185            }
186        }
187    }
188
189    @Override
190    protected void onResume() {
191        Log.i(LOG_TAG, "onResume()...");
192        super.onResume();
193    }
194
195    @Override
196    protected void onPause() {
197        Log.i(LOG_TAG, "onPause()...");
198        super.onPause();
199    }
200
201    // View.OnClickListener implementation
202    @Override
203    public void onClick(View view) {
204        int id = view.getId();
205        Log.i(LOG_TAG, "onClick(View " + view + ", id " + id + ")...");
206
207        switch (id) {
208            case R.id.button1:
209                Log.i(LOG_TAG, "onClick: button1...");
210                makeTestCall();
211                break;
212            default:
213                Log.w(LOG_TAG, "onClick: unexpected View: " + view);
214                break;
215        }
216    }
217
218    private void makeTestCall() {
219        Log.i(LOG_TAG, "##### makeTestCall()...");
220
221        mProgressBar.setVisibility(View.VISIBLE);
222        mResult.setVisibility(View.INVISIBLE);
223
224        try {
225            Intent performProvisioningIntent =
226                    new Intent(OtaUtils.ACTION_PERFORM_CDMA_PROVISIONING);
227
228            // Set the magic extra to force "non-interactive mode" for the
229            // OTASP call.
230            performProvisioningIntent.putExtra(OtaUtils.EXTRA_OVERRIDE_INTERACTIVE_MODE, false);
231
232            // Pass a PendingIntent along with the
233            // ACTION_PERFORM_CDMA_PROVISIONING intent, which allows
234            // results to be sent back to us.
235            Intent resultIntent = new Intent(this, this.getClass());
236            PendingIntent pendingResultIntent =
237                    PendingIntent.getActivity(this, 0,
238                                              resultIntent, 0);
239            performProvisioningIntent.putExtra(OtaUtils.EXTRA_OTASP_RESULT_CODE_PENDING_INTENT,
240                                               pendingResultIntent);
241
242            Log.i(LOG_TAG, "- Firing off PERFORM_CDMA_PROVISIONING intent: "
243                  + performProvisioningIntent);
244            Bundle extras = performProvisioningIntent.getExtras();
245            if (extras != null) Log.i(LOG_TAG, "      - intent extras = " + extras);
246
247            // Originally, we would simply call
248            //     startActivity(performProvisioningIntent);
249            // to launch the InCallScreenShowActivation activity and *not* expect
250            // a result.  (Note that calling the plain startActivity()
251            // method *guarantees* that your onActivityResult() method
252            // will NOT be called at all.)
253
254            // Now, we ask for a result:
255            startActivityForResult(performProvisioningIntent,
256                                   PERFORM_CDMA_PROVISIONING_REQUEST_CODE);
257
258            // On a non-voice-capable device, the InCallScreenShowActivation activity
259            // will kick off the OTASP call and immediately return, passing
260            // the code RESULT_STARTED_NONINTERACTIVE_OTASP to our
261            // onActivityResult method.
262
263        } catch (ActivityNotFoundException e) {
264            Log.w(LOG_TAG, "Couldn't show activiation UI; ActivityNotFoundException: " + e);
265        }
266    }
267}
268