use of android.app.AppOpsManager in project BaseProject by feer921.
the class Util method enableWriteSmsOpt.
@SuppressLint("NewApi")
public static void enableWriteSmsOpt(Context mContext) {
if (Build.VERSION.SDK_INT >= 19) {
// Android 4.4
// 及以上才有以下功能,并且可直接设置程序有写短信权限,如果以下运行有异常,则表示可能系统为Android5.0
String PHONE_PACKAGE_NAME = mContext.getPackageName();
PackageManager packageManager = mContext.getPackageManager();
AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
try {
PackageInfo info = packageManager.getPackageInfo(PHONE_PACKAGE_NAME, 0);
Field field = AppOpsManager.class.getDeclaredField("OP_WRITE_SMS");
// 写短信权限
int WRITE_SMS = field.getInt(appOps);
Method method = AppOpsManager.class.getMethod("setMode", int.class, int.class, String.class, int.class);
method.invoke(appOps, WRITE_SMS, info.applicationInfo.uid, PHONE_PACKAGE_NAME, AppOpsManager.MODE_ALLOWED);
} catch (Exception ex) {
// 表明该系统可能为Android5.0,且需要将程序设置成默认的短信程序
}
}
}
use of android.app.AppOpsManager in project android_frameworks_opt_telephony by LineageOS.
the class AppSmsManager method createAppSpecificSmsToken.
/**
* Create an app specific incoming SMS request for the the calling package.
*
* This method returns a token that if included in a subsequent incoming SMS message the
* {@link Intents.SMS_RECEIVED_ACTION} intent will be delivered only to the calling package and
* will not require the application have the {@link Manifest.permission#RECEIVE_SMS} permission.
*
* An app can only have one request at a time, if the app already has a request it will be
* dropped and the new one will be added.
*
* @return Token to include in an SMS to have it delivered directly to the app.
*/
public String createAppSpecificSmsToken(String callingPkg, PendingIntent intent) {
// Check calling uid matches callingpkg.
AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
appOps.checkPackage(Binder.getCallingUid(), callingPkg);
// Generate a nonce to store the request under.
String token = generateNonce();
synchronized (mLock) {
// Only allow one request in flight from a package.
if (mPackageMap.containsKey(callingPkg)) {
removeRequestLocked(mPackageMap.get(callingPkg));
}
// Store state.
AppRequestInfo info = new AppRequestInfo(callingPkg, intent, token);
addRequestLocked(info);
}
return token;
}
use of android.app.AppOpsManager in project yoo_home_Android by culturer.
the class PermissionUtil method hasPermissionBelowMarshmallow.
/**
* 6.0以下判断是否有权限
* 理论上6.0以上才需处理权限,但有的国内rom在6.0以下就添加了权限
* 其实此方式也可以用于判断6.0以上版本,只不过有更简单的canDrawOverlays代替
*/
static boolean hasPermissionBelowMarshmallow(Context context) {
try {
AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Method dispatchMethod = AppOpsManager.class.getMethod("checkOp", int.class, int.class, String.class);
// AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
return AppOpsManager.MODE_ALLOWED == (Integer) dispatchMethod.invoke(manager, 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName());
} catch (Exception e) {
return false;
}
}
use of android.app.AppOpsManager in project android_packages_apps_Settings by SudaMod.
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 SudaMod.
the class VpnSettings method getVpnApps.
static List<AppVpnInfo> getVpnApps(Context context, boolean includeProfiles) {
List<AppVpnInfo> result = Lists.newArrayList();
final Set<Integer> profileIds;
if (includeProfiles) {
profileIds = new ArraySet<>();
for (UserHandle profile : UserManager.get(context).getUserProfiles()) {
profileIds.add(profile.getIdentifier());
}
} else {
profileIds = Collections.singleton(UserHandle.myUserId());
}
// Fetch VPN-enabled apps from AppOps.
AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
List<AppOpsManager.PackageOps> apps = aom.getPackagesForOps(new int[] { OP_ACTIVATE_VPN });
if (apps != null) {
for (AppOpsManager.PackageOps pkg : apps) {
int userId = UserHandle.getUserId(pkg.getUid());
if (!profileIds.contains(userId)) {
// Skip packages for users outside of our profile group.
continue;
}
// Look for a MODE_ALLOWED permission to activate VPN.
boolean allowed = false;
for (AppOpsManager.OpEntry op : pkg.getOps()) {
if (op.getOp() == OP_ACTIVATE_VPN && op.getMode() == AppOpsManager.MODE_ALLOWED) {
allowed = true;
}
}
if (allowed) {
result.add(new AppVpnInfo(userId, pkg.getPackageName()));
}
}
}
Collections.sort(result);
return result;
}
Aggregations