From dba7359d233f08df63df4fc520b74e49b7ab7dba Mon Sep 17 00:00:00 2001 From: Brandon Dayauon Date: Thu, 8 Jun 2023 13:52:06 -0700 Subject: [PATCH] Fix custom buttons with translated string The reason why the buttons aren't being updated with the translated string is because the cache the views get the string from is not updated/loaded with the new language. Rather, the update happens after the view has been inflated. With this change, when the string cache updates in bindStringCache(), we update the UI right then. bug: 280958663 bug: 288442609 test: Manual Change-Id: I7a49ee401d5a5f3268cfaef1abee8153e913a8ce (cherry picked from commit 076dcdfd5445536f31c723b055de0064d2772531) --- src/com/android/launcher3/Launcher.java | 1 + .../allapps/ActivityAllAppsContainerView.java | 27 +++++++++++++++++++ .../launcher3/allapps/WorkEduCard.java | 14 ++++++---- .../launcher3/allapps/WorkModeSwitch.java | 12 ++++++--- .../launcher3/allapps/WorkPausedCard.java | 4 +++ .../launcher3/model/BaseLauncherBinder.java | 8 +++--- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 25eb16079f..fed123f1b3 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -3053,6 +3053,7 @@ public class Launcher extends StatefulActivity @Override public void bindStringCache(StringCache cache) { mStringCache = cache; + mAppsView.updateWorkUI(); } @Override diff --git a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java index d4140d851c..421dfd3f74 100644 --- a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java +++ b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java @@ -16,6 +16,8 @@ package com.android.launcher3.allapps; import static com.android.launcher3.allapps.ActivityAllAppsContainerView.AdapterHolder.SEARCH; +import static com.android.launcher3.allapps.BaseAllAppsAdapter.VIEW_TYPE_WORK_DISABLED_CARD; +import static com.android.launcher3.allapps.BaseAllAppsAdapter.VIEW_TYPE_WORK_EDU_CARD; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_COUNT; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_TAP_ON_PERSONAL_TAB; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_TAP_ON_WORK_TAB; @@ -77,6 +79,7 @@ import com.android.launcher3.keyboard.FocusedItemDecorator; import com.android.launcher3.model.StringCache; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.testing.shared.TestProtocol; +import com.android.launcher3.util.Executors; import com.android.launcher3.util.ItemInfoMatcher; import com.android.launcher3.util.Themes; import com.android.launcher3.views.ActivityContext; @@ -1110,6 +1113,30 @@ public class ActivityAllAppsContainerView return view.getGlobalVisibleRect(new Rect()); } + /** Called in Launcher#bindStringCache() to update the UI when cache is updated. */ + public void updateWorkUI() { + setDeviceManagementResources(); + if (mWorkManager.getWorkModeSwitch() != null) { + mWorkManager.getWorkModeSwitch().updateStringFromCache(); + } + inflateWorkCardsIfNeeded(); + } + + private void inflateWorkCardsIfNeeded() { + AllAppsRecyclerView workRV = mAH.get(AdapterHolder.WORK).mRecyclerView; + if (workRV != null) { + for (int i = 0; i < workRV.getChildCount(); i++) { + View currentView = workRV.getChildAt(i); + int currentItemViewType = workRV.getChildViewHolder(currentView).getItemViewType(); + if (currentItemViewType == VIEW_TYPE_WORK_EDU_CARD) { + ((WorkEduCard) currentView).updateStringFromCache(); + } else if (currentItemViewType == VIEW_TYPE_WORK_DISABLED_CARD) { + ((WorkPausedCard) currentView).updateStringFromCache(); + } + } + } + } + @VisibleForTesting public boolean isPersonalTabVisible() { return isDescendantViewVisible(R.id.tab_personal); diff --git a/src/com/android/launcher3/allapps/WorkEduCard.java b/src/com/android/launcher3/allapps/WorkEduCard.java index b4cdc967dc..1059097c5a 100644 --- a/src/com/android/launcher3/allapps/WorkEduCard.java +++ b/src/com/android/launcher3/allapps/WorkEduCard.java @@ -76,11 +76,7 @@ public class WorkEduCard extends FrameLayout implements super.onFinishInflate(); findViewById(R.id.action_btn).setOnClickListener(this); - StringCache cache = mActivityContext.getStringCache(); - if (cache != null) { - TextView title = findViewById(R.id.work_apps_paused_title); - title.setText(cache.workProfileEdu); - } + updateStringFromCache(); } @Override @@ -121,4 +117,12 @@ public class WorkEduCard extends FrameLayout implements public void setPosition(int position) { mPosition = position; } + + public void updateStringFromCache() { + StringCache cache = mActivityContext.getStringCache(); + if (cache != null) { + TextView title = findViewById(R.id.work_apps_paused_title); + title.setText(cache.workProfileEdu); + } + } } diff --git a/src/com/android/launcher3/allapps/WorkModeSwitch.java b/src/com/android/launcher3/allapps/WorkModeSwitch.java index 8c2fb195c6..f84876b52c 100644 --- a/src/com/android/launcher3/allapps/WorkModeSwitch.java +++ b/src/com/android/launcher3/allapps/WorkModeSwitch.java @@ -92,10 +92,7 @@ public class WorkModeSwitch extends LinearLayout implements Insettable, } setInsets(mActivityContext.getDeviceProfile().getInsets()); - StringCache cache = mActivityContext.getStringCache(); - if (cache != null) { - mTextView.setText(cache.workProfilePauseButton); - } + updateStringFromCache(); getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); } @@ -213,4 +210,11 @@ public class WorkModeSwitch extends LinearLayout implements Insettable, public int getScrollThreshold() { return mScrollThreshold; } + + public void updateStringFromCache(){ + StringCache cache = mActivityContext.getStringCache(); + if (cache != null) { + mTextView.setText(cache.workProfilePauseButton); + } + } } diff --git a/src/com/android/launcher3/allapps/WorkPausedCard.java b/src/com/android/launcher3/allapps/WorkPausedCard.java index 26a78039cb..18826673a1 100644 --- a/src/com/android/launcher3/allapps/WorkPausedCard.java +++ b/src/com/android/launcher3/allapps/WorkPausedCard.java @@ -57,6 +57,10 @@ public class WorkPausedCard extends LinearLayout implements View.OnClickListener mBtn = findViewById(R.id.enable_work_apps); mBtn.setOnClickListener(this); + updateStringFromCache(); + } + + public void updateStringFromCache() { StringCache cache = mActivityContext.getStringCache(); if (cache != null) { setWorkProfilePausedResources(cache); diff --git a/src/com/android/launcher3/model/BaseLauncherBinder.java b/src/com/android/launcher3/model/BaseLauncherBinder.java index 5d85b1cf6e..5997637c9d 100644 --- a/src/com/android/launcher3/model/BaseLauncherBinder.java +++ b/src/com/android/launcher3/model/BaseLauncherBinder.java @@ -312,7 +312,8 @@ public abstract class BaseLauncherBinder { currentScreenIds, pendingTasks, workspaceItemCount, isBindSync); }, mUiExecutor); - mCallbacks.bindStringCache(mBgDataModel.stringCache.clone()); + StringCache cacheClone = mBgDataModel.stringCache.clone(); + executeCallbacksTask(c -> c.bindStringCache(cacheClone), pendingExecutor); } private void bindWorkspaceItems( @@ -440,9 +441,8 @@ public abstract class BaseLauncherBinder { .resumeModelPush(FLAG_LOADER_RUNNING); }); - for (Callbacks cb : mCallbacksList) { - cb.bindStringCache(mBgDataModel.stringCache.clone()); - } + StringCache cacheClone = mBgDataModel.stringCache.clone(); + executeCallbacksTask(c -> c.bindStringCache(cacheClone), mUiExecutor); } private void bindWorkspaceItems(final ArrayList workspaceItems) {