• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.network;
18 
19 import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
20 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.UserManager;
25 import android.provider.Settings;
26 import android.telephony.SubscriptionInfo;
27 import android.telephony.SubscriptionManager;
28 import android.telephony.euicc.EuiccManager;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settings.network.telephony.MobileNetworkActivity;
33 import com.android.settings.network.telephony.MobileNetworkUtils;
34 import com.android.settings.widget.AddPreference;
35 import com.android.settingslib.Utils;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 
38 import java.util.List;
39 
40 import androidx.lifecycle.Lifecycle;
41 import androidx.lifecycle.LifecycleObserver;
42 import androidx.lifecycle.OnLifecycleEvent;
43 import androidx.preference.Preference;
44 import androidx.preference.PreferenceScreen;
45 
46 public class MobileNetworkSummaryController extends AbstractPreferenceController implements
47         SubscriptionsChangeListener.SubscriptionsChangeListenerClient, LifecycleObserver,
48         PreferenceControllerMixin {
49     private static final String TAG = "MobileNetSummaryCtlr";
50 
51     private static final String KEY = "mobile_network_list";
52 
53     private SubscriptionManager mSubscriptionManager;
54     private UserManager mUserManager;
55     private SubscriptionsChangeListener mChangeListener;
56     private AddPreference mPreference;
57 
58     /**
59      * This controls the summary text and click behavior of the "Mobile network" item on the
60      * Network & internet page. There are 3 separate cases depending on the number of mobile network
61      * subscriptions:
62      * <ul>
63      * <li>No subscription: click action begins a UI flow to add a network subscription, and
64      * the summary text indicates this</li>
65      *
66      * <li>One subscription: click action takes you to details for that one network, and
67      * the summary text is the network name</li>
68      *
69      * <li>More than one subscription: click action takes you to a page listing the subscriptions,
70      * and the summary text gives the count of SIMs</li>
71      * </ul>
72      */
MobileNetworkSummaryController(Context context, Lifecycle lifecycle)73     public MobileNetworkSummaryController(Context context, Lifecycle lifecycle) {
74         super(context);
75         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
76         mUserManager = context.getSystemService(UserManager.class);
77         if (lifecycle != null) {
78           mChangeListener = new SubscriptionsChangeListener(context, this);
79           lifecycle.addObserver(this);
80         }
81     }
82 
83     @OnLifecycleEvent(ON_RESUME)
onResume()84     public void onResume() {
85         mChangeListener.start();
86         update();
87     }
88 
89     @OnLifecycleEvent(ON_PAUSE)
onPause()90     public void onPause() {
91         mChangeListener.stop();
92     }
93 
94     @Override
displayPreference(PreferenceScreen screen)95     public void displayPreference(PreferenceScreen screen) {
96         super.displayPreference(screen);
97         mPreference = screen.findPreference(getPreferenceKey());
98     }
99 
100     @Override
getSummary()101     public CharSequence getSummary() {
102         final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(
103                 mContext);
104         if (subs.isEmpty()) {
105             if (MobileNetworkUtils.showEuiccSettings(mContext)) {
106                 return mContext.getResources().getString(
107                         R.string.mobile_network_summary_add_a_network);
108             }
109             return null;
110         } else if (subs.size() == 1) {
111             final SubscriptionInfo info = subs.get(0);
112             final int subId = info.getSubscriptionId();
113             if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId)) {
114                 return mContext.getString(R.string.mobile_network_tap_to_activate,
115                         SubscriptionUtil.getDisplayName(info));
116             } else {
117                 return subs.get(0).getDisplayName();
118             }
119         } else {
120             final int count = subs.size();
121             return mContext.getResources().getQuantityString(R.plurals.mobile_network_summary_count,
122                     count, count);
123         }
124     }
125 
startAddSimFlow()126     private void startAddSimFlow() {
127         final Intent intent = new Intent(EuiccManager.ACTION_PROVISION_EMBEDDED_SUBSCRIPTION);
128         intent.putExtra(EuiccManager.EXTRA_FORCE_PROVISION, true);
129         mContext.startActivity(intent);
130     }
131 
update()132     private void update() {
133         if (mPreference == null) {
134             return;
135         }
136         refreshSummary(mPreference);
137         mPreference.setOnPreferenceClickListener(null);
138         mPreference.setOnAddClickListener(null);
139         mPreference.setFragment(null);
140         mPreference.setEnabled(!mChangeListener.isAirplaneModeOn());
141 
142         final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(
143                 mContext);
144 
145         if (subs.isEmpty()) {
146             if (MobileNetworkUtils.showEuiccSettings(mContext)) {
147                 mPreference.setOnPreferenceClickListener((Preference pref) -> {
148                     startAddSimFlow();
149                     return true;
150                 });
151             } else {
152                 mPreference.setEnabled(false);
153             }
154         } else {
155             // We have one or more existing subscriptions, so we want the plus button if eSIM is
156             // supported.
157             if (MobileNetworkUtils.showEuiccSettings(mContext)) {
158                 mPreference.setAddWidgetEnabled(!mChangeListener.isAirplaneModeOn());
159                 mPreference.setOnAddClickListener(p -> startAddSimFlow());
160             }
161 
162             if (subs.size() == 1) {
163                 mPreference.setOnPreferenceClickListener((Preference pref) -> {
164                     final SubscriptionInfo info = subs.get(0);
165                     final int subId = info.getSubscriptionId();
166                     if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId)) {
167                         mSubscriptionManager.setSubscriptionEnabled(subId, true);
168                     } else {
169                         final Intent intent = new Intent(mContext, MobileNetworkActivity.class);
170                         intent.putExtra(Settings.EXTRA_SUB_ID, subs.get(0).getSubscriptionId());
171                         mContext.startActivity(intent);
172                     }
173                     return true;
174                 });
175             } else {
176                 mPreference.setFragment(MobileNetworkListFragment.class.getCanonicalName());
177             }
178         }
179     }
180 
181     @Override
isAvailable()182     public boolean isAvailable() {
183         return !Utils.isWifiOnly(mContext) && mUserManager.isAdminUser();
184     }
185 
186     @Override
getPreferenceKey()187     public String getPreferenceKey() {
188         return KEY;
189     }
190 
191     @Override
onAirplaneModeChanged(boolean airplaneModeEnabled)192     public void onAirplaneModeChanged(boolean airplaneModeEnabled) {
193         update();
194     }
195 
196     @Override
onSubscriptionsChanged()197     public void onSubscriptionsChanged() {
198         refreshSummary(mPreference);
199         update();
200     }
201 }
202