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