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