1 /*
2  * Copyright (C) 2020 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 static com.android.settings.network.InternetUpdater.INTERNET_CELLULAR;
23 import static com.android.settings.network.InternetUpdater.INTERNET_ETHERNET;
24 import static com.android.settings.network.InternetUpdater.INTERNET_NETWORKS_AVAILABLE;
25 import static com.android.settings.network.InternetUpdater.INTERNET_OFF;
26 import static com.android.settings.network.InternetUpdater.INTERNET_WIFI;
27 
28 import android.content.Context;
29 import android.graphics.drawable.Drawable;
30 import android.telephony.SubscriptionInfo;
31 import android.telephony.SubscriptionManager;
32 
33 import androidx.annotation.IdRes;
34 import androidx.annotation.VisibleForTesting;
35 import androidx.lifecycle.Lifecycle;
36 import androidx.lifecycle.LifecycleObserver;
37 import androidx.lifecycle.OnLifecycleEvent;
38 import androidx.preference.Preference;
39 import androidx.preference.PreferenceScreen;
40 
41 import com.android.settings.R;
42 import com.android.settings.widget.SummaryUpdater;
43 import com.android.settings.wifi.WifiSummaryUpdater;
44 import com.android.settingslib.Utils;
45 import com.android.settingslib.core.AbstractPreferenceController;
46 import com.android.settingslib.utils.ThreadUtils;
47 
48 import java.util.HashMap;
49 import java.util.Map;
50 
51 /**
52  * PreferenceController to update the internet state.
53  */
54 public class InternetPreferenceController extends AbstractPreferenceController implements
55         LifecycleObserver, SummaryUpdater.OnSummaryChangeListener,
56         InternetUpdater.InternetChangeListener {
57 
58     public static final String KEY = "internet_settings";
59 
60     private Preference mPreference;
61     private final WifiSummaryUpdater mSummaryHelper;
62     private InternetUpdater mInternetUpdater;
63     private @InternetUpdater.InternetType int mInternetType;
64 
65     @VisibleForTesting
66     static Map<Integer, Integer> sIconMap = new HashMap<>();
67     static {
sIconMap.put(INTERNET_OFF, R.drawable.ic_no_internet_unavailable)68         sIconMap.put(INTERNET_OFF, R.drawable.ic_no_internet_unavailable);
sIconMap.put(INTERNET_NETWORKS_AVAILABLE, R.drawable.ic_no_internet_available)69         sIconMap.put(INTERNET_NETWORKS_AVAILABLE, R.drawable.ic_no_internet_available);
sIconMap.put(INTERNET_WIFI, R.drawable.ic_wifi_signal_4)70         sIconMap.put(INTERNET_WIFI, R.drawable.ic_wifi_signal_4);
sIconMap.put(INTERNET_CELLULAR, R.drawable.ic_network_cell)71         sIconMap.put(INTERNET_CELLULAR, R.drawable.ic_network_cell);
sIconMap.put(INTERNET_ETHERNET, R.drawable.ic_settings_ethernet)72         sIconMap.put(INTERNET_ETHERNET, R.drawable.ic_settings_ethernet);
73     }
74 
75     private static Map<Integer, Integer> sSummaryMap = new HashMap<>();
76     static {
sSummaryMap.put(INTERNET_OFF, R.string.condition_airplane_title)77         sSummaryMap.put(INTERNET_OFF, R.string.condition_airplane_title);
sSummaryMap.put(INTERNET_NETWORKS_AVAILABLE, R.string.networks_available)78         sSummaryMap.put(INTERNET_NETWORKS_AVAILABLE, R.string.networks_available);
sSummaryMap.put(INTERNET_WIFI, 0)79         sSummaryMap.put(INTERNET_WIFI, 0);
sSummaryMap.put(INTERNET_CELLULAR, 0)80         sSummaryMap.put(INTERNET_CELLULAR, 0);
sSummaryMap.put(INTERNET_ETHERNET, R.string.to_switch_networks_disconnect_ethernet)81         sSummaryMap.put(INTERNET_ETHERNET, R.string.to_switch_networks_disconnect_ethernet);
82     }
83 
InternetPreferenceController(Context context, Lifecycle lifecycle)84     public InternetPreferenceController(Context context, Lifecycle lifecycle) {
85         super(context);
86         if (lifecycle == null) {
87             throw new IllegalArgumentException("Lifecycle must be set");
88         }
89         mSummaryHelper = new WifiSummaryUpdater(mContext, this);
90         mInternetUpdater = new InternetUpdater(context, lifecycle, this);
91         mInternetType = mInternetUpdater.getInternetType();
92         lifecycle.addObserver(this);
93     }
94 
95     @Override
displayPreference(PreferenceScreen screen)96     public void displayPreference(PreferenceScreen screen) {
97         super.displayPreference(screen);
98         mPreference = screen.findPreference(KEY);
99     }
100 
101     @Override
updateState(Preference preference)102     public void updateState(Preference preference) {
103         if (mPreference == null) {
104             return;
105         }
106 
107         final @IdRes int icon = sIconMap.get(mInternetType);
108         if (icon != 0) {
109             final Drawable drawable = mContext.getDrawable(icon);
110             if (drawable != null) {
111                 drawable.setTintList(
112                         Utils.getColorAttr(mContext, android.R.attr.colorControlNormal));
113                 mPreference.setIcon(drawable);
114             }
115         }
116 
117         if (mInternetType == INTERNET_WIFI) {
118             mPreference.setSummary(mSummaryHelper.getSummary());
119             return;
120         }
121 
122         if (mInternetType == INTERNET_CELLULAR) {
123             updateCellularSummary();
124             return;
125         }
126 
127         final @IdRes int summary = sSummaryMap.get(mInternetType);
128         if (summary != 0) {
129             mPreference.setSummary(summary);
130         }
131     }
132 
133     @Override
isAvailable()134     public boolean isAvailable() {
135         return true;
136     }
137 
138     @Override
getPreferenceKey()139     public String getPreferenceKey() {
140         return KEY;
141     }
142 
143     /** @OnLifecycleEvent(ON_RESUME) */
144     @OnLifecycleEvent(ON_RESUME)
onResume()145     public void onResume() {
146         mSummaryHelper.register(true);
147     }
148 
149     /** @OnLifecycleEvent(ON_PAUSE) */
150     @OnLifecycleEvent(ON_PAUSE)
onPause()151     public void onPause() {
152         mSummaryHelper.register(false);
153     }
154 
155     /**
156      * Called when internet type is changed.
157      *
158      * @param internetType the internet type
159      */
160     @Override
onInternetTypeChanged(@nternetUpdater.InternetType int internetType)161     public void onInternetTypeChanged(@InternetUpdater.InternetType int internetType) {
162         final boolean needUpdate = (internetType != mInternetType);
163         mInternetType = internetType;
164         if (needUpdate) {
165             ThreadUtils.postOnMainThread(() -> {
166                 updateState(mPreference);
167             });
168         }
169     }
170 
171     /**
172      * Called when airplane mode state is changed.
173      */
174     @Override
onAirplaneModeChanged(boolean isAirplaneModeOn)175     public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
176         ThreadUtils.postOnMainThread(() -> {
177             updateState(mPreference);
178         });
179     }
180 
181     @Override
onSummaryChanged(String summary)182     public void onSummaryChanged(String summary) {
183         if (mInternetType == INTERNET_WIFI && mPreference != null) {
184             mPreference.setSummary(summary);
185         }
186     }
187 
188     @VisibleForTesting
updateCellularSummary()189     void updateCellularSummary() {
190         final SubscriptionManager subscriptionManager =
191                 mContext.getSystemService(SubscriptionManager.class);
192         if (subscriptionManager == null) {
193             return;
194         }
195         SubscriptionInfo subInfo = subscriptionManager.getDefaultDataSubscriptionInfo();
196         if (subInfo == null) {
197             return;
198         }
199         mPreference.setSummary(SubscriptionUtil.getUniqueSubscriptionDisplayName(
200                 subInfo, mContext));
201     }
202 }
203