• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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;
18 
19 import android.content.Context;
20 import android.util.Log;
21 
22 import androidx.annotation.VisibleForTesting;
23 import androidx.preference.PreferenceCategory;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settings.wifi.WifiPickerTrackerHelper;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 /**
31  * This controls mobile network display of the internet page that only appears when there
32  * are active mobile subscriptions. It shows an overview of available mobile network
33  * connections with an entry for each subscription.
34  *
35  *  {@link NetworkMobileProviderController} is used to show subscription status on internet
36  *  page for provider model. This original class can refer to {@link MultiNetworkHeaderController},
37  *
38   */
39 public class NetworkMobileProviderController extends BasePreferenceController implements
40         SubscriptionsPreferenceController.UpdateListener {
41 
42     private static final String TAG = NetworkMobileProviderController.class.getSimpleName();
43 
44     public static final String PREF_KEY_PROVIDER_MOBILE_NETWORK = "provider_model_mobile_network";
45     private static final int PREFERENCE_START_ORDER = 10;
46 
47     private PreferenceCategory mPreferenceCategory;
48     private PreferenceScreen mPreferenceScreen;
49 
50     private SubscriptionsPreferenceController mSubscriptionsController;
51 
52     private int mOriginalExpandedChildrenCount;
53     private boolean mHide;
54 
NetworkMobileProviderController(Context context, String key)55     public NetworkMobileProviderController(Context context, String key) {
56         super(context, key);
57     }
58 
59     /**
60      * Initialize NetworkMobileProviderController
61      * @param lifecycle Lifecycle of Settings
62      */
init(Lifecycle lifecycle)63     public void init(Lifecycle lifecycle) {
64         mSubscriptionsController = createSubscriptionsController(lifecycle);
65     }
66 
67     @VisibleForTesting
createSubscriptionsController(Lifecycle lifecycle)68     SubscriptionsPreferenceController createSubscriptionsController(Lifecycle lifecycle) {
69         if (mSubscriptionsController == null) {
70             return new SubscriptionsPreferenceController(
71                     mContext,
72                     lifecycle,
73                     this,
74                     PREF_KEY_PROVIDER_MOBILE_NETWORK,
75                     PREFERENCE_START_ORDER);
76         }
77         return mSubscriptionsController;
78     }
79 
80     @Override
displayPreference(PreferenceScreen screen)81     public void displayPreference(PreferenceScreen screen) {
82         super.displayPreference(screen);
83         if (mSubscriptionsController == null) {
84             Log.e(TAG, "[displayPreference] SubscriptionsController is null.");
85             return;
86         }
87         mPreferenceScreen = screen;
88         mOriginalExpandedChildrenCount = mPreferenceScreen.getInitialExpandedChildrenCount();
89         mPreferenceCategory = screen.findPreference(PREF_KEY_PROVIDER_MOBILE_NETWORK);
90         mPreferenceCategory.setVisible(isAvailable());
91         // TODO(tomhsu) For the provider model, subscriptionsController shall do more
92         // implementation of preference type change and summary control.
93         mSubscriptionsController.displayPreference(screen);
94     }
95 
96     @Override
getAvailabilityStatus()97     public int getAvailabilityStatus() {
98         if (mHide || mSubscriptionsController == null) {
99             return CONDITIONALLY_UNAVAILABLE;
100         }
101         return mSubscriptionsController.isAvailable() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
102     }
103 
104     @Override
onChildrenUpdated()105     public void onChildrenUpdated() {
106         final boolean available = isAvailable();
107         // TODO(b/129893781) we need a better way to express where the advanced collapsing starts
108         // for preference groups that have items dynamically added/removed in the top expanded
109         // section.
110         if (mOriginalExpandedChildrenCount != Integer.MAX_VALUE) {
111             if (available) {
112                 mPreferenceScreen.setInitialExpandedChildrenCount(
113                         mOriginalExpandedChildrenCount + mPreferenceCategory.getPreferenceCount());
114             } else {
115                 mPreferenceScreen.setInitialExpandedChildrenCount(mOriginalExpandedChildrenCount);
116             }
117         }
118         mPreferenceCategory.setVisible(available);
119     }
120 
setWifiPickerTrackerHelper(WifiPickerTrackerHelper helper)121     public void setWifiPickerTrackerHelper(WifiPickerTrackerHelper helper) {
122         if (mSubscriptionsController != null) {
123             mSubscriptionsController.setWifiPickerTrackerHelper(helper);
124         }
125     }
126 
127     /**
128      * Hides the preference.
129      */
hidePreference(boolean hide, boolean immediately)130     public void hidePreference(boolean hide, boolean immediately) {
131         mHide = hide;
132         if (immediately) {
133             mPreferenceCategory.setVisible(hide ? false : isAvailable());
134         }
135     }
136 }
137