1a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana/*
2a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * Copyright (C) 2009 The Android Open Source Project
3a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana *
4a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * Licensed under the Apache License, Version 2.0 (the "License");
5a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * you may not use this file except in compliance with the License.
6a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * You may obtain a copy of the License at
7a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana *
8a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana *      http://www.apache.org/licenses/LICENSE-2.0
9a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana *
10a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * Unless required by applicable law or agreed to in writing, software
11a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * distributed under the License is distributed on an "AS IS" BASIS,
12a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * See the License for the specific language governing permissions and
14a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * limitations under the License.
15a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana */
16a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
17a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintanapackage android.accounts;
18a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
19a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintanaimport android.app.Activity;
20a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintanaimport android.content.Intent;
21a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintanaimport android.os.Bundle;
22a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
23a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana/**
24a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * Base class for implementing an Activity that is used to help implement an
25756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * AbstractAccountAuthenticator. If the AbstractAccountAuthenticator needs to use an activity
26756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * to handle the request then it can have the activity extend AccountAuthenticatorActivity.
27756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * The AbstractAccountAuthenticator passes in the response to the intent using the following:
28756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * <pre>
2946703b099516c383a6882815bcf9cd4df0ec538dBrian Carlstrom *      intent.putExtra({@link AccountManager#KEY_ACCOUNT_AUTHENTICATOR_RESPONSE}, response);
30756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * </pre>
31756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * The activity then sets the result that is to be handed to the response via
32756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * {@link #setAccountAuthenticatorResult(android.os.Bundle)}.
33a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana * This result will be sent as the result of the request when the activity finishes. If this
34756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * is never set or if it is set to null then error {@link AccountManager#ERROR_CODE_CANCELED}
35756b735e9312ee52618158270f0bdd0ec691a712Fred Quintana * will be called on the response.
36a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana */
37a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintanapublic class AccountAuthenticatorActivity extends Activity {
38a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
39a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    private Bundle mResultBundle = null;
40a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
41a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    /**
42a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * Set the result that is to be sent as the result of the request that caused this
43a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * Activity to be launched. If result is null or this method is never called then
44a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * the request will be canceled.
45a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * @param result this is returned as the result of the AbstractAccountAuthenticator request
46a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     */
47a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    public final void setAccountAuthenticatorResult(Bundle result) {
48a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        mResultBundle = result;
49a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    }
50a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
51a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    /**
52a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
53a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * icicle is non-zero.
54a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * @param icicle the save instance data of this Activity, may be null
55a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     */
56a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    protected void onCreate(Bundle icicle) {
57a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        super.onCreate(icicle);
58a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
5931957f1badbb900bbfe211317e1ea992d650a72dFred Quintana        mAccountAuthenticatorResponse =
6031957f1badbb900bbfe211317e1ea992d650a72dFred Quintana                getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
61a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
62a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        if (mAccountAuthenticatorResponse != null) {
63a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana            mAccountAuthenticatorResponse.onRequestContinued();
64a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        }
65a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    }
66a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana
67a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    /**
68a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
69a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana     */
70a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    public void finish() {
71a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        if (mAccountAuthenticatorResponse != null) {
72a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana            // send the result bundle back if set, otherwise send an error.
73a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana            if (mResultBundle != null) {
74a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana                mAccountAuthenticatorResponse.onResult(mResultBundle);
75a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana            } else {
7631957f1badbb900bbfe211317e1ea992d650a72dFred Quintana                mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
7731957f1badbb900bbfe211317e1ea992d650a72dFred Quintana                        "canceled");
78a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana            }
79a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana            mAccountAuthenticatorResponse = null;
80a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        }
81a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana        super.finish();
82a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana    }
83a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana}
84