blob: f9284e67f9c36d298fd720db2282d7fb357c882a [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;
Fred Quintanaa698f422009-04-08 19:14:54 -070020import android.os.Bundle;
21
22/**
23 * Base class for implementing an Activity that is used to help implement an
Fred Quintana756b7352009-10-21 13:43:10 -070024 * AbstractAccountAuthenticator. If the AbstractAccountAuthenticator needs to use an activity
25 * to handle the request then it can have the activity extend AccountAuthenticatorActivity.
26 * The AbstractAccountAuthenticator passes in the response to the intent using the following:
27 * <pre>
Brian Carlstrom46703b02011-04-06 15:41:29 -070028 * intent.putExtra({@link AccountManager#KEY_ACCOUNT_AUTHENTICATOR_RESPONSE}, response);
Fred Quintana756b7352009-10-21 13:43:10 -070029 * </pre>
30 * The activity then sets the result that is to be handed to the response via
31 * {@link #setAccountAuthenticatorResult(android.os.Bundle)}.
Fred Quintanaa698f422009-04-08 19:14:54 -070032 * This result will be sent as the result of the request when the activity finishes. If this
Fred Quintana756b7352009-10-21 13:43:10 -070033 * is never set or if it is set to null then error {@link AccountManager#ERROR_CODE_CANCELED}
34 * will be called on the response.
Fred Quintanaa698f422009-04-08 19:14:54 -070035 */
36public class AccountAuthenticatorActivity extends Activity {
37 private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
38 private Bundle mResultBundle = null;
39
40 /**
41 * Set the result that is to be sent as the result of the request that caused this
42 * Activity to be launched. If result is null or this method is never called then
43 * the request will be canceled.
44 * @param result this is returned as the result of the AbstractAccountAuthenticator request
45 */
46 public final void setAccountAuthenticatorResult(Bundle result) {
47 mResultBundle = result;
48 }
49
50 /**
51 * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
52 * icicle is non-zero.
53 * @param icicle the save instance data of this Activity, may be null
54 */
55 protected void onCreate(Bundle icicle) {
56 super.onCreate(icicle);
57
Fred Quintana31957f12009-10-21 13:43:10 -070058 mAccountAuthenticatorResponse =
59 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
Fred Quintanaa698f422009-04-08 19:14:54 -070060
61 if (mAccountAuthenticatorResponse != null) {
62 mAccountAuthenticatorResponse.onRequestContinued();
63 }
64 }
65
66 /**
Fred Quintanaa698f422009-04-08 19:14:54 -070067 * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
68 */
69 public void finish() {
70 if (mAccountAuthenticatorResponse != null) {
71 // send the result bundle back if set, otherwise send an error.
72 if (mResultBundle != null) {
73 mAccountAuthenticatorResponse.onResult(mResultBundle);
74 } else {
Fred Quintana31957f12009-10-21 13:43:10 -070075 mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
76 "canceled");
Fred Quintanaa698f422009-04-08 19:14:54 -070077 }
78 mAccountAuthenticatorResponse = null;
79 }
80 super.finish();
81 }
82}