1/*
2 * Copyright (C) 2017 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.settingslib.deviceinfo;
18
19import android.bluetooth.BluetoothAdapter;
20import android.content.Context;
21import android.net.ConnectivityManager;
22import android.net.wifi.WifiManager;
23import android.os.PersistableBundle;
24import android.support.annotation.VisibleForTesting;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27import android.telephony.CarrierConfigManager;
28import android.telephony.SubscriptionManager;
29import android.telephony.TelephonyManager;
30
31import com.android.settingslib.R;
32import com.android.settingslib.core.lifecycle.Lifecycle;
33
34/**
35 * Preference controller for IMS status
36 */
37public abstract class AbstractImsStatusPreferenceController
38        extends AbstractConnectivityPreferenceController {
39
40    @VisibleForTesting
41    static final String KEY_IMS_REGISTRATION_STATE = "ims_reg_state";
42
43    private static final String[] CONNECTIVITY_INTENTS = {
44            BluetoothAdapter.ACTION_STATE_CHANGED,
45            ConnectivityManager.CONNECTIVITY_ACTION,
46            WifiManager.LINK_CONFIGURATION_CHANGED_ACTION,
47            WifiManager.NETWORK_STATE_CHANGED_ACTION,
48    };
49
50    private Preference mImsStatus;
51
52    public AbstractImsStatusPreferenceController(Context context,
53            Lifecycle lifecycle) {
54        super(context, lifecycle);
55    }
56
57    @Override
58    public boolean isAvailable() {
59        CarrierConfigManager configManager = mContext.getSystemService(CarrierConfigManager.class);
60        int subId = SubscriptionManager.getDefaultDataSubscriptionId();
61        PersistableBundle config = null;
62        if (configManager != null) {
63            config = configManager.getConfigForSubId(subId);
64        }
65        return config != null && config.getBoolean(
66                CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL);
67    }
68
69    @Override
70    public String getPreferenceKey() {
71        return KEY_IMS_REGISTRATION_STATE;
72    }
73
74    @Override
75    public void displayPreference(PreferenceScreen screen) {
76        super.displayPreference(screen);
77        mImsStatus = screen.findPreference(KEY_IMS_REGISTRATION_STATE);
78        updateConnectivity();
79    }
80
81    @Override
82    protected String[] getConnectivityIntents() {
83        return CONNECTIVITY_INTENTS;
84    }
85
86    @Override
87    protected void updateConnectivity() {
88        int subId = SubscriptionManager.getDefaultDataSubscriptionId();
89        if (mImsStatus != null) {
90            TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
91            mImsStatus.setSummary((tm != null && tm.isImsRegistered(subId)) ?
92                    R.string.ims_reg_status_registered : R.string.ims_reg_status_not_registered);
93        }
94    }
95}
96