1 /*
2  * Copyright (C) 2014 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.email.activity.setup;
18 
19 import android.os.Bundle;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.CheckBox;
25 import android.widget.Spinner;
26 
27 import com.android.email.R;
28 import com.android.email.activity.UiUtilities;
29 import com.android.email.service.EmailServiceUtils;
30 import com.android.emailcommon.provider.Account;
31 import com.android.emailcommon.provider.Policy;
32 import com.android.emailcommon.service.SyncWindow;
33 
34 public class AccountSetupOptionsFragment extends AccountSetupFragment {
35     private Spinner mCheckFrequencyView;
36     private Spinner mSyncWindowView;
37     private View mSyncwindowLabel;
38     private CheckBox mNotifyView;
39     private CheckBox mSyncContactsView;
40     private CheckBox mSyncCalendarView;
41     private CheckBox mSyncEmailView;
42     private CheckBox mBackgroundAttachmentsView;
43 
44     /** Default sync window for new EAS accounts */
45     private static final int SYNC_WINDOW_EAS_DEFAULT = SyncWindow.SYNC_WINDOW_1_WEEK;
46 
47     public interface Callback extends AccountSetupFragment.Callback {
48 
49     }
50 
newInstance()51     public static AccountSetupOptionsFragment newInstance() {
52         return new AccountSetupOptionsFragment();
53     }
54 
55     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)56     public View onCreateView(LayoutInflater inflater, ViewGroup container,
57             Bundle savedInstanceState) {
58         final View view = inflateTemplatedView(inflater, container,
59                 R.layout.account_setup_options_fragment, R.string.account_setup_options_headline);
60 
61         mCheckFrequencyView = UiUtilities.getView(view, R.id.account_check_frequency);
62         mSyncWindowView = UiUtilities.getView(view, R.id.account_sync_window);
63         mNotifyView = UiUtilities.getView(view, R.id.account_notify);
64         mNotifyView.setChecked(true);
65         mSyncContactsView = UiUtilities.getView(view, R.id.account_sync_contacts);
66         mSyncCalendarView = UiUtilities.getView(view, R.id.account_sync_calendar);
67         mSyncEmailView = UiUtilities.getView(view, R.id.account_sync_email);
68         mSyncEmailView.setChecked(true);
69         mBackgroundAttachmentsView = UiUtilities.getView(view, R.id.account_background_attachments);
70         mBackgroundAttachmentsView.setChecked(true);
71         mSyncwindowLabel = UiUtilities.getView(view, R.id.account_sync_window_label);
72 
73         return view;
74     }
75 
76     @Override
onActivityCreated(Bundle savedInstanceState)77     public void onActivityCreated(Bundle savedInstanceState) {
78         super.onActivityCreated(savedInstanceState);
79 
80         final View view = getView();
81 
82         final SetupDataFragment setupData =
83                 ((SetupDataFragment.SetupDataContainer) getActivity()).getSetupData();
84         final Account account = setupData.getAccount();
85 
86         final EmailServiceUtils.EmailServiceInfo serviceInfo =
87                 setupData.getIncomingServiceInfo(getActivity());
88 
89         final CharSequence[] frequencyValues = serviceInfo.syncIntervals;
90         final CharSequence[] frequencyEntries = serviceInfo.syncIntervalStrings;
91 
92         // Now create the array used by the sync interval Spinner
93         final SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length];
94         for (int i = 0; i < frequencyEntries.length; i++) {
95             checkFrequencies[i] = new SpinnerOption(
96                     Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString());
97         }
98         final ArrayAdapter<SpinnerOption> checkFrequenciesAdapter =
99                 new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item,
100                         checkFrequencies);
101         checkFrequenciesAdapter
102                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
103         mCheckFrequencyView.setAdapter(checkFrequenciesAdapter);
104         SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, account.getSyncInterval());
105 
106         if (serviceInfo.offerLookback) {
107             enableLookbackSpinner(account);
108         }
109 
110         if (serviceInfo.syncContacts) {
111             mSyncContactsView.setVisibility(View.VISIBLE);
112             mSyncContactsView.setChecked(true);
113             UiUtilities.setVisibilitySafe(view, R.id.account_sync_contacts_divider, View.VISIBLE);
114         }
115         if (serviceInfo.syncCalendar) {
116             mSyncCalendarView.setVisibility(View.VISIBLE);
117             mSyncCalendarView.setChecked(true);
118             UiUtilities.setVisibilitySafe(view, R.id.account_sync_calendar_divider, View.VISIBLE);
119         }
120 
121         if (!serviceInfo.offerAttachmentPreload) {
122             mBackgroundAttachmentsView.setVisibility(View.GONE);
123             UiUtilities.setVisibilitySafe(view, R.id.account_background_attachments_divider,
124                     View.GONE);
125         }
126     }
127 
128     /**
129      * Enable an additional spinner using the arrays normally handled by preferences
130      */
enableLookbackSpinner(Account account)131     private void enableLookbackSpinner(Account account) {
132         // Show everything
133         mSyncWindowView.setVisibility(View.VISIBLE);
134         mSyncwindowLabel.setVisibility(View.VISIBLE);
135 
136         // Generate spinner entries using XML arrays used by the preferences
137         final CharSequence[] windowValues = getResources().getTextArray(
138                 R.array.account_settings_mail_window_values);
139         final CharSequence[] windowEntries = getResources().getTextArray(
140                 R.array.account_settings_mail_window_entries);
141 
142         // Find a proper maximum for email lookback, based on policy (if we have one)
143         int maxEntry = windowEntries.length;
144         final Policy policy = account.mPolicy;
145         if (policy != null) {
146             final int maxLookback = policy.mMaxEmailLookback;
147             if (maxLookback != 0) {
148                 // Offset/Code   0      1      2      3      4        5
149                 // Entries      auto, 1 day, 3 day, 1 week, 2 week, 1 month
150                 // Lookback     N/A   1 day, 3 day, 1 week, 2 week, 1 month
151                 // Since our test below is i < maxEntry, we must set maxEntry to maxLookback + 1
152                 maxEntry = maxLookback + 1;
153             }
154         }
155 
156         // Now create the array used by the Spinner
157         final SpinnerOption[] windowOptions = new SpinnerOption[maxEntry];
158         int defaultIndex = -1;
159         for (int i = 0; i < maxEntry; i++) {
160             final int value = Integer.valueOf(windowValues[i].toString());
161             windowOptions[i] = new SpinnerOption(value, windowEntries[i].toString());
162             if (value == SYNC_WINDOW_EAS_DEFAULT) {
163                 defaultIndex = i;
164             }
165         }
166 
167         final ArrayAdapter<SpinnerOption> windowOptionsAdapter =
168                 new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item,
169                         windowOptions);
170         windowOptionsAdapter
171                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
172         mSyncWindowView.setAdapter(windowOptionsAdapter);
173 
174         SpinnerOption.setSpinnerOptionValue(mSyncWindowView, account.getSyncLookback());
175         if (defaultIndex >= 0) {
176             mSyncWindowView.setSelection(defaultIndex);
177         }
178     }
179 
getBackgroundAttachmentsValue()180     public boolean getBackgroundAttachmentsValue() {
181         return mBackgroundAttachmentsView.isChecked();
182     }
183 
getCheckFrequencyValue()184     public Integer getCheckFrequencyValue() {
185         return (Integer)((SpinnerOption)mCheckFrequencyView.getSelectedItem()).value;
186     }
187 
188     /**
189      * @return Sync window value or null if view is hidden
190      */
getAccountSyncWindowValue()191     public Integer getAccountSyncWindowValue() {
192         if (mSyncWindowView.getVisibility() != View.VISIBLE) {
193             return null;
194         }
195         return (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value;
196     }
197 
getSyncEmailValue()198     public boolean getSyncEmailValue() {
199         return mSyncEmailView.isChecked();
200     }
201 
getSyncCalendarValue()202     public boolean getSyncCalendarValue() {
203         return mSyncCalendarView.isChecked();
204     }
205 
getSyncContactsValue()206     public boolean getSyncContactsValue() {
207         return mSyncContactsView.isChecked();
208     }
209 
getNotifyValue()210     public boolean getNotifyValue() {
211         return mNotifyView.isChecked();
212     }
213 }
214