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();
}
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;
}
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);
}
}
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();
}
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);
}
Aggregations