Java Code Examples for android.app.AppOpsManager#checkOpNoThrow()
The following examples show how to use
android.app.AppOpsManager#checkOpNoThrow() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatWindowParamManager.java From FloatWindow with Apache License 2.0 | 6 votes |
public static boolean checkOverlayPermission(Context context) { AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); try { if (appOpsMgr != null) { int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), context.getPackageName()); if (mode == 0) { return true; } } } catch (Exception e) { e.printStackTrace(); } return false; }
Example 2
Source File: MessageUtil.java From GcmForMojo with GNU General Public License v3.0 | 6 votes |
/** * 判断是否有用权限 * * @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 = 0; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName); } return (mode == AppOpsManager.MODE_ALLOWED); } catch (PackageManager.NameNotFoundException e) { return true; } }
Example 3
Source File: FloatWindowParamManager.java From FloatWindow with Apache License 2.0 | 6 votes |
public static boolean checkOverlayPermission(Context context) { AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); try { if (appOpsMgr != null) { int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), context.getPackageName()); if (mode == 0) { return true; } } } catch (Exception e) { e.printStackTrace(); } return false; }
Example 4
Source File: PermissionCheckHelper.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 6 votes |
private static boolean checkOpsPermission(Context context, String permission) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { try { AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); String opsName = AppOpsManager.permissionToOp(permission); if (opsName == null) { return true; } int opsMode = appOpsManager.checkOpNoThrow(opsName, Process.myUid(), context.getPackageName()); return opsMode == AppOpsManager.MODE_ALLOWED; } catch (Exception ex) { return true; } } return true; }
Example 5
Source File: PermissionUtils.java From EnhancedScreenshotNotification with GNU General Public License v3.0 | 6 votes |
/** * Check if application can draw over other apps * @param context Context * @return Boolean */ public static boolean canDrawOverlays(@NonNull Context context) { final int sdkInt = Build.VERSION.SDK_INT; if (sdkInt >= Build.VERSION_CODES.M) { if (sdkInt == Build.VERSION_CODES.O) { // Sometimes Settings.canDrawOverlays returns false after allowing permission. // Google Issue Tracker: https://issuetracker.google.com/issues/66072795 AppOpsManager appOpsMgr = context.getSystemService(AppOpsManager.class); if (appOpsMgr != null) { int mode = appOpsMgr.checkOpNoThrow( "android:system_alert_window", android.os.Process.myUid(), context.getPackageName() ); return mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED; } else { return false; } } // Default return android.provider.Settings.canDrawOverlays(context); } return true; // This fallback may returns a incorrect result. }
Example 6
Source File: MainPreferenceFragment.java From kcanotify with GNU General Public License v3.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean hasUsageStatPermission(Context context) { AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; }
Example 7
Source File: MainActivity.java From More-For-GO with GNU General Public License v3.0 | 5 votes |
private boolean hasUsageAccess() throws PackageManager.NameNotFoundException { PackageManager packageManager = getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0); AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName); return mode == AppOpsManager.MODE_ALLOWED; }
Example 8
Source File: U.java From Taskbar with Apache License 2.0 | 5 votes |
public static AlertDialog showRecentAppsDialog(Context context, Callbacks callbacks) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !isSystemApp(context)) { ApplicationInfo applicationInfo = null; try { applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { /* Gracefully fail */ } if(applicationInfo != null) { AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName); if(mode != AppOpsManager.MODE_ALLOWED) { AlertDialog.Builder builder; if(hasAndroidTVSettings(context)) builder = buildRecentAppsDialogAndroidTV(context, callbacks); else builder = buildRecentAppsDialogStandard(context, callbacks); AlertDialog dialog = builder.create(); dialog.show(); dialog.setCancelable(false); return dialog; } } } callbacks.onFinish.run(); return null; }
Example 9
Source File: DataManager.java From AppsMonitor with MIT License | 5 votes |
public boolean hasPermission(Context context) { AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); if (appOps != null) { int mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; } return false; }
Example 10
Source File: TaskListActivity.java From batteryhub with Apache License 2.0 | 5 votes |
@TargetApi(21) private boolean hasSpecialPermission(final Context context) { AppOpsManager appOps = (AppOpsManager) context .getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; }
Example 11
Source File: PermissionUtil.java From SoloPi with Apache License 2.0 | 5 votes |
/** * 检查使用状态权限 * @param context * @return */ @TargetApi(Build.VERSION_CODES.KITKAT) public static boolean isUsageStatPermissionOn(Context context) { AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; }
Example 12
Source File: ProfilesActivity.java From ScreenShift with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private boolean hasUsageAccess() { try { PackageManager packageManager = getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0); AppOpsManager appOpsManager = (AppOpsManager) 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 false; } }
Example 13
Source File: MainActivity.java From GrabQQPWD with Apache License 2.0 | 5 votes |
private boolean checkUsagePermission() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = 0; mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), getPackageName()); boolean granted = mode == AppOpsManager.MODE_ALLOWED; if (!granted) { Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); startActivityForResult(intent, 2); return false; } } return true; }
Example 14
Source File: MainActivity.java From foregroundappchecker with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.KITKAT) private boolean hasUsageStatsPermission(Context context) { AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()); boolean granted = mode == AppOpsManager.MODE_ALLOWED; return granted; }
Example 15
Source File: AppUtils.java From Common with Apache License 2.0 | 4 votes |
private static String getForegroundProcessName(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> pInfo = am.getRunningAppProcesses(); if (pInfo != null && pInfo.size() > 0) { for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) { if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return aInfo.processName; } } } if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) { PackageManager pm = context.getPackageManager(); Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); Log.i("ProcessUtils", list.toString()); if (list.size() <= 0) { Log.i("ProcessUtils", "getForegroundProcessName: noun of access to usage information."); return ""; } try { // Access to usage information. ApplicationInfo info = pm.getApplicationInfo(context.getPackageName(), 0); AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { Log.i("ProcessUtils", "getForegroundProcessName: refuse to device usage stats."); return ""; } UsageStatsManager usageStatsManager = (UsageStatsManager) context .getSystemService(Context.USAGE_STATS_SERVICE); List<UsageStats> usageStatsList = null; if (usageStatsManager != null) { long endTime = System.currentTimeMillis(); long beginTime = endTime - 86400000 * 7; usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime); } if (usageStatsList == null || usageStatsList.isEmpty()) return null; UsageStats recentStats = null; for (UsageStats usageStats : usageStatsList) { if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) { recentStats = usageStats; } } return recentStats == null ? null : recentStats.getPackageName(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } return ""; }
Example 16
Source File: TaskController.java From batteryhub with Apache License 2.0 | 4 votes |
@TargetApi(21) private List<Task> getRunningTasksNougat() { final List<Task> tasks = new ArrayList<>(); AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow( "android:get_usage_stats", Process.myUid(), mContext.getPackageName() ); boolean granted = mode == AppOpsManager.MODE_ALLOWED; if (!granted) return tasks; final List<UsageStats> list = UStats.getUsageStatsList(mContext); for (UsageStats stats : list) { String packageName = stats.getPackageName(); /* Exclude the app itself from the list */ if (packageName.equals(BuildConfig.APPLICATION_ID)) continue; PackageInfo packageInfo = getPackageInfo(packageName, 0); if (packageInfo == null) continue; /** Remove system apps if necessary */ if (isSystemApp(packageInfo) && SettingsUtils.isSystemAppsHidden(mContext)) { continue; } /** Remove apps without label */ String appLabel = packageInfo.applicationInfo.loadLabel(mPackageManager).toString(); if (appLabel.isEmpty()) continue; int uid = packageInfo.applicationInfo.uid; Task task = getTaskByUid(tasks, uid); if (task == null) { task = new Task(uid, packageInfo.applicationInfo.processName); task.setPackageInfo(packageInfo); task.setLabel(appLabel); task.setIsAutoStart(isAutoStartApp(packageName)); task.setHasBackgroundService(hasBackgroundServices(packageName)); tasks.add(task); } } // Dirty quick sorting Collections.sort(tasks, new Comparator<Task>() { @Override public int compare(Task t1, Task t2) { return t1.getLabel().compareTo(t2.getLabel()); } }); return tasks; }
Example 17
Source File: Utility.java From Shelter with Do What The F*ck You Want To Public License | 4 votes |
public static boolean checkSpecialAccessPermission(Context context, String name) { AppOpsManager appops = context.getSystemService(AppOpsManager.class); int mode = appops.checkOpNoThrow(name, android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; }
Example 18
Source File: UsageStatsUtils.java From FCM-for-Mojo with GNU General Public License v3.0 | 4 votes |
public static boolean granted(Context context) { AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; }
Example 19
Source File: ProcessUtils.java From Android-UtilCode with Apache License 2.0 | 4 votes |
/** * 获取前台线程包名 * <p>当不是查看当前App,且SDK大于21时, * 需添加权限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p> * * @return 前台应用包名 */ public static String getForegroundProcessName() { ActivityManager manager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses(); if (pInfo != null && pInfo.size() != 0) { for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) { if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return aInfo.processName; } } } if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) { PackageManager packageManager = Utils.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(Utils.getContext().getPackageName(), 0); AppOpsManager aom = (AppOpsManager) Utils.getContext().getSystemService(Context.APP_OPS_SERVICE); if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { Utils.getContext().startActivity(intent); } if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { LogUtils.d("getForegroundApp", "没有打开\"有权查看使用权限的应用\"选项"); return null; } UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.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 { LogUtils.d("getForegroundApp", "无\"有权查看使用权限的应用\"选项"); } } return null; }
Example 20
Source File: ProcessUtils.java From AndroidUtilCode with Apache License 2.0 | 4 votes |
/** * Return the foreground process name. * <p>Target APIs greater than 21 must hold * {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p> * * @return the foreground process name */ public static String getForegroundProcessName() { ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE); //noinspection ConstantConditions List<ActivityManager.RunningAppProcessInfo> pInfo = am.getRunningAppProcesses(); if (pInfo != null && pInfo.size() > 0) { for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) { if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return aInfo.processName; } } } if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) { PackageManager pm = Utils.getApp().getPackageManager(); Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); Log.i("ProcessUtils", list.toString()); if (list.size() <= 0) { Log.i("ProcessUtils", "getForegroundProcessName: noun of access to usage information."); return ""; } try {// Access to usage information. ApplicationInfo info = pm.getApplicationInfo(Utils.getApp().getPackageName(), 0); AppOpsManager aom = (AppOpsManager) Utils.getApp().getSystemService(Context.APP_OPS_SERVICE); if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Utils.getApp().startActivity(intent); } if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) { Log.i("ProcessUtils", "getForegroundProcessName: refuse to device usage stats."); return ""; } UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getApp() .getSystemService(Context.USAGE_STATS_SERVICE); List<UsageStats> usageStatsList = null; if (usageStatsManager != null) { long endTime = System.currentTimeMillis(); long beginTime = endTime - 86400000 * 7; usageStatsList = usageStatsManager .queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime); } if (usageStatsList == null || usageStatsList.isEmpty()) return ""; UsageStats recentStats = null; for (UsageStats usageStats : usageStatsList) { if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) { recentStats = usageStats; } } return recentStats == null ? null : recentStats.getPackageName(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } return ""; }