1 /* 2 * Copyright (C) 2018 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.app.settings.SettingsEnums; 20 import android.content.Context; 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.WifiConnectionPreferenceController; 28 import com.android.settingslib.core.lifecycle.Lifecycle; 29 30 // This controls a header at the top of the Network & internet page that only appears when there 31 // are two or more active mobile subscriptions. It shows an overview of available network 32 // connections with an entry for wifi (if connected) and an entry for each subscription. 33 public class MultiNetworkHeaderController extends BasePreferenceController implements 34 WifiConnectionPreferenceController.UpdateListener, 35 SubscriptionsPreferenceController.UpdateListener { 36 public static final String TAG = "MultiNetworkHdrCtrl"; 37 38 private WifiConnectionPreferenceController mWifiController; 39 private SubscriptionsPreferenceController mSubscriptionsController; 40 private PreferenceCategory mPreferenceCategory; 41 private PreferenceScreen mPreferenceScreen; 42 private int mOriginalExpandedChildrenCount; 43 MultiNetworkHeaderController(Context context, String key)44 public MultiNetworkHeaderController(Context context, String key) { 45 super(context, key); 46 } 47 init(Lifecycle lifecycle)48 public void init(Lifecycle lifecycle) { 49 mWifiController = createWifiController(lifecycle); 50 mSubscriptionsController = createSubscriptionsController(lifecycle); 51 } 52 53 @VisibleForTesting createWifiController(Lifecycle lifecycle)54 WifiConnectionPreferenceController createWifiController(Lifecycle lifecycle) { 55 final int prefOrder = 0; 56 return new WifiConnectionPreferenceController(mContext, lifecycle, this, mPreferenceKey, 57 prefOrder, SettingsEnums.SETTINGS_NETWORK_CATEGORY); 58 } 59 60 @VisibleForTesting createSubscriptionsController(Lifecycle lifecycle)61 SubscriptionsPreferenceController createSubscriptionsController(Lifecycle lifecycle) { 62 final int prefStartOrder = 10; 63 return new SubscriptionsPreferenceController(mContext, lifecycle, this, mPreferenceKey, 64 prefStartOrder); 65 } 66 67 @Override displayPreference(PreferenceScreen screen)68 public void displayPreference(PreferenceScreen screen) { 69 super.displayPreference(screen); 70 mPreferenceScreen = screen; 71 mOriginalExpandedChildrenCount = mPreferenceScreen.getInitialExpandedChildrenCount(); 72 mPreferenceCategory = screen.findPreference(mPreferenceKey); 73 mPreferenceCategory.setVisible(isAvailable()); 74 mWifiController.displayPreference(screen); 75 mSubscriptionsController.displayPreference(screen); 76 } 77 78 @Override getAvailabilityStatus()79 public int getAvailabilityStatus() { 80 if (mSubscriptionsController == null || !mSubscriptionsController.isAvailable()) { 81 return CONDITIONALLY_UNAVAILABLE; 82 } else { 83 return AVAILABLE; 84 } 85 } 86 87 @Override onChildrenUpdated()88 public void onChildrenUpdated() { 89 final boolean available = isAvailable(); 90 // TODO(b/129893781) we need a better way to express where the advanced collapsing starts 91 // for preference groups that have items dynamically added/removed in the top expanded 92 // section. 93 if (mOriginalExpandedChildrenCount != Integer.MAX_VALUE) { 94 if (available) { 95 mPreferenceScreen.setInitialExpandedChildrenCount( 96 mOriginalExpandedChildrenCount + mPreferenceCategory.getPreferenceCount()); 97 } else { 98 mPreferenceScreen.setInitialExpandedChildrenCount(mOriginalExpandedChildrenCount); 99 } 100 } 101 mPreferenceCategory.setVisible(available); 102 } 103 } 104