blob: ae01497d0cc42b657d61181c19107010440a3bc8 [file] [log] [blame]
Ihab Awad104f8062014-07-17 11:29:35 -07001/*
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
17package com.android.telecomm;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.preference.ListPreference;
22import android.preference.Preference;
23import android.preference.PreferenceFragment;
Evan Charlton89176372014-07-19 18:23:09 -070024import android.telecomm.PhoneAccountHandle;
Ihab Awad104f8062014-07-17 11:29:35 -070025
26import java.util.HashMap;
27import java.util.List;
28import java.util.Map;
29import java.util.Objects;
30
31public 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 Charlton89176372014-07-19 18:23:09 -070045 private Map<String, PhoneAccountHandle> mAccountByValue = new HashMap<>();
Ihab Awad104f8062014-07-17 11:29:35 -070046
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 Charlton89176372014-07-19 18:23:09 -070055 List<PhoneAccountHandle> accountHandles = mRegistrar.getEnabledPhoneAccounts();
56 PhoneAccountHandle currentDefault = mRegistrar.getDefaultOutgoingPhoneAccount();
Ihab Awad104f8062014-07-17 11:29:35 -070057
Evan Charlton89176372014-07-19 18:23:09 -070058 String[] entryValues = new String[accountHandles.size() + 1];
59 String[] entries = new String[accountHandles.size() + 1];
Ihab Awad104f8062014-07-17 11:29:35 -070060
Evan Charlton89176372014-07-19 18:23:09 -070061 int selectedIndex = accountHandles.size(); // Points to "ask every time" by default
Ihab Awad104f8062014-07-17 11:29:35 -070062 int i = 0;
Evan Charlton89176372014-07-19 18:23:09 -070063 for ( ; i < accountHandles.size(); i++) {
Ihab Awad104f8062014-07-17 11:29:35 -070064 entryValues[i] = Integer.toString(i);
Santos Cordon72429422014-07-21 18:43:18 +000065 entries[i] = mRegistrar
Evan Charlton94d01622014-07-20 12:32:05 -070066 .getPhoneAccount(accountHandles.get(i))
Santos Cordon72429422014-07-21 18:43:18 +000067 .getLabel();
Evan Charlton89176372014-07-19 18:23:09 -070068 if (Objects.equals(currentDefault, accountHandles.get(i))) {
Ihab Awad104f8062014-07-17 11:29:35 -070069 selectedIndex = i;
70 }
Evan Charlton89176372014-07-19 18:23:09 -070071 mAccountByValue.put(entryValues[i], accountHandles.get(i));
Ihab Awad104f8062014-07-17 11:29:35 -070072 }
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}