AccountSetupOptions.java revision a290f503f14432163f74548a5e5d1dc5003ad049
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.mail.Store;
22import com.android.email.provider.EmailContent;
23
24import android.app.Activity;
25import android.content.Intent;
26import android.os.Bundle;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.ArrayAdapter;
30import android.widget.CheckBox;
31import android.widget.Spinner;
32
33public class AccountSetupOptions extends Activity implements OnClickListener {
34
35    private static final String EXTRA_ACCOUNT = "account";
36    private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
37
38    private Spinner mCheckFrequencyView;
39    private Spinner mSyncWindowView;
40    private CheckBox mDefaultView;
41    private CheckBox mNotifyView;
42    private EmailContent.Account mAccount;
43
44    public static void actionOptions(Activity fromActivity, EmailContent.Account account,
45            boolean makeDefault) {
46        Intent i = new Intent(fromActivity, AccountSetupOptions.class);
47        i.putExtra(EXTRA_ACCOUNT, account);
48        i.putExtra(EXTRA_MAKE_DEFAULT, makeDefault);
49        fromActivity.startActivity(i);
50    }
51
52    @Override
53    public void onCreate(Bundle savedInstanceState) {
54        super.onCreate(savedInstanceState);
55        setContentView(R.layout.account_setup_options);
56
57        mCheckFrequencyView = (Spinner)findViewById(R.id.account_check_frequency);
58        mSyncWindowView = (Spinner) findViewById(R.id.account_sync_window);
59        mDefaultView = (CheckBox)findViewById(R.id.account_default);
60        mNotifyView = (CheckBox)findViewById(R.id.account_notify);
61
62        findViewById(R.id.next).setOnClickListener(this);
63
64        mAccount = (EmailContent.Account) getIntent().getParcelableExtra(EXTRA_ACCOUNT);
65        boolean makeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
66
67        // Generate spinner entries using XML arrays used by the preferences
68        int frequencyValuesId;
69        int frequencyEntriesId;
70        Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(this), this);
71        if (info.mPushSupported) {
72            frequencyValuesId = R.array.account_settings_check_frequency_values_push;
73            frequencyEntriesId = R.array.account_settings_check_frequency_entries_push;
74        } else {
75            frequencyValuesId = R.array.account_settings_check_frequency_values;
76            frequencyEntriesId = R.array.account_settings_check_frequency_entries;
77        }
78        CharSequence[] frequencyValues = getResources().getTextArray(frequencyValuesId);
79        CharSequence[] frequencyEntries = getResources().getTextArray(frequencyEntriesId);
80
81        // Now create the array used by the Spinner
82        SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length];
83        for (int i = 0; i < frequencyEntries.length; i++) {
84            checkFrequencies[i] = new SpinnerOption(
85                    Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString());
86        }
87
88        ArrayAdapter<SpinnerOption> checkFrequenciesAdapter = new ArrayAdapter<SpinnerOption>(this,
89                android.R.layout.simple_spinner_item, checkFrequencies);
90        checkFrequenciesAdapter
91                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
92        mCheckFrequencyView.setAdapter(checkFrequenciesAdapter);
93
94        if (info.mVisibleLimitDefault == -1) {
95            enableEASSyncWindowSpinner();
96        }
97
98        if (mAccount.mIsDefault || makeDefault) {
99            mDefaultView.setChecked(true);
100        }
101        mNotifyView.setChecked(
102                (mAccount.getFlags() & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL) != 0);
103        SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, mAccount
104                .getAutomaticCheckIntervalMinutes());
105    }
106
107    private void onDone() {
108        mAccount.setDescription(mAccount.getEmail());
109        int newFlags = mAccount.getFlags() & ~(EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL);
110        if (mNotifyView.isChecked()) {
111            newFlags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL;
112        }
113        mAccount.setFlags(newFlags);
114        mAccount.setAutomaticCheckIntervalMinutes((Integer)((SpinnerOption)mCheckFrequencyView
115                .getSelectedItem()).value);
116        if (mSyncWindowView.getVisibility() == View.VISIBLE) {
117            int window = (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value;
118            mAccount.setSyncWindow(window);
119        }
120        mAccount.setDefaultAccount(mDefaultView.isChecked());
121        mAccount.saveOrUpdate(this);
122        Email.setServicesEnabled(this);
123        AccountSetupNames.actionSetNames(this, mAccount.mId);
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