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 
17 package com.android.settingslib.deviceinfo;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.content.Context;
21 import android.net.ConnectivityManager;
22 import android.net.wifi.WifiManager;
23 import android.os.PersistableBundle;
24 import android.support.annotation.VisibleForTesting;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 import android.telephony.CarrierConfigManager;
28 import android.telephony.SubscriptionManager;
29 import android.telephony.TelephonyManager;
30 
31 import com.android.settingslib.R;
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 
34 /**
35  * Preference controller for IMS status
36  */
37 public 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 
AbstractImsStatusPreferenceController(Context context, Lifecycle lifecycle)52     public AbstractImsStatusPreferenceController(Context context,
53             Lifecycle lifecycle) {
54         super(context, lifecycle);
55     }
56 
57     @Override
isAvailable()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
getPreferenceKey()70     public String getPreferenceKey() {
71         return KEY_IMS_REGISTRATION_STATE;
72     }
73 
74     @Override
displayPreference(PreferenceScreen screen)75     public void displayPreference(PreferenceScreen screen) {
76         super.displayPreference(screen);
77         mImsStatus = screen.findPreference(KEY_IMS_REGISTRATION_STATE);
78         updateConnectivity();
79     }
80 
81     @Override
getConnectivityIntents()82     protected String[] getConnectivityIntents() {
83         return CONNECTIVITY_INTENTS;
84     }
85 
86     @Override
updateConnectivity()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