From 69ae2a3e98e4783c49bde516210c75037df2a6f5 Mon Sep 17 00:00:00 2001 From: Suphon Thanakornpakapong Date: Mon, 27 Jun 2022 21:04:00 +0700 Subject: [PATCH] Reduce app suggestions flickering --- .../features/launcher/LauncherActivity.java | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java b/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java index a23192abaf..4244d221bd 100755 --- a/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/launcher/LauncherActivity.java @@ -79,7 +79,6 @@ import androidx.viewpager.widget.ViewPager; import com.jakewharton.rxbinding3.widget.RxTextView; - import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -87,8 +86,10 @@ import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import foundation.e.blisslauncher.BlissLauncher; import foundation.e.blisslauncher.BuildConfig; @@ -130,7 +131,6 @@ import foundation.e.blisslauncher.core.utils.AppUtils; import foundation.e.blisslauncher.core.utils.Constants; import foundation.e.blisslauncher.core.utils.DepthManager; import foundation.e.blisslauncher.core.utils.GraphicsUtil; -import foundation.e.blisslauncher.core.utils.ListUtil; import foundation.e.blisslauncher.core.utils.ThemesKt; import foundation.e.blisslauncher.core.utils.UserHandle; import foundation.e.blisslauncher.core.wallpaper.WallpaperManagerCompat; @@ -211,7 +211,7 @@ public class LauncherActivity extends AppCompatActivity implements private boolean forceRefreshSuggestedApps = false; - private List mUsageStats; + private List mSuggestedApps = new ArrayList<>(); private FrameLayout swipeSearchContainer; private InsettableRelativeLayout workspace; private View blurLayer; // Blur layer for folders and search container. @@ -1269,32 +1269,38 @@ public class LauncherActivity extends AppCompatActivity implements GridLayout suggestedAppsGridLayout = viewGroup.findViewById(R.id.suggestedAppGrid); AppUsageStats appUsageStats = new AppUsageStats(this); List usageStats = appUsageStats.getUsageStats(); - if (usageStats.size() > 0) { - openUsageAccessSettingsTv.setVisibility(GONE); - suggestedAppsGridLayout.setVisibility(VISIBLE); - - // Check if usage stats have been changed or not to avoid unnecessary flickering - if (forceRefresh || mUsageStats == null || mUsageStats.size() != usageStats.size() - || !ListUtil.areEqualLists(mUsageStats, usageStats)) { - mUsageStats = usageStats; - if (suggestedAppsGridLayout.getChildCount() > 0) { - suggestedAppsGridLayout.removeAllViews(); - } - int i = 0; - while (suggestedAppsGridLayout.getChildCount() < 4 && i < mUsageStats.size()) { - ApplicationItem appItem = AppUtils.createAppItem(this, - mUsageStats.get(i).getPackageName(), new UserHandle()); - if (appItem != null) { - BlissFrameLayout view = prepareSuggestedApp(appItem); - addAppToGrid(suggestedAppsGridLayout, view); - } - i++; - } - } - } else { + + if (usageStats.size() == 0) { + // no usage stats, show setup prompt openUsageAccessSettingsTv.setVisibility(VISIBLE); suggestedAppsGridLayout.setVisibility(GONE); + mSuggestedApps = new ArrayList<>(); + return; } + + openUsageAccessSettingsTv.setVisibility(GONE); + suggestedAppsGridLayout.setVisibility(VISIBLE); + + List suggestedApps = usageStats.stream() + .map(UsageStats::getPackageName) + .map(packageName -> AppUtils.createAppItem(this, packageName, new UserHandle())) + .filter(Objects::nonNull) + .limit(4) + .collect(Collectors.toList()); + + if (!forceRefresh && suggestedApps.equals(mSuggestedApps)) { + // no changes, skip update + return; + } + + suggestedAppsGridLayout.removeAllViews(); + suggestedApps.stream() + .map(this::prepareSuggestedApp) + .forEach(view -> addAppToGrid(suggestedAppsGridLayout, view)); + mSuggestedApps = suggestedApps; + + // remove force flag + forceRefreshSuggestedApps = false; } /** -- GitLab