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.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.os.UserManager; 23 import android.telephony.SubscriptionInfo; 24 import android.telephony.SubscriptionManager; 25 import android.telephony.TelephonyManager; 26 27 import androidx.preference.Preference; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.FragmentController; 31 import com.android.car.settings.common.PreferenceController; 32 33 import java.util.List; 34 35 /** Controls the preference for accessing mobile network settings. */ 36 public class MobileNetworkEntryPreferenceController extends 37 PreferenceController<Preference> implements 38 SubscriptionsChangeListener.SubscriptionsChangeAction { 39 40 private final UserManager mUserManager; 41 private final SubscriptionsChangeListener mChangeListener; 42 private final SubscriptionManager mSubscriptionManager; 43 private final ConnectivityManager mConnectivityManager; 44 private final TelephonyManager mTelephonyManager; 45 MobileNetworkEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46 public MobileNetworkEntryPreferenceController(Context context, String preferenceKey, 47 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 48 super(context, preferenceKey, fragmentController, uxRestrictions); 49 mUserManager = UserManager.get(context); 50 mChangeListener = new SubscriptionsChangeListener(context, /* action= */ this); 51 mSubscriptionManager = context.getSystemService(SubscriptionManager.class); 52 mConnectivityManager = context.getSystemService(ConnectivityManager.class); 53 mTelephonyManager = context.getSystemService(TelephonyManager.class); 54 } 55 56 @Override getPreferenceType()57 protected Class<Preference> getPreferenceType() { 58 return Preference.class; 59 } 60 61 @Override onStartInternal()62 protected void onStartInternal() { 63 mChangeListener.start(); 64 } 65 66 @Override onStopInternal()67 protected void onStopInternal() { 68 mChangeListener.stop(); 69 } 70 71 @Override getAvailabilityStatus()72 protected int getAvailabilityStatus() { 73 if (!NetworkUtils.hasMobileNetwork(mConnectivityManager)) { 74 return UNSUPPORTED_ON_DEVICE; 75 } 76 77 boolean isNotAdmin = !mUserManager.isAdminUser(); 78 boolean hasRestriction = 79 mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); 80 if (isNotAdmin || hasRestriction) { 81 return DISABLED_FOR_PROFILE; 82 } 83 return AVAILABLE; 84 } 85 86 @Override updateState(Preference preference)87 protected void updateState(Preference preference) { 88 List<SubscriptionInfo> subs = SubscriptionUtils.getAvailableSubscriptions( 89 mSubscriptionManager, mTelephonyManager); 90 preference.setEnabled(!subs.isEmpty()); 91 preference.setSummary(getSummary(subs)); 92 } 93 94 @Override handlePreferenceClicked(Preference preference)95 protected boolean handlePreferenceClicked(Preference preference) { 96 List<SubscriptionInfo> subs = SubscriptionUtils.getAvailableSubscriptions( 97 mSubscriptionManager, mTelephonyManager); 98 if (subs.isEmpty()) { 99 return true; 100 } 101 102 if (subs.size() == 1) { 103 getFragmentController().launchFragment( 104 MobileNetworkFragment.newInstance(subs.get(0).getSubscriptionId())); 105 } else { 106 getFragmentController().launchFragment(new MobileNetworkListFragment()); 107 } 108 return true; 109 } 110 111 @Override onSubscriptionsChanged()112 public void onSubscriptionsChanged() { 113 refreshUi(); 114 } 115 getSummary(List<SubscriptionInfo> subs)116 private CharSequence getSummary(List<SubscriptionInfo> subs) { 117 int count = subs.size(); 118 if (subs.isEmpty()) { 119 return null; 120 } else if (count == 1) { 121 return subs.get(0).getDisplayName(); 122 } else { 123 return getContext().getResources().getQuantityString( 124 R.plurals.mobile_network_summary_count, count, count); 125 } 126 } 127 } 128