OSDN Git Service

Fix account type dialog re-creation on orientation change.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / accounts / AddAccountSettings.java
1 /*
2  * Copyright (C) 2008 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.android.settings.accounts;
18
19 import android.accounts.AccountManager;
20 import android.accounts.AccountManagerCallback;
21 import android.accounts.AccountManagerFuture;
22 import android.accounts.AuthenticatorException;
23 import android.accounts.OperationCanceledException;
24 import android.app.Activity;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.util.Log;
28
29 import java.io.IOException;
30
31 /**
32  * Entry point Actiivty for account setup. Works as follows
33  *
34  * 1) When the other Activities launch this Activity, it launches {@link ChooseAccountActivity}
35  *    without showing anything.
36  * 2) After receiving an account type from ChooseAccountActivity, this Activity launches the
37  *    account setup specified by AccountManager.
38  * 3) After the account setup, this Activity finishes without showing anything.
39  *
40  * Note:
41  * Previously this Activity did what {@link ChooseAccountActivity} does right now, but we
42  * currently delegate the work to the other Activity. When we let this Activity do that work, users
43  * would see the list of account types when leaving this Activity, since the UI is already ready
44  * when returning from each account setup, which doesn't look good.
45  */
46 public class AddAccountSettings extends Activity {
47     private static final String TAG = "AccountSettings";
48
49     /* package */ static final String EXTRA_SELECTED_ACCOUNT = "selected_account";
50
51     private static final int CHOOSE_ACCOUNT_REQUEST = 1;
52
53     private AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
54         public void run(AccountManagerFuture<Bundle> future) {
55             try {
56                 Bundle bundle = future.getResult();
57                 bundle.keySet();
58                 setResult(RESULT_OK);
59                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "account added: " + bundle);
60             } catch (OperationCanceledException e) {
61                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "addAccount was canceled");
62             } catch (IOException e) {
63                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "addAccount failed: " + e);
64             } catch (AuthenticatorException e) {
65                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "addAccount failed: " + e);
66             } finally {
67                 finish();
68             }
69         }
70     };
71
72     @Override
73     public void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75
76         final String[] authorities =
77                 getIntent().getStringArrayExtra(AccountPreferenceBase.AUTHORITIES_FILTER_KEY);
78         final String[] accountTypes =
79                 getIntent().getStringArrayExtra(AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY);
80         final Intent intent = new Intent(this, ChooseAccountActivity.class);
81         if (authorities != null) {
82             intent.putExtra(AccountPreferenceBase.AUTHORITIES_FILTER_KEY, authorities);
83         }
84         if (accountTypes != null) {
85             intent.putExtra(AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY, accountTypes);
86         }
87         startActivityForResult(intent, CHOOSE_ACCOUNT_REQUEST);
88     }
89
90     @Override
91     public void onActivityResult(int requestCode, int resultCode, Intent data) {
92         switch (requestCode) {
93         case CHOOSE_ACCOUNT_REQUEST:
94             if (resultCode == RESULT_CANCELED) {
95                 setResult(resultCode);
96                 finish();
97                 return;
98             }
99             // Go to account setup screen. finish() is called inside mCallback.
100             addAccount(data.getStringExtra(EXTRA_SELECTED_ACCOUNT));
101             break;
102         }
103     }
104
105     private void addAccount(String accountType) {
106         AccountManager.get(this).addAccount(
107                 accountType,
108                 null, /* authTokenType */
109                 null, /* requiredFeatures */
110                 null, /* addAccountOptions */
111                 this,
112                 mCallback,
113                 null /* handler */);
114     }
115 }