Fall back to Wi-Fi data display in data usage screen with no SIM.

Bug: 70950124

Test: manual
Test: make RunSettingsRoboTests
Change-Id: I06bf78e54119819be87e15baca7e5b6a241958cb
This commit is contained in:
Jan Nordqvist
2018-03-26 15:29:44 -07:00
parent c62ecf45db
commit 9eb43ddddf
13 changed files with 420 additions and 73 deletions

View File

@@ -14,11 +14,17 @@
package com.android.settings.datausage;
import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.TYPE_WIFI;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.INetworkStatsService;
import android.net.INetworkStatsSession;
import android.net.NetworkTemplate;
import android.net.TrafficStats;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -31,10 +37,44 @@ import java.util.List;
public final class DataUsageUtils {
static final boolean TEST_RADIOS = false;
static final String TEST_RADIOS_PROP = "test.radios";
private static final String ETHERNET = "ethernet";
private DataUsageUtils() {
}
/**
* Test if device has an ethernet network connection.
*/
public static boolean hasEthernet(Context context) {
if (DataUsageUtils.TEST_RADIOS) {
return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains(ETHERNET);
}
final ConnectivityManager conn = ConnectivityManager.from(context);
final boolean hasEthernet = conn.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET);
final long ethernetBytes;
try {
INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
INetworkStatsSession statsSession = statsService.openSession();
if (statsSession != null) {
ethernetBytes = statsSession.getSummaryForNetwork(
NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
.getTotalBytes();
TrafficStats.closeQuietly(statsSession);
} else {
ethernetBytes = 0;
}
} catch (RemoteException e) {
throw new RuntimeException(e);
}
// only show ethernet when both hardware present and traffic has occurred
return hasEthernet && ethernetBytes > 0;
}
/**
* Returns whether device has mobile data.
* TODO: This is the opposite to Utils.isWifiOnly(), it should be refactored into 1 method.
@@ -53,10 +93,19 @@ public final class DataUsageUtils {
return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
}
ConnectivityManager connectivityManager = ConnectivityManager.from(context);
ConnectivityManager connectivityManager =
context.getSystemService(ConnectivityManager.class);
return connectivityManager != null && connectivityManager.isNetworkSupported(TYPE_WIFI);
}
public static boolean hasSim(Context context) {
TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
final int simState = telephonyManager.getSimState();
// Note that pulling the SIM card returns UNKNOWN, not ABSENT.
return simState != TelephonyManager.SIM_STATE_ABSENT
&& simState != TelephonyManager.SIM_STATE_UNKNOWN;
}
/**
* Returns the default subscription if available else returns
* SubscriptionManager#INVALID_SUBSCRIPTION_ID