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