155344342de211db4c99d57ddfc550864d9113376Santos Cordon/*
255344342de211db4c99d57ddfc550864d9113376Santos Cordon * Copyright (C) 2015 The Android Open Source Project
355344342de211db4c99d57ddfc550864d9113376Santos Cordon *
455344342de211db4c99d57ddfc550864d9113376Santos Cordon * Licensed under the Apache License, Version 2.0 (the "License");
555344342de211db4c99d57ddfc550864d9113376Santos Cordon * you may not use this file except in compliance with the License.
655344342de211db4c99d57ddfc550864d9113376Santos Cordon * You may obtain a copy of the License at
755344342de211db4c99d57ddfc550864d9113376Santos Cordon *
855344342de211db4c99d57ddfc550864d9113376Santos Cordon *      http://www.apache.org/licenses/LICENSE-2.0
955344342de211db4c99d57ddfc550864d9113376Santos Cordon *
1055344342de211db4c99d57ddfc550864d9113376Santos Cordon * Unless required by applicable law or agreed to in writing, software
1155344342de211db4c99d57ddfc550864d9113376Santos Cordon * distributed under the License is distributed on an "AS IS" BASIS,
1255344342de211db4c99d57ddfc550864d9113376Santos Cordon * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1355344342de211db4c99d57ddfc550864d9113376Santos Cordon * See the License for the specific language governing permissions and
1455344342de211db4c99d57ddfc550864d9113376Santos Cordon * limitations under the License.
1555344342de211db4c99d57ddfc550864d9113376Santos Cordon */
1655344342de211db4c99d57ddfc550864d9113376Santos Cordon
1755344342de211db4c99d57ddfc550864d9113376Santos Cordonpackage com.android.server.telecom.settings;
1855344342de211db4c99d57ddfc550864d9113376Santos Cordon
1955344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.content.Context;
2055344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.graphics.drawable.Icon;
2155344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.os.Bundle;
2255344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.preference.PreferenceFragment;
2355344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.preference.PreferenceScreen;
2455344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.preference.SwitchPreference;
2555344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.telecom.PhoneAccount;
2655344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.telecom.PhoneAccountHandle;
2755344342de211db4c99d57ddfc550864d9113376Santos Cordonimport android.telecom.TelecomManager;
2855344342de211db4c99d57ddfc550864d9113376Santos Cordon
2955344342de211db4c99d57ddfc550864d9113376Santos Cordonimport com.android.server.telecom.R;
3055344342de211db4c99d57ddfc550864d9113376Santos Cordon
3155344342de211db4c99d57ddfc550864d9113376Santos Cordonimport java.util.List;
3255344342de211db4c99d57ddfc550864d9113376Santos Cordon
3355344342de211db4c99d57ddfc550864d9113376Santos Cordon/**
3455344342de211db4c99d57ddfc550864d9113376Santos Cordon * Lists all call-capable {@link PhoneAccount}s which are not SIM-based and provides a settings
3555344342de211db4c99d57ddfc550864d9113376Santos Cordon * for enabling and disabling each of the accounts.  Only enabled accounts will be (1) listed as
3655344342de211db4c99d57ddfc550864d9113376Santos Cordon * options with which to place a call and (2) capable of receiving incoming calls through the
3755344342de211db4c99d57ddfc550864d9113376Santos Cordon * default dialer UI.
3855344342de211db4c99d57ddfc550864d9113376Santos Cordon */
3955344342de211db4c99d57ddfc550864d9113376Santos Cordonpublic class EnableAccountPreferenceFragment extends PreferenceFragment {
4055344342de211db4c99d57ddfc550864d9113376Santos Cordon
41ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon    private final class AccountSwitchPreference extends SwitchPreference {
4255344342de211db4c99d57ddfc550864d9113376Santos Cordon        private final PhoneAccount mAccount;
4355344342de211db4c99d57ddfc550864d9113376Santos Cordon
4455344342de211db4c99d57ddfc550864d9113376Santos Cordon        public AccountSwitchPreference(Context context, PhoneAccount account) {
4555344342de211db4c99d57ddfc550864d9113376Santos Cordon            super(context);
4655344342de211db4c99d57ddfc550864d9113376Santos Cordon            mAccount = account;
4755344342de211db4c99d57ddfc550864d9113376Santos Cordon
4855344342de211db4c99d57ddfc550864d9113376Santos Cordon            setTitle(account.getLabel());
4955344342de211db4c99d57ddfc550864d9113376Santos Cordon            setSummary(account.getShortDescription());
5055344342de211db4c99d57ddfc550864d9113376Santos Cordon            Icon icon = account.getIcon();
5155344342de211db4c99d57ddfc550864d9113376Santos Cordon            if (icon != null) {
5255344342de211db4c99d57ddfc550864d9113376Santos Cordon                setIcon(icon.loadDrawable(context));
5355344342de211db4c99d57ddfc550864d9113376Santos Cordon            }
54ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon            setChecked(account.isEnabled());
5555344342de211db4c99d57ddfc550864d9113376Santos Cordon        }
5655344342de211db4c99d57ddfc550864d9113376Santos Cordon
5755344342de211db4c99d57ddfc550864d9113376Santos Cordon        /** ${inheritDoc} */
5855344342de211db4c99d57ddfc550864d9113376Santos Cordon        @Override
5955344342de211db4c99d57ddfc550864d9113376Santos Cordon        protected void onClick() {
6055344342de211db4c99d57ddfc550864d9113376Santos Cordon            super.onClick();
6155344342de211db4c99d57ddfc550864d9113376Santos Cordon
62ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon            mTelecomManager.enablePhoneAccount(mAccount.getAccountHandle(), isChecked());
6355344342de211db4c99d57ddfc550864d9113376Santos Cordon        }
6455344342de211db4c99d57ddfc550864d9113376Santos Cordon    }
6555344342de211db4c99d57ddfc550864d9113376Santos Cordon
6655344342de211db4c99d57ddfc550864d9113376Santos Cordon    private TelecomManager mTelecomManager;
6755344342de211db4c99d57ddfc550864d9113376Santos Cordon
6855344342de211db4c99d57ddfc550864d9113376Santos Cordon    @Override
6955344342de211db4c99d57ddfc550864d9113376Santos Cordon    public void onCreate(Bundle savedInstanceState) {
7055344342de211db4c99d57ddfc550864d9113376Santos Cordon        super.onCreate(savedInstanceState);
7155344342de211db4c99d57ddfc550864d9113376Santos Cordon        mTelecomManager = TelecomManager.from(getActivity());
7255344342de211db4c99d57ddfc550864d9113376Santos Cordon    }
7355344342de211db4c99d57ddfc550864d9113376Santos Cordon
7455344342de211db4c99d57ddfc550864d9113376Santos Cordon
7555344342de211db4c99d57ddfc550864d9113376Santos Cordon    @Override
7655344342de211db4c99d57ddfc550864d9113376Santos Cordon    public void onResume() {
7755344342de211db4c99d57ddfc550864d9113376Santos Cordon        super.onResume();
7855344342de211db4c99d57ddfc550864d9113376Santos Cordon
79ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon        PreferenceScreen screen = getPreferenceScreen();
80ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon        if (screen != null) {
81ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon            screen.removeAll();
82ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon        }
83ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon
8455344342de211db4c99d57ddfc550864d9113376Santos Cordon        addPreferencesFromResource(R.xml.enable_account_preference);
85ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon        screen = getPreferenceScreen();
8655344342de211db4c99d57ddfc550864d9113376Santos Cordon
87ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon        List<PhoneAccountHandle> accountHandles =
88ea5cb93d81099f17654e0fdf9b8eda3bfa89f081Santos Cordon                mTelecomManager.getCallCapablePhoneAccounts(true /* includeDisabledAccounts */);
8955344342de211db4c99d57ddfc550864d9113376Santos Cordon
9055344342de211db4c99d57ddfc550864d9113376Santos Cordon        Context context = getActivity();
9155344342de211db4c99d57ddfc550864d9113376Santos Cordon        for (PhoneAccountHandle handle : accountHandles) {
9255344342de211db4c99d57ddfc550864d9113376Santos Cordon            PhoneAccount account = mTelecomManager.getPhoneAccount(handle);
9355344342de211db4c99d57ddfc550864d9113376Santos Cordon            if (account != null) {
9455344342de211db4c99d57ddfc550864d9113376Santos Cordon                final boolean isSimAccount =
9555344342de211db4c99d57ddfc550864d9113376Santos Cordon                        0 != (account.getCapabilities() & PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
9655344342de211db4c99d57ddfc550864d9113376Santos Cordon                if (!isSimAccount) {
9755344342de211db4c99d57ddfc550864d9113376Santos Cordon                    screen.addPreference(new AccountSwitchPreference(context, account));
9855344342de211db4c99d57ddfc550864d9113376Santos Cordon                }
9955344342de211db4c99d57ddfc550864d9113376Santos Cordon            }
10055344342de211db4c99d57ddfc550864d9113376Santos Cordon        }
10155344342de211db4c99d57ddfc550864d9113376Santos Cordon    }
10255344342de211db4c99d57ddfc550864d9113376Santos Cordon}
103