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