Search in sources :

Example 41 with PackageInfo

use of android.content.pm.PackageInfo 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 42 with PackageInfo

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

the class BackupManagerService method clearApplicationDataSynchronous.

// clear an application's data, blocking until the operation completes or times out
void clearApplicationDataSynchronous(String packageName) {
    // Don't wipe packages marked allowClearUserData=false
    try {
        PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
        if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
            if (MORE_DEBUG)
                Slog.i(TAG, "allowClearUserData=false so not wiping " + packageName);
            return;
        }
    } catch (NameNotFoundException e) {
        Slog.w(TAG, "Tried to clear data for " + packageName + " but not found");
        return;
    }
    ClearDataObserver observer = new ClearDataObserver();
    synchronized (mClearDataLock) {
        mClearingData = true;
        try {
            mActivityManager.clearApplicationUserData(packageName, observer, 0);
        } catch (RemoteException e) {
        // can't happen because the activity manager is in this process
        }
        // only wait 10 seconds for the clear data to happen
        long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
        while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
            try {
                mClearDataLock.wait(5000);
            } catch (InterruptedException e) {
                // won't happen, but still.
                mClearingData = false;
            }
        }
    }
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) RemoteException(android.os.RemoteException)

Example 43 with PackageInfo

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

the class ActivityStackSupervisor method getActionRestrictionForCallingPackage.

private int getActionRestrictionForCallingPackage(String action, String callingPackage, int callingPid, int callingUid) {
    if (action == null) {
        return ACTIVITY_RESTRICTION_NONE;
    }
    String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
    if (permission == null) {
        return ACTIVITY_RESTRICTION_NONE;
    }
    final PackageInfo packageInfo;
    try {
        packageInfo = mService.mContext.getPackageManager().getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
    } catch (PackageManager.NameNotFoundException e) {
        Slog.i(TAG, "Cannot find package info for " + callingPackage);
        return ACTIVITY_RESTRICTION_NONE;
    }
    if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
        return ACTIVITY_RESTRICTION_NONE;
    }
    if (mService.checkPermission(permission, callingPid, callingUid) == PackageManager.PERMISSION_DENIED) {
        return ACTIVITY_RESTRICTION_PERMISSION;
    }
    final int opCode = AppOpsManager.permissionToOpCode(permission);
    if (opCode == AppOpsManager.OP_NONE) {
        return ACTIVITY_RESTRICTION_NONE;
    }
    if (mService.mAppOpsService.noteOperation(opCode, callingUid, callingPackage) != AppOpsManager.MODE_ALLOWED) {
        return ACTIVITY_RESTRICTION_APPOP;
    }
    return ACTIVITY_RESTRICTION_NONE;
}
Also used : PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo)

Example 44 with PackageInfo

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

the class PrinterInfo method loadIcon.

/**
     * Get the icon to be used for this printer. If no per printer icon is available, the printer's
     * service's icon is returned. If the printer has a custom icon this icon might get requested
     * asynchronously. Once the icon is loaded the discovery sessions will be notified that the
     * printer changed.
     *
     * @param context The context that will be using the icons
     * @return The icon to be used for the printer or null if no icon could be found.
     * @hide
     */
@TestApi
@Nullable
public Drawable loadIcon(@NonNull Context context) {
    Drawable drawable = null;
    PackageManager packageManager = context.getPackageManager();
    if (mHasCustomPrinterIcon) {
        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
        Icon icon = printManager.getCustomPrinterIcon(mId);
        if (icon != null) {
            drawable = icon.loadDrawable(context);
        }
    }
    if (drawable == null) {
        try {
            String packageName = mId.getServiceName().getPackageName();
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            ApplicationInfo appInfo = packageInfo.applicationInfo;
            // If no custom icon is available, try the icon from the resources
            if (mIconResourceId != 0) {
                drawable = packageManager.getDrawable(packageName, mIconResourceId, appInfo);
            }
            // Fall back to the printer's service's icon if no per printer icon could be found
            if (drawable == null) {
                drawable = appInfo.loadIcon(packageManager);
            }
        } catch (NameNotFoundException e) {
        }
    }
    return drawable;
}
Also used : PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Drawable(android.graphics.drawable.Drawable) ApplicationInfo(android.content.pm.ApplicationInfo) Icon(android.graphics.drawable.Icon) TestApi(android.annotation.TestApi) Nullable(android.annotation.Nullable)

Example 45 with PackageInfo

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

the class WebViewFactory method getWebViewContextAndSetProvider.

private static Context getWebViewContextAndSetProvider() {
    Application initialApplication = AppGlobals.getInitialApplication();
    try {
        WebViewProviderResponse response = null;
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewUpdateService.waitForAndGetProvider()");
        try {
            response = getUpdateService().waitForAndGetProvider();
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        if (response.status != LIBLOAD_SUCCESS && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) {
            throw new MissingWebViewPackageException("Failed to load WebView provider: " + getWebViewPreparationErrorReason(response.status));
        }
        // Register to be killed before fetching package info - so that we will be
        // killed if the package info goes out-of-date.
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "ActivityManager.addPackageDependency()");
        try {
            ActivityManagerNative.getDefault().addPackageDependency(response.packageInfo.packageName);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        // Fetch package info and verify it against the chosen package
        PackageInfo newPackageInfo = null;
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()");
        try {
            newPackageInfo = initialApplication.getPackageManager().getPackageInfo(response.packageInfo.packageName, PackageManager.GET_SHARED_LIBRARY_FILES | PackageManager.MATCH_DEBUG_TRIAGED_MISSING | // installed for the current user
            PackageManager.MATCH_UNINSTALLED_PACKAGES | // Fetch signatures for verification
            PackageManager.GET_SIGNATURES | // Get meta-data for meta data flag verification
            PackageManager.GET_META_DATA);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        // Validate the newly fetched package info, throws MissingWebViewPackageException on
        // failure
        verifyPackageInfo(response.packageInfo, newPackageInfo);
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "initialApplication.createApplicationContext");
        try {
            // Construct an app context to load the Java code into the current app.
            Context webViewContext = initialApplication.createApplicationContext(newPackageInfo.applicationInfo, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
            sPackageInfo = newPackageInfo;
            return webViewContext;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
    } catch (RemoteException | PackageManager.NameNotFoundException e) {
        throw new MissingWebViewPackageException("Failed to load WebView provider: " + e);
    }
}
Also used : Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) RemoteException(android.os.RemoteException) Application(android.app.Application)

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