Search in sources :

Example 1 with OpEntry

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

the class ShadowAppOpsManagerTest method assertOps.

/**
 * Assert that the results contain the expected op codes.
 */
private void assertOps(List<PackageOps> pkgOps, Integer... expectedOps) {
    Set<Integer> actualOps = new HashSet<>();
    for (PackageOps pkgOp : pkgOps) {
        for (OpEntry entry : pkgOp.getOps()) {
            actualOps.add(entry.getOp());
        }
    }
    assertThat(actualOps).containsAtLeastElementsIn(expectedOps);
}
Also used : OpEntry(android.app.AppOpsManager.OpEntry) PackageOps(android.app.AppOpsManager.PackageOps) HashSet(java.util.HashSet)

Example 2 with OpEntry

use of android.app.AppOpsManager.OpEntry 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)

Example 3 with OpEntry

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

the class ShadowAppOpsManager method getOpsForPackage.

@Implementation(minSdk = KITKAT)
@HiddenApi
public List<PackageOps> getOpsForPackage(int uid, String packageName, int[] ops) {
    Set<Integer> opFilter = new HashSet<>();
    if (ops != null) {
        for (int op : ops) {
            opFilter.add(op);
        }
    }
    List<OpEntry> opEntries = new ArrayList<>();
    for (Integer op : storedOps.get(Key.create(uid, packageName, null))) {
        if (opFilter.isEmpty() || opFilter.contains(op)) {
            opEntries.add(toOpEntry(op, AppOpsManager.MODE_ALLOWED));
        }
    }
    return ImmutableList.of(new PackageOps(packageName, uid, opEntries));
}
Also used : ArrayList(java.util.ArrayList) AttributedOpEntry(android.app.AppOpsManager.AttributedOpEntry) OpEntry(android.app.AppOpsManager.OpEntry) PackageOps(android.app.AppOpsManager.PackageOps) HashSet(java.util.HashSet) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 4 with OpEntry

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

the class ShadowAppOpsManager method toOpEntry.

protected OpEntry toOpEntry(Integer op, int mode) {
    if (RuntimeEnvironment.getApiLevel() < Build.VERSION_CODES.M) {
        return ReflectionHelpers.callConstructor(OpEntry.class, ClassParameter.from(int.class, op), ClassParameter.from(int.class, mode), ClassParameter.from(long.class, OP_TIME), ClassParameter.from(long.class, REJECT_TIME), ClassParameter.from(int.class, DURATION));
    } else if (RuntimeEnvironment.getApiLevel() < Build.VERSION_CODES.Q) {
        return ReflectionHelpers.callConstructor(OpEntry.class, ClassParameter.from(int.class, op), ClassParameter.from(int.class, mode), ClassParameter.from(long.class, OP_TIME), ClassParameter.from(long.class, REJECT_TIME), ClassParameter.from(int.class, DURATION), ClassParameter.from(int.class, PROXY_UID), ClassParameter.from(String.class, PROXY_PACKAGE));
    } else if (RuntimeEnvironment.getApiLevel() < Build.VERSION_CODES.R) {
        final long key = AppOpsManager.makeKey(AppOpsManager.UID_STATE_TOP, AppOpsManager.OP_FLAG_SELF);
        final LongSparseLongArray accessTimes = new LongSparseLongArray();
        accessTimes.put(key, OP_TIME);
        final LongSparseLongArray rejectTimes = new LongSparseLongArray();
        rejectTimes.put(key, REJECT_TIME);
        final LongSparseLongArray durations = new LongSparseLongArray();
        durations.put(key, DURATION);
        final LongSparseLongArray proxyUids = new LongSparseLongArray();
        proxyUids.put(key, PROXY_UID);
        final LongSparseArray<String> proxyPackages = new LongSparseArray<>();
        proxyPackages.put(key, PROXY_PACKAGE);
        return ReflectionHelpers.callConstructor(OpEntry.class, ClassParameter.from(int.class, op), ClassParameter.from(boolean.class, false), ClassParameter.from(int.class, mode), ClassParameter.from(LongSparseLongArray.class, accessTimes), ClassParameter.from(LongSparseLongArray.class, rejectTimes), ClassParameter.from(LongSparseLongArray.class, durations), ClassParameter.from(LongSparseLongArray.class, proxyUids), ClassParameter.from(LongSparseArray.class, proxyPackages));
    } else {
        final long key = AppOpsManager.makeKey(AppOpsManager.UID_STATE_TOP, AppOpsManager.OP_FLAG_SELF);
        LongSparseArray<NoteOpEvent> accessEvents = new LongSparseArray<>();
        LongSparseArray<NoteOpEvent> rejectEvents = new LongSparseArray<>();
        accessEvents.put(key, new NoteOpEvent(OP_TIME, DURATION, new OpEventProxyInfo(PROXY_UID, PROXY_PACKAGE, null)));
        rejectEvents.put(key, new NoteOpEvent(REJECT_TIME, -1, null));
        return new OpEntry(op, mode, Collections.singletonMap(null, new AttributedOpEntry(op, false, accessEvents, rejectEvents)));
    }
}
Also used : LongSparseLongArray(android.util.LongSparseLongArray) LongSparseArray(android.util.LongSparseArray) NoteOpEvent(android.app.AppOpsManager.NoteOpEvent) AttributedOpEntry(android.app.AppOpsManager.AttributedOpEntry) OpEntry(android.app.AppOpsManager.OpEntry) OpEventProxyInfo(android.app.AppOpsManager.OpEventProxyInfo) AttributedOpEntry(android.app.AppOpsManager.AttributedOpEntry)

Aggregations

OpEntry (android.app.AppOpsManager.OpEntry)4 AttributedOpEntry (android.app.AppOpsManager.AttributedOpEntry)3 PackageOps (android.app.AppOpsManager.PackageOps)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 HiddenApi (org.robolectric.annotation.HiddenApi)2 Implementation (org.robolectric.annotation.Implementation)2 NoteOpEvent (android.app.AppOpsManager.NoteOpEvent)1 OpEventProxyInfo (android.app.AppOpsManager.OpEventProxyInfo)1 ArrayMap (android.util.ArrayMap)1 LongSparseArray (android.util.LongSparseArray)1 LongSparseLongArray (android.util.LongSparseLongArray)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1