Search in sources :

Example 41 with HiddenApi

use of org.robolectric.annotation.HiddenApi 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 42 with HiddenApi

use of org.robolectric.annotation.HiddenApi 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 43 with HiddenApi

use of org.robolectric.annotation.HiddenApi in project robolectric by robolectric.

the class ShadowArscAssetManager method readAssetChar.

@HiddenApi
@Implementation(minSdk = LOLLIPOP)
protected final int readAssetChar(long asset) {
    Asset a = getAsset(asset);
    byte[] b = new byte[1];
    int res = a.read(b, 1);
    return res == 1 ? b[0] & 0xff : -1;
}
Also used : Asset(org.robolectric.res.android.Asset) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 44 with HiddenApi

use of org.robolectric.annotation.HiddenApi in project robolectric by robolectric.

the class ShadowArscAssetManager method getArrayIntResource.

@HiddenApi
@Implementation
public int[] getArrayIntResource(int arrayResId) {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return null;
    }
    final ResTable res = am.getResources();
    // final ResTable::bag_entry* startOfBag;
    final Ref<bag_entry[]> startOfBag = new Ref<>(null);
    final int N = res.lockBag(arrayResId, startOfBag);
    if (N < 0) {
        return null;
    }
    int[] array = new int[N];
    if (array == null) {
        res.unlockBag(startOfBag);
        return null;
    }
    final Ref<Res_value> valueRef = new Ref<>(null);
    bag_entry[] bag = startOfBag.get();
    for (int i = 0; i < N; i++) {
        valueRef.set(bag[i].map.value);
        // Take care of resolving the found resource to its final value.
        int block = res.resolveReference(valueRef, bag[i].stringBlock, null, null, null);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                // seems like this is missing from android_util_AssetManager.cpp?
                res.unlockBag(startOfBag);
                throw new IllegalStateException("Bad resource!");
            // return array;
            }
        }
        Res_value value = valueRef.get();
        if (value.dataType >= DataType.TYPE_FIRST_INT && value.dataType <= DataType.TYPE_LAST_INT) {
            int intVal = value.data;
            // env->SetIntArrayRegion(array, i, 1, &intVal);
            array[i] = intVal;
        }
    }
    res.unlockBag(startOfBag);
    return array;
}
Also used : Ref(org.robolectric.res.android.Ref) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) CppAssetManager(org.robolectric.res.android.CppAssetManager) ResTable(org.robolectric.res.android.ResTable) ResTable.bag_entry(org.robolectric.res.android.ResTable.bag_entry) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 45 with HiddenApi

use of org.robolectric.annotation.HiddenApi in project robolectric by robolectric.

the class ShadowArscAssetManager method openNonAssetNative.

@HiddenApi
@Implementation
protected final Number openNonAssetNative(int cookie, String fileName, int accessMode) throws FileNotFoundException {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    ALOGV("openNonAssetNative in %s (Java object %s)\n", am, AssetManager.class);
    String fileName8 = fileName;
    if (fileName8 == null) {
        return RuntimeEnvironment.castNativePtr(-1);
    }
    AccessMode mode = AccessMode.fromInt(accessMode);
    if (mode != Asset.AccessMode.ACCESS_UNKNOWN && mode != Asset.AccessMode.ACCESS_RANDOM && mode != Asset.AccessMode.ACCESS_STREAMING && mode != Asset.AccessMode.ACCESS_BUFFER) {
        throw new IllegalArgumentException("Bad access mode");
    }
    Asset a = isTruthy(cookie) ? am.openNonAsset(cookie, fileName8, mode) : am.openNonAsset(fileName8, mode, null);
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    long assetId = Registries.NATIVE_ASSET_REGISTRY.register(a);
    // todo: something better than this [xw]
    a.onClose = () -> destroyAsset(assetId);
    // printf("Created Asset Stream: %p\n", a);
    return RuntimeEnvironment.castNativePtr(assetId);
}
Also used : CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) AccessMode(org.robolectric.res.android.Asset.AccessMode) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

HiddenApi (org.robolectric.annotation.HiddenApi)61 Implementation (org.robolectric.annotation.Implementation)61 CppAssetManager (org.robolectric.res.android.CppAssetManager)18 Ref (org.robolectric.res.android.Ref)9 ResTable (org.robolectric.res.android.ResTable)9 FileTypedResource (org.robolectric.res.FileTypedResource)8 Asset (org.robolectric.res.android.Asset)8 TypedResource (org.robolectric.res.TypedResource)6 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)6 FileNotFoundException (java.io.FileNotFoundException)5 ArrayList (java.util.ArrayList)5 ResTableTheme (org.robolectric.res.android.ResTableTheme)5 SuppressLint (android.annotation.SuppressLint)4 ResName (org.robolectric.res.ResName)4 ResTable.bag_entry (org.robolectric.res.android.ResTable.bag_entry)4 ResTable_config (org.robolectric.res.android.ResTable_config)4 IOException (java.io.IOException)3 AttributedOpEntry (android.app.AppOpsManager.AttributedOpEntry)2 OpEntry (android.app.AppOpsManager.OpEntry)2 PackageOps (android.app.AppOpsManager.PackageOps)2