Search in sources :

Example 66 with AppOpsManager

use of android.app.AppOpsManager in project android_packages_apps_Settings by LineageOS.

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 67 with AppOpsManager

use of android.app.AppOpsManager in project android_packages_apps_Settings by LineageOS.

the class AppOpsSummary method resetCounters.

private void resetCounters() {
    final AppOpsManager appOps = (AppOpsManager) mActivity.getSystemService(Context.APP_OPS_SERVICE);
    if (appOps == null) {
        return;
    }
    appOps.resetCounters();
    // reload content
    resetAdapter();
}
Also used : AppOpsManager(android.app.AppOpsManager)

Example 68 with AppOpsManager

use of android.app.AppOpsManager in project android_packages_apps_Settings by LineageOS.

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(com.android.internal.annotations.VisibleForTesting)

Example 69 with AppOpsManager

use of android.app.AppOpsManager in project AndroidUtilLib by SiberiaDante.

the class SDProcessUtil method getForegroundProcessName.

/**
 * 获取前台线程包名
 * <p>当不是查看当前App,且SDK大于21时,
 * 需添加权限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p>
 *
 * @return 前台应用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager = (ActivityManager) SDAndroidLib.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
    if (infos != null && infos.size() != 0) {
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return info.processName;
            }
        }
    }
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = SDAndroidLib.getContext().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        System.out.println(list);
        if (list.size() > 0) {
            // 有"有权查看使用权限的应用"选项
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(SDAndroidLib.getContext().getPackageName(), 0);
                AppOpsManager aom = (AppOpsManager) SDAndroidLib.getContext().getSystemService(Context.APP_OPS_SERVICE);
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    SDAndroidLib.getContext().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    SDToastUtil.toast("getForegroundApp---没有打开\"有权查看使用权限的应用\"选项");
                    return null;
                }
                UsageStatsManager usageStatsManager = (UsageStatsManager) SDAndroidLib.getContext().getSystemService(Context.USAGE_STATS_SERVICE);
                long endTime = System.currentTimeMillis();
                long beginTime = endTime - 86400000 * 7;
                List<UsageStats> usageStatses = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
                if (usageStatses == null || usageStatses.isEmpty())
                    return null;
                UsageStats recentStats = null;
                for (UsageStats usageStats : usageStatses) {
                    if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
                        recentStats = usageStats;
                    }
                }
                return recentStats == null ? null : recentStats.getPackageName();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            SDToastUtil.toast("getForegroundApp----无\"有权查看使用权限的应用\"选项");
        }
    }
    return null;
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) UsageStats(android.app.usage.UsageStats) ActivityManager(android.app.ActivityManager) ResolveInfo(android.content.pm.ResolveInfo) AppOpsManager(android.app.AppOpsManager) PackageManager(android.content.pm.PackageManager) UsageStatsManager(android.app.usage.UsageStatsManager)

Example 70 with AppOpsManager

use of android.app.AppOpsManager in project AndroidUtilLib by SiberiaDante.

the class SDAppUtil method isNotificationEnable.

/**
 * 判断手机通知权限是否打开
 *
 * @return
 */
public static boolean isNotificationEnable() {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
        return true;
    }
    AppOpsManager mAppOps = (AppOpsManager) SDAndroidLib.getContext().getSystemService(Context.APP_OPS_SERVICE);
    ApplicationInfo appInfo = SDAndroidLib.getContext().getApplicationInfo();
    String pkg = SDAndroidLib.getContext().getApplicationContext().getPackageName();
    int uid = appInfo.uid;
    Class appOpsClass = null;
    /* Context.APP_OPS_MANAGER */
    try {
        appOpsClass = Class.forName(AppOpsManager.class.getName());
        Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
        Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
        int value = (Integer) opPostNotificationValue.get(Integer.class);
        return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) Method(java.lang.reflect.Method) SuppressLint(android.annotation.SuppressLint) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) AppOpsManager(android.app.AppOpsManager)

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