Search in sources :

Example 1 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class ShadowPackageManagerTest method getAllPermissionGroups_duplicatePermission.

@Test
public void getAllPermissionGroups_duplicatePermission() {
    assertThat(packageManager.getAllPermissionGroups(0)).hasSize(1);
    // Package 1
    Package pkg = new Package(TEST_PACKAGE_NAME);
    ApplicationInfo appInfo = pkg.applicationInfo;
    appInfo.flags = ApplicationInfo.FLAG_INSTALLED;
    appInfo.packageName = TEST_PACKAGE_NAME;
    appInfo.sourceDir = TEST_APP_PATH;
    appInfo.name = TEST_PACKAGE_LABEL;
    PermissionGroupInfo contactsPermissionGroupInfoApp1 = new PermissionGroupInfo();
    contactsPermissionGroupInfoApp1.name = Manifest.permission_group.CONTACTS;
    PermissionGroup contactsPermissionGroupApp1 = new PermissionGroup(pkg, contactsPermissionGroupInfoApp1);
    pkg.permissionGroups.add(contactsPermissionGroupApp1);
    PermissionGroupInfo storagePermissionGroupInfoApp1 = new PermissionGroupInfo();
    storagePermissionGroupInfoApp1.name = permission_group.STORAGE;
    PermissionGroup storagePermissionGroupApp1 = new PermissionGroup(pkg, storagePermissionGroupInfoApp1);
    pkg.permissionGroups.add(storagePermissionGroupApp1);
    shadowOf(packageManager).addPackageInternal(pkg);
    // Package 2, contains one permission group that is the same
    Package pkg2 = new Package(TEST_PACKAGE2_NAME);
    ApplicationInfo appInfo2 = pkg2.applicationInfo;
    appInfo2.flags = ApplicationInfo.FLAG_INSTALLED;
    appInfo2.packageName = TEST_PACKAGE2_NAME;
    appInfo2.sourceDir = TEST_APP2_PATH;
    appInfo2.name = TEST_PACKAGE2_LABEL;
    PermissionGroupInfo contactsPermissionGroupInfoApp2 = new PermissionGroupInfo();
    contactsPermissionGroupInfoApp2.name = Manifest.permission_group.CONTACTS;
    PermissionGroup contactsPermissionGroupApp2 = new PermissionGroup(pkg2, contactsPermissionGroupInfoApp2);
    pkg2.permissionGroups.add(contactsPermissionGroupApp2);
    PermissionGroupInfo calendarPermissionGroupInfoApp2 = new PermissionGroupInfo();
    calendarPermissionGroupInfoApp2.name = permission_group.CALENDAR;
    PermissionGroup calendarPermissionGroupApp2 = new PermissionGroup(pkg2, calendarPermissionGroupInfoApp2);
    pkg2.permissionGroups.add(calendarPermissionGroupApp2);
    shadowOf(packageManager).addPackageInternal(pkg2);
    // Make sure that the duplicate permission group does not show up in the list
    // Total list should be: contacts, storage, calendar, "org.robolectric.package_permission_group"
    List<PermissionGroupInfo> allPermissionGroups = packageManager.getAllPermissionGroups(0);
    assertThat(allPermissionGroups).hasSize(4);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) PermissionGroupInfo(android.content.pm.PermissionGroupInfo) PermissionGroup(android.content.pm.PackageParser.PermissionGroup) Package(android.content.pm.PackageParser.Package) Test(org.junit.Test)

Example 2 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class AndroidTestEnvironment method loadAppPackage_measured.

private Package loadAppPackage_measured(Config config, AndroidManifest appManifest) {
    Package parsedPackage;
    if (RuntimeEnvironment.useLegacyResources()) {
        injectResourceStuffForLegacy(appManifest);
        if (appManifest.getAndroidManifestFile() != null && Files.exists(appManifest.getAndroidManifestFile())) {
            parsedPackage = LegacyManifestParser.createPackage(appManifest);
        } else {
            parsedPackage = new Package("org.robolectric.default");
            parsedPackage.applicationInfo.targetSdkVersion = appManifest.getTargetSdkVersion();
        }
        // Support overriding the package name specified in the Manifest.
        if (!Config.DEFAULT_PACKAGE_NAME.equals(config.packageName())) {
            parsedPackage.packageName = config.packageName();
            parsedPackage.applicationInfo.packageName = config.packageName();
        } else {
            parsedPackage.packageName = appManifest.getPackageName();
            parsedPackage.applicationInfo.packageName = appManifest.getPackageName();
        }
    } else {
        RuntimeEnvironment.compileTimeSystemResourcesFile = compileSdk.getJarPath();
        Path packageFile = appManifest.getApkFile();
        parsedPackage = ShadowPackageParser.callParsePackage(packageFile);
    }
    return parsedPackage;
}
Also used : Path(java.nio.file.Path) ResourcePath(org.robolectric.res.ResourcePath) Package(android.content.pm.PackageParser.Package)

Example 3 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class ShadowPackageParser method callParsePackage.

/**
 * Parses an AndroidManifest.xml file using the framework PackageParser.
 */
public static Package callParsePackage(Path apkFile) {
    PackageParser packageParser = new PackageParser();
    int flags = PackageParser.PARSE_IGNORE_PROCESSES;
    try {
        Package thePackage;
        if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.LOLLIPOP) {
            // PackageParser#setMinAspectRatio(Package)
            if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.Q) {
                QHelper.setCallback(packageParser);
            }
            thePackage = packageParser.parsePackage(apkFile.toFile(), flags);
        } else {
            // JB -> KK
            thePackage = reflector(_PackageParser_.class, packageParser).parsePackage(apkFile.toFile(), Fs.externalize(apkFile), new DisplayMetrics(), flags);
        }
        if (thePackage == null) {
            List<LogItem> logItems = ShadowLog.getLogsForTag("PackageParser");
            if (logItems.isEmpty()) {
                throw new RuntimeException("Failed to parse package " + apkFile);
            } else {
                LogItem logItem = logItems.get(0);
                throw new RuntimeException("Failed to parse package " + apkFile + ": " + logItem.msg, logItem.throwable);
            }
        }
        return thePackage;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PackageParser(android.content.pm.PackageParser) LogItem(org.robolectric.shadows.ShadowLog.LogItem) Package(android.content.pm.PackageParser.Package) DisplayMetrics(android.util.DisplayMetrics)

Example 4 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class LegacyManifestParserTest method createPackage_protectionLevelDangerous_shouldParseCorrectFlags.

@Test
public void createPackage_protectionLevelDangerous_shouldParseCorrectFlags() {
    Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
    int protectionLevel = getPermissionInfo(parsedPackage.permissions, "dangerous_permission").protectionLevel;
    assertThat(protectionLevel).isEqualTo(PermissionInfo.PROTECTION_DANGEROUS);
}
Also used : Package(android.content.pm.PackageParser.Package) Test(org.junit.Test)

Example 5 with Package

use of android.content.pm.PackageParser.Package 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

Package (android.content.pm.PackageParser.Package)9 Test (org.junit.Test)5 ApplicationInfo (android.content.pm.ApplicationInfo)2 PermissionGroup (android.content.pm.PackageParser.PermissionGroup)2 ActivityThread (android.app.ActivityThread)1 Application (android.app.Application)1 LoadedApk (android.app.LoadedApk)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 ActivityInfo (android.content.pm.ActivityInfo)1 PackageManager (android.content.pm.PackageManager)1 PackageParser (android.content.pm.PackageParser)1 Activity (android.content.pm.PackageParser.Activity)1 ActivityIntentInfo (android.content.pm.PackageParser.ActivityIntentInfo)1 Permission (android.content.pm.PackageParser.Permission)1 Service (android.content.pm.PackageParser.Service)1 ServiceIntentInfo (android.content.pm.PackageParser.ServiceIntentInfo)1 PathPermission (android.content.pm.PathPermission)1 PermissionGroupInfo (android.content.pm.PermissionGroupInfo)1 ProviderInfo (android.content.pm.ProviderInfo)1