• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.phone.settings;
18 
19 import com.android.phone.R;
20 
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.os.UserHandle;
26 import android.preference.ListPreference;
27 import android.preference.Preference;
28 import android.telecom.PhoneAccountHandle;
29 import android.telecom.TelecomManager;
30 import android.util.AttributeSet;
31 
32 import java.util.List;
33 import java.util.Objects;
34 
35 public class AccountSelectionPreference extends ListPreference implements
36         Preference.OnPreferenceChangeListener {
37 
38     public interface AccountSelectionListener {
onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account)39         boolean onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account);
onAccountSelectionDialogShow(AccountSelectionPreference pref)40         void onAccountSelectionDialogShow(AccountSelectionPreference pref);
onAccountChanged(AccountSelectionPreference pref)41         void onAccountChanged(AccountSelectionPreference pref);
42     }
43 
44     private final Context mContext;
45     private AccountSelectionListener mListener;
46     private PhoneAccountHandle[] mAccounts;
47     private String[] mEntryValues;
48     private CharSequence[] mEntries;
49     private boolean mShowSelectionInSummary = true;
50 
AccountSelectionPreference(Context context)51     public AccountSelectionPreference(Context context) {
52         this(context, null);
53     }
54 
AccountSelectionPreference(Context context, AttributeSet attrs)55     public AccountSelectionPreference(Context context, AttributeSet attrs) {
56         super(context, attrs);
57         mContext = context;
58         setOnPreferenceChangeListener(this);
59     }
60 
setListener(AccountSelectionListener listener)61     public void setListener(AccountSelectionListener listener) {
62         mListener = listener;
63     }
64 
setShowSelectionInSummary(boolean value)65     public void setShowSelectionInSummary(boolean value) {
66         mShowSelectionInSummary = value;
67     }
68 
setModel( TelecomManager telecomManager, List<PhoneAccountHandle> accountsList, PhoneAccountHandle currentSelection, CharSequence nullSelectionString)69     public void setModel(
70             TelecomManager telecomManager,
71             List<PhoneAccountHandle> accountsList,
72             PhoneAccountHandle currentSelection,
73             CharSequence nullSelectionString) {
74 
75         mAccounts = accountsList.toArray(new PhoneAccountHandle[accountsList.size()]);
76         mEntryValues = new String[mAccounts.length + 1];
77         mEntries = new CharSequence[mAccounts.length + 1];
78 
79         int selectedIndex = mAccounts.length;  // Points to nullSelectionString by default
80         int i = 0;
81         for ( ; i < mAccounts.length; i++) {
82             CharSequence label = telecomManager.getPhoneAccount(mAccounts[i]).getLabel();
83             mEntries[i] = label == null ? null : label.toString();
84             mEntryValues[i] = Integer.toString(i);
85             if (Objects.equals(currentSelection, mAccounts[i])) {
86                 selectedIndex = i;
87             }
88         }
89         mEntryValues[i] = Integer.toString(i);
90         mEntries[i] = nullSelectionString;
91 
92         setEntryValues(mEntryValues);
93         setEntries(mEntries);
94         setValueIndex(selectedIndex);
95         if (mShowSelectionInSummary) {
96             setSummary(mEntries[selectedIndex]);
97         }
98     }
99 
100     @Override
onPreferenceChange(Preference preference, Object newValue)101     public boolean onPreferenceChange(Preference preference, Object newValue) {
102         if (mListener != null) {
103             int index = Integer.parseInt((String) newValue);
104             PhoneAccountHandle account = index < mAccounts.length ? mAccounts[index] : null;
105             if (mListener.onAccountSelected(this, account)) {
106                 if (mShowSelectionInSummary) {
107                     setSummary(mEntries[index]);
108                 }
109                 if (index != findIndexOfValue(getValue())) {
110                     setValueIndex(index);
111                     mListener.onAccountChanged(this);
112                 }
113                 return true;
114             }
115         }
116         return false;
117     }
118 
119     /**
120      * Modifies the dialog to change the default "Cancel" button to "Choose Accounts", which
121      * triggers the {@link PhoneAccountSelectionPreferenceActivity} to be shown.
122      *
123      * @param builder The {@code AlertDialog.Builder}.
124      */
125     @Override
126     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
127         // Notify the listener that the dialog is about to be built.  This is important so that the
128         // list of enabled accounts can be updated prior to showing the dialog.
129         mListener.onAccountSelectionDialogShow(this);
130 
131         super.onPrepareDialogBuilder(builder);
132     }
133 }
134