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 CheckBox mNotifyView; 38 private CheckBox mSyncContactsView; 39 private CheckBox mSyncCalendarView; 40 private CheckBox mSyncEmailView; 41 private CheckBox mBackgroundAttachmentsView; 42 private View mAccountSyncWindowRow; 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 51 public static AccountSetupOptionsFragment newInstance() { 52 return new AccountSetupOptionsFragment(); 53 } 54 55 @Override 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 mAccountSyncWindowRow = UiUtilities.getView(view, R.id.account_sync_window_row); 72 73 return view; 74 } 75 76 @Override 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<SpinnerOption>(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 */ 131 private void enableLookbackSpinner(Account account) { 132 // Show everything 133 mAccountSyncWindowRow.setVisibility(View.VISIBLE); 134 135 // Generate spinner entries using XML arrays used by the preferences 136 final CharSequence[] windowValues = getResources().getTextArray( 137 R.array.account_settings_mail_window_values); 138 final CharSequence[] windowEntries = getResources().getTextArray( 139 R.array.account_settings_mail_window_entries); 140 141 // Find a proper maximum for email lookback, based on policy (if we have one) 142 int maxEntry = windowEntries.length; 143 final Policy policy = account.mPolicy; 144 if (policy != null) { 145 final int maxLookback = policy.mMaxEmailLookback; 146 if (maxLookback != 0) { 147 // Offset/Code 0 1 2 3 4 5 148 // Entries auto, 1 day, 3 day, 1 week, 2 week, 1 month 149 // Lookback N/A 1 day, 3 day, 1 week, 2 week, 1 month 150 // Since our test below is i < maxEntry, we must set maxEntry to maxLookback + 1 151 maxEntry = maxLookback + 1; 152 } 153 } 154 155 // Now create the array used by the Spinner 156 final SpinnerOption[] windowOptions = new SpinnerOption[maxEntry]; 157 int defaultIndex = -1; 158 for (int i = 0; i < maxEntry; i++) { 159 final int value = Integer.valueOf(windowValues[i].toString()); 160 windowOptions[i] = new SpinnerOption(value, windowEntries[i].toString()); 161 if (value == SYNC_WINDOW_EAS_DEFAULT) { 162 defaultIndex = i; 163 } 164 } 165 166 final ArrayAdapter<SpinnerOption> windowOptionsAdapter = 167 new ArrayAdapter<SpinnerOption>(getActivity(), android.R.layout.simple_spinner_item, 168 windowOptions); 169 windowOptionsAdapter 170 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 171 mSyncWindowView.setAdapter(windowOptionsAdapter); 172 173 SpinnerOption.setSpinnerOptionValue(mSyncWindowView, account.getSyncLookback()); 174 if (defaultIndex >= 0) { 175 mSyncWindowView.setSelection(defaultIndex); 176 } 177 } 178 179 public boolean getBackgroundAttachmentsValue() { 180 return mBackgroundAttachmentsView.isChecked(); 181 } 182 183 public Integer getCheckFrequencyValue() { 184 return (Integer)((SpinnerOption)mCheckFrequencyView.getSelectedItem()).value; 185 } 186 187 /** 188 * @return Sync window value or null if view is hidden 189 */ 190 public Integer getAccountSyncWindowValue() { 191 if (mAccountSyncWindowRow.getVisibility() != View.VISIBLE) { 192 return null; 193 } 194 return (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value; 195 } 196 197 public boolean getSyncEmailValue() { 198 return mSyncEmailView.isChecked(); 199 } 200 201 public boolean getSyncCalendarValue() { 202 return mSyncCalendarView.isChecked(); 203 } 204 205 public boolean getSyncContactsValue() { 206 return mSyncContactsView.isChecked(); 207 } 208 209 public boolean getNotifyValue() { 210 return mNotifyView.isChecked(); 211 } 212 } 213