Fix two problems related to data connectivity in the multi-SIM header

When you have multiple active SIMs, the Network & internet page has a
header showing entries for each one, with summary text indicating which
one is used for data (and whether it is just set as the default, or
actively using data). We were not properly setting this text when either
data wasn't being used, eg when connected to wifi, or mobile data was
disabled for this SIM. This CL fixes both these problems by adding new
helper classes to listen for relevant events.

Test: make RunSettingsRoboTests
Fixes: 124394250
Fixes: 128857712
Change-Id: I34f2679752fa41a50247dd0b12581cbfd77a34f6
This commit is contained in:
Antony Sargent
2019-05-07 16:01:09 -07:00
parent 02a54a0150
commit 4bb253358b
6 changed files with 419 additions and 7 deletions

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 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 android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
/** Helper class to listen for changes in the enabled state of mobile data. */
public class MobileDataEnabledListener extends ContentObserver {
private Context mContext;
private Client mClient;
private int mSubId;
public interface Client {
void onMobileDataEnabledChange();
}
public MobileDataEnabledListener(Context context, Client client) {
super(new Handler());
mContext = context;
mClient = client;
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
}
/** Starts listening to changes in the enabled state for data on the given subscription id. */
public void start(int subId) {
mSubId = subId;
Uri uri;
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA);
} else {
uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + mSubId);
}
mContext.getContentResolver().registerContentObserver(uri, true /*notifyForDescendants*/,
this);
}
public int getSubId() {
return mSubId;
}
public MobileDataEnabledListener stop() {
mContext.getContentResolver().unregisterContentObserver(this);
return this;
}
@Override
public void onChange(boolean selfChange) {
mClient.onMobileDataEnabledChange();
}
}

View File

@@ -21,6 +21,9 @@ import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -35,6 +38,7 @@ import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.network.telephony.DataConnectivityListener;
import com.android.settings.network.telephony.MobileNetworkActivity;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -46,14 +50,18 @@ import java.util.Map;
* available if there are 2 or more subscriptions.
*/
public class SubscriptionsPreferenceController extends AbstractPreferenceController implements
LifecycleObserver, SubscriptionsChangeListener.SubscriptionsChangeListenerClient {
LifecycleObserver, SubscriptionsChangeListener.SubscriptionsChangeListenerClient,
MobileDataEnabledListener.Client, DataConnectivityListener.Client {
private static final String TAG = "SubscriptionsPrefCntrlr";
private UpdateListener mUpdateListener;
private String mPreferenceGroupKey;
private PreferenceGroup mPreferenceGroup;
private SubscriptionManager mManager;
private ConnectivityManager mConnectivityManager;
private SubscriptionsChangeListener mSubscriptionsListener;
private MobileDataEnabledListener mDataEnabledListener;
private DataConnectivityListener mConnectivityListener;
// Map of subscription id to Preference
private Map<Integer, Preference> mSubscriptionPreferences;
@@ -89,20 +97,27 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mPreferenceGroupKey = preferenceGroupKey;
mStartOrder = startOrder;
mManager = context.getSystemService(SubscriptionManager.class);
mConnectivityManager = mContext.getSystemService(ConnectivityManager.class);
mSubscriptionPreferences = new ArrayMap<>();
mSubscriptionsListener = new SubscriptionsChangeListener(context, this);
mDataEnabledListener = new MobileDataEnabledListener(context, this);
mConnectivityListener = new DataConnectivityListener(context, this);
lifecycle.addObserver(this);
}
@OnLifecycleEvent(ON_RESUME)
public void onResume() {
mSubscriptionsListener.start();
mDataEnabledListener.start(SubscriptionManager.getDefaultDataSubscriptionId());
mConnectivityListener.start();
update();
}
@OnLifecycleEvent(ON_PAUSE)
public void onPause() {
mSubscriptionsListener.stop();
mDataEnabledListener.stop();
mConnectivityListener.stop();
}
@Override
@@ -158,6 +173,19 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mUpdateListener.onChildrenUpdated();
}
private boolean activeNetworkIsCellular() {
final Network activeNetwork = mConnectivityManager.getActiveNetwork();
if (activeNetwork == null) {
return false;
}
final NetworkCapabilities networkCapabilities = mConnectivityManager.getNetworkCapabilities(
activeNetwork);
if (networkCapabilities == null) {
return false;
}
return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
}
/**
* The summary can have either 1 or 2 lines depending on which services (calls, SMS, data) this
* subscription is the default for.
@@ -187,10 +215,10 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
if (subId == dataDefaultSubId) {
final TelephonyManager telMgrForSub = mContext.getSystemService(
TelephonyManager.class).createForSubscriptionId(subId);
final int dataState = telMgrForSub.getDataState();
if (dataState == TelephonyManager.DATA_CONNECTED) {
boolean dataEnabled = telMgrForSub.isDataEnabled();
if (dataEnabled && activeNetworkIsCellular()) {
line2 = mContext.getString(R.string.mobile_data_active);
} else if (!telMgrForSub.isDataEnabled()) {
} else if (!dataEnabled) {
line2 = mContext.getString(R.string.mobile_data_off);
} else {
line2 = mContext.getString(R.string.default_for_mobile_data);
@@ -231,6 +259,22 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
@Override
public void onSubscriptionsChanged() {
// See if we need to change which sub id we're using to listen for enabled/disabled changes.
int defaultDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
if (defaultDataSubId != mDataEnabledListener.getSubId()) {
mDataEnabledListener.stop();
mDataEnabledListener.start(defaultDataSubId);
}
update();
}
@Override
public void onMobileDataEnabledChange() {
update();
}
@Override
public void onDataConnectivityChange() {
update();
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2019 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.telephony;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
/** A helper class to listen to a few different kinds of connectivity changes that could be relevant
* to changes in which network is active, and whether the active network has internet data
* connectivity. */
public class DataConnectivityListener extends ConnectivityManager.NetworkCallback {
private Context mContext;
private ConnectivityManager mConnectivityManager;
private final NetworkRequest mNetworkRequest;
private Client mClient;
public interface Client {
void onDataConnectivityChange();
}
public DataConnectivityListener(Context context, Client client) {
mContext = context;
mConnectivityManager = mContext.getSystemService(ConnectivityManager.class);
mClient = client;
mNetworkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build();
}
public void start() {
mConnectivityManager.registerNetworkCallback(mNetworkRequest, this,
mContext.getMainThreadHandler());
}
public void stop() {
mConnectivityManager.unregisterNetworkCallback(this);
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
final Network activeNetwork = mConnectivityManager.getActiveNetwork();
if (activeNetwork != null && activeNetwork.equals(network)) {
mClient.onDataConnectivityChange();
}
}
@Override
public void onLosing(Network network, int maxMsToLive) {
mClient.onDataConnectivityChange();
}
@Override
public void onLost(Network network) {
mClient.onDataConnectivityChange();
}
}