1 /*
2  * Copyright (C) 2015 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.contacts.common.preference;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.preference.ListPreference;
22 import android.util.AttributeSet;
23 
24 import com.android.contacts.common.model.AccountTypeManager;
25 import com.android.contacts.common.model.account.AccountType;
26 import com.android.contacts.common.model.account.AccountTypeWithDataSet;
27 import com.android.contacts.common.model.account.AccountWithDataSet;
28 
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 
34 public class DefaultAccountPreference extends ListPreference {
35     private ContactsPreferences mPreferences;
36     private Map<String, AccountWithDataSet> mAccountMap;
37 
DefaultAccountPreference(Context context)38     public DefaultAccountPreference(Context context) {
39         super(context);
40         prepare();
41     }
42 
DefaultAccountPreference(Context context, AttributeSet attrs)43     public DefaultAccountPreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45         prepare();
46     }
47 
prepare()48     private void prepare() {
49         mPreferences = new ContactsPreferences(getContext());
50         mAccountMap = new HashMap<>();
51         final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(getContext());
52         List<AccountWithDataSet> accounts = accountTypeManager.getAccounts(true);
53         for (AccountWithDataSet account : accounts) {
54             mAccountMap.put(account.name, account);
55         }
56         final Set<String> accountNames = mAccountMap.keySet();
57         final String[] accountNamesArray = accountNames.toArray(new String[accountNames.size()]);
58         setEntries(accountNamesArray);
59         setEntryValues(accountNamesArray);
60         final String defaultAccount = String.valueOf(mPreferences.getDefaultAccount());
61         if (accounts.size() == 1) {
62             setValue(accounts.get(0).name);
63         } else if (accountNames.contains(defaultAccount)) {
64             setValue(defaultAccount);
65         } else {
66             setValue(null);
67         }
68     }
69 
70     @Override
shouldPersist()71     protected boolean shouldPersist() {
72         return false;   // This preference takes care of its own storage
73     }
74 
75     @Override
getSummary()76     public CharSequence getSummary() {
77         return mPreferences.getDefaultAccount();
78     }
79 
80     @Override
persistString(String value)81     protected boolean persistString(String value) {
82         if (value == null && mPreferences.getDefaultAccount() == null) {
83             return true;
84         }
85         if (value == null || mPreferences.getDefaultAccount() == null
86                 || !value.equals(mPreferences.getDefaultAccount())) {
87             mPreferences.setDefaultAccount(mAccountMap.get(value));
88             notifyChanged();
89         }
90         return true;
91     }
92 
93     @Override
94     // UX recommendation is not to show cancel button on such lists.
onPrepareDialogBuilder(AlertDialog.Builder builder)95     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
96         super.onPrepareDialogBuilder(builder);
97         builder.setNegativeButton(null, null);
98     }
99 }
100