Search in sources :

Example 91 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class ShortcutService method hasShortcutHostPermissionInner.

// This method is extracted so we can directly call this method from unit tests,
// even when hasShortcutPermission() is overridden.
@VisibleForTesting
boolean hasShortcutHostPermissionInner(@NonNull String callingPackage, int userId) {
    synchronized (mLock) {
        throwIfUserLockedL(userId);
        final ShortcutUser user = getUserShortcutsLocked(userId);
        // Always trust the in-memory cache.
        final ComponentName cached = user.getCachedLauncher();
        if (cached != null) {
            if (cached.getPackageName().equals(callingPackage)) {
                return true;
            }
        }
        // If the cached one doesn't match, then go ahead
        final List<ResolveInfo> allHomeCandidates = new ArrayList<>();
        // Default launcher from package manager.
        final long startGetHomeActivitiesAsUser = injectElapsedRealtime();
        final ComponentName defaultLauncher = mPackageManagerInternal.getHomeActivitiesAsUser(allHomeCandidates, userId);
        logDurationStat(Stats.GET_DEFAULT_HOME, startGetHomeActivitiesAsUser);
        ComponentName detected;
        if (defaultLauncher != null) {
            detected = defaultLauncher;
            if (DEBUG) {
                Slog.v(TAG, "Default launcher from PM: " + detected);
            }
        } else {
            detected = user.getLastKnownLauncher();
            if (detected != null) {
                if (injectIsActivityEnabledAndExported(detected, userId)) {
                    if (DEBUG) {
                        Slog.v(TAG, "Cached launcher: " + detected);
                    }
                } else {
                    Slog.w(TAG, "Cached launcher " + detected + " no longer exists");
                    detected = null;
                    user.clearLauncher();
                }
            }
        }
        if (detected == null) {
            // If we reach here, that means it's the first check since the user was created,
            // and there's already multiple launchers and there's no default set.
            // Find the system one with the highest priority.
            // (We need to check the priority too because of FallbackHome in Settings.)
            // If there's no system launcher yet, then no one can access shortcuts, until
            // the user explicitly
            final int size = allHomeCandidates.size();
            int lastPriority = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                final ResolveInfo ri = allHomeCandidates.get(i);
                if (!ri.activityInfo.applicationInfo.isSystemApp()) {
                    continue;
                }
                if (DEBUG) {
                    Slog.d(TAG, String.format("hasShortcutPermissionInner: pkg=%s prio=%d", ri.activityInfo.getComponentName(), ri.priority));
                }
                if (ri.priority < lastPriority) {
                    continue;
                }
                detected = ri.activityInfo.getComponentName();
                lastPriority = ri.priority;
            }
        }
        // Update the cache.
        user.setLauncher(detected);
        if (detected != null) {
            if (DEBUG) {
                Slog.v(TAG, "Detected launcher: " + detected);
            }
            return detected.getPackageName().equals(callingPackage);
        } else {
            // Default launcher not found.
            return false;
        }
    }
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ArrayList(java.util.ArrayList) ComponentName(android.content.ComponentName) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 92 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class ShortcutService method saveBaseStateLocked.

@VisibleForTesting
void saveBaseStateLocked() {
    final AtomicFile file = getBaseStateFile();
    if (DEBUG) {
        Slog.d(TAG, "Saving to " + file.getBaseFile());
    }
    FileOutputStream outs = null;
    try {
        outs = file.startWrite();
        // Write to XML
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(outs, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, TAG_ROOT);
        // Body.
        writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime);
        // Epilogue.
        out.endTag(null, TAG_ROOT);
        out.endDocument();
        // Close.
        file.finishWrite(outs);
    } catch (IOException e) {
        Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
        file.failWrite(outs);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 93 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class ShortcutService method shrinkBitmap.

@VisibleForTesting
static Bitmap shrinkBitmap(Bitmap in, int maxSize) {
    // Original width/height.
    final int ow = in.getWidth();
    final int oh = in.getHeight();
    if ((ow <= maxSize) && (oh <= maxSize)) {
        if (DEBUG) {
            Slog.d(TAG, String.format("Icon size %dx%d, no need to shrink", ow, oh));
        }
        return in;
    }
    final int longerDimension = Math.max(ow, oh);
    // New width and height.
    final int nw = ow * maxSize / longerDimension;
    final int nh = oh * maxSize / longerDimension;
    if (DEBUG) {
        Slog.d(TAG, String.format("Icon size %dx%d, shrinking to %dx%d", ow, oh, nw, nh));
    }
    final Bitmap scaledBitmap = Bitmap.createBitmap(nw, nh, Bitmap.Config.ARGB_8888);
    final Canvas c = new Canvas(scaledBitmap);
    final RectF dst = new RectF(0, 0, nw, nh);
    c.drawBitmap(in, /*src=*/
    null, dst, /* paint =*/
    null);
    return scaledBitmap;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 94 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class ShortcutService method checkPackageChanges.

/**
     * Called when a user is unlocked.
     * - Check all known packages still exist, and otherwise perform cleanup.
     * - If a package still exists, check the version code.  If it's been updated, may need to
     * update timestamps of its shortcuts.
     */
@VisibleForTesting
void checkPackageChanges(@UserIdInt int ownerUserId) {
    if (DEBUG) {
        Slog.d(TAG, "checkPackageChanges() ownerUserId=" + ownerUserId);
    }
    if (injectIsSafeModeEnabled()) {
        Slog.i(TAG, "Safe mode, skipping checkPackageChanges()");
        return;
    }
    final long start = injectElapsedRealtime();
    try {
        final ArrayList<PackageWithUser> gonePackages = new ArrayList<>();
        synchronized (mLock) {
            final ShortcutUser user = getUserShortcutsLocked(ownerUserId);
            // Find packages that have been uninstalled.
            user.forAllPackageItems(spi -> {
                if (spi.getPackageInfo().isShadow()) {
                    return;
                }
                if (!isPackageInstalled(spi.getPackageName(), spi.getPackageUserId())) {
                    if (DEBUG) {
                        Slog.d(TAG, "Uninstalled: " + spi.getPackageName() + " user " + spi.getPackageUserId());
                    }
                    gonePackages.add(PackageWithUser.of(spi));
                }
            });
            if (gonePackages.size() > 0) {
                for (int i = gonePackages.size() - 1; i >= 0; i--) {
                    final PackageWithUser pu = gonePackages.get(i);
                    cleanUpPackageLocked(pu.packageName, ownerUserId, pu.userId, /* appStillExists = */
                    false);
                }
            }
            rescanUpdatedPackagesLocked(ownerUserId, user.getLastAppScanTime(), /* forceRescan=*/
            false);
        }
    } finally {
        logDurationStat(Stats.CHECK_PACKAGE_CHANGES, start);
    }
    verifyStates();
}
Also used : PackageWithUser(com.android.server.pm.ShortcutUser.PackageWithUser) ArrayList(java.util.ArrayList) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 95 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class UserManagerService method writeApplicationRestrictionsLP.

@VisibleForTesting
static void writeApplicationRestrictionsLP(Bundle restrictions, AtomicFile restrictionsFile) {
    FileOutputStream fos = null;
    try {
        fos = restrictionsFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, StandardCharsets.UTF_8.name());
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_RESTRICTIONS);
        writeBundle(restrictions, serializer);
        serializer.endTag(null, TAG_RESTRICTIONS);
        serializer.endDocument();
        restrictionsFile.finishWrite(fos);
    } catch (Exception e) {
        restrictionsFile.failWrite(fos);
        Slog.e(LOG_TAG, "Error writing application restrictions list", e);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ErrnoException(android.system.ErrnoException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.android.internal.annotations.VisibleForTesting)141 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)18 ComponentName (android.content.ComponentName)15 File (java.io.File)14 RemoteException (android.os.RemoteException)13 ArraySet (android.util.ArraySet)10 AtomicFile (android.util.AtomicFile)10 FileInputStream (java.io.FileInputStream)10 Locale (java.util.Locale)10 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)9 FileNotFoundException (java.io.FileNotFoundException)9 Notification (android.app.Notification)6 ErrnoException (android.system.ErrnoException)6 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)6 FileOutputStream (java.io.FileOutputStream)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)6 ITransientNotification (android.app.ITransientNotification)5 UserInfo (android.content.pm.UserInfo)5