AccountSettings.java revision 71cc035c55079bf89283d0acd4ff2712f75a82e1
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
17package com.android.email.activity.setup;
18
19import com.android.email.Email;
20import com.android.email.R;
21import com.android.email.activity.Welcome;
22import com.android.email.mail.Sender;
23import com.android.email.mail.Store;
24import com.android.email.provider.EmailContent.Account;
25import com.android.email.provider.EmailContent.AccountColumns;
26import com.android.email.provider.EmailContent.HostAuth;
27
28import android.app.Activity;
29import android.content.Intent;
30import android.database.Cursor;
31import android.os.Bundle;
32import android.util.Log;
33import android.view.KeyEvent;
34
35/**
36 * TODO: This implements preferences for a single account.  We need to move this to the
37 * newer "header" oriented preferences UI for proper XL operation;  The open question is the
38 * desired phone UX behavior.
39 *
40 * TODO: Move all "Restore" ops & other queries out of lifecycle methods and out of UI thread
41 */
42public class AccountSettings extends Activity implements AccountSettingsFragment.Callback {
43    // NOTE: This string must match the one in res/xml/account_preferences.xml
44    private static final String ACTION_ACCOUNT_MANAGER_ENTRY =
45        "com.android.email.activity.setup.ACCOUNT_MANAGER_ENTRY";
46    // NOTE: This constant should eventually be defined in android.accounts.Constants, but for
47    // now we define it here
48    private static final String ACCOUNT_MANAGER_EXTRA_ACCOUNT = "account";
49    private static final String EXTRA_ACCOUNT_ID = "account_id";
50
51    // UI values
52    /* package */ AccountSettingsFragment mFragment;
53
54    // Account data values
55    private long mAccountId = -1;
56    private Account mAccount;
57
58    /**
59     * Display (and edit) settings for a specific account
60     */
61    public static void actionSettings(Activity fromActivity, long accountId) {
62        Intent i = new Intent(fromActivity, AccountSettings.class);
63        i.putExtra(EXTRA_ACCOUNT_ID, accountId);
64        fromActivity.startActivity(i);
65    }
66
67    @Override
68    public void onCreate(Bundle savedInstanceState) {
69        super.onCreate(savedInstanceState);
70
71        Intent i = getIntent();
72        if (ACTION_ACCOUNT_MANAGER_ENTRY.equals(i.getAction())) {
73            // This case occurs if we're changing account settings from Settings -> Accounts
74            setAccountIdFromAccountManagerIntent();
75        } else {
76            // Otherwise, we're called from within the Email app and look for our extra
77            mAccountId = i.getLongExtra(EXTRA_ACCOUNT_ID, -1);
78        }
79
80        // If there's no accountId, we're done
81        if (mAccountId == -1) {
82            finish();
83            return;
84        }
85
86        mAccount = Account.restoreAccountWithId(this, mAccountId);
87        // Similarly, if the account has been deleted
88        if (mAccount == null) {
89            finish();
90            return;
91        }
92        mAccount.mHostAuthRecv = HostAuth.restoreHostAuthWithId(this, mAccount.mHostAuthKeyRecv);
93        mAccount.mHostAuthSend = HostAuth.restoreHostAuthWithId(this, mAccount.mHostAuthKeySend);
94        // Or if HostAuth's have been deleted
95        if (mAccount.mHostAuthRecv == null || mAccount.mHostAuthSend == null) {
96            finish();
97            return;
98        }
99
100        // Now set up the UI and the fragment
101        setContentView(R.layout.account_settings);
102        mFragment = (AccountSettingsFragment) findFragmentById(R.id.settings_fragment);
103        mFragment.setCallback(this);
104        mFragment.setAccount(mAccount);
105    }
106
107    private void setAccountIdFromAccountManagerIntent() {
108        // First, get the AccountManager account that we've been ask to handle
109        android.accounts.Account acct =
110            (android.accounts.Account)getIntent()
111            .getParcelableExtra(ACCOUNT_MANAGER_EXTRA_ACCOUNT);
112        // Find a HostAuth using eas and whose login is the name of the AccountManager account
113        Cursor c = getContentResolver().query(Account.CONTENT_URI,
114                new String[] {AccountColumns.ID}, AccountColumns.EMAIL_ADDRESS + "=?",
115                new String[] {acct.name}, null);
116        try {
117            if (c.moveToFirst()) {
118                mAccountId = c.getLong(0);
119            }
120        } finally {
121            c.close();
122        }
123    }
124
125    @Override
126    public void onResume() {
127        super.onResume();
128
129        // Exit immediately if the accounts list has changed (e.g. externally deleted)
130        if (Email.getNotifyUiAccountsChanged()) {
131            Welcome.actionStart(this);
132            finish();
133            return;
134        }
135    }
136
137    @Override
138    public boolean onKeyDown(int keyCode, KeyEvent event) {
139        if (keyCode == KeyEvent.KEYCODE_BACK) {
140            mFragment.saveSettings();
141        }
142        return super.onKeyDown(keyCode, event);
143    }
144
145    /**
146     * Implements AccountSettingsFragment.Callback
147     */
148    @Override
149    public void onIncomingSettings() {
150        try {
151            Store store = Store.getInstance(mAccount.getStoreUri(this), getApplication(), null);
152            if (store != null) {
153                Class<? extends android.app.Activity> setting = store.getSettingActivityClass();
154                if (setting != null) {
155                    java.lang.reflect.Method m = setting.getMethod("actionEditIncomingSettings",
156                            Activity.class, int.class, Account.class);
157                    m.invoke(null, this, SetupData.FLOW_MODE_EDIT, mAccount);
158                }
159            }
160        } catch (Exception e) {
161            Log.d(Email.LOG_TAG, "Error while trying to invoke store settings.", e);
162        }
163    }
164
165    /**
166     * Implements AccountSettingsFragment.Callback
167     */
168    @Override
169    public void onOutgoingSettings() {
170        try {
171            Sender sender = Sender.getInstance(getApplication(), mAccount.getSenderUri(this));
172            if (sender != null) {
173                Class<? extends android.app.Activity> setting = sender.getSettingActivityClass();
174                if (setting != null) {
175                    java.lang.reflect.Method m = setting.getMethod("actionEditOutgoingSettings",
176                            Activity.class, int.class, Account.class);
177                    m.invoke(null, this, SetupData.FLOW_MODE_EDIT, mAccount);
178                }
179            }
180        } catch (Exception e) {
181            Log.d(Email.LOG_TAG, "Error while trying to invoke sender settings.", e);
182        }
183    }
184
185    /**
186     * Implements AccountSettingsFragment.Callback
187     */
188    @Override
189    public void abandonEdit() {
190        finish();
191    }
192}
193