diff options
author | Xiao Ma <xiaom@google.com> | 2019-09-10 15:47:33 +0900 |
---|---|---|
committer | Xiao Ma <xiaom@google.com> | 2019-09-26 17:49:54 +0900 |
commit | 08d1f4e878a08672ffa60245c4c4aa5999b4d608 (patch) | |
tree | 0716c080473a72e26cb6580b0c401bc5388c5350 /src/android/net/util/NetworkStackUtils.java | |
parent | dc9ef64019b294472c95a4130655141dc5e46fb6 (diff) |
Add a general method to check version validity of DeviceConfig property.
Considering boolean experimental flag is likely to cause misconfiguration,
particularly when NetworkStack module rolls back to previous version. It's
much safer to determine whether or not to enable one specific experimental
feature by comparing flag version with module version.
Test: atest NetworkStackTests
Change-Id: Ie0c9527eb87b75c998584f2b4439cc0e309e8b28
Diffstat (limited to 'src/android/net/util/NetworkStackUtils.java')
-rw-r--r-- | src/android/net/util/NetworkStackUtils.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/android/net/util/NetworkStackUtils.java b/src/android/net/util/NetworkStackUtils.java index 596e5e3..0a18c0e 100644 --- a/src/android/net/util/NetworkStackUtils.java +++ b/src/android/net/util/NetworkStackUtils.java @@ -18,7 +18,10 @@ package android.net.util; import android.annotation.NonNull; import android.annotation.Nullable; +import android.content.Context; +import android.content.pm.PackageManager.NameNotFoundException; import android.provider.DeviceConfig; +import android.util.Log; import android.util.SparseArray; import java.io.FileDescriptor; @@ -34,6 +37,8 @@ import java.util.function.Predicate; * Collection of utilities for the network stack. */ public class NetworkStackUtils { + private static final String TAG = "NetworkStackUtils"; + /** * A list of captive portal detection specifications used in addition to the fallback URLs. * Each spec has the format url@@/@@statusCodeRegex@@/@@contentRegex. Specs are separated @@ -216,6 +221,30 @@ public class NetworkStackUtils { } /** + * Check whether or not one specific experimental feature for a particular namespace from + * {@link DeviceConfig} is enabled by comparing NetworkStack module version {@link NetworkStack} + * with current version of property. If this property version is valid, the corresponding + * experimental feature would be enabled, otherwise disabled. + * @param context The global context information about an app environment. + * @param namespace The namespace containing the property to look up. + * @param name The name of the property to look up. + * @return true if this feature is enabled, or false if disabled. + */ + public static boolean isFeatureEnabled(@NonNull Context context, @NonNull String namespace, + @NonNull String name) { + try { + final int propertyVersion = getDeviceConfigPropertyInt(namespace, name, + 0 /* default value */); + final long packageVersion = context.getPackageManager().getPackageInfo( + context.getPackageName(), 0).getLongVersionCode(); + return (propertyVersion != 0 && packageVersion >= (long) propertyVersion); + } catch (NameNotFoundException e) { + Log.e(TAG, "Could not find the package name", e); + return false; + } + } + + /** * Attaches a socket filter that accepts DHCP packets to the given socket. */ public static native void attachDhcpFilter(FileDescriptor fd) throws SocketException; |