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);
}
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();
}
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);
}
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;
}
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;
}
Aggregations