1 /*
2  * Copyright (C) 2015 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.server.telecom.settings;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Icon;
21 import android.os.Bundle;
22 import android.preference.Preference;
23 import android.preference.PreferenceFragment;
24 import android.preference.PreferenceScreen;
25 import android.preference.SwitchPreference;
26 import android.telecom.Log;
27 import android.telecom.PhoneAccount;
28 import android.telecom.PhoneAccountHandle;
29 import android.telecom.TelecomManager;
30 
31 import com.android.server.telecom.R;
32 
33 import java.util.List;
34 
35 /**
36  * Lists all call-capable {@link PhoneAccount}s which are not SIM-based and provides a settings
37  * for enabling and disabling each of the accounts.  Only enabled accounts will be (1) listed as
38  * options with which to place a call and (2) capable of receiving incoming calls through the
39  * default dialer UI.
40  */
41 public class EnableAccountPreferenceFragment extends PreferenceFragment {
42 
43     private final class AccountSwitchPreference extends SwitchPreference {
44         private final PhoneAccount mAccount;
45 
AccountSwitchPreference(Context context, PhoneAccount account)46         public AccountSwitchPreference(Context context, PhoneAccount account) {
47             super(context);
48             mAccount = account;
49 
50             setTitle(account.getLabel());
51             setSummary(account.getShortDescription());
52             Icon icon = account.getIcon();
53             if (icon != null) {
54                 setIcon(icon.loadDrawable(context));
55             }
56             setChecked(account.isEnabled());
57             setOnPreferenceChangeListener(this::onPreferenceChange);
58         }
59 
onPreferenceChange(Preference preference, Object newValue)60         private boolean onPreferenceChange(Preference preference, Object newValue) {
61             Log.d(this, "onPreferenceChange: key = %s", preference.getKey());
62             Log.d(this, "  preference = '%s'", preference);
63             Log.d(this, "  newValue = '%b'", newValue);
64 
65             mTelecomManager.enablePhoneAccount(mAccount.getAccountHandle(), (boolean) newValue);
66             return true;
67         }
68     }
69 
70     private TelecomManager mTelecomManager;
71 
72     @Override
onCreate(Bundle savedInstanceState)73     public void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75         mTelecomManager = TelecomManager.from(getActivity());
76     }
77 
78 
79     @Override
onResume()80     public void onResume() {
81         super.onResume();
82 
83         PreferenceScreen screen = getPreferenceScreen();
84         if (screen != null) {
85             screen.removeAll();
86         }
87 
88         addPreferencesFromResource(R.xml.enable_account_preference);
89         screen = getPreferenceScreen();
90 
91         List<PhoneAccountHandle> accountHandles =
92                 mTelecomManager.getCallCapablePhoneAccounts(true /* includeDisabledAccounts */);
93 
94         Context context = getActivity();
95         for (PhoneAccountHandle handle : accountHandles) {
96             PhoneAccount account = mTelecomManager.getPhoneAccount(handle);
97             if (account != null) {
98                 final boolean isSimAccount =
99                         0 != (account.getCapabilities() & PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
100                 if (!isSimAccount) {
101                     screen.addPreference(new AccountSwitchPreference(context, account));
102                 }
103             }
104         }
105     }
106 }
107