AccountSetupOptions.java revision 9197f428946d5c77613b64a50c93a46e2acf62dc
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.Account;
20import com.android.email.Email;
21import com.android.email.Preferences;
22import com.android.email.R;
23import com.android.email.mail.Store;
24
25import android.app.Activity;
26import android.content.Intent;
27import android.os.Bundle;
28import android.view.View;
29import android.view.View.OnClickListener;
30import android.widget.ArrayAdapter;
31import android.widget.CheckBox;
32import android.widget.Spinner;
33
34public class AccountSetupOptions extends Activity implements OnClickListener {
35    private static final String EXTRA_ACCOUNT = "account";
36
37    private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
38
39    private Spinner mCheckFrequencyView;
40    private Spinner mSyncWindowView;
41
42    private CheckBox mDefaultView;
43
44    private CheckBox mNotifyView;
45
46    private Account mAccount;
47
48    public static void actionOptions(Activity fromActivity, Account account, boolean makeDefault) {
49        Intent i = new Intent(fromActivity, AccountSetupOptions.class);
50        i.putExtra(EXTRA_ACCOUNT, account);
51        i.putExtra(EXTRA_MAKE_DEFAULT, makeDefault);
52        fromActivity.startActivity(i);
53    }
54
55    @Override
56    public void onCreate(Bundle savedInstanceState) {
57        super.onCreate(savedInstanceState);
58        setContentView(R.layout.account_setup_options);
59
60        mCheckFrequencyView = (Spinner)findViewById(R.id.account_check_frequency);
61        mSyncWindowView = (Spinner) findViewById(R.id.account_sync_window);
62        mDefaultView = (CheckBox)findViewById(R.id.account_default);
63        mNotifyView = (CheckBox)findViewById(R.id.account_notify);
64
65        findViewById(R.id.next).setOnClickListener(this);
66
67        mAccount = (Account)getIntent().getSerializableExtra(EXTRA_ACCOUNT);
68        boolean makeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
69
70        // Generate spinner entries using XML arrays used by the preferences
71        int frequencyValuesId;
72        int frequencyEntriesId;
73        Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(), this);
74        if (info.mPushSupported) {
75            frequencyValuesId = R.array.account_settings_check_frequency_values_push;
76            frequencyEntriesId = R.array.account_settings_check_frequency_entries_push;
77        } else {
78            frequencyValuesId = R.array.account_settings_check_frequency_values;
79            frequencyEntriesId = R.array.account_settings_check_frequency_entries;
80        }
81        CharSequence[] frequencyValues = getResources().getTextArray(frequencyValuesId);
82        CharSequence[] frequencyEntries = getResources().getTextArray(frequencyEntriesId);
83
84        // Now create the array used by the Spinner
85        SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length];
86        for (int i = 0; i < frequencyEntries.length; i++) {
87            checkFrequencies[i] = new SpinnerOption(
88                    Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString());
89        }
90
91        ArrayAdapter<SpinnerOption> checkFrequenciesAdapter = new ArrayAdapter<SpinnerOption>(this,
92                android.R.layout.simple_spinner_item, checkFrequencies);
93        checkFrequenciesAdapter
94                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
95        mCheckFrequencyView.setAdapter(checkFrequenciesAdapter);
96
97        if (info.mVisibleLimitDefault == -1) {
98            enableEASSyncWindowSpinner();
99        }
100
101        if (mAccount.equals(Preferences.getPreferences(this).getDefaultAccount()) || makeDefault) {
102            mDefaultView.setChecked(true);
103        }
104        mNotifyView.setChecked(mAccount.isNotifyNewMail());
105        SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, mAccount
106                .getAutomaticCheckIntervalMinutes());
107    }
108
109    private void onDone() {
110        mAccount.setDescription(mAccount.getEmail());
111        mAccount.setNotifyNewMail(mNotifyView.isChecked());
112        mAccount.setAutomaticCheckIntervalMinutes((Integer)((SpinnerOption)mCheckFrequencyView
113                .getSelectedItem()).value);
114        if (mSyncWindowView.getVisibility() == View.VISIBLE) {
115            int window = (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value;
116            mAccount.setSyncWindow(window);
117        }
118        mAccount.save(Preferences.getPreferences(this));
119        if (mDefaultView.isChecked()) {
120            Preferences.getPreferences(this).setDefaultAccount(mAccount);
121        }
122        Email.setServicesEnabled(this);
123        AccountSetupNames.actionSetNames(this, mAccount);
124        finish();
125    }
126
127    public void onClick(View v) {
128        switch (v.getId()) {
129            case R.id.next:
130                onDone();
131                break;
132        }
133    }
134
135    /**
136     * Enable an additional spinner using the arrays normally handled by preferences
137     */
138    private void enableEASSyncWindowSpinner() {
139        // Show everything
140        findViewById(R.id.account_sync_window_label).setVisibility(View.VISIBLE);
141        mSyncWindowView.setVisibility(View.VISIBLE);
142
143        // Generate spinner entries using XML arrays used by the preferences
144        CharSequence[] windowValues = getResources().getTextArray(
145                R.array.account_settings_mail_window_values);
146        CharSequence[] windowEntries = getResources().getTextArray(
147                R.array.account_settings_mail_window_entries);
148
149        // Now create the array used by the Spinner
150        SpinnerOption[] windowOptions = new SpinnerOption[windowEntries.length];
151        for (int i = 0; i < windowEntries.length; i++) {
152            windowOptions[i] = new SpinnerOption(
153                    Integer.valueOf(windowValues[i].toString()), windowEntries[i].toString());
154        }
155
156        ArrayAdapter<SpinnerOption> windowOptionsAdapter = new ArrayAdapter<SpinnerOption>(this,
157                android.R.layout.simple_spinner_item, windowOptions);
158        windowOptionsAdapter
159                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
160        mSyncWindowView.setAdapter(windowOptionsAdapter);
161
162        SpinnerOption.setSpinnerOptionValue(mSyncWindowView, mAccount.getSyncWindow());
163    }
164}
165