1 /*
2  * Copyright (C) 2019 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.settings.network.telephony;
18 
19 import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
20 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
21 
22 import android.content.Context;
23 import android.telephony.SubscriptionManager;
24 
25 import androidx.lifecycle.Lifecycle;
26 import androidx.lifecycle.LifecycleObserver;
27 import androidx.lifecycle.OnLifecycleEvent;
28 import androidx.preference.PreferenceCategory;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settings.network.SubscriptionsChangeListener;
32 
33 /**
34  * Preference controller group which can hide UI option from visible when SIM is no longer in
35  * active.
36  */
37 public class DisabledSubscriptionController extends TelephonyBasePreferenceController implements
38         SubscriptionsChangeListener.SubscriptionsChangeListenerClient, LifecycleObserver {
39     private PreferenceCategory mCategory;
40     private SubscriptionsChangeListener mChangeListener;
41     private SubscriptionManager mSubscriptionManager;
42 
43     /**
44      * Constructor of preference controller
45      */
DisabledSubscriptionController(Context context, String preferenceKey)46     public DisabledSubscriptionController(Context context, String preferenceKey) {
47         super(context, preferenceKey);
48         mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
49         mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
50         mChangeListener = new SubscriptionsChangeListener(context, this);
51     }
52 
53     /**
54      * Re-initialize the configuration based on subscription id provided
55      */
init(Lifecycle lifecycle, int subId)56     public void init(Lifecycle lifecycle, int subId) {
57         lifecycle.addObserver(this);
58         mSubId = subId;
59     }
60 
61     @OnLifecycleEvent(ON_RESUME)
onResume()62     public void onResume() {
63         mChangeListener.start();
64         update();
65     }
66 
67     @OnLifecycleEvent(ON_PAUSE)
onPause()68     public void onPause() {
69         mChangeListener.stop();
70     }
71 
72     @Override
displayPreference(PreferenceScreen screen)73     public void displayPreference(PreferenceScreen screen) {
74         super.displayPreference(screen);
75         mCategory = screen.findPreference(getPreferenceKey());
76         update();
77     }
78 
update()79     private void update() {
80         if (mCategory == null || !SubscriptionManager.isValidSubscriptionId(mSubId)) {
81             return;
82         }
83         // TODO b/135222940: re-evaluate whether to use mSubscriptionManager#isSubscriptionEnabled
84         mCategory.setVisible(mSubscriptionManager.isActiveSubscriptionId(mSubId));
85     }
86 
87     @Override
getAvailabilityStatus(int subId)88     public int getAvailabilityStatus(int subId) {
89         return AVAILABLE_UNSEARCHABLE;
90     }
91 
92     @Override
onAirplaneModeChanged(boolean airplaneModeEnabled)93     public void onAirplaneModeChanged(boolean airplaneModeEnabled) {
94     }
95 
96     @Override
onSubscriptionsChanged()97     public void onSubscriptionsChanged() {
98         update();
99     }
100 }
101