Search in sources :

Example 36 with NameNotFoundException

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;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Context(android.content.Context) ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Bundle(android.os.Bundle) HashSet(java.util.HashSet)

Example 37 with NameNotFoundException

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;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) SpannableString(android.text.SpannableString)

Example 38 with NameNotFoundException

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;
}
Also used : IBackupTransport(com.android.internal.backup.IBackupTransport) Message(android.os.Message) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RemoteException(android.os.RemoteException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) ActivityNotFoundException(android.content.ActivityNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException)

Example 39 with NameNotFoundException

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;
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RemoteException(android.os.RemoteException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) ActivityNotFoundException(android.content.ActivityNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException) BufferedInputStream(java.io.BufferedInputStream) HashSet(java.util.HashSet)

Example 40 with NameNotFoundException

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);
}
Also used : IntentFilter(android.content.IntentFilter) RandomAccessFile(java.io.RandomAccessFile) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

Aggregations

NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1012 PackageManager (android.content.pm.PackageManager)358 PackageInfo (android.content.pm.PackageInfo)291 ApplicationInfo (android.content.pm.ApplicationInfo)235 Intent (android.content.Intent)143 ComponentName (android.content.ComponentName)134 ActivityInfo (android.content.pm.ActivityInfo)125 Resources (android.content.res.Resources)112 Context (android.content.Context)105 Drawable (android.graphics.drawable.Drawable)93 Bundle (android.os.Bundle)93 IOException (java.io.IOException)91 UserHandle (android.os.UserHandle)79 ResolveInfo (android.content.pm.ResolveInfo)72 ArrayList (java.util.ArrayList)68 RemoteException (android.os.RemoteException)63 File (java.io.File)57 TextView (android.widget.TextView)52 View (android.view.View)47 FileNotFoundException (java.io.FileNotFoundException)44