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