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.car.settings.network; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.os.Bundle; 22 import android.provider.Settings; 23 import android.telephony.SubscriptionInfo; 24 import android.telephony.SubscriptionManager; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.VisibleForTesting; 28 import androidx.annotation.XmlRes; 29 30 import com.android.car.settings.R; 31 import com.android.car.settings.common.SettingsFragment; 32 import com.android.car.settings.datausage.DataUsagePreferenceController; 33 import com.android.car.settings.datausage.DataUsageSummaryPreferenceController; 34 import com.android.car.settings.datausage.DataWarningAndLimitPreferenceController; 35 import com.android.car.settings.search.CarBaseSearchIndexProvider; 36 import com.android.car.ui.toolbar.ToolbarController; 37 import com.android.internal.util.CollectionUtils; 38 import com.android.settingslib.search.SearchIndexable; 39 40 import com.google.android.collect.Lists; 41 42 import java.util.Arrays; 43 import java.util.List; 44 45 /** Mobile network settings homepage. */ 46 @SearchIndexable 47 public class MobileNetworkFragment extends SettingsFragment implements 48 MobileNetworkUpdateManager.MobileNetworkUpdateListener { 49 50 @VisibleForTesting 51 static final String ARG_NETWORK_SUB_ID = "network_sub_id"; 52 53 private SubscriptionManager mSubscriptionManager; 54 private MobileNetworkUpdateManager mMobileNetworkUpdateManager; 55 private CharSequence mTitle; 56 57 /** 58 * Creates a new instance of the {@link MobileNetworkFragment}, which shows settings related to 59 * the given {@code subId}. 60 */ newInstance(int subId)61 public static MobileNetworkFragment newInstance(int subId) { 62 MobileNetworkFragment fragment = new MobileNetworkFragment(); 63 Bundle args = new Bundle(); 64 args.putInt(ARG_NETWORK_SUB_ID, subId); 65 fragment.setArguments(args); 66 return fragment; 67 } 68 69 @Override 70 @XmlRes getPreferenceScreenResId()71 protected int getPreferenceScreenResId() { 72 return R.xml.mobile_network_fragment; 73 } 74 75 @Override onAttach(Context context)76 public void onAttach(Context context) { 77 super.onAttach(context); 78 mSubscriptionManager = getSubscriptionManager(context); 79 80 int subId = getArguments() != null 81 ? getArguments().getInt(ARG_NETWORK_SUB_ID, MobileNetworkUpdateManager.SUB_ID_NULL) 82 : MobileNetworkUpdateManager.SUB_ID_NULL; 83 mMobileNetworkUpdateManager = getMobileNetworkUpdateManager(context, subId); 84 getLifecycle().addObserver(mMobileNetworkUpdateManager); 85 86 List<MobileNetworkUpdateManager.MobileNetworkUpdateListener> listeners = 87 Lists.newArrayList( 88 this, 89 use(MobileDataTogglePreferenceController.class, 90 R.string.pk_mobile_data_toggle), 91 use(RoamingPreferenceController.class, R.string.pk_mobile_roaming_toggle)); 92 for (MobileNetworkUpdateManager.MobileNetworkUpdateListener listener : listeners) { 93 mMobileNetworkUpdateManager.registerListener(listener); 94 } 95 96 List<NetworkBasePreferenceController> preferenceControllers = 97 Arrays.asList( 98 use(DataUsageSummaryPreferenceController.class, 99 R.string.pk_data_usage_summary), 100 use(MobileDataTogglePreferenceController.class, 101 R.string.pk_mobile_data_toggle), 102 use(RoamingPreferenceController.class, R.string.pk_mobile_roaming_toggle), 103 use(DataUsagePreferenceController.class, R.string.pk_app_data_usage), 104 use(DataWarningAndLimitPreferenceController.class, 105 R.string.pk_data_warning_and_limit)); 106 107 for (NetworkBasePreferenceController preferenceController : 108 preferenceControllers) { 109 preferenceController.setFields(subId); 110 } 111 } 112 113 @Override setupToolbar(@onNull ToolbarController toolbar)114 protected void setupToolbar(@NonNull ToolbarController toolbar) { 115 super.setupToolbar(toolbar); 116 117 if (mTitle != null) { 118 toolbar.setTitle(mTitle); 119 } 120 } 121 122 @Override onMobileNetworkUpdated(int subId)123 public void onMobileNetworkUpdated(int subId) { 124 SubscriptionInfo info = null; 125 126 if (subId != MobileNetworkUpdateManager.SUB_ID_NULL) { 127 for (SubscriptionInfo subscriptionInfo : 128 mSubscriptionManager.getSelectableSubscriptionInfoList()) { 129 if (subscriptionInfo.getSubscriptionId() == subId) { 130 info = subscriptionInfo; 131 } 132 } 133 } 134 135 if (info == null && !CollectionUtils.isEmpty( 136 mSubscriptionManager.getActiveSubscriptionInfoList())) { 137 info = mSubscriptionManager.getActiveSubscriptionInfoList().get(0); 138 } 139 140 if (info != null) { 141 // It is possible for this to be called before the activity is fully created. If so, 142 // cache the value so that it can be constructed when setupToolbar is called. 143 mTitle = info.getDisplayName(); 144 if (getToolbar() != null) { 145 getToolbar().setTitle(mTitle); 146 } 147 } 148 } 149 150 @VisibleForTesting getSubscriptionManager(Context context)151 SubscriptionManager getSubscriptionManager(Context context) { 152 return context.getSystemService(SubscriptionManager.class); 153 } 154 155 @VisibleForTesting getMobileNetworkUpdateManager(Context context, int subId)156 MobileNetworkUpdateManager getMobileNetworkUpdateManager(Context context, int subId) { 157 return new MobileNetworkUpdateManager(context, subId); 158 } 159 160 public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 161 new CarBaseSearchIndexProvider(R.xml.mobile_network_fragment, 162 Settings.ACTION_NETWORK_OPERATOR_SETTINGS) { 163 @Override 164 protected boolean isPageSearchEnabled(Context context) { 165 return NetworkUtils.hasMobileNetwork( 166 context.getSystemService(ConnectivityManager.class)); 167 } 168 }; 169 } 170