Skip to content

Commit

Permalink
add ConnectivityUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
u-fred committed Oct 22, 2024
1 parent 21e12b8 commit 32c58a4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/api/module-lib-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ package android.content.pm {

}

package android.ext {

public class ConnectivityUtil {
method public static boolean isRegularAppWithLockdownVpnEnabled(@NonNull android.content.Context, int);
method public static boolean isSystem(@NonNull android.content.Context, int);
}

}

package android.ext.settings {

public class ConnChecksSetting {
Expand Down
6 changes: 6 additions & 0 deletions core/api/module-lib-lint-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,12 @@ UnflaggedApi: android.companion.CompanionDeviceManager.OnTransportsChangedListen
New API must be flagged with @FlaggedApi: method android.companion.CompanionDeviceManager.OnTransportsChangedListener.onTransportsChanged(java.util.List<android.companion.AssociationInfo>)
UnflaggedApi: android.content.Context#REMOTE_AUTH_SERVICE:
New API must be flagged with @FlaggedApi: field android.content.Context.REMOTE_AUTH_SERVICE
UnflaggedApi: android.ext.ConnectivityUtil:
New API must be flagged with @FlaggedApi: class android.ext.ConnectivityUtil
UnflaggedApi: android.ext.ConnectivityUtil#isRegularAppWithLockdownVpnEnabled(android.content.Context, int):
New API must be flagged with @FlaggedApi: method android.ext.ConnectivityUtil.isRegularAppWithLockdownVpnEnabled(android.content.Context,int)
UnflaggedApi: android.ext.ConnectivityUtil#isSystem(android.content.Context, int):
New API must be flagged with @FlaggedApi: method android.ext.ConnectivityUtil.isSystem(android.content.Context,int)
UnflaggedApi: android.ext.settings.ConnChecksSetting:
New API must be flagged with @FlaggedApi: class android.ext.settings.ConnChecksSetting
UnflaggedApi: android.ext.settings.ConnChecksSetting#VAL_DEFAULT:
Expand Down
50 changes: 50 additions & 0 deletions core/java/android/ext/ConnectivityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package android.ext;

import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.provider.Settings;

/** @hide */
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public class ConnectivityUtil {
private ConnectivityUtil() {}
/**
* Return true if this uid is a core system component, or if the uid is known to PackageManager
* and at least one of the packages that use it is a system app.
*/
public static boolean isSystem(@NonNull Context context, int uid) {
if (UserHandle.isCore(uid)) {
return true;
}

PackageManager pm = context.getPackageManager();
String[] packageNames = pm.getPackagesForUid(uid);
if (packageNames == null) {
return false;
}
for (String packageName : packageNames) {
try {
ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
if (appInfo.isSystemApp()) {
return true;
}
} catch (PackageManager.NameNotFoundException ignored) { }
}
return false;
}

public static boolean isRegularAppWithLockdownVpnEnabled(@NonNull Context context, int uid) {
if (isSystem(context, uid)) {
return false;
}

ContentResolver cr = context.createContextAsUser(UserHandle.getUserHandleForUid(
uid), 0).getContentResolver();
return Settings.Secure.getInt(cr, Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, 0) == 1;
}
}

0 comments on commit 32c58a4

Please sign in to comment.