AccountAuthenticatorActivity.java revision 756b735e9312ee52618158270f0bdd0ec691a712
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 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 use an activity 26 * to handle the request then it can have the activity extend AccountAuthenticatorActivity. 27 * The AbstractAccountAuthenticator passes in the response to the intent using the following: 28 * <pre> 29 * intent.putExtra(Constants.ACCOUNT_AUTHENTICATOR_RESPONSE_KEY, response); 30 * </pre> 31 * The activity then sets the result that is to be handed to the response via 32 * {@link #setAccountAuthenticatorResult(android.os.Bundle)}. 33 * This result will be sent as the result of the request when the activity finishes. If this 34 * is never set or if it is set to null then error {@link AccountManager#ERROR_CODE_CANCELED} 35 * will be called on the response. 36 */ 37public class AccountAuthenticatorActivity extends Activity { 38 private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null; 39 private Bundle mResultBundle = null; 40 41 /** 42 * Set the result that is to be sent as the result of the request that caused this 43 * Activity to be launched. If result is null or this method is never called then 44 * the request will be canceled. 45 * @param result this is returned as the result of the AbstractAccountAuthenticator request 46 */ 47 public final void setAccountAuthenticatorResult(Bundle result) { 48 mResultBundle = result; 49 } 50 51 /** 52 * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the 53 * icicle is non-zero. 54 * @param icicle the save instance data of this Activity, may be null 55 */ 56 protected void onCreate(Bundle icicle) { 57 super.onCreate(icicle); 58 59 if (icicle == null) { 60 Intent intent = getIntent(); 61 mAccountAuthenticatorResponse = 62 intent.getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE); 63 } else { 64 mAccountAuthenticatorResponse = 65 icicle.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE); 66 } 67 68 if (mAccountAuthenticatorResponse != null) { 69 mAccountAuthenticatorResponse.onRequestContinued(); 70 } 71 } 72 73 /** 74 * Saves the AccountAuthenticatorResponse in the instance state. 75 * @param outState where to store any instance data 76 */ 77 protected void onSaveInstanceState(Bundle outState) { 78 outState.putParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, 79 mAccountAuthenticatorResponse); 80 super.onSaveInstanceState(outState); 81 } 82 83 /** 84 * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present. 85 */ 86 public void finish() { 87 if (mAccountAuthenticatorResponse != null) { 88 // send the result bundle back if set, otherwise send an error. 89 if (mResultBundle != null) { 90 mAccountAuthenticatorResponse.onResult(mResultBundle); 91 } else { 92 mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); 93 } 94 mAccountAuthenticatorResponse = null; 95 } 96 super.finish(); 97 } 98} 99