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.core.BasePreferenceController; 32 import com.android.settings.network.SubscriptionsChangeListener; 33 34 public class DisabledSubscriptionController extends BasePreferenceController implements 35 SubscriptionsChangeListener.SubscriptionsChangeListenerClient, LifecycleObserver { 36 private PreferenceCategory mCategory; 37 private int mSubId; 38 private SubscriptionsChangeListener mChangeListener; 39 private SubscriptionManager mSubscriptionManager; 40 DisabledSubscriptionController(Context context, String preferenceKey)41 public DisabledSubscriptionController(Context context, String preferenceKey) { 42 super(context, preferenceKey); 43 mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 44 mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class); 45 mChangeListener = new SubscriptionsChangeListener(context, this); 46 } 47 init(Lifecycle lifecycle, int subId)48 public void init(Lifecycle lifecycle, int subId) { 49 lifecycle.addObserver(this); 50 mSubId = subId; 51 } 52 53 @OnLifecycleEvent(ON_RESUME) onResume()54 public void onResume() { 55 mChangeListener.start(); 56 update(); 57 } 58 59 @OnLifecycleEvent(ON_PAUSE) onPause()60 public void onPause() { 61 mChangeListener.stop(); 62 } 63 64 @Override displayPreference(PreferenceScreen screen)65 public void displayPreference(PreferenceScreen screen) { 66 super.displayPreference(screen); 67 mCategory = screen.findPreference(getPreferenceKey()); 68 update(); 69 } 70 update()71 private void update() { 72 if (mCategory == null || mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 73 return; 74 } 75 // TODO b/135222940: re-evaluate whether to use mSubscriptionManager#isSubscriptionEnabled 76 mCategory.setVisible(mSubscriptionManager.isActiveSubId(mSubId)); 77 } 78 79 @Override getAvailabilityStatus()80 public int getAvailabilityStatus() { 81 return AVAILABLE_UNSEARCHABLE; 82 } 83 84 @Override onAirplaneModeChanged(boolean airplaneModeEnabled)85 public void onAirplaneModeChanged(boolean airplaneModeEnabled) { 86 } 87 88 @Override onSubscriptionsChanged()89 public void onSubscriptionsChanged() { 90 update(); 91 } 92 } 93