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.VisibleForTesting; 27 import androidx.annotation.XmlRes; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.SettingsFragment; 31 import com.android.car.settings.search.CarBaseSearchIndexProvider; 32 import com.android.internal.util.CollectionUtils; 33 import com.android.settingslib.search.SearchIndexable; 34 35 import com.google.android.collect.Lists; 36 37 import java.util.List; 38 39 /** Mobile network settings homepage. */ 40 @SearchIndexable 41 public class MobileNetworkFragment extends SettingsFragment implements 42 MobileNetworkUpdateManager.MobileNetworkUpdateListener { 43 44 @VisibleForTesting 45 static final String ARG_NETWORK_SUB_ID = "network_sub_id"; 46 47 private SubscriptionManager mSubscriptionManager; 48 private MobileNetworkUpdateManager mMobileNetworkUpdateManager; 49 private CharSequence mTitle; 50 51 /** 52 * Creates a new instance of the {@link MobileNetworkFragment}, which shows settings related to 53 * the given {@code subId}. 54 */ newInstance(int subId)55 public static MobileNetworkFragment newInstance(int subId) { 56 MobileNetworkFragment fragment = new MobileNetworkFragment(); 57 Bundle args = new Bundle(); 58 args.putInt(ARG_NETWORK_SUB_ID, subId); 59 fragment.setArguments(args); 60 return fragment; 61 } 62 63 @Override 64 @XmlRes getPreferenceScreenResId()65 protected int getPreferenceScreenResId() { 66 return R.xml.mobile_network_fragment; 67 } 68 69 @Override onAttach(Context context)70 public void onAttach(Context context) { 71 super.onAttach(context); 72 mSubscriptionManager = context.getSystemService(SubscriptionManager.class); 73 74 int subId = getArguments() != null 75 ? getArguments().getInt(ARG_NETWORK_SUB_ID, MobileNetworkUpdateManager.SUB_ID_NULL) 76 : MobileNetworkUpdateManager.SUB_ID_NULL; 77 mMobileNetworkUpdateManager = new MobileNetworkUpdateManager(context, subId); 78 getLifecycle().addObserver(mMobileNetworkUpdateManager); 79 80 List<MobileNetworkUpdateManager.MobileNetworkUpdateListener> listeners = 81 Lists.newArrayList( 82 this, 83 use(MobileDataTogglePreferenceController.class, 84 R.string.pk_mobile_data_toggle), 85 use(RoamingPreferenceController.class, R.string.pk_mobile_roaming_toggle)); 86 for (MobileNetworkUpdateManager.MobileNetworkUpdateListener listener : listeners) { 87 mMobileNetworkUpdateManager.registerListener(listener); 88 } 89 } 90 91 @Override onActivityCreated(Bundle savedInstanceState)92 public void onActivityCreated(Bundle savedInstanceState) { 93 super.onActivityCreated(savedInstanceState); 94 95 if (mTitle != null) { 96 getToolbar().setTitle(mTitle); 97 } 98 } 99 100 @Override onMobileNetworkUpdated(int subId)101 public void onMobileNetworkUpdated(int subId) { 102 SubscriptionInfo info = null; 103 104 if (subId != MobileNetworkUpdateManager.SUB_ID_NULL) { 105 for (SubscriptionInfo subscriptionInfo : 106 mSubscriptionManager.getSelectableSubscriptionInfoList()) { 107 if (subscriptionInfo.getSubscriptionId() == subId) { 108 info = subscriptionInfo; 109 } 110 } 111 } 112 113 if (info == null && !CollectionUtils.isEmpty( 114 mSubscriptionManager.getActiveSubscriptionInfoList())) { 115 info = mSubscriptionManager.getActiveSubscriptionInfoList().get(0); 116 } 117 118 if (info != null) { 119 // It is possible for this to be called before the activity is fully created. If so, 120 // cache the value so that it can be constructed onActivityCreated. 121 mTitle = info.getDisplayName(); 122 if (getToolbar() != null) { 123 getToolbar().setTitle(mTitle); 124 } 125 } 126 } 127 128 public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 129 new CarBaseSearchIndexProvider(R.xml.mobile_network_fragment, 130 Settings.ACTION_NETWORK_OPERATOR_SETTINGS) { 131 @Override 132 protected boolean isPageSearchEnabled(Context context) { 133 return NetworkUtils.hasMobileNetwork( 134 context.getSystemService(ConnectivityManager.class)); 135 } 136 }; 137 } 138