AccountSetupOptions.java revision ea6fea9bb22368c10083d5dce52adae86e51a243
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
41    private CheckBox mDefaultView;
42
43    private CheckBox mNotifyView;
44
45    private Account mAccount;
46
47    public static void actionOptions(Activity fromActivity, Account account, boolean makeDefault) {
48        Intent i = new Intent(fromActivity, AccountSetupOptions.class);
49        i.putExtra(EXTRA_ACCOUNT, account);
50        i.putExtra(EXTRA_MAKE_DEFAULT, makeDefault);
51        fromActivity.startActivity(i);
52    }
53
54    @Override
55    public void onCreate(Bundle savedInstanceState) {
56        super.onCreate(savedInstanceState);
57        setContentView(R.layout.account_setup_options);
58
59        mCheckFrequencyView = (Spinner)findViewById(R.id.account_check_frequency);
60        mDefaultView = (CheckBox)findViewById(R.id.account_default);
61        mNotifyView = (CheckBox)findViewById(R.id.account_notify);
62
63        findViewById(R.id.next).setOnClickListener(this);
64
65        mAccount = (Account)getIntent().getSerializableExtra(EXTRA_ACCOUNT);
66        boolean makeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
67
68        // Generate spinner entries using XML arrays used by the preferences
69        int frequencyValuesId;
70        int frequencyEntriesId;
71        Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(), this);
72        if (info.mPushSupported) {
73            frequencyValuesId = R.array.account_settings_check_frequency_values_push;
74            frequencyEntriesId = R.array.account_settings_check_frequency_entries_push;
75        } else {
76            frequencyValuesId = R.array.account_settings_check_frequency_values;
77            frequencyEntriesId = R.array.account_settings_check_frequency_entries;
78        }
79        CharSequence[] frequencyValues = getResources().getTextArray(frequencyValuesId);
80        CharSequence[] frequencyEntries = getResources().getTextArray(frequencyEntriesId);
81
82        // Now create the array used by the Spinner
83        SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length];
84        for (int i = 0; i < frequencyEntries.length; i++) {
85            checkFrequencies[i] = new SpinnerOption(
86                    Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString());
87        }
88
89        ArrayAdapter<SpinnerOption> checkFrequenciesAdapter = new ArrayAdapter<SpinnerOption>(this,
90                android.R.layout.simple_spinner_item, checkFrequencies);
91        checkFrequenciesAdapter
92                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
93        mCheckFrequencyView.setAdapter(checkFrequenciesAdapter);
94
95        if (mAccount.equals(Preferences.getPreferences(this).getDefaultAccount()) || makeDefault) {
96            mDefaultView.setChecked(true);
97        }
98        mNotifyView.setChecked(mAccount.isNotifyNewMail());
99        SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, mAccount
100                .getAutomaticCheckIntervalMinutes());
101    }
102
103    private void onDone() {
104        mAccount.setDescription(mAccount.getEmail());
105        mAccount.setNotifyNewMail(mNotifyView.isChecked());
106        mAccount.setAutomaticCheckIntervalMinutes((Integer)((SpinnerOption)mCheckFrequencyView
107                .getSelectedItem()).value);
108        mAccount.save(Preferences.getPreferences(this));
109        if (mDefaultView.isChecked()) {
110            Preferences.getPreferences(this).setDefaultAccount(mAccount);
111        }
112        Email.setServicesEnabled(this);
113        AccountSetupNames.actionSetNames(this, mAccount);
114        finish();
115    }
116
117    public void onClick(View v) {
118        switch (v.getId()) {
119            case R.id.next:
120                onDone();
121                break;
122        }
123    }
124}
125