Dynamically enable/disable watch for RAT type changes
Add ContentObserver to observe the status of NETSTATS_COMBINE_SUBTYPE_ENABLED to determine that NetworkStatsSubscriptionsMonitor start or stop monitoring data usage per RAT type changes. Bug: 154080515 Test: atest NetworkStatsServiceTest#testDynamicWatchForNetworkRatTypeChanges Change-Id: I272d589633c9186b45753228b05aa0b40b7c7b89
This commit is contained in:
committed by
Chalard Jean
parent
fc00838335
commit
e436804466
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user