Search in sources :

Example 41 with AppOpsManager

use of android.app.AppOpsManager in project AndroidProcess by wenmingvs.

the class BackgroundUtil method HavaPermissionForTest.

/**
 * 判断是否有用权限
 *
 * @param context 上下文参数
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean HavaPermissionForTest(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);
    } catch (PackageManager.NameNotFoundException e) {
        return true;
    }
}
Also used : AppOpsManager(android.app.AppOpsManager) PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) TargetApi(android.annotation.TargetApi)

Example 42 with AppOpsManager

use of android.app.AppOpsManager in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class AppManagementFragment method appHasVpnPermission.

@VisibleForTesting
static boolean appHasVpnPermission(Context context, @NonNull ApplicationInfo application) {
    final AppOpsManager service = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    final List<AppOpsManager.PackageOps> ops = service.getOpsForPackage(application.uid, application.packageName, new int[] { OP_ACTIVATE_VPN });
    return !ArrayUtils.isEmpty(ops);
}
Also used : AppOpsManager(android.app.AppOpsManager) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 43 with AppOpsManager

use of android.app.AppOpsManager in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class PictureInPictureDetails method setEnterPipStateForPackage.

/**
 * Sets whether the app associated with the given {@param packageName} is allowed to enter
 * picture-in-picture.
 */
static void setEnterPipStateForPackage(Context context, int uid, String packageName, boolean value) {
    final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
    final int newMode = value ? MODE_ALLOWED : MODE_ERRORED;
    appOps.setMode(OP_PICTURE_IN_PICTURE, uid, packageName, newMode);
}
Also used : AppOpsManager(android.app.AppOpsManager)

Example 44 with AppOpsManager

use of android.app.AppOpsManager in project MLib by DaoBillTang.

the class SystemUtils method havaPermissionForTest.

/**
 * 判断是否有用权限
 *
 * @param context 上下文参数
 */
@TargetApi(19)
private static boolean havaPermissionForTest(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);
    } catch (PackageManager.NameNotFoundException e) {
        return true;
    }
}
Also used : AppOpsManager(android.app.AppOpsManager) PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Example 45 with AppOpsManager

use of android.app.AppOpsManager in project platform_frameworks_base by android.

the class RecentLocationApps method getAppList.

/**
     * Fills a list of applications which queried location recently within specified time.
     */
public List<Request> getAppList() {
    // Retrieve a location usage list from AppOps
    AppOpsManager aoManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
    List<AppOpsManager.PackageOps> appOps = aoManager.getPackagesForOps(LOCATION_OPS);
    final int appOpsCount = appOps != null ? appOps.size() : 0;
    // Process the AppOps list and generate a preference list.
    ArrayList<Request> requests = new ArrayList<>(appOpsCount);
    final long now = System.currentTimeMillis();
    final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    final List<UserHandle> profiles = um.getUserProfiles();
    for (int i = 0; i < appOpsCount; ++i) {
        AppOpsManager.PackageOps ops = appOps.get(i);
        // Don't show the Android System in the list - it's not actionable for the user.
        // Also don't show apps belonging to background users except managed users.
        String packageName = ops.getPackageName();
        int uid = ops.getUid();
        int userId = UserHandle.getUserId(uid);
        boolean isAndroidOs = (uid == Process.SYSTEM_UID) && ANDROID_SYSTEM_PACKAGE_NAME.equals(packageName);
        if (isAndroidOs || !profiles.contains(new UserHandle(userId))) {
            continue;
        }
        Request request = getRequestFromOps(now, ops);
        if (request != null) {
            requests.add(request);
        }
    }
    return requests;
}
Also used : ArrayList(java.util.ArrayList) AppOpsManager(android.app.AppOpsManager) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle)

Aggregations

AppOpsManager (android.app.AppOpsManager)101 ApplicationInfo (android.content.pm.ApplicationInfo)28 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)27 PackageManager (android.content.pm.PackageManager)18 PackageOps (android.app.AppOpsManager.PackageOps)14 ArrayList (java.util.ArrayList)14 UserHandle (android.os.UserHandle)13 IOException (java.io.IOException)10 Context (android.content.Context)9 PackageInfo (android.content.pm.PackageInfo)9 Method (java.lang.reflect.Method)9 TargetApi (android.annotation.TargetApi)8 HashMap (java.util.HashMap)8 RemoteException (android.os.RemoteException)7 ActivityManager (android.app.ActivityManager)6 Intent (android.content.Intent)6 ResolveInfo (android.content.pm.ResolveInfo)6 UserManager (android.os.UserManager)6 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)6 Field (java.lang.reflect.Field)6