Search in sources :

Example 26 with PackageOps

use of android.app.AppOpsManager.PackageOps in project android_packages_apps_Settings by SudaMod.

the class DevelopmentSettings method updateMockLocation.

private void updateMockLocation() {
    AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
    List<PackageOps> packageOps = appOpsManager.getPackagesForOps(MOCK_LOCATION_APP_OPS);
    if (packageOps != null) {
        for (PackageOps packageOp : packageOps) {
            if (packageOp.getOps().get(0).getMode() == AppOpsManager.MODE_ALLOWED) {
                mMockLocationApp = packageOps.get(0).getPackageName();
                break;
            }
        }
    }
    if (!TextUtils.isEmpty(mMockLocationApp)) {
        String label = mMockLocationApp;
        try {
            ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(mMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
            CharSequence appLabel = getPackageManager().getApplicationLabel(ai);
            if (appLabel != null) {
                label = appLabel.toString();
            }
        } catch (PackageManager.NameNotFoundException e) {
        /* ignore */
        }
        mMockLocationAppPref.setSummary(getString(R.string.mock_location_app_set, label));
        mHaveDebugSettings = true;
    } else {
        mMockLocationAppPref.setSummary(getString(R.string.mock_location_app_not_set));
    }
}
Also used : AppOpsManager(android.app.AppOpsManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) PackageOps(android.app.AppOpsManager.PackageOps)

Example 27 with PackageOps

use of android.app.AppOpsManager.PackageOps in project platform_packages_apps_Settings by BlissRoms.

the class AppStateAppOpsBridge method loadAppOpsStates.

/*
     * This method will set the appOpMode field of the associated PermissionState, which describes
     * a particular package.
     */
private void loadAppOpsStates(SparseArray<ArrayMap<String, PermissionState>> entries) {
    // Find out which packages have been granted permission from AppOps.
    final List<AppOpsManager.PackageOps> packageOps = mAppOpsManager.getPackagesForOps(mAppOpsOpCodes);
    final int packageOpsCount = packageOps != null ? packageOps.size() : 0;
    for (int i = 0; i < packageOpsCount; i++) {
        final AppOpsManager.PackageOps packageOp = packageOps.get(i);
        final int userId = UserHandle.getUserId(packageOp.getUid());
        if (!isThisUserAProfileOfCurrentUser(userId)) {
            // This AppOp does not belong to any of this user's profiles.
            continue;
        }
        final ArrayMap<String, PermissionState> entriesForProfile = entries.get(userId);
        if (entriesForProfile == null) {
            continue;
        }
        final PermissionState pe = entriesForProfile.get(packageOp.getPackageName());
        if (pe == null) {
            Log.w(TAG, "AppOp permission exists for package " + packageOp.getPackageName() + " of user " + userId + " but package doesn't exist or did not request " + mPermissions + " access");
            continue;
        }
        if (packageOp.getOps().size() < 1) {
            Log.w(TAG, "No AppOps permission exists for package " + packageOp.getPackageName());
            continue;
        }
        pe.appOpMode = packageOp.getOps().get(0).getMode();
    }
}
Also used : AppOpsManager(android.app.AppOpsManager) PackageOps(android.app.AppOpsManager.PackageOps) PackageOps(android.app.AppOpsManager.PackageOps)

Example 28 with PackageOps

use of android.app.AppOpsManager.PackageOps in project platform_packages_apps_Settings by BlissRoms.

the class DevelopmentSettings method writeMockLocation.

private void writeMockLocation() {
    AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
    // Disable the app op of the previous mock location app if such.
    List<PackageOps> packageOps = appOpsManager.getPackagesForOps(MOCK_LOCATION_APP_OPS);
    if (packageOps != null) {
        // Should be one but in case we are in a bad state due to use of command line tools.
        for (PackageOps packageOp : packageOps) {
            if (packageOp.getOps().get(0).getMode() != AppOpsManager.MODE_ERRORED) {
                String oldMockLocationApp = packageOp.getPackageName();
                try {
                    ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(oldMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
                    appOpsManager.setMode(AppOpsManager.OP_MOCK_LOCATION, ai.uid, oldMockLocationApp, AppOpsManager.MODE_ERRORED);
                } catch (NameNotFoundException e) {
                /* ignore */
                }
            }
        }
    }
    // Enable the app op of the new mock location app if such.
    if (!TextUtils.isEmpty(mMockLocationApp)) {
        try {
            ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(mMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
            appOpsManager.setMode(AppOpsManager.OP_MOCK_LOCATION, ai.uid, mMockLocationApp, AppOpsManager.MODE_ALLOWED);
        } catch (NameNotFoundException e) {
        /* ignore */
        }
    }
}
Also used : AppOpsManager(android.app.AppOpsManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) PackageOps(android.app.AppOpsManager.PackageOps)

Example 29 with PackageOps

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

the class AppStateAppOpsBridge method loadAppOpsStates.

/*
     * This method will set the appOpMode field of the associated PermissionState, which describes
     * a particular package.
     */
private void loadAppOpsStates(SparseArray<ArrayMap<String, PermissionState>> entries) {
    // Find out which packages have been granted permission from AppOps.
    final List<AppOpsManager.PackageOps> packageOps = mAppOpsManager.getPackagesForOps(mAppOpsOpCodes);
    final int packageOpsCount = packageOps != null ? packageOps.size() : 0;
    for (int i = 0; i < packageOpsCount; i++) {
        final AppOpsManager.PackageOps packageOp = packageOps.get(i);
        final int userId = UserHandle.getUserId(packageOp.getUid());
        if (!isThisUserAProfileOfCurrentUser(userId)) {
            // This AppOp does not belong to any of this user's profiles.
            continue;
        }
        final ArrayMap<String, PermissionState> entriesForProfile = entries.get(userId);
        if (entriesForProfile == null) {
            continue;
        }
        final PermissionState pe = entriesForProfile.get(packageOp.getPackageName());
        if (pe == null) {
            Log.w(TAG, "AppOp permission exists for package " + packageOp.getPackageName() + " of user " + userId + " but package doesn't exist or did not request " + mPermissions + " access");
            continue;
        }
        if (packageOp.getOps().size() < 1) {
            Log.w(TAG, "No AppOps permission exists for package " + packageOp.getPackageName());
            continue;
        }
        pe.appOpMode = packageOp.getOps().get(0).getMode();
    }
}
Also used : AppOpsManager(android.app.AppOpsManager) PackageOps(android.app.AppOpsManager.PackageOps) PackageOps(android.app.AppOpsManager.PackageOps)

Example 30 with PackageOps

use of android.app.AppOpsManager.PackageOps in project robolectric by robolectric.

the class ShadowAppOpsManager method getPackagesForOps.

/**
 * Returns app op details for all packages for which one of {@link #setMode} methods was used to
 * set the value of one of the given app ops (it does return those set to 'default' mode, while
 * the true implementation usually doesn't). Also, we don't enforce any permission checks which
 * might be needed in the true implementation.
 *
 * @param ops The set of operations you are interested in, or null if you want all of them.
 * @return app ops information about each package, containing only ops that were specified as an
 *     argument
 */
// to be consistent with setMode() shadow implementations
@Implementation(minSdk = KITKAT)
@HiddenApi
protected List<PackageOps> getPackagesForOps(int[] ops) {
    Set<Integer> relevantOps;
    if (ops != null) {
        relevantOps = IntStream.of(ops).boxed().collect(toSet());
    } else {
        relevantOps = new HashSet<>();
    }
    // Aggregating op data per each package.
    // (uid, packageName) => [(op, mode)]
    Multimap<Key, OpEntry> perPackageMap = MultimapBuilder.hashKeys().hashSetValues().build();
    for (Map.Entry<Key, Integer> appOpInfo : appModeMap.entrySet()) {
        Key key = appOpInfo.getKey();
        if (ops == null || relevantOps.contains(key.getOpCode())) {
            Key packageKey = Key.create(key.getUid(), key.getPackageName(), null);
            OpEntry opEntry = toOpEntry(key.getOpCode(), appOpInfo.getValue());
            perPackageMap.put(packageKey, opEntry);
        }
    }
    List<PackageOps> result = new ArrayList<>();
    // Creating resulting PackageOps objects using all op info collected per package.
    for (Map.Entry<Key, Collection<OpEntry>> packageInfo : perPackageMap.asMap().entrySet()) {
        Key key = packageInfo.getKey();
        result.add(new PackageOps(key.getPackageName(), key.getUid(), new ArrayList<>(packageInfo.getValue())));
    }
    return result.isEmpty() ? null : result;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) AttributedOpEntry(android.app.AppOpsManager.AttributedOpEntry) OpEntry(android.app.AppOpsManager.OpEntry) Map(java.util.Map) HashMap(java.util.HashMap) ArrayMap(android.util.ArrayMap) PackageOps(android.app.AppOpsManager.PackageOps) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

PackageOps (android.app.AppOpsManager.PackageOps)31 AppOpsManager (android.app.AppOpsManager)21 ApplicationInfo (android.content.pm.ApplicationInfo)14 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)14 PackageManager (android.content.pm.PackageManager)7 RemoteException (android.os.RemoteException)7 UserHandle (android.os.UserHandle)7 OpEntry (android.app.AppOpsManager.OpEntry)3 AttributedOpEntry (android.app.AppOpsManager.AttributedOpEntry)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 HiddenApi (org.robolectric.annotation.HiddenApi)2 Implementation (org.robolectric.annotation.Implementation)2 ArrayMap (android.util.ArrayMap)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1