Search in sources :

Example 46 with PackageInfo

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

the class NetworkPolicyManagerServiceTest method _setUp.

public void _setUp() throws Exception {
    super.setUp();
    setCurrentTimeMillis(TEST_START);
    // intercept various broadcasts, and pretend that uids have packages
    mServiceContext = new BroadcastInterceptingContext(getContext()) {

        @Override
        public PackageManager getPackageManager() {
            return new MockPackageManager() {

                @Override
                public String[] getPackagesForUid(int uid) {
                    return new String[] { "com.example" };
                }

                @Override
                public PackageInfo getPackageInfo(String packageName, int flags) {
                    final PackageInfo info = new PackageInfo();
                    final Signature signature;
                    if ("android".equals(packageName)) {
                        signature = new Signature("F00D");
                    } else {
                        signature = new Signature("DEAD");
                    }
                    info.signatures = new Signature[] { signature };
                    return info;
                }
            };
        }

        @Override
        public void startActivity(Intent intent) {
        // ignored
        }
    };
    mPolicyDir = getContext().getFilesDir();
    if (mPolicyDir.exists()) {
        IoUtils.deleteContents(mPolicyDir);
    }
    mActivityManager = createMock(IActivityManager.class);
    mStatsService = createMock(INetworkStatsService.class);
    mNetworkManager = createMock(INetworkManagementService.class);
    mPolicyListener = createMock(INetworkPolicyListener.class);
    mTime = createMock(TrustedTime.class);
    mConnManager = createMock(IConnectivityManager.class);
    mNotifManager = createMock(INotificationManager.class);
    mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, mNetworkManager, mTime, mPolicyDir, true);
    mService.bindConnectivityManager(mConnManager);
    mService.bindNotificationManager(mNotifManager);
    // RemoteCallbackList needs a binder to use as key
    expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce();
    replay();
    mService.registerListener(mPolicyListener);
    verifyAndReset();
    // catch IProcessObserver during systemReady()
    final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>();
    mActivityManager.registerProcessObserver(capture(processObserver));
    expectLastCall().atLeastOnce();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetworkManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
    expectCurrentTime();
    replay();
    mService.systemReady();
    verifyAndReset();
    mProcessObserver = processObserver.getValue();
    mNetworkObserver = networkObserver.getValue();
}
Also used : PackageInfo(android.content.pm.PackageInfo) TrustedTime(android.util.TrustedTime) INetworkManagementService(android.os.INetworkManagementService) MockPackageManager(android.test.mock.MockPackageManager) Intent(android.content.Intent) INetworkPolicyListener(android.net.INetworkPolicyListener) Capture(org.easymock.Capture) NetworkPolicyManagerService(com.android.server.net.NetworkPolicyManagerService) PackageManager(android.content.pm.PackageManager) MockPackageManager(android.test.mock.MockPackageManager) INotificationManager(android.app.INotificationManager) Signature(android.content.pm.Signature) INetworkStatsService(android.net.INetworkStatsService) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) IConnectivityManager(android.net.IConnectivityManager) IProcessObserver(android.app.IProcessObserver) IActivityManager(android.app.IActivityManager)

Example 47 with PackageInfo

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

the class BaseShortcutManagerTest method genPackage.

protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
    final PackageInfo pi = new PackageInfo();
    pi.packageName = packageName;
    pi.applicationInfo = new ApplicationInfo();
    pi.applicationInfo.uid = uid;
    pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED | ApplicationInfo.FLAG_ALLOW_BACKUP;
    pi.versionCode = version;
    pi.applicationInfo.versionCode = version;
    pi.signatures = genSignatures(signatures);
    return pi;
}
Also used : PackageInfo(android.content.pm.PackageInfo) ApplicationInfo(android.content.pm.ApplicationInfo)

Example 48 with PackageInfo

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

the class AppSecurityPermissions method getPermissionsForPackage.

private void getPermissionsForPackage(String packageName, Set<MyPermissionInfo> permSet) {
    try {
        PackageInfo pkgInfo = mPm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        extractPerms(pkgInfo, permSet, pkgInfo);
    } catch (NameNotFoundException e) {
        Log.w(TAG, "Couldn't retrieve permissions for package: " + packageName);
    }
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo)

Example 49 with PackageInfo

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

the class HelpUtils method uriWithAddedParameters.

/**
     * Adds two query parameters into the Uri, namely the language code and the version code
     * of the app's package as gotten via the context.
     * @return the uri with added query parameters
     */
public static Uri uriWithAddedParameters(Context context, Uri baseUri) {
    Uri.Builder builder = baseUri.buildUpon();
    // Add in the preferred language
    builder.appendQueryParameter(PARAM_LANGUAGE_CODE, Locale.getDefault().toString());
    // Add in the package version code
    if (sCachedVersionCode == null) {
        // There is no cached version code, so try to get it from the package manager.
        try {
            // cache the version code
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            sCachedVersionCode = Integer.toString(info.versionCode);
            // append the version code to the uri
            builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode);
        } catch (NameNotFoundException e) {
            // Cannot find the package name, so don't add in the version parameter
            // This shouldn't happen.
            Log.wtf(TAG, "Invalid package name for context", e);
        }
    } else {
        builder.appendQueryParameter(PARAM_VERSION, sCachedVersionCode);
    }
    // Build the full uri and return it
    return builder.build();
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Uri(android.net.Uri)

Example 50 with PackageInfo

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

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();
    }
    long homeVersion = 0;
    ArrayList<byte[]> homeSigHashes = null;
    PackageInfo homeInfo = null;
    String homeInstaller = null;
    ComponentName home = getPreferredHomeComponent();
    if (home != null) {
        try {
            homeInfo = mPackageManager.getPackageInfo(home.getPackageName(), PackageManager.GET_SIGNATURES);
            homeInstaller = mPackageManager.getInstallerPackageName(home.getPackageName());
            homeVersion = homeInfo.versionCode;
            homeSigHashes = BackupUtils.hashSignatureArray(homeInfo.signatures);
        } catch (NameNotFoundException e) {
            Slog.w(TAG, "Can't access preferred home info");
            // proceed as though there were no preferred home set
            home = null;
        }
    }
    try {
        // We need to push a new preferred-home-app record if:
        //    1. the version of the home app has changed since our last backup;
        //    2. the home app [or absence] we now use differs from the prior state,
        // OR 3. it looks like we use the same home app + version as before, but
        //       the signatures don't match so we treat them as different apps.
        final boolean needHomeBackup = (homeVersion != mStoredHomeVersion) || !Objects.equals(home, mStoredHomeComponent) || (home != null && !BackupUtils.signaturesMatch(mStoredHomeSigHashes, homeInfo));
        if (needHomeBackup) {
            if (DEBUG) {
                Slog.i(TAG, "Home preference changed; backing up new state " + home);
            }
            if (home != null) {
                outputBufferStream.writeUTF(home.flattenToString());
                outputBufferStream.writeLong(homeVersion);
                outputBufferStream.writeUTF(homeInstaller != null ? homeInstaller : "");
                writeSignatureHashArray(outputBufferStream, homeSigHashes);
                writeEntity(data, DEFAULT_HOME_KEY, outputBuffer.toByteArray());
            } else {
                data.writeEntityHeader(DEFAULT_HOME_KEY, -1);
            }
        }
        /*
             * 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.
             */
        outputBuffer.reset();
        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 hash array of the package
                     */
                // marshal the version code in a canonical form
                outputBuffer.reset();
                outputBufferStream.writeInt(info.versionCode);
                writeSignatureHashArray(outputBufferStream, BackupUtils.hashSignatureArray(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());
            }
        }
        // a backup / uninstall / backup / reinstall sequence.
        if (DEBUG) {
            if (mExisting.size() > 0) {
                StringBuilder sb = new StringBuilder(64);
                sb.append("Preserving metadata for deleted packages:");
                for (String app : mExisting) {
                    sb.append(' ');
                    sb.append(app);
                }
                Slog.v(TAG, sb.toString());
            }
        }
    } 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, home, homeVersion, homeSigHashes, newState);
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) DataOutputStream(java.io.DataOutputStream) PackageInfo(android.content.pm.PackageInfo) ComponentName(android.content.ComponentName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

PackageInfo (android.content.pm.PackageInfo)1567 PackageManager (android.content.pm.PackageManager)703 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