Search in sources :

Example 1 with IconCache

use of com.android.launcher3.icons.IconCache in project Launcher3 by chislon.

the class LauncherBackupHelper method backupWidgets.

/**
 * Write all the static widget resources we need to render placeholders
 * for a package that is not installed.
 *
 * @param in notes from last backup
 * @param data output stream for key/value pairs
 * @param out notes about this backup
 * @param keys keys to mark as clean in the notes for next backup
 * @throws IOException
 */
private void backupWidgets(Journal in, BackupDataOutput data, Journal out, ArrayList<Key> keys) throws IOException {
    // persist static widget info that hasn't been persisted yet
    final LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
    if (appState == null) {
        // try again later
        dataChanged();
        if (DEBUG)
            Log.d(TAG, "Launcher is not initialized, delaying widget backup");
        return;
    }
    final ContentResolver cr = mContext.getContentResolver();
    final WidgetPreviewLoader previewLoader = new WidgetPreviewLoader(mContext);
    final PagedViewCellLayout widgetSpacingLayout = new PagedViewCellLayout(mContext);
    final IconCache iconCache = appState.getIconCache();
    final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
    final DeviceProfile profile = appState.getDynamicGrid().getDeviceProfile();
    if (DEBUG)
        Log.d(TAG, "cellWidthPx: " + profile.cellWidthPx);
    // read the old ID set
    Set<String> savedIds = getSavedIdsByType(Key.WIDGET, in);
    if (DEBUG)
        Log.d(TAG, "widgets savedIds.size()=" + savedIds.size());
    int startRows = out.rows;
    if (DEBUG)
        Log.d(TAG, "starting here: " + startRows);
    String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET;
    Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, where, null, null);
    Set<String> currentIds = new HashSet<String>(cursor.getCount());
    try {
        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            final long id = cursor.getLong(ID_INDEX);
            final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX);
            final int spanX = cursor.getInt(SPANX_INDEX);
            final int spanY = cursor.getInt(SPANY_INDEX);
            final ComponentName provider = ComponentName.unflattenFromString(providerName);
            Key key = null;
            String backupKey = null;
            if (provider != null) {
                key = getKey(Key.WIDGET, providerName);
                backupKey = keyToBackupKey(key);
                currentIds.add(backupKey);
            } else {
                Log.w(TAG, "empty intent on appwidget: " + id);
            }
            if (savedIds.contains(backupKey)) {
                if (DEBUG)
                    Log.d(TAG, "already saved widget " + backupKey);
                // remember that we already backed this up previously
                keys.add(key);
            } else if (backupKey != null) {
                if (DEBUG)
                    Log.d(TAG, "I can count this high: " + out.rows);
                if ((out.rows - startRows) < MAX_WIDGETS_PER_PASS) {
                    if (DEBUG)
                        Log.d(TAG, "saving widget " + backupKey);
                    previewLoader.setPreviewSize(spanX * profile.cellWidthPx, spanY * profile.cellHeightPx, widgetSpacingLayout);
                    byte[] blob = packWidget(dpi, previewLoader, iconCache, provider);
                    keys.add(key);
                    writeRowToBackup(key, blob, out, data);
                } else {
                    if (DEBUG)
                        Log.d(TAG, "scheduling another run for widget " + backupKey);
                    // too many widgets for this pass, request another.
                    dataChanged();
                }
            }
        }
    } finally {
        cursor.close();
    }
    if (DEBUG)
        Log.d(TAG, "widget currentIds.size()=" + currentIds.size());
    // these IDs must have been deleted
    savedIds.removeAll(currentIds);
    out.rows += removeDeletedKeysFromBackup(savedIds, data);
}
Also used : Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) ComponentName(android.content.ComponentName) Key(com.android.launcher3.backup.BackupProtos.Key) HashSet(java.util.HashSet)

Example 2 with IconCache

use of com.android.launcher3.icons.IconCache in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsModel method setWidgetsAndShortcuts.

private synchronized void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts, LauncherAppState app, @Nullable PackageUserKey packageUser) {
    if (DEBUG) {
        Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
    }
    // Temporary cache for {@link PackageItemInfos} to avoid having to go through
    // {@link mPackageItemInfos} to locate the key to be used for {@link #mWidgetsList}
    PackageItemInfoCache packageItemInfoCache = new PackageItemInfoCache();
    if (packageUser == null) {
        // Clear the list if this is an update on all widgets and shortcuts.
        mWidgetsList.clear();
    } else {
        // Otherwise, only clear the widgets and shortcuts for the changed package.
        mWidgetsList.remove(packageItemInfoCache.getOrCreate(new WidgetPackageOrCategoryKey(packageUser)));
    }
    // add and update.
    mWidgetsList.putAll(rawWidgetsShortcuts.stream().filter(new WidgetValidityCheck(app)).collect(Collectors.groupingBy(item -> packageItemInfoCache.getOrCreate(getWidgetPackageOrCategoryKey(item)))));
    // Update each package entry
    IconCache iconCache = app.getIconCache();
    for (PackageItemInfo p : packageItemInfoCache.values()) {
        iconCache.getTitleAndIconForApp(p, true);
    }
}
Also used : IconCache(com.android.launcher3.icons.IconCache) PackageItemInfo(com.android.launcher3.model.data.PackageItemInfo)

Example 3 with IconCache

use of com.android.launcher3.icons.IconCache in project android_packages_apps_Launcher3 by crdroidandroid.

the class TaskUtils method getTitle.

/**
 * TODO: remove this once we switch to getting the icon and label from IconCache.
 */
public static CharSequence getTitle(Context context, Task task) {
    UserHandle user = UserHandle.of(task.key.userId);
    ApplicationInfo applicationInfo = new PackageManagerHelper(context).getApplicationInfo(task.getTopComponent().getPackageName(), user, 0);
    if (applicationInfo == null) {
        Log.e(TAG, "Failed to get title for task " + task);
        return "";
    }
    PackageManager packageManager = context.getPackageManager();
    return packageManager.getUserBadgedLabel(applicationInfo.loadLabel(packageManager), user);
}
Also used : PackageManager(android.content.pm.PackageManager) UserHandle(android.os.UserHandle) ApplicationInfo(android.content.pm.ApplicationInfo) PackageManagerHelper(com.android.launcher3.util.PackageManagerHelper)

Example 4 with IconCache

use of com.android.launcher3.icons.IconCache in project android_packages_apps_Launcher3 by crdroidandroid.

the class PopupPopulator method createUpdateRunnable.

/**
 * Returns a runnable to update the provided shortcuts and notifications
 */
public static Runnable createUpdateRunnable(final BaseDraggingActivity launcher, final ItemInfo originalInfo, final Handler uiHandler, final PopupContainerWithArrow container, final List<DeepShortcutView> shortcutViews, final List<NotificationKeyData> notificationKeys) {
    final ComponentName activity = originalInfo.getTargetComponent();
    final UserHandle user = originalInfo.user;
    return () -> {
        if (!notificationKeys.isEmpty()) {
            NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
            final List<NotificationInfo> infos;
            if (notificationListener == null) {
                infos = Collections.emptyList();
            } else {
                infos = notificationListener.getNotificationsForKeys(notificationKeys).stream().map(sbn -> new NotificationInfo(launcher, sbn, originalInfo)).collect(Collectors.toList());
            }
            uiHandler.post(() -> container.applyNotificationInfos(infos));
        }
        List<ShortcutInfo> shortcuts = new ShortcutRequest(launcher, user).withContainer(activity).query(ShortcutRequest.PUBLISHED);
        String shortcutIdToDeDupe = notificationKeys.isEmpty() ? null : notificationKeys.get(0).shortcutId;
        shortcuts = PopupPopulator.sortAndFilterShortcuts(shortcuts, shortcutIdToDeDupe);
        IconCache cache = LauncherAppState.getInstance(launcher).getIconCache();
        for (int i = 0; i < shortcuts.size() && i < shortcutViews.size(); i++) {
            final ShortcutInfo shortcut = shortcuts.get(i);
            final WorkspaceItemInfo si = new WorkspaceItemInfo(shortcut, launcher);
            cache.getUnbadgedShortcutIcon(si, shortcut);
            si.rank = i;
            si.container = CONTAINER_SHORTCUTS;
            final DeepShortcutView view = shortcutViews.get(i);
            uiHandler.post(() -> view.applyShortcutInfo(si, shortcut, container));
        }
    };
}
Also used : Iterator(java.util.Iterator) ComponentName(android.content.ComponentName) ShortcutInfo(android.content.pm.ShortcutInfo) CONTAINER_SHORTCUTS(com.android.launcher3.LauncherSettings.Favorites.CONTAINER_SHORTCUTS) LauncherAppState(com.android.launcher3.LauncherAppState) ItemInfo(com.android.launcher3.model.data.ItemInfo) DeepShortcutView(com.android.launcher3.shortcuts.DeepShortcutView) BaseDraggingActivity(com.android.launcher3.BaseDraggingActivity) IconCache(com.android.launcher3.icons.IconCache) Collectors(java.util.stream.Collectors) NotificationInfo(com.android.launcher3.notification.NotificationInfo) NotificationKeyData(com.android.launcher3.notification.NotificationKeyData) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(androidx.annotation.Nullable) NotificationListener(com.android.launcher3.notification.NotificationListener) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) Handler(android.os.Handler) UserHandle(android.os.UserHandle) ShortcutRequest(com.android.launcher3.shortcuts.ShortcutRequest) Comparator(java.util.Comparator) VisibleForTesting(androidx.annotation.VisibleForTesting) Collections(java.util.Collections) NotificationInfo(com.android.launcher3.notification.NotificationInfo) ShortcutInfo(android.content.pm.ShortcutInfo) UserHandle(android.os.UserHandle) IconCache(com.android.launcher3.icons.IconCache) ComponentName(android.content.ComponentName) ArrayList(java.util.ArrayList) List(java.util.List) ShortcutRequest(com.android.launcher3.shortcuts.ShortcutRequest) DeepShortcutView(com.android.launcher3.shortcuts.DeepShortcutView) NotificationListener(com.android.launcher3.notification.NotificationListener) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo)

Example 5 with IconCache

use of com.android.launcher3.icons.IconCache in project android_packages_apps_Launcher3 by crdroidandroid.

the class CacheDataUpdatedTaskTest method setup.

@Before
public void setup() throws Exception {
    mModelHelper = new LauncherModelHelper();
    mModelHelper.initializeData("/cache_data_updated_task_data.txt");
    // Add placeholder entries in the cache to simulate update
    Context context = RuntimeEnvironment.application;
    IconCache iconCache = LauncherAppState.getInstance(context).getIconCache();
    CachingLogic<ItemInfo> placeholderLogic = new CachingLogic<ItemInfo>() {

        @Override
        public ComponentName getComponent(ItemInfo info) {
            return info.getTargetComponent();
        }

        @Override
        public UserHandle getUser(ItemInfo info) {
            return info.user;
        }

        @Override
        public CharSequence getLabel(ItemInfo info) {
            return NEW_LABEL_PREFIX + info.id;
        }

        @NonNull
        @Override
        public BitmapInfo loadIcon(Context context, ItemInfo info) {
            return BitmapInfo.of(Bitmap.createBitmap(1, 1, Config.ARGB_8888), Color.RED);
        }
    };
    UserManager um = context.getSystemService(UserManager.class);
    for (ItemInfo info : mModelHelper.getBgDataModel().itemsIdMap) {
        iconCache.addIconToDBAndMemCache(info, placeholderLogic, new PackageInfo(), um.getSerialNumberForUser(info.user), true);
    }
}
Also used : Context(android.content.Context) ItemInfo(com.android.launcher3.model.data.ItemInfo) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) UserManager(android.os.UserManager) PackageInfo(android.content.pm.PackageInfo) LauncherModelHelper(com.android.launcher3.util.LauncherModelHelper) IconCache(com.android.launcher3.icons.IconCache) CachingLogic(com.android.launcher3.icons.cache.CachingLogic) Before(org.junit.Before)

Aggregations

ComponentName (android.content.ComponentName)5 IconCache (com.android.launcher3.icons.IconCache)5 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)5 Context (android.content.Context)3 List (java.util.List)3 ContentResolver (android.content.ContentResolver)2 Intent (android.content.Intent)2 ShortcutInfo (android.content.pm.ShortcutInfo)2 Cursor (android.database.Cursor)2 Bitmap (android.graphics.Bitmap)2 Drawable (android.graphics.drawable.Drawable)2 UserHandle (android.os.UserHandle)2 ItemInfo (com.android.launcher3.model.data.ItemInfo)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Animator (android.animation.Animator)1 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 AnimatorSet (android.animation.AnimatorSet)1 ObjectAnimator (android.animation.ObjectAnimator)1