blob: 0319ab9bdc9e0d596015aa12a7f2d054bf604f7c [file] [log] [blame]
Fred Quintanaa698f422009-04-08 19:14:54 -07001/*
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 android.accounts;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.os.Bundle;
22
23/**
24 * Base class for implementing an Activity that is used to help implement an
25 * AbstractAccountAuthenticator. If the AbstractAccountAuthenticator needs to return an Intent
26 * that is to be used to launch an Activity that needs to return results to satisfy an
27 * AbstractAccountAuthenticator request, it should store the AccountAuthenticatorResponse
28 * inside of the Intent as follows:
29 * <p>
30 * intent.putExtra(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY, response);
31 * <p>
32 * The activity that it launches should extend the AccountAuthenticatorActivity. If this
33 * activity has a result that satisfies the original request it sets it via:
34 * <p>
35 * setAccountAuthenticatorResult(result)
36 * <p>
37 * This result will be sent as the result of the request when the activity finishes. If this
38 * is never set or if it is set to null then the request will be canceled when the activity
39 * finishes.
40 */
41public class AccountAuthenticatorActivity extends Activity {
42 private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
43 private Bundle mResultBundle = null;
44
45 /**
46 * Set the result that is to be sent as the result of the request that caused this
47 * Activity to be launched. If result is null or this method is never called then
48 * the request will be canceled.
49 * @param result this is returned as the result of the AbstractAccountAuthenticator request
50 */
51 public final void setAccountAuthenticatorResult(Bundle result) {
52 mResultBundle = result;
53 }
54
55 /**
56 * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
57 * icicle is non-zero.
58 * @param icicle the save instance data of this Activity, may be null
59 */
60 protected void onCreate(Bundle icicle) {
61 super.onCreate(icicle);
62
63 if (icicle == null) {
64 Intent intent = getIntent();
65 mAccountAuthenticatorResponse =
66 intent.getParcelableExtra(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY);
67 } else {
68 mAccountAuthenticatorResponse =
69 icicle.getParcelable(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY);
70 }
71
72 if (mAccountAuthenticatorResponse != null) {
73 mAccountAuthenticatorResponse.onRequestContinued();
74 }
75 }
76
77 /**
78 * Saves the AccountAuthenticatorResponse in the instance state.
79 * @param outState where to store any instance data
80 */
81 protected void onSaveInstanceState(Bundle outState) {
82 outState.putParcelable(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY,
83 mAccountAuthenticatorResponse);
84 super.onSaveInstanceState(outState);
85 }
86
87 /**
88 * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
89 */
90 public void finish() {
91 if (mAccountAuthenticatorResponse != null) {
92 // send the result bundle back if set, otherwise send an error.
93 if (mResultBundle != null) {
94 mAccountAuthenticatorResponse.onResult(mResultBundle);
95 } else {
96 mAccountAuthenticatorResponse.onError(Constants.ERROR_CODE_CANCELED, "canceled");
97 }
98 mAccountAuthenticatorResponse = null;
99 }
100 super.finish();
101 }
102}