Search in sources :

Example 1 with ContentProviderData

use of org.robolectric.manifest.ContentProviderData in project robolectric by robolectric.

the class ShadowContentResolverTest method getProvider_shouldSetAuthority.

@Test
public void getProvider_shouldSetAuthority() throws RemoteException {
    AndroidManifest manifest = ShadowApplication.getInstance().getAppManifest();
    ContentProviderData testProviderData = new ContentProviderData("org.robolectric.shadows.ShadowContentResolverTest$TestContentProvider", null, AUTHORITY, null, null, null);
    try {
        manifest.getContentProviders().add(testProviderData);
        Uri uri = Uri.parse("content://" + AUTHORITY + "/shadows");
        ContentProvider provider = ShadowContentResolver.getProvider(uri);
        // unfortunately, there is no direct way of testing if authority is set or not
        // however, it's checked in ContentProvider.Transport method calls (validateIncomingUri), so it's the closest we can test against
        // should not throw
        provider.getIContentProvider().getType(uri);
    } finally {
        manifest.getContentProviders().remove(testProviderData);
    }
}
Also used : ContentProviderData(org.robolectric.manifest.ContentProviderData) ContentProvider(android.content.ContentProvider) AndroidManifest(org.robolectric.manifest.AndroidManifest) Uri(android.net.Uri) Test(org.junit.Test)

Example 2 with ContentProviderData

use of org.robolectric.manifest.ContentProviderData in project robolectric by robolectric.

the class DefaultPackageManager method getProviderInfo.

@Override
public ProviderInfo getProviderInfo(ComponentName className, int flags) throws NameNotFoundException {
    String packageName = className.getPackageName();
    AndroidManifest androidManifest = androidManifests.get(packageName);
    String classString = resolvePackageName(packageName, className);
    for (ContentProviderData contentProviderData : androidManifest.getContentProviders()) {
        if (contentProviderData.getClassName().equals(classString)) {
            ProviderInfo providerInfo = new ProviderInfo();
            providerInfo.packageName = packageName;
            providerInfo.name = contentProviderData.getClassName();
            // todo: support multiple authorities
            providerInfo.authority = contentProviderData.getAuthorities();
            providerInfo.readPermission = contentProviderData.getReadPermission();
            providerInfo.writePermission = contentProviderData.getWritePermission();
            providerInfo.pathPermissions = createPathPermissions(contentProviderData.getPathPermissionDatas());
            if ((flags & GET_META_DATA) != 0) {
                providerInfo.metaData = metaDataToBundle(contentProviderData.getMetaData().getValueMap());
            }
            return providerInfo;
        }
    }
    return null;
}
Also used : ContentProviderData(org.robolectric.manifest.ContentProviderData) ProviderInfo(android.content.pm.ProviderInfo) AndroidManifest(org.robolectric.manifest.AndroidManifest)

Example 3 with ContentProviderData

use of org.robolectric.manifest.ContentProviderData in project robolectric by robolectric.

the class DefaultPackageManager method addManifest.

@Override
public void addManifest(AndroidManifest androidManifest, int labelRes) {
    androidManifests.put(androidManifest.getPackageName(), androidManifest);
    PackageInfo packageInfo = new PackageInfo();
    packageInfo.packageName = androidManifest.getPackageName();
    packageInfo.versionName = androidManifest.getVersionName();
    packageInfo.versionCode = androidManifest.getVersionCode();
    Map<String, ActivityData> activityDatas = androidManifest.getActivityDatas();
    for (ActivityData data : activityDatas.values()) {
        String name = data.getName();
        String activityName = name.startsWith(".") ? androidManifest.getPackageName() + name : name;
        addResolveInfoForIntent(new Intent(activityName), new ResolveInfo());
    }
    ContentProviderData[] cpdata = androidManifest.getContentProviders().toArray(new ContentProviderData[] {});
    if (cpdata.length == 0) {
        packageInfo.providers = null;
    } else {
        packageInfo.providers = new ProviderInfo[cpdata.length];
        for (int i = 0; i < cpdata.length; i++) {
            ProviderInfo info = new ProviderInfo();
            // todo: support multiple authorities
            info.authority = cpdata[i].getAuthorities();
            info.name = cpdata[i].getClassName();
            info.packageName = androidManifest.getPackageName();
            packageInfo.providers[i] = info;
        }
    }
    // Populate information related to BroadcastReceivers. Broadcast receivers can be queried in two
    // possible ways,
    // 1. PackageManager#getPackageInfo(...),
    // 2. PackageManager#queryBroadcastReceivers(...)
    // The following piece of code will let you enable querying receivers through both the methods.
    List<ActivityInfo> receiverActivityInfos = new ArrayList<>();
    for (int i = 0; i < androidManifest.getBroadcastReceivers().size(); ++i) {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.name = androidManifest.getBroadcastReceivers().get(i).getClassName();
        activityInfo.permission = androidManifest.getBroadcastReceivers().get(i).getPermission();
        receiverActivityInfos.add(activityInfo);
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        IntentFilter filter = new IntentFilter();
        for (String action : androidManifest.getBroadcastReceivers().get(i).getActions()) {
            filter.addAction(action);
        }
        resolveInfo.filter = filter;
        for (String action : androidManifest.getBroadcastReceivers().get(i).getActions()) {
            Intent intent = new Intent(action);
            intent.setPackage(androidManifest.getPackageName());
            addResolveInfoForIntent(intent, resolveInfo);
        }
    }
    packageInfo.receivers = receiverActivityInfos.toArray(new ActivityInfo[0]);
    String[] usedPermissions = androidManifest.getUsedPermissions().toArray(new String[] {});
    if (usedPermissions.length == 0) {
        packageInfo.requestedPermissions = null;
    } else {
        packageInfo.requestedPermissions = usedPermissions;
    }
    ApplicationInfo applicationInfo = new ApplicationInfo();
    applicationInfo.flags = decodeFlags(androidManifest.getApplicationAttributes());
    applicationInfo.targetSdkVersion = androidManifest.getTargetSdkVersion();
    applicationInfo.packageName = androidManifest.getPackageName();
    applicationInfo.processName = androidManifest.getProcessName();
    applicationInfo.name = androidManifest.getApplicationName();
    applicationInfo.metaData = metaDataToBundle(androidManifest.getApplicationMetaData());
    applicationInfo.sourceDir = new File(".").getAbsolutePath();
    applicationInfo.dataDir = TempDirectory.create().toAbsolutePath().toString();
    if (RuntimeEnvironment.getApiLevel() >= N) {
        applicationInfo.credentialProtectedDataDir = TempDirectory.create().toAbsolutePath().toString();
        applicationInfo.deviceProtectedDataDir = TempDirectory.create().toAbsolutePath().toString();
    }
    applicationInfo.labelRes = labelRes;
    String labelRef = androidManifest.getLabelRef();
    if (labelRef != null && !labelRef.startsWith("@")) {
        applicationInfo.nonLocalizedLabel = labelRef;
    }
    packageInfo.applicationInfo = applicationInfo;
    addPackage(packageInfo);
}
Also used : ActivityData(org.robolectric.manifest.ActivityData) ContentProviderData(org.robolectric.manifest.ContentProviderData) ActivityInfo(android.content.pm.ActivityInfo) IntentFilter(android.content.IntentFilter) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) ResolveInfo(android.content.pm.ResolveInfo) ProviderInfo(android.content.pm.ProviderInfo) File(java.io.File)

Example 4 with ContentProviderData

use of org.robolectric.manifest.ContentProviderData in project robolectric by robolectric.

the class LegacyManifestParser method createPackage.

public static Package createPackage(AndroidManifest androidManifest) {
    Package pkg = new Package(androidManifest.getPackageName());
    pkg.mVersionName = androidManifest.getVersionName();
    pkg.mVersionCode = androidManifest.getVersionCode();
    Map<String, PermissionItemData> permissionItemData = androidManifest.getPermissions();
    for (PermissionItemData itemData : permissionItemData.values()) {
        Permission permission = new Permission(pkg, createPermissionInfo(pkg, itemData));
        permission.metaData = permission.info.metaData;
        pkg.permissions.add(permission);
    }
    Map<String, PermissionGroupItemData> permissionGroupItemData = androidManifest.getPermissionGroups();
    for (PermissionGroupItemData itemData : permissionGroupItemData.values()) {
        PermissionGroup permissionGroup = new PermissionGroup(pkg, createPermissionGroupInfo(pkg, itemData));
        permissionGroup.metaData = permissionGroup.info.metaData;
        pkg.permissionGroups.add(permissionGroup);
    }
    pkg.requestedPermissions.addAll(androidManifest.getUsedPermissions());
    if (RuntimeEnvironment.getApiLevel() < VERSION_CODES.M) {
        List<Boolean> permissionsRequired = ReflectionHelpers.getField(pkg, "requestedPermissionsRequired");
        permissionsRequired.addAll(buildBooleanList(pkg.requestedPermissions.size(), true));
    }
    pkg.applicationInfo.flags = decodeFlags(androidManifest.getApplicationAttributes());
    pkg.applicationInfo.targetSdkVersion = androidManifest.getTargetSdkVersion();
    pkg.applicationInfo.packageName = androidManifest.getPackageName();
    pkg.applicationInfo.processName = androidManifest.getProcessName();
    if (!Strings.isNullOrEmpty(androidManifest.getApplicationName())) {
        pkg.applicationInfo.className = buildClassName(pkg.applicationInfo.packageName, androidManifest.getApplicationName());
        if (RuntimeEnvironment.getApiLevel() >= VERSION_CODES.N_MR1) {
            pkg.applicationInfo.name = pkg.applicationInfo.className;
        }
    }
    pkg.applicationInfo.metaData = metaDataToBundle(androidManifest.getApplicationMetaData());
    pkg.applicationInfo.uid = Process.myUid();
    if (androidManifest.getThemeRef() != null) {
        pkg.applicationInfo.theme = RuntimeEnvironment.getAppResourceTable().getResourceId(ResName.qualifyResName(androidManifest.getThemeRef().replace("@", ""), pkg.packageName, "style"));
    }
    int labelRes = 0;
    if (androidManifest.getLabelRef() != null) {
        String fullyQualifiedName = ResName.qualifyResName(androidManifest.getLabelRef(), androidManifest.getPackageName());
        Integer id = fullyQualifiedName == null ? null : RuntimeEnvironment.getAppResourceTable().getResourceId(new ResName(fullyQualifiedName));
        labelRes = id != null ? id : 0;
    }
    pkg.applicationInfo.labelRes = labelRes;
    String labelRef = androidManifest.getLabelRef();
    if (labelRef != null && !labelRef.startsWith("@")) {
        pkg.applicationInfo.nonLocalizedLabel = labelRef;
    }
    Map<String, ActivityData> activityDatas = androidManifest.getActivityDatas();
    for (ActivityData data : activityDatas.values()) {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.name = buildClassName(pkg.packageName, data.getName());
        activityInfo.packageName = pkg.packageName;
        activityInfo.configChanges = getConfigChanges(data);
        activityInfo.parentActivityName = data.getParentActivityName();
        activityInfo.metaData = metaDataToBundle(data.getMetaData().getValueMap());
        activityInfo.applicationInfo = pkg.applicationInfo;
        activityInfo.targetActivity = data.getTargetActivityName();
        activityInfo.exported = data.isExported();
        activityInfo.permission = data.getPermission();
        activityInfo.enabled = data.isEnabled();
        String themeRef;
        // Based on ShadowActivity
        if (data.getThemeRef() != null) {
            themeRef = data.getThemeRef();
        } else {
            themeRef = androidManifest.getThemeRef();
        }
        if (themeRef != null) {
            activityInfo.theme = RuntimeEnvironment.getAppResourceTable().getResourceId(ResName.qualifyResName(themeRef.replace("@", ""), pkg.packageName, "style"));
        }
        if (data.getLabel() != null) {
            activityInfo.labelRes = RuntimeEnvironment.getAppResourceTable().getResourceId(ResName.qualifyResName(data.getLabel().replace("@", ""), pkg.packageName, "string"));
            if (activityInfo.labelRes == 0) {
                activityInfo.nonLocalizedLabel = data.getLabel();
            }
        }
        Activity activity = createActivity(pkg, activityInfo);
        for (IntentFilterData intentFilterData : data.getIntentFilters()) {
            ActivityIntentInfo outInfo = new ActivityIntentInfo(activity);
            populateIntentInfo(intentFilterData, outInfo);
            activity.intents.add(outInfo);
        }
        pkg.activities.add(activity);
    }
    for (ContentProviderData data : androidManifest.getContentProviders()) {
        ProviderInfo info = new ProviderInfo();
        populateComponentInfo(info, pkg, data);
        info.authority = data.getAuthorities();
        List<PathPermission> permissions = new ArrayList<>();
        for (PathPermissionData permissionData : data.getPathPermissionDatas()) {
            permissions.add(createPathPermission(permissionData));
        }
        info.pathPermissions = permissions.toArray(new PathPermission[permissions.size()]);
        info.readPermission = data.getReadPermission();
        info.writePermission = data.getWritePermission();
        info.grantUriPermissions = data.getGrantUriPermissions();
        info.enabled = data.isEnabled();
        pkg.providers.add(createProvider(pkg, info));
    }
    for (BroadcastReceiverData data : androidManifest.getBroadcastReceivers()) {
        ActivityInfo info = new ActivityInfo();
        populateComponentInfo(info, pkg, data);
        info.permission = data.getPermission();
        info.exported = data.isExported();
        info.enabled = data.isEnabled();
        Activity receiver = createActivity(pkg, info);
        for (IntentFilterData intentFilterData : data.getIntentFilters()) {
            ActivityIntentInfo outInfo = new ActivityIntentInfo(receiver);
            populateIntentInfo(intentFilterData, outInfo);
            receiver.intents.add(outInfo);
        }
        pkg.receivers.add(receiver);
    }
    for (ServiceData data : androidManifest.getServices()) {
        ServiceInfo info = new ServiceInfo();
        populateComponentInfo(info, pkg, data);
        info.permission = data.getPermission();
        info.exported = data.isExported();
        info.enabled = data.isEnabled();
        Service service = createService(pkg, info);
        for (IntentFilterData intentFilterData : data.getIntentFilters()) {
            ServiceIntentInfo outInfo = new ServiceIntentInfo(service);
            populateIntentInfo(intentFilterData, outInfo);
            service.intents.add(outInfo);
        }
        pkg.services.add(service);
    }
    String codePath = RuntimeEnvironment.getTempDirectory().createIfNotExists(pkg.packageName + "-codePath").toAbsolutePath().toString();
    if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
        pkg.codePath = codePath;
    } else {
        ReflectionHelpers.setField(Package.class, pkg, "mPath", codePath);
    }
    return pkg;
}
Also used : PermissionItemData(org.robolectric.manifest.PermissionItemData) ContentProviderData(org.robolectric.manifest.ContentProviderData) ArrayList(java.util.ArrayList) PathPermissionData(org.robolectric.manifest.PathPermissionData) PermissionGroup(android.content.pm.PackageParser.PermissionGroup) Activity(android.content.pm.PackageParser.Activity) ServiceInfo(android.content.pm.ServiceInfo) ProviderInfo(android.content.pm.ProviderInfo) Permission(android.content.pm.PackageParser.Permission) PathPermission(android.content.pm.PathPermission) ActivityData(org.robolectric.manifest.ActivityData) ActivityInfo(android.content.pm.ActivityInfo) IntentFilterData(org.robolectric.manifest.IntentFilterData) PathPermission(android.content.pm.PathPermission) BroadcastReceiverData(org.robolectric.manifest.BroadcastReceiverData) PermissionGroupItemData(org.robolectric.manifest.PermissionGroupItemData) Service(android.content.pm.PackageParser.Service) ActivityIntentInfo(android.content.pm.PackageParser.ActivityIntentInfo) ServiceData(org.robolectric.manifest.ServiceData) ServiceIntentInfo(android.content.pm.PackageParser.ServiceIntentInfo) ResName(org.robolectric.res.ResName) Package(android.content.pm.PackageParser.Package)

Aggregations

ContentProviderData (org.robolectric.manifest.ContentProviderData)4 ProviderInfo (android.content.pm.ProviderInfo)3 ActivityInfo (android.content.pm.ActivityInfo)2 ArrayList (java.util.ArrayList)2 ActivityData (org.robolectric.manifest.ActivityData)2 AndroidManifest (org.robolectric.manifest.AndroidManifest)2 ContentProvider (android.content.ContentProvider)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageInfo (android.content.pm.PackageInfo)1 Activity (android.content.pm.PackageParser.Activity)1 ActivityIntentInfo (android.content.pm.PackageParser.ActivityIntentInfo)1 Package (android.content.pm.PackageParser.Package)1 Permission (android.content.pm.PackageParser.Permission)1 PermissionGroup (android.content.pm.PackageParser.PermissionGroup)1 Service (android.content.pm.PackageParser.Service)1 ServiceIntentInfo (android.content.pm.PackageParser.ServiceIntentInfo)1 PathPermission (android.content.pm.PathPermission)1 ResolveInfo (android.content.pm.ResolveInfo)1