use of android.content.pm.PackageManager.NameNotFoundException in project XobotOS by xamarin.
the class PreferenceManager method inflateFromIntent.
/**
* Inflates a preference hierarchy from the preference hierarchies of
* {@link Activity Activities} that match the given {@link Intent}. An
* {@link Activity} defines its preference hierarchy with meta-data using
* the {@link #METADATA_KEY_PREFERENCES} key.
* <p>
* If a preference hierarchy is given, the new preference hierarchies will
* be merged in.
*
* @param queryIntent The intent to match activities.
* @param rootPreferences Optional existing hierarchy to merge the new
* hierarchies into.
* @return The root hierarchy (if one was not provided, the new hierarchy's
* root).
*/
PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
final HashSet<String> inflatedRes = new HashSet<String>();
for (int i = activities.size() - 1; i >= 0; i--) {
final ActivityInfo activityInfo = activities.get(i).activityInfo;
final Bundle metaData = activityInfo.metaData;
if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
continue;
}
// Need to concat the package with res ID since the same res ID
// can be re-used across contexts
final String uniqueResId = activityInfo.packageName + ":" + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);
if (!inflatedRes.contains(uniqueResId)) {
inflatedRes.add(uniqueResId);
final Context context;
try {
context = mContext.createPackageContext(activityInfo.packageName, 0);
} catch (NameNotFoundException e) {
Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": " + Log.getStackTraceString(e));
continue;
}
final PreferenceInflater inflater = new PreferenceInflater(context, this);
final XmlResourceParser parser = activityInfo.loadXmlMetaData(context.getPackageManager(), METADATA_KEY_PREFERENCES);
rootPreferences = (PreferenceScreen) inflater.inflate(parser, rootPreferences, true);
parser.close();
}
}
rootPreferences.onAttachedToHierarchy(this);
return rootPreferences;
}
use of android.content.pm.PackageManager.NameNotFoundException in project XobotOS by xamarin.
the class SuggestionsAdapter method getActivityIcon.
/**
* Gets the activity or application icon for an activity.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the acitivy or the application
* have an icon set.
*/
private Drawable getActivityIcon(ComponentName component) {
PackageManager pm = mContext.getPackageManager();
final ActivityInfo activityInfo;
try {
activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
} catch (NameNotFoundException ex) {
Log.w(LOG_TAG, ex.toString());
return null;
}
int iconId = activityInfo.getIconResource();
if (iconId == 0)
return null;
String pkg = component.getPackageName();
Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
if (drawable == null) {
Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " + component.flattenToShortString());
return null;
}
return drawable;
}
use of android.content.pm.PackageManager.NameNotFoundException in project android_frameworks_base by ResurrectionRemix.
the class BackupManagerService method requestBackup.
public int requestBackup(String[] packages, IBackupObserver observer) {
mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "requestBackup");
if (packages == null || packages.length < 1) {
Slog.e(TAG, "No packages named for backup request");
sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
throw new IllegalArgumentException("No packages are provided for backup");
}
IBackupTransport transport = getTransport(mCurrentTransport);
if (transport == null) {
sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
return BackupManager.ERROR_TRANSPORT_ABORTED;
}
ArrayList<String> fullBackupList = new ArrayList<>();
ArrayList<String> kvBackupList = new ArrayList<>();
for (String packageName : packages) {
try {
PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
if (!appIsEligibleForBackup(packageInfo.applicationInfo)) {
sendBackupOnPackageResult(observer, packageName, BackupManager.ERROR_BACKUP_NOT_ALLOWED);
continue;
}
if (appGetsFullBackup(packageInfo)) {
fullBackupList.add(packageInfo.packageName);
} else {
kvBackupList.add(packageInfo.packageName);
}
} catch (NameNotFoundException e) {
sendBackupOnPackageResult(observer, packageName, BackupManager.ERROR_PACKAGE_NOT_FOUND);
}
}
EventLog.writeEvent(EventLogTags.BACKUP_REQUESTED, packages.length, kvBackupList.size(), fullBackupList.size());
if (MORE_DEBUG) {
Slog.i(TAG, "Backup requested for " + packages.length + " packages, of them: " + fullBackupList.size() + " full backups, " + kvBackupList.size() + " k/v backups");
}
String dirName;
try {
dirName = transport.transportDirName();
} catch (Exception e) {
Slog.e(TAG, "Transport unavailable while attempting backup: " + e.getMessage());
sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
return BackupManager.ERROR_TRANSPORT_ABORTED;
}
Message msg = mBackupHandler.obtainMessage(MSG_REQUEST_BACKUP);
msg.obj = new BackupParams(transport, dirName, kvBackupList, fullBackupList, observer, true);
mBackupHandler.sendMessage(msg);
return BackupManager.SUCCESS;
}
use of android.content.pm.PackageManager.NameNotFoundException in project android_frameworks_base by ResurrectionRemix.
the class BackupManagerService method readFullBackupSchedule.
private ArrayList<FullBackupEntry> readFullBackupSchedule() {
boolean changed = false;
ArrayList<FullBackupEntry> schedule = null;
List<PackageInfo> apps = PackageManagerBackupAgent.getStorableApplications(mPackageManager);
if (mFullBackupScheduleFile.exists()) {
FileInputStream fstream = null;
BufferedInputStream bufStream = null;
DataInputStream in = null;
try {
fstream = new FileInputStream(mFullBackupScheduleFile);
bufStream = new BufferedInputStream(fstream);
in = new DataInputStream(bufStream);
int version = in.readInt();
if (version != SCHEDULE_FILE_VERSION) {
Slog.e(TAG, "Unknown backup schedule version " + version);
return null;
}
final int N = in.readInt();
schedule = new ArrayList<FullBackupEntry>(N);
// HashSet instead of ArraySet specifically because we want the eventual
// lookups against O(hundreds) of entries to be as fast as possible, and
// we discard the set immediately after the scan so the extra memory
// overhead is transient.
HashSet<String> foundApps = new HashSet<String>(N);
for (int i = 0; i < N; i++) {
String pkgName = in.readUTF();
long lastBackup = in.readLong();
// all apps that we've addressed already
foundApps.add(pkgName);
try {
PackageInfo pkg = mPackageManager.getPackageInfo(pkgName, 0);
if (appGetsFullBackup(pkg) && appIsEligibleForBackup(pkg.applicationInfo)) {
schedule.add(new FullBackupEntry(pkgName, lastBackup));
} else {
if (DEBUG) {
Slog.i(TAG, "Package " + pkgName + " no longer eligible for full backup");
}
}
} catch (NameNotFoundException e) {
if (DEBUG) {
Slog.i(TAG, "Package " + pkgName + " not installed; dropping from full backup");
}
}
}
// scan to make sure that we're tracking all full-backup candidates properly
for (PackageInfo app : apps) {
if (appGetsFullBackup(app) && appIsEligibleForBackup(app.applicationInfo)) {
if (!foundApps.contains(app.packageName)) {
if (MORE_DEBUG) {
Slog.i(TAG, "New full backup app " + app.packageName + " found");
}
schedule.add(new FullBackupEntry(app.packageName, 0));
changed = true;
}
}
}
Collections.sort(schedule);
} catch (Exception e) {
Slog.e(TAG, "Unable to read backup schedule", e);
mFullBackupScheduleFile.delete();
schedule = null;
} finally {
IoUtils.closeQuietly(in);
IoUtils.closeQuietly(bufStream);
IoUtils.closeQuietly(fstream);
}
}
if (schedule == null) {
// no prior queue record, or unable to read it. Set up the queue
// from scratch.
changed = true;
schedule = new ArrayList<FullBackupEntry>(apps.size());
for (PackageInfo info : apps) {
if (appGetsFullBackup(info) && appIsEligibleForBackup(info.applicationInfo)) {
schedule.add(new FullBackupEntry(info.packageName, 0));
}
}
}
if (changed) {
writeFullBackupScheduleAsync();
}
return schedule;
}
use of android.content.pm.PackageManager.NameNotFoundException in project android_frameworks_base by ResurrectionRemix.
the class BackupManagerService method initPackageTracking.
private void initPackageTracking() {
if (MORE_DEBUG)
Slog.v(TAG, "` tracking");
// Remember our ancestral dataset
mTokenFile = new File(mBaseStateDir, "ancestral");
try {
RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r");
int version = tf.readInt();
if (version == CURRENT_ANCESTRAL_RECORD_VERSION) {
mAncestralToken = tf.readLong();
mCurrentToken = tf.readLong();
int numPackages = tf.readInt();
if (numPackages >= 0) {
mAncestralPackages = new HashSet<String>();
for (int i = 0; i < numPackages; i++) {
String pkgName = tf.readUTF();
mAncestralPackages.add(pkgName);
}
}
}
tf.close();
} catch (FileNotFoundException fnf) {
// Probably innocuous
Slog.v(TAG, "No ancestral data");
} catch (IOException e) {
Slog.w(TAG, "Unable to read token file", e);
}
// Keep a log of what apps we've ever backed up. Because we might have
// rebooted in the middle of an operation that was removing something from
// this log, we sanity-check its contents here and reconstruct it.
mEverStored = new File(mBaseStateDir, "processed");
File tempProcessedFile = new File(mBaseStateDir, "processed.new");
// Ignore it -- we'll validate "processed" against the current package set.
if (tempProcessedFile.exists()) {
tempProcessedFile.delete();
}
// file to continue the recordkeeping.
if (mEverStored.exists()) {
RandomAccessFile temp = null;
RandomAccessFile in = null;
try {
temp = new RandomAccessFile(tempProcessedFile, "rws");
in = new RandomAccessFile(mEverStored, "r");
// Loop until we hit EOF
while (true) {
String pkg = in.readUTF();
try {
// is this package still present?
mPackageManager.getPackageInfo(pkg, 0);
// if we get here then yes it is; remember it
mEverStoredApps.add(pkg);
temp.writeUTF(pkg);
if (MORE_DEBUG)
Slog.v(TAG, " + " + pkg);
} catch (NameNotFoundException e) {
// nope, this package was uninstalled; don't include it
if (MORE_DEBUG)
Slog.v(TAG, " - " + pkg);
}
}
} catch (EOFException e) {
// old one with the new one then reopen the file for continuing use.
if (!tempProcessedFile.renameTo(mEverStored)) {
Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored);
}
} catch (IOException e) {
Slog.e(TAG, "Error in processed file", e);
} finally {
try {
if (temp != null)
temp.close();
} catch (IOException e) {
}
try {
if (in != null)
in.close();
} catch (IOException e) {
}
}
}
synchronized (mQueueLock) {
// Resume the full-data backup queue
mFullBackupQueue = readFullBackupSchedule();
}
// Register for broadcasts about package install, etc., so we can
// update the provider list.
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
mContext.registerReceiver(mBroadcastReceiver, filter);
// Register for events related to sdcard installation.
IntentFilter sdFilter = new IntentFilter();
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
mContext.registerReceiver(mBroadcastReceiver, sdFilter);
}
Aggregations