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