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.preference;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.preference.DialogPreference;
23 import android.util.AttributeSet;
24 import android.view.View;
25 
26 import com.android.contacts.model.account.AccountInfo;
27 import com.android.contacts.model.account.AccountWithDataSet;
28 import com.android.contacts.util.AccountsListAdapter;
29 
30 import java.util.List;
31 
32 public class DefaultAccountPreference extends DialogPreference {
33     private ContactsPreferences mPreferences;
34     private AccountsListAdapter mListAdapter;
35     private List<AccountInfo> mAccounts;
36     private int mChosenIndex = -1;
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 
setAccounts(List<AccountInfo> accounts)48     public void setAccounts(List<AccountInfo> accounts) {
49         mAccounts = accounts;
50         if (mListAdapter != null) {
51             mListAdapter.setAccounts(accounts, null);
52             notifyChanged();
53         }
54     }
55 
56     @Override
onCreateDialogView()57     protected View onCreateDialogView() {
58         prepare();
59         return super.onCreateDialogView();
60     }
61 
prepare()62     private void prepare() {
63         mPreferences = new ContactsPreferences(getContext());
64         mListAdapter = new AccountsListAdapter(getContext());
65         if (mAccounts != null) {
66             mListAdapter.setAccounts(mAccounts, 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         final AccountWithDataSet defaultAccount = mPreferences.getDefaultAccount();
78         if (defaultAccount == null || mAccounts == null ||
79                 !AccountInfo.contains(mAccounts, defaultAccount)) {
80             return null;
81         } else {
82             return AccountInfo.getAccount(mAccounts, defaultAccount).getNameLabel();
83         }
84     }
85 
86     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)87     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
88         super.onPrepareDialogBuilder(builder);
89         // UX recommendation is not to show buttons on such lists.
90         builder.setNegativeButton(null, null);
91         builder.setPositiveButton(null, null);
92         builder.setAdapter(mListAdapter, new DialogInterface.OnClickListener() {
93             @Override
94             public void onClick(DialogInterface dialog, int which) {
95                 mChosenIndex = which;
96             }
97         });
98     }
99 
100     @Override
onDialogClosed(boolean positiveResult)101     protected void onDialogClosed(boolean positiveResult) {
102         final AccountWithDataSet currentDefault = mPreferences.getDefaultAccount();
103 
104         if (mChosenIndex != -1) {
105             final AccountWithDataSet chosenAccount = mListAdapter.getItem(mChosenIndex);
106             if (!chosenAccount.equals(currentDefault)) {
107                 mPreferences.setDefaultAccount(chosenAccount);
108                 notifyChanged();
109             }
110         } // else the user dismissed this dialog so leave the preference unchanged.
111     }
112 }
113