Merge "Dynamically enable/disable watch for RAT type changes" am: 04baaeb973 am: abc4648d47 am: eb1a7159b0 am: 17f6c1ba40

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1315574

Change-Id: I61d7242d2c597e680953d2603b0b54962dd749b3
This commit is contained in:
Aaron Huang
2020-06-18 04:40:20 +00:00
committed by Automerger Merge Worker

View File

@@ -87,6 +87,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.DataUsageRequest;
import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsService;
@@ -103,6 +104,7 @@ import android.net.NetworkStats.NonMonotonicObserver;
import android.net.NetworkStatsHistory;
import android.net.NetworkTemplate;
import android.net.TrafficStats;
import android.net.Uri;
import android.net.netstats.provider.INetworkStatsProvider;
import android.net.netstats.provider.INetworkStatsProviderCallback;
import android.net.netstats.provider.NetworkStatsProvider;
@@ -213,6 +215,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private final boolean mUseBpfTrafficStats;
private final ContentObserver mContentObserver;
private final ContentResolver mContentResolver;
@VisibleForTesting
public static final String ACTION_NETWORK_STATS_POLL =
"com.android.server.action.NETWORK_STATS_POLL";
@@ -438,6 +443,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mHandler = new NetworkStatsHandler(handlerThread.getLooper());
mNetworkStatsSubscriptionsMonitor = deps.makeSubscriptionsMonitor(mContext,
new HandlerExecutor(mHandler), this);
mContentResolver = mContext.getContentResolver();
mContentObserver = mDeps.makeContentObserver(mHandler, mSettings,
mNetworkStatsSubscriptionsMonitor);
}
/**
@@ -466,6 +474,25 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return new NetworkStatsSubscriptionsMonitor(context, executor, (subscriberId, type) ->
service.handleOnCollapsedRatTypeChanged());
}
/**
* Create a ContentObserver instance which is used to observe settings changes,
* and dispatch onChange events on handler thread.
*/
public @NonNull ContentObserver makeContentObserver(@NonNull Handler handler,
@NonNull NetworkStatsSettings settings,
@NonNull NetworkStatsSubscriptionsMonitor monitor) {
return new ContentObserver(handler) {
@Override
public void onChange(boolean selfChange, @NonNull Uri uri) {
if (!settings.getCombineSubtypeEnabled()) {
monitor.start();
} else {
monitor.stop();
}
}
};
}
}
private void registerLocalService() {
@@ -530,11 +557,14 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime,
mSettings.getPollInterval(), pollIntent);
// TODO: listen to settings changed to support dynamically enable/disable.
// watch for networkType changes
if (!mSettings.getCombineSubtypeEnabled()) {
mNetworkStatsSubscriptionsMonitor.start();
}
mContentResolver.registerContentObserver(Settings.Global
.getUriFor(Settings.Global.NETSTATS_COMBINE_SUBTYPE_ENABLED),
false /* notifyForDescendants */, mContentObserver);
// Post a runnable on handler thread to call onChange(). It's for getting current value of
// NETSTATS_COMBINE_SUBTYPE_ENABLED to decide start or stop monitoring RAT type changes.
mHandler.post(() -> mContentObserver.onChange(false, Settings.Global
.getUriFor(Settings.Global.NETSTATS_COMBINE_SUBTYPE_ENABLED)));
registerGlobalAlert();
}
@@ -560,6 +590,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mNetworkStatsSubscriptionsMonitor.stop();
}
mContentResolver.unregisterContentObserver(mContentObserver);
final long currentTime = mClock.millis();
// persist any pending stats