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