Merge branch 'material_toolbar' of https://github.com/owncloud/android into material_...
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountAuthenticatorActivity.java
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
17 package com.owncloud.android.authentication;
18
19 import android.accounts.AccountAuthenticatorResponse;
20 import android.accounts.AccountManager;
21 import android.os.Bundle;
22 import android.support.v7.app.ActionBarActivity;
23 import android.support.v7.app.AppCompatActivity;
24
25 /*
26 * Base class for implementing an Activity that is used to help implement an AbstractAccountAuthenticator.
27 * If the AbstractAccountAuthenticator needs to use an activity to handle the request then it can have the activity extend
28 * AccountAuthenticatorActivity. The AbstractAccountAuthenticator passes in the response to the intent using the following:
29 * intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
30 *
31 * The activity then sets the result that is to be handed to the response via setAccountAuthenticatorResult(android.os.Bundle).
32 * This result will be sent as the result of the request when the activity finishes. If this is never set or if it is set to null
33 * then error AccountManager.ERROR_CODE_CANCELED will be called on the response.
34 */
35
36 public class AccountAuthenticatorActivity extends AppCompatActivity {
37
38 private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
39 private Bundle mResultBundle = null;
40
41
42 /**
43 * Set the result that is to be sent as the result of the request that caused this Activity to be launched.
44 * If result is null or this method is never called then the request will be canceled.
45 *
46 * @param result this is returned as the result of the AbstractAccountAuthenticator request
47 */
48 public final void setAccountAuthenticatorResult(Bundle result) {
49 mResultBundle = result;
50 }
51
52 /**
53 * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
54 * icicle is non-zero.
55 * @param icicle the save instance data of this Activity, may be null
56 */
57 protected void onCreate(Bundle icicle) {
58 super.onCreate(icicle);
59
60 mAccountAuthenticatorResponse =
61 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
62
63 if (mAccountAuthenticatorResponse != null) {
64 mAccountAuthenticatorResponse.onRequestContinued();
65 }
66 }
67
68 /**
69 * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
70 */
71 public void finish() {
72 if (mAccountAuthenticatorResponse != null) {
73 // send the result bundle back if set, otherwise send an error.
74 if (mResultBundle != null) {
75 mAccountAuthenticatorResponse.onResult(mResultBundle);
76 } else {
77 mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
78 "canceled");
79 }
80 mAccountAuthenticatorResponse = null;
81 }
82 super.finish();
83 }
84 }