Example usage for android.util ArraySet ArraySet

List of usage examples for android.util ArraySet ArraySet

Introduction

In this page you can find the example usage for android.util ArraySet ArraySet.

Prototype

public ArraySet() 

Source Link

Document

Create a new empty ArraySet.

Usage

From source file:com.android.packageinstaller.permission.ui.PermissionAppsFragment.java

@Override
public void onPermissionsLoaded(PermissionApps permissionApps) {
    Context context = getPreferenceManager().getContext();

    if (context == null) {
        return;/*from  ww  w .ja v a 2s.  c om*/
    }

    boolean isTelevision = DeviceUtils.isTelevision(context);
    PreferenceScreen screen = getPreferenceScreen();

    ArraySet<String> preferencesToRemove = new ArraySet<>();
    for (int i = 0, n = screen.getPreferenceCount(); i < n; i++) {
        preferencesToRemove.add(screen.getPreference(i).getKey());
    }
    if (mExtraScreen != null) {
        for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) {
            preferencesToRemove.add(mExtraScreen.getPreference(i).getKey());
        }
    }

    for (PermissionApp app : permissionApps.getApps()) {
        if (!Utils.shouldShowPermission(app)) {
            continue;
        }

        String key = app.getKey();
        preferencesToRemove.remove(key);
        Preference existingPref = screen.findPreference(key);
        if (existingPref == null && mExtraScreen != null) {
            existingPref = mExtraScreen.findPreference(key);
        }

        boolean isSystemApp = Utils.isSystem(app, mLauncherPkgs);
        if (isSystemApp && !isTelevision && !mShowSystem) {
            if (existingPref != null) {
                screen.removePreference(existingPref);
            }
            continue;
        }

        if (existingPref != null) {
            // If existing preference - only update its state.
            if (app.isPolicyFixed()) {
                existingPref.setSummary(getString(R.string.permission_summary_enforced_by_policy));
            }
            existingPref.setPersistent(false);
            existingPref.setEnabled(!app.isPolicyFixed());
            if (existingPref instanceof SwitchPreference) {
                ((SwitchPreference) existingPref).setChecked(app.areRuntimePermissionsGranted());
            }
            continue;
        }

        SwitchPreference pref = new SwitchPreference(context);
        pref.setOnPreferenceChangeListener(this);
        pref.setKey(app.getKey());
        pref.setIcon(app.getIcon());
        pref.setTitle(app.getLabel());
        if (app.isPolicyFixed()) {
            pref.setSummary(getString(R.string.permission_summary_enforced_by_policy));
        }
        pref.setPersistent(false);
        pref.setEnabled(!app.isPolicyFixed());
        pref.setChecked(app.areRuntimePermissionsGranted());

        if (isSystemApp && isTelevision) {
            if (mExtraScreen == null) {
                mExtraScreen = getPreferenceManager().createPreferenceScreen(context);
            }
            mExtraScreen.addPreference(pref);
        } else {
            screen.addPreference(pref);
        }
    }

    if (mExtraScreen != null) {
        preferencesToRemove.remove(KEY_SHOW_SYSTEM_PREFS);
        Preference pref = screen.findPreference(KEY_SHOW_SYSTEM_PREFS);

        if (pref == null) {
            pref = new Preference(context);
            pref.setKey(KEY_SHOW_SYSTEM_PREFS);
            pref.setIcon(Utils.applyTint(context, R.drawable.ic_toc, android.R.attr.colorControlNormal));
            pref.setTitle(R.string.preference_show_system_apps);
            pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    SystemAppsFragment frag = new SystemAppsFragment();
                    setPermissionName(frag, getArguments().getString(Intent.EXTRA_PERMISSION_NAME));
                    frag.setTargetFragment(PermissionAppsFragment.this, 0);
                    getFragmentManager().beginTransaction().replace(android.R.id.content, frag)
                            .addToBackStack("SystemApps").commit();
                    return true;
                }
            });
            screen.addPreference(pref);
        }

        int grantedCount = 0;
        for (int i = 0, n = mExtraScreen.getPreferenceCount(); i < n; i++) {
            if (((SwitchPreference) mExtraScreen.getPreference(i)).isChecked()) {
                grantedCount++;
            }
        }
        pref.setSummary(getString(R.string.app_permissions_group_summary, grantedCount,
                mExtraScreen.getPreferenceCount()));
    }

    for (String key : preferencesToRemove) {
        Preference pref = screen.findPreference(key);
        if (pref != null) {
            screen.removePreference(pref);
        } else if (mExtraScreen != null) {
            pref = mExtraScreen.findPreference(key);
            if (pref != null) {
                mExtraScreen.removePreference(pref);
            }
        }
    }

    setLoading(false /* loading */, true /* animate */);

    if (mOnPermissionsLoadedListener != null) {
        mOnPermissionsLoadedListener.onPermissionsLoaded(permissionApps);
    }
}

From source file:android.content.pm.PackageParser.java

private static void collectCertificates(Package pkg, File apkFile, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();

    StrictJarFile jarFile = null;/*from   w w  w . j  av  a 2s  .co  m*/
    try {
        jarFile = new StrictJarFile(apkPath);

        // Always verify manifest, regardless of source
        final ZipEntry manifestEntry = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);
        if (manifestEntry == null) {
            throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
                    "Package " + apkPath + " has no manifest");
        }

        final List<ZipEntry> toVerify = new ArrayList<>();
        toVerify.add(manifestEntry);

        // If we're parsing an untrusted package, verify all contents
        if ((flags & PARSE_IS_SYSTEM) == 0) {
            final Iterator<ZipEntry> i = jarFile.iterator();
            while (i.hasNext()) {
                final ZipEntry entry = i.next();

                if (entry.isDirectory())
                    continue;
                if (entry.getName().startsWith("META-INF/"))
                    continue;
                if (entry.getName().equals(ANDROID_MANIFEST_FILENAME))
                    continue;

                toVerify.add(entry);
            }
        }

        // Verify that entries are signed consistently with the first entry
        // we encountered. Note that for splits, certificates may have
        // already been populated during an earlier parse of a base APK.
        for (ZipEntry entry : toVerify) {
            final Certificate[][] entryCerts = loadCertificates(jarFile, entry);
            if (ArrayUtils.isEmpty(entryCerts)) {
                throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
                        "Package " + apkPath + " has no certificates at entry " + entry.getName());
            }
            final Signature[] entrySignatures = convertToSignatures(entryCerts);

            if (pkg.mCertificates == null) {
                pkg.mCertificates = entryCerts;
                pkg.mSignatures = entrySignatures;
                pkg.mSigningKeys = new ArraySet<PublicKey>();
                for (int i = 0; i < entryCerts.length; i++) {
                    pkg.mSigningKeys.add(entryCerts[i][0].getPublicKey());
                }
            } else {
                if (!Signature.areExactMatch(pkg.mSignatures, entrySignatures)) {
                    throw new PackageParserException(INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES,
                            "Package " + apkPath + " has mismatched certificates at entry " + entry.getName());
                }
            }
        }
    } catch (GeneralSecurityException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING,
                "Failed to collect certificates from " + apkPath, e);
    } catch (IOException | RuntimeException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
                "Failed to collect certificates from " + apkPath, e);
    } finally {
        closeQuietly(jarFile);
    }
}

From source file:android.content.pm.PackageParser.java

private boolean parseKeySets(Package owner, Resources res, XmlPullParser parser, AttributeSet attrs,
        String[] outError) throws XmlPullParserException, IOException {
    // we've encountered the 'key-sets' tag
    // all the keys and keysets that we want must be defined here
    // so we're going to iterate over the parser and pull out the things we want
    int outerDepth = parser.getDepth();
    int currentKeySetDepth = -1;
    int type;//  w w w .j av a2s . co  m
    String currentKeySet = null;
    ArrayMap<String, PublicKey> publicKeys = new ArrayMap<String, PublicKey>();
    ArraySet<String> upgradeKeySets = new ArraySet<String>();
    ArrayMap<String, ArraySet<String>> definedKeySets = new ArrayMap<String, ArraySet<String>>();
    ArraySet<String> improperKeySets = new ArraySet<String>();
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG) {
            if (parser.getDepth() == currentKeySetDepth) {
                currentKeySet = null;
                currentKeySetDepth = -1;
            }
            continue;
        }
        String tagName = parser.getName();
        if (tagName.equals("key-set")) {
            if (currentKeySet != null) {
                outError[0] = "Improperly nested 'key-set' tag at " + parser.getPositionDescription();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                return false;
            }
            final TypedArray sa = res.obtainAttributes(attrs,
                    com.android.internal.R.styleable.AndroidManifestKeySet);
            final String keysetName = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestKeySet_name);
            definedKeySets.put(keysetName, new ArraySet<String>());
            currentKeySet = keysetName;
            currentKeySetDepth = parser.getDepth();
            sa.recycle();
        } else if (tagName.equals("public-key")) {
            if (currentKeySet == null) {
                outError[0] = "Improperly nested 'key-set' tag at " + parser.getPositionDescription();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                return false;
            }
            final TypedArray sa = res.obtainAttributes(attrs,
                    com.android.internal.R.styleable.AndroidManifestPublicKey);
            final String publicKeyName = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPublicKey_name);
            final String encodedKey = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPublicKey_value);
            if (encodedKey == null && publicKeys.get(publicKeyName) == null) {
                outError[0] = "'public-key' " + publicKeyName + " must define a public-key value"
                        + " on first use at " + parser.getPositionDescription();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                sa.recycle();
                return false;
            } else if (encodedKey != null) {
                PublicKey currentKey = parsePublicKey(encodedKey);
                if (currentKey == null) {
                    Slog.w(TAG,
                            "No recognized valid key in 'public-key' tag at " + parser.getPositionDescription()
                                    + " key-set " + currentKeySet
                                    + " will not be added to the package's defined key-sets.");
                    sa.recycle();
                    improperKeySets.add(currentKeySet);
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                if (publicKeys.get(publicKeyName) == null || publicKeys.get(publicKeyName).equals(currentKey)) {

                    /* public-key first definition, or matches old definition */
                    publicKeys.put(publicKeyName, currentKey);
                } else {
                    outError[0] = "Value of 'public-key' " + publicKeyName
                            + " conflicts with previously defined value at " + parser.getPositionDescription();
                    mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                    sa.recycle();
                    return false;
                }
            }
            definedKeySets.get(currentKeySet).add(publicKeyName);
            sa.recycle();
            XmlUtils.skipCurrentTag(parser);
        } else if (tagName.equals("upgrade-key-set")) {
            final TypedArray sa = res.obtainAttributes(attrs,
                    com.android.internal.R.styleable.AndroidManifestUpgradeKeySet);
            String name = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestUpgradeKeySet_name);
            upgradeKeySets.add(name);
            sa.recycle();
            XmlUtils.skipCurrentTag(parser);
        } else if (RIGID_PARSER) {
            outError[0] = "Bad element under <key-sets>: " + parser.getName() + " at " + mArchiveSourcePath
                    + " " + parser.getPositionDescription();
            mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
            return false;
        } else {
            Slog.w(TAG, "Unknown element under <key-sets>: " + parser.getName() + " at " + mArchiveSourcePath
                    + " " + parser.getPositionDescription());
            XmlUtils.skipCurrentTag(parser);
            continue;
        }
    }
    Set<String> publicKeyNames = publicKeys.keySet();
    if (publicKeyNames.removeAll(definedKeySets.keySet())) {
        outError[0] = "Package" + owner.packageName + " AndroidManifext.xml "
                + "'key-set' and 'public-key' names must be distinct.";
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return false;
    }
    owner.mKeySetMapping = new ArrayMap<String, ArraySet<PublicKey>>();
    for (ArrayMap.Entry<String, ArraySet<String>> e : definedKeySets.entrySet()) {
        final String keySetName = e.getKey();
        if (e.getValue().size() == 0) {
            Slog.w(TAG,
                    "Package" + owner.packageName + " AndroidManifext.xml " + "'key-set' " + keySetName
                            + " has no valid associated 'public-key'."
                            + " Not including in package's defined key-sets.");
            continue;
        } else if (improperKeySets.contains(keySetName)) {
            Slog.w(TAG,
                    "Package" + owner.packageName + " AndroidManifext.xml " + "'key-set' " + keySetName
                            + " contained improper 'public-key'"
                            + " tags. Not including in package's defined key-sets.");
            continue;
        }
        owner.mKeySetMapping.put(keySetName, new ArraySet<PublicKey>());
        for (String s : e.getValue()) {
            owner.mKeySetMapping.get(keySetName).add(publicKeys.get(s));
        }
    }
    if (owner.mKeySetMapping.keySet().containsAll(upgradeKeySets)) {
        owner.mUpgradeKeySets = upgradeKeySets;
    } else {
        outError[0] = "Package" + owner.packageName + " AndroidManifext.xml "
                + "does not define all 'upgrade-key-set's .";
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return false;
    }
    return true;
}