diff options
author | Xiao Ma <xiaom@google.com> | 2019-09-26 22:34:35 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-09-26 22:34:35 -0700 |
commit | 48ea3266c1110873157c165e86dcec63e21e51e6 (patch) | |
tree | 005a3bfe92d006d399c3eddc6c8e57f6b95b1b67 /src/android/net/util/NetworkStackUtils.java | |
parent | 3809da8d7cac90901855445dc9d1163ec3e268be (diff) | |
parent | 32e50e09b563a7523a1662a9bbcbf7abab30d620 (diff) |
Merge "Add a general method to check version validity of DeviceConfig property."
am: 32e50e09b5
Change-Id: I9600c7ac183783f9ac3550a759d208943f218580
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; |