Search in sources :

Example 16 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ParanoidAndroid.

the class PackageManagerBackupAgent method onBackup.

// The backed up data is the signature block for each app, keyed by
// the package name.
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) {
    if (DEBUG)
        Slog.v(TAG, "onBackup()");
    // we'll reuse these
    ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
    DataOutputStream outputBufferStream = new DataOutputStream(outputBuffer);
    parseStateFile(oldState);
    // "already backed up" map built by parseStateFile().
    if (mStoredIncrementalVersion == null || !mStoredIncrementalVersion.equals(Build.VERSION.INCREMENTAL)) {
        Slog.i(TAG, "Previous metadata " + mStoredIncrementalVersion + " mismatch vs " + Build.VERSION.INCREMENTAL + " - rewriting");
        mExisting.clear();
    }
    try {
        /*
             * Global metadata:
             *
             * int SDKversion -- the SDK version of the OS itself on the device
             *                   that produced this backup set.  Used to reject
             *                   backups from later OSes onto earlier ones.
             * String incremental -- the incremental release name of the OS stored in
             *                       the backup set.
             */
        if (!mExisting.contains(GLOBAL_METADATA_KEY)) {
            if (DEBUG)
                Slog.v(TAG, "Storing global metadata key");
            outputBufferStream.writeInt(Build.VERSION.SDK_INT);
            outputBufferStream.writeUTF(Build.VERSION.INCREMENTAL);
            writeEntity(data, GLOBAL_METADATA_KEY, outputBuffer.toByteArray());
        } else {
            if (DEBUG)
                Slog.v(TAG, "Global metadata key already stored");
            // don't consider it to have been skipped/deleted
            mExisting.remove(GLOBAL_METADATA_KEY);
        }
        // write its signature block to the output, keyed on the package name.
        for (PackageInfo pkg : mAllPackages) {
            String packName = pkg.packageName;
            if (packName.equals(GLOBAL_METADATA_KEY)) {
                // We've already handled the metadata key; skip it here
                continue;
            } else {
                PackageInfo info = null;
                try {
                    info = mPackageManager.getPackageInfo(packName, PackageManager.GET_SIGNATURES);
                } catch (NameNotFoundException e) {
                    // Weird; we just found it, and now are told it doesn't exist.
                    // Treat it as having been removed from the device.
                    mExisting.add(packName);
                    continue;
                }
                if (mExisting.contains(packName)) {
                    // We have backed up this app before.  Check whether the version
                    // of the backup matches the version of the current app; if they
                    // don't match, the app has been updated and we need to store its
                    // metadata again.  In either case, take it out of mExisting so that
                    // we don't consider it deleted later.
                    mExisting.remove(packName);
                    if (info.versionCode == mStateVersions.get(packName).versionCode) {
                        continue;
                    }
                }
                if (info.signatures == null || info.signatures.length == 0) {
                    Slog.w(TAG, "Not backing up package " + packName + " since it appears to have no signatures.");
                    continue;
                }
                // We need to store this app's metadata
                /*
                     * Metadata for each package:
                     *
                     * int version       -- [4] the package's versionCode
                     * byte[] signatures -- [len] flattened Signature[] of the package
                     */
                // marshal the version code in a canonical form
                outputBuffer.reset();
                outputBufferStream.writeInt(info.versionCode);
                writeSignatureArray(outputBufferStream, info.signatures);
                if (DEBUG) {
                    Slog.v(TAG, "+ writing metadata for " + packName + " version=" + info.versionCode + " entityLen=" + outputBuffer.size());
                }
                // Now we can write the backup entity for this package
                writeEntity(data, packName, outputBuffer.toByteArray());
            }
        }
        // on the device.  Write a deletion entity for them.
        for (String app : mExisting) {
            if (DEBUG)
                Slog.v(TAG, "- removing metadata for deleted pkg " + app);
            try {
                data.writeEntityHeader(app, -1);
            } catch (IOException e) {
                Slog.e(TAG, "Unable to write package deletions!");
                return;
            }
        }
    } catch (IOException e) {
        // Real error writing data
        Slog.e(TAG, "Unable to write package backup data file!");
        return;
    }
    // Finally, write the new state blob -- just the list of all apps we handled
    writeStateFile(mAllPackages, newState);
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) DataOutputStream(java.io.DataOutputStream) PackageInfo(android.content.pm.PackageInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 17 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ParanoidAndroid.

the class PackageManagerBackupAgent method writeStateFile.

// Util: write out our new backup state file
private void writeStateFile(List<PackageInfo> pkgs, ParcelFileDescriptor stateFile) {
    FileOutputStream outstream = new FileOutputStream(stateFile.getFileDescriptor());
    DataOutputStream out = new DataOutputStream(outstream);
    try {
        // by the time we get here we know we've stored the global metadata record
        out.writeUTF(GLOBAL_METADATA_KEY);
        out.writeInt(Build.VERSION.SDK_INT);
        out.writeUTF(Build.VERSION.INCREMENTAL);
        // now write all the app names too
        for (PackageInfo pkg : pkgs) {
            out.writeUTF(pkg.packageName);
            out.writeInt(pkg.versionCode);
        }
    } catch (IOException e) {
        Slog.e(TAG, "Unable to write package manager state file!");
        return;
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) PackageInfo(android.content.pm.PackageInfo) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 18 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ParanoidAndroid.

the class ServiceWatcher method bindBestPackageLocked.

/**
     * Searches and binds to the best package, or do nothing
     * if the best package is already bound.
     * Only checks the named package, or checks all packages if it
     * is null.
     * Return true if a new package was found to bind to.
     */
private boolean bindBestPackageLocked(String justCheckThisPackage) {
    Intent intent = new Intent(mAction);
    if (justCheckThisPackage != null) {
        intent.setPackage(justCheckThisPackage);
    }
    List<ResolveInfo> rInfos = mPm.queryIntentServicesAsUser(intent, PackageManager.GET_META_DATA, UserHandle.USER_OWNER);
    int bestVersion = Integer.MIN_VALUE;
    String bestPackage = null;
    boolean bestIsMultiuser = false;
    if (rInfos != null) {
        for (ResolveInfo rInfo : rInfos) {
            String packageName = rInfo.serviceInfo.packageName;
            // check signature
            try {
                PackageInfo pInfo;
                pInfo = mPm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
                if (!isSignatureMatch(pInfo.signatures)) {
                    Log.w(mTag, packageName + " resolves service " + mAction + ", but has wrong signature, ignoring");
                    continue;
                }
            } catch (NameNotFoundException e) {
                Log.wtf(mTag, e);
                continue;
            }
            // check metadata
            int version = Integer.MIN_VALUE;
            boolean isMultiuser = false;
            if (rInfo.serviceInfo.metaData != null) {
                version = rInfo.serviceInfo.metaData.getInt(EXTRA_SERVICE_VERSION, Integer.MIN_VALUE);
                isMultiuser = rInfo.serviceInfo.metaData.getBoolean(EXTRA_SERVICE_IS_MULTIUSER);
            }
            if (version > mVersion) {
                bestVersion = version;
                bestPackage = packageName;
                bestIsMultiuser = isMultiuser;
            }
        }
        if (D) {
            Log.d(mTag, String.format("bindBestPackage for %s : %s found %d, %s", mAction, (justCheckThisPackage == null ? "" : "(" + justCheckThisPackage + ") "), rInfos.size(), (bestPackage == null ? "no new best package" : "new best package: " + bestPackage)));
        }
    } else {
        if (D)
            Log.d(mTag, "Unable to query intent services for action: " + mAction);
    }
    if (bestPackage != null) {
        bindToPackageLocked(bestPackage, bestVersion, bestIsMultiuser);
        return true;
    }
    return false;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Intent(android.content.Intent)

Example 19 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ParanoidAndroid.

the class PackageManagerService method generatePackageInfo.

PackageInfo generatePackageInfo(PackageParser.Package p, int flags, int userId) {
    if (!sUserManager.exists(userId))
        return null;
    PackageInfo pi;
    final PackageSetting ps = (PackageSetting) p.mExtras;
    if (ps == null) {
        return null;
    }
    final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps;
    final PackageUserState state = ps.readUserState(userId);
    return PackageParser.generatePackageInfo(p, gp.gids, flags, ps.firstInstallTime, ps.lastUpdateTime, gp.grantedPermissions, state, userId);
}
Also used : PackageUserState(android.content.pm.PackageUserState) PackageInfo(android.content.pm.PackageInfo)

Example 20 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ParanoidAndroid.

the class PackageManagerService method addPackageHoldingPermissions.

private void addPackageHoldingPermissions(ArrayList<PackageInfo> list, PackageSetting ps, String[] permissions, boolean[] tmp, int flags, int userId) {
    int numMatch = 0;
    final GrantedPermissions gp = ps.sharedUser != null ? ps.sharedUser : ps;
    for (int i = 0; i < permissions.length; i++) {
        if (gp.grantedPermissions.contains(permissions[i])) {
            tmp[i] = true;
            numMatch++;
        } else {
            tmp[i] = false;
        }
    }
    if (numMatch == 0) {
        return;
    }
    PackageInfo pi;
    if (ps.pkg != null) {
        pi = generatePackageInfo(ps.pkg, flags, userId);
    } else {
        pi = generatePackageInfoFromSettingsLPw(ps.name, flags, userId);
    }
    if ((flags & PackageManager.GET_PERMISSIONS) == 0) {
        if (numMatch == permissions.length) {
            pi.requestedPermissions = permissions;
        } else {
            pi.requestedPermissions = new String[numMatch];
            numMatch = 0;
            for (int i = 0; i < permissions.length; i++) {
                if (tmp[i]) {
                    pi.requestedPermissions[numMatch] = permissions[i];
                    numMatch++;
                }
            }
        }
    }
    list.add(pi);
}
Also used : PackageInfo(android.content.pm.PackageInfo)

Aggregations

PackageInfo (android.content.pm.PackageInfo)1566 PackageManager (android.content.pm.PackageManager)702 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)327 ApplicationInfo (android.content.pm.ApplicationInfo)217 Test (org.junit.Test)193 RemoteException (android.os.RemoteException)179 ArrayList (java.util.ArrayList)166 Intent (android.content.Intent)126 IOException (java.io.IOException)121 Context (android.content.Context)81 IPackageManager (android.content.pm.IPackageManager)65 ResolveInfo (android.content.pm.ResolveInfo)65 File (java.io.File)62 SuppressLint (android.annotation.SuppressLint)58 ComponentName (android.content.ComponentName)57 ActivityInfo (android.content.pm.ActivityInfo)57 View (android.view.View)57 TextView (android.widget.TextView)54 Signature (android.content.pm.Signature)51 OverlayInfo (android.content.om.OverlayInfo)42