Files
app_Settings/src/com/android/settings/network/InternetPreferenceController.java
Ling Ma 4e7f783906 Convert dataDuringCall to autoDataSWitch
The feature is enabled via
TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH. The feature only
applies to non-DDS. If enabled, we automatically switch to the non-DDS for data traffic when it has better availability than the DDS.

The UI change are(as required by b/247880971):
1. data during call is replaced by auto data switch, and moved under
   mobile data toggle.
2. Network & internet and Internet page now show the currently active
   subscription, instead of the defalut subscription. If the currently
   active sub is not the default, it's reflected in summary as
   "temporarily".

Test: manual
Bug: 244064524
Change-Id: Ica1eba99cee0d4af528d58f1c7bd1439400bfa66
2022-10-14 13:23:08 -07:00

214 lines
7.3 KiB
Java

/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import static com.android.settings.network.InternetUpdater.INTERNET_CELLULAR;
import static com.android.settings.network.InternetUpdater.INTERNET_ETHERNET;
import static com.android.settings.network.InternetUpdater.INTERNET_NETWORKS_AVAILABLE;
import static com.android.settings.network.InternetUpdater.INTERNET_OFF;
import static com.android.settings.network.InternetUpdater.INTERNET_WIFI;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import androidx.annotation.IdRes;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.widget.SummaryUpdater;
import com.android.settings.wifi.WifiSummaryUpdater;
import com.android.settingslib.Utils;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.utils.ThreadUtils;
import java.util.HashMap;
import java.util.Map;
/**
* PreferenceController to update the internet state.
*/
public class InternetPreferenceController extends AbstractPreferenceController implements
LifecycleObserver, SummaryUpdater.OnSummaryChangeListener,
InternetUpdater.InternetChangeListener {
public static final String KEY = "internet_settings";
private Preference mPreference;
private final WifiSummaryUpdater mSummaryHelper;
private InternetUpdater mInternetUpdater;
private @InternetUpdater.InternetType int mInternetType;
@VisibleForTesting
static Map<Integer, Integer> sIconMap = new HashMap<>();
static {
sIconMap.put(INTERNET_OFF, R.drawable.ic_no_internet_unavailable);
sIconMap.put(INTERNET_NETWORKS_AVAILABLE, R.drawable.ic_no_internet_available);
sIconMap.put(INTERNET_WIFI, R.drawable.ic_wifi_signal_4);
sIconMap.put(INTERNET_CELLULAR, R.drawable.ic_network_cell);
sIconMap.put(INTERNET_ETHERNET, R.drawable.ic_settings_ethernet);
}
private static Map<Integer, Integer> sSummaryMap = new HashMap<>();
static {
sSummaryMap.put(INTERNET_OFF, R.string.condition_airplane_title);
sSummaryMap.put(INTERNET_NETWORKS_AVAILABLE, R.string.networks_available);
sSummaryMap.put(INTERNET_WIFI, 0);
sSummaryMap.put(INTERNET_CELLULAR, 0);
sSummaryMap.put(INTERNET_ETHERNET, R.string.to_switch_networks_disconnect_ethernet);
}
public InternetPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
if (lifecycle == null) {
throw new IllegalArgumentException("Lifecycle must be set");
}
mSummaryHelper = new WifiSummaryUpdater(mContext, this);
mInternetUpdater = new InternetUpdater(context, lifecycle, this);
mInternetType = mInternetUpdater.getInternetType();
lifecycle.addObserver(this);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(KEY);
}
@Override
public void updateState(Preference preference) {
if (mPreference == null) {
return;
}
final @IdRes int icon = sIconMap.get(mInternetType);
if (icon != 0) {
final Drawable drawable = mContext.getDrawable(icon);
if (drawable != null) {
drawable.setTintList(
Utils.getColorAttr(mContext, android.R.attr.colorControlNormal));
mPreference.setIcon(drawable);
}
}
if (mInternetType == INTERNET_WIFI) {
mPreference.setSummary(mSummaryHelper.getSummary());
return;
}
if (mInternetType == INTERNET_CELLULAR) {
updateCellularSummary();
return;
}
final @IdRes int summary = sSummaryMap.get(mInternetType);
if (summary != 0) {
mPreference.setSummary(summary);
}
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY;
}
/** @OnLifecycleEvent(ON_RESUME) */
@OnLifecycleEvent(ON_RESUME)
public void onResume() {
mSummaryHelper.register(true);
}
/** @OnLifecycleEvent(ON_PAUSE) */
@OnLifecycleEvent(ON_PAUSE)
public void onPause() {
mSummaryHelper.register(false);
}
/**
* Called when internet type is changed.
*
* @param internetType the internet type
*/
@Override
public void onInternetTypeChanged(@InternetUpdater.InternetType int internetType) {
final boolean needUpdate = (internetType != mInternetType);
mInternetType = internetType;
if (needUpdate) {
ThreadUtils.postOnMainThread(() -> {
updateState(mPreference);
});
}
}
/**
* Called when airplane mode state is changed.
*/
@Override
public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
ThreadUtils.postOnMainThread(() -> {
updateState(mPreference);
});
}
@Override
public void onSummaryChanged(String summary) {
if (mInternetType == INTERNET_WIFI && mPreference != null) {
mPreference.setSummary(summary);
}
}
@VisibleForTesting
void updateCellularSummary() {
final SubscriptionManager subscriptionManager =
mContext.getSystemService(SubscriptionManager.class);
if (subscriptionManager == null) {
return;
}
SubscriptionInfo subInfo = subscriptionManager.getActiveSubscriptionInfo(
SubscriptionManager.getActiveDataSubscriptionId());
SubscriptionInfo defaultSubInfo = subscriptionManager.getDefaultDataSubscriptionInfo();
subInfo = subscriptionManager.isSubscriptionVisible(subInfo) ? subInfo : defaultSubInfo;
if (subInfo == null) {
return;
}
CharSequence summary;
if (subInfo.equals(defaultSubInfo)) {
// DDS is active
summary = SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, mContext);
} else {
summary = mContext.getString(
R.string.mobile_data_temp_using,
SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, mContext));
}
mPreference.setSummary(summary);
}
}