Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 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.telecomm; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.os.Bundle; |
| 21 | import android.preference.ListPreference; |
| 22 | import android.preference.Preference; |
| 23 | import android.preference.PreferenceFragment; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 24 | import android.telecomm.PhoneAccountHandle; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.HashMap; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | public class PhoneAccountPreferencesActivity extends Activity { |
| 32 | |
| 33 | private static final String KEY_DEFAULT_OUTGOING_ACCOUNT = "default_outgoing_account"; |
| 34 | |
| 35 | @Override |
| 36 | public void onCreate(Bundle savedInstanceState) { |
| 37 | super.onCreate(savedInstanceState); |
| 38 | setContentView(R.layout.phone_account_preferences); |
| 39 | } |
| 40 | |
| 41 | public static class PreferencesFragment extends PreferenceFragment |
| 42 | implements ListPreference.OnPreferenceChangeListener { |
| 43 | private ListPreference mDefaultOutgoingAccount; |
| 44 | private PhoneAccountRegistrar mRegistrar; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 45 | private Map<String, PhoneAccountHandle> mAccountByValue = new HashMap<>(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | public void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
| 50 | |
| 51 | addPreferencesFromResource(R.xml.phone_account_preferences); |
| 52 | mDefaultOutgoingAccount = (ListPreference) findPreference(KEY_DEFAULT_OUTGOING_ACCOUNT); |
| 53 | |
| 54 | mRegistrar = TelecommApp.getInstance().getPhoneAccountRegistrar(); |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 55 | List<PhoneAccountHandle> accountHandles = mRegistrar.getEnabledPhoneAccounts(); |
| 56 | PhoneAccountHandle currentDefault = mRegistrar.getDefaultOutgoingPhoneAccount(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 57 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 58 | String[] entryValues = new String[accountHandles.size() + 1]; |
| 59 | String[] entries = new String[accountHandles.size() + 1]; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 60 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 61 | int selectedIndex = accountHandles.size(); // Points to "ask every time" by default |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 62 | int i = 0; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 63 | for ( ; i < accountHandles.size(); i++) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 64 | entryValues[i] = Integer.toString(i); |
Santos Cordon | 7242942 | 2014-07-21 18:43:18 +0000 | [diff] [blame] | 65 | entries[i] = mRegistrar |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 66 | .getPhoneAccount(accountHandles.get(i)) |
Santos Cordon | 7242942 | 2014-07-21 18:43:18 +0000 | [diff] [blame] | 67 | .getLabel(); |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 68 | if (Objects.equals(currentDefault, accountHandles.get(i))) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 69 | selectedIndex = i; |
| 70 | } |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 71 | mAccountByValue.put(entryValues[i], accountHandles.get(i)); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 72 | } |
| 73 | entryValues[i] = Integer.toString(i); |
| 74 | entries[i] = getString(R.string.account_ask_every_time); |
| 75 | mAccountByValue.put(entryValues[i], null); |
| 76 | |
| 77 | mDefaultOutgoingAccount.setEntryValues(entryValues); |
| 78 | mDefaultOutgoingAccount.setEntries(entries); |
| 79 | mDefaultOutgoingAccount.setValueIndex(selectedIndex); |
| 80 | mDefaultOutgoingAccount.setOnPreferenceChangeListener(this); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean onPreferenceChange(Preference p, Object o) { |
| 85 | if (p == mDefaultOutgoingAccount) { |
| 86 | mRegistrar.setDefaultOutgoingPhoneAccount(mAccountByValue.get(o)); |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | } |