Merge changes I54c2258c,I47b2d3ac

* changes:
  [MS38] Remove android.os.HandlerExecutor dependencies
  [MS58] Expose Apis which will be used by data migration utility
This commit is contained in:
Junyu Lai
2022-01-24 14:08:56 +00:00
committed by Gerrit Code Review
5 changed files with 70 additions and 41 deletions

View File

@@ -16,6 +16,7 @@
package android.net; package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.NetworkTemplate.NETWORK_TYPE_ALL; import static android.net.NetworkTemplate.NETWORK_TYPE_ALL;
@@ -24,6 +25,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.content.Context; import android.content.Context;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.service.NetworkIdentityProto; import android.service.NetworkIdentityProto;
@@ -46,8 +48,8 @@ import java.util.Objects;
* *
* @hide * @hide
*/ */
// @SystemApi(client = MODULE_LIBRARIES) @SystemApi(client = MODULE_LIBRARIES)
public class NetworkIdentity implements Comparable<NetworkIdentity> { public class NetworkIdentity {
private static final String TAG = "NetworkIdentity"; private static final String TAG = "NetworkIdentity";
/** @hide */ /** @hide */
@@ -299,30 +301,30 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
return oemManaged; return oemManaged;
} }
@Override /** @hide */
public int compareTo(@NonNull NetworkIdentity another) { public static int compare(@NonNull NetworkIdentity left, @NonNull NetworkIdentity right) {
Objects.requireNonNull(another); Objects.requireNonNull(right);
int res = Integer.compare(mType, another.mType); int res = Integer.compare(left.mType, right.mType);
if (res == 0) { if (res == 0) {
res = Integer.compare(mRatType, another.mRatType); res = Integer.compare(left.mRatType, right.mRatType);
} }
if (res == 0 && mSubscriberId != null && another.mSubscriberId != null) { if (res == 0 && left.mSubscriberId != null && right.mSubscriberId != null) {
res = mSubscriberId.compareTo(another.mSubscriberId); res = left.mSubscriberId.compareTo(right.mSubscriberId);
} }
if (res == 0 && mWifiNetworkKey != null && another.mWifiNetworkKey != null) { if (res == 0 && left.mWifiNetworkKey != null && right.mWifiNetworkKey != null) {
res = mWifiNetworkKey.compareTo(another.mWifiNetworkKey); res = left.mWifiNetworkKey.compareTo(right.mWifiNetworkKey);
} }
if (res == 0) { if (res == 0) {
res = Boolean.compare(mRoaming, another.mRoaming); res = Boolean.compare(left.mRoaming, right.mRoaming);
} }
if (res == 0) { if (res == 0) {
res = Boolean.compare(mMetered, another.mMetered); res = Boolean.compare(left.mMetered, right.mMetered);
} }
if (res == 0) { if (res == 0) {
res = Boolean.compare(mDefaultNetwork, another.mDefaultNetwork); res = Boolean.compare(left.mDefaultNetwork, right.mDefaultNetwork);
} }
if (res == 0) { if (res == 0) {
res = Integer.compare(mOemManaged, another.mOemManaged); res = Integer.compare(left.mOemManaged, right.mOemManaged);
} }
return res; return res;
} }
@@ -362,7 +364,14 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
/** /**
* Add an {@link NetworkStateSnapshot} into the {@link NetworkIdentity} instance. * Add an {@link NetworkStateSnapshot} into the {@link NetworkIdentity} instance.
* This is to read roaming, metered, wifikey... from the snapshot for convenience. * This is a useful shorthand that will read from the snapshot and set the
* following fields, if they are set in the snapshot :
* - type
* - subscriberId
* - roaming
* - metered
* - oemManaged
* - wifiNetworkKey
* *
* @param snapshot The target {@link NetworkStateSnapshot} object. * @param snapshot The target {@link NetworkStateSnapshot} object.
* @return The builder object. * @return The builder object.
@@ -415,6 +424,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
/** /**
* Set the Radio Access Technology(RAT) type of the network. * Set the Radio Access Technology(RAT) type of the network.
* *
* No RAT type is specified by default. Call clearRatType to reset.
*
* @param ratType the Radio Access Technology(RAT) type if applicable. See * @param ratType the Radio Access Technology(RAT) type if applicable. See
* {@code TelephonyManager.NETWORK_TYPE_*}. * {@code TelephonyManager.NETWORK_TYPE_*}.
* *
@@ -470,6 +481,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
/** /**
* Set whether this network is roaming. * Set whether this network is roaming.
* *
* This field is false by default. Call with false to reset.
*
* @param roaming the roaming status of the network. * @param roaming the roaming status of the network.
* @return this builder. * @return this builder.
*/ */
@@ -482,6 +495,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
/** /**
* Set whether this network is metered. * Set whether this network is metered.
* *
* This field is false by default. Call with false to reset.
*
* @param metered the meteredness of the network. * @param metered the meteredness of the network.
* @return this builder. * @return this builder.
*/ */
@@ -494,6 +509,8 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
/** /**
* Set whether this network is the default network. * Set whether this network is the default network.
* *
* This field is false by default. Call with false to reset.
*
* @param defaultNetwork the default network status of the network. * @param defaultNetwork the default network status of the network.
* @return this builder. * @return this builder.
*/ */

View File

@@ -27,6 +27,7 @@ import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
/** /**
* Identity of a {@code iface}, defined by the set of {@link NetworkIdentity} * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity}
@@ -34,9 +35,7 @@ import java.util.Objects;
* *
* @hide * @hide
*/ */
// @SystemApi(client = MODULE_LIBRARIES) public class NetworkIdentitySet extends HashSet<NetworkIdentity> {
public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
Comparable<NetworkIdentitySet> {
private static final int VERSION_INIT = 1; private static final int VERSION_INIT = 1;
private static final int VERSION_ADD_ROAMING = 2; private static final int VERSION_ADD_ROAMING = 2;
private static final int VERSION_ADD_NETWORK_ID = 3; private static final int VERSION_ADD_NETWORK_ID = 3;
@@ -51,6 +50,11 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
super(); super();
} }
/** @hide */
public NetworkIdentitySet(@NonNull Set<NetworkIdentity> ident) {
super(ident);
}
/** @hide */ /** @hide */
public NetworkIdentitySet(DataInput in) throws IOException { public NetworkIdentitySet(DataInput in) throws IOException {
final int version = in.readInt(); final int version = in.readInt();
@@ -189,15 +193,15 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
} }
} }
@Override public static int compare(@NonNull NetworkIdentitySet left, @NonNull NetworkIdentitySet right) {
public int compareTo(@NonNull NetworkIdentitySet another) { Objects.requireNonNull(left);
Objects.requireNonNull(another); Objects.requireNonNull(right);
if (isEmpty()) return -1; if (left.isEmpty()) return -1;
if (another.isEmpty()) return 1; if (right.isEmpty()) return 1;
final NetworkIdentity ident = iterator().next(); final NetworkIdentity leftIdent = left.iterator().next();
final NetworkIdentity anotherIdent = another.iterator().next(); final NetworkIdentity rightIdent = right.iterator().next();
return ident.compareTo(anotherIdent); return NetworkIdentity.compare(leftIdent, rightIdent);
} }
/** /**

View File

@@ -72,6 +72,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
/** /**
* Collection of {@link NetworkStatsHistory}, stored based on combined key of * Collection of {@link NetworkStatsHistory}, stored based on combined key of
@@ -702,7 +703,7 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
private ArrayList<Key> getSortedKeys() { private ArrayList<Key> getSortedKeys() {
final ArrayList<Key> keys = new ArrayList<>(); final ArrayList<Key> keys = new ArrayList<>();
keys.addAll(mStats.keySet()); keys.addAll(mStats.keySet());
Collections.sort(keys); Collections.sort(keys, (left, right) -> Key.compare(left, right));
return keys; return keys;
} }
@@ -812,7 +813,7 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
* the identifier that associate with the {@link NetworkStatsHistory} object to identify * the identifier that associate with the {@link NetworkStatsHistory} object to identify
* a certain record in the {@link NetworkStatsCollection} object. * a certain record in the {@link NetworkStatsCollection} object.
*/ */
public static class Key implements Comparable<Key> { public static class Key {
/** @hide */ /** @hide */
public final NetworkIdentitySet ident; public final NetworkIdentitySet ident;
/** @hide */ /** @hide */
@@ -832,6 +833,11 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
* @param set Set of the record, see {@code NetworkStats#SET_*}. * @param set Set of the record, see {@code NetworkStats#SET_*}.
* @param tag Tag of the record, see {@link TrafficStats#setThreadStatsTag(int)}. * @param tag Tag of the record, see {@link TrafficStats#setThreadStatsTag(int)}.
*/ */
public Key(@NonNull Set<NetworkIdentity> ident, int uid, int set, int tag) {
this(new NetworkIdentitySet(Objects.requireNonNull(ident)), uid, set, tag);
}
/** @hide */
public Key(@NonNull NetworkIdentitySet ident, int uid, int set, int tag) { public Key(@NonNull NetworkIdentitySet ident, int uid, int set, int tag) {
this.ident = Objects.requireNonNull(ident); this.ident = Objects.requireNonNull(ident);
this.uid = uid; this.uid = uid;
@@ -855,21 +861,22 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
return false; return false;
} }
@Override /** @hide */
public int compareTo(@NonNull Key another) { public static int compare(@NonNull Key left, @NonNull Key right) {
Objects.requireNonNull(another); Objects.requireNonNull(left);
Objects.requireNonNull(right);
int res = 0; int res = 0;
if (ident != null && another.ident != null) { if (left.ident != null && right.ident != null) {
res = ident.compareTo(another.ident); res = NetworkIdentitySet.compare(left.ident, right.ident);
} }
if (res == 0) { if (res == 0) {
res = Integer.compare(uid, another.uid); res = Integer.compare(left.uid, right.uid);
} }
if (res == 0) { if (res == 0) {
res = Integer.compare(set, another.set); res = Integer.compare(left.set, right.set);
} }
if (res == 0) { if (res == 0) {
res = Integer.compare(tag, another.tag); res = Integer.compare(left.tag, right.tag);
} }
return res; return res;
} }

View File

@@ -16,6 +16,7 @@
package android.net; package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.TAG_NONE; import static android.net.NetworkStats.TAG_NONE;
@@ -31,6 +32,7 @@ import static android.text.format.DateUtils.SECOND_IN_MILLIS;
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational; import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.os.Build; import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
@@ -67,7 +69,7 @@ import java.util.Random;
* *
* @hide * @hide
*/ */
// @SystemApi(client = MODULE_LIBRARIES) @SystemApi(client = MODULE_LIBRARIES)
public final class NetworkStatsHistory implements Parcelable { public final class NetworkStatsHistory implements Parcelable {
private static final int VERSION_INIT = 1; private static final int VERSION_INIT = 1;
private static final int VERSION_ADD_PACKETS = 2; private static final int VERSION_ADD_PACKETS = 2;

View File

@@ -106,7 +106,6 @@ import android.os.Binder;
import android.os.DropBoxManager; import android.os.DropBoxManager;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
@@ -450,7 +449,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
handlerThread.start(); handlerThread.start();
mHandler = new NetworkStatsHandler(handlerThread.getLooper()); mHandler = new NetworkStatsHandler(handlerThread.getLooper());
mNetworkStatsSubscriptionsMonitor = deps.makeSubscriptionsMonitor(mContext, mNetworkStatsSubscriptionsMonitor = deps.makeSubscriptionsMonitor(mContext,
new HandlerExecutor(mHandler), this); (command) -> mHandler.post(command) , this);
mContentResolver = mContext.getContentResolver(); mContentResolver = mContext.getContentResolver();
mContentObserver = mDeps.makeContentObserver(mHandler, mSettings, mContentObserver = mDeps.makeContentObserver(mHandler, mSettings,
mNetworkStatsSubscriptionsMonitor); mNetworkStatsSubscriptionsMonitor);
@@ -557,7 +556,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// watch for tethering changes // watch for tethering changes
final TetheringManager tetheringManager = mContext.getSystemService(TetheringManager.class); final TetheringManager tetheringManager = mContext.getSystemService(TetheringManager.class);
tetheringManager.registerTetheringEventCallback( tetheringManager.registerTetheringEventCallback(
new HandlerExecutor(mHandler), mTetherListener); (command) -> mHandler.post(command), mTetherListener);
// listen for periodic polling events // listen for periodic polling events
final IntentFilter pollFilter = new IntentFilter(ACTION_NETWORK_STATS_POLL); final IntentFilter pollFilter = new IntentFilter(ACTION_NETWORK_STATS_POLL);