updates = new ArrayList<>();
- for (ShortcutInfo info : sm.getPinnedShortcuts()) {
- if (!info.getId().startsWith(SHORTCUT_ID_PREFIX)) {
- continue;
- }
- ComponentName cn = ComponentName.unflattenFromString(
- info.getId().substring(SHORTCUT_ID_PREFIX.length()));
- ResolveInfo ri = pm.resolveActivity(new Intent(SHORTCUT_PROBE).setComponent(cn), 0);
- if (ri == null) {
- continue;
- }
- updates.add(new ShortcutInfo.Builder(mContext, info.getId())
- .setShortLabel(ri.loadLabel(pm)).build());
- }
- if (!updates.isEmpty()) {
- sm.updateShortcuts(updates);
- }
- return null;
- }
-}
diff --git a/src/com/android/settings/shortcut/ShortcutsUpdater.java b/src/com/android/settings/shortcut/ShortcutsUpdater.java
new file mode 100644
index 00000000000..90a60fda379
--- /dev/null
+++ b/src/com/android/settings/shortcut/ShortcutsUpdater.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.shortcut;
+
+import static com.android.settings.shortcut.Shortcuts.SHORTCUT_ID_PREFIX;
+import static com.android.settings.shortcut.Shortcuts.SHORTCUT_PROBE;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import android.app.Flags;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.pm.ShortcutInfo;
+import android.content.pm.ShortcutManager;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.android.settings.Settings;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ShortcutsUpdater {
+
+ /**
+ * Update label, icon, and intent of pinned shortcuts to Settings subpages.
+ *
+ * Should be called whenever any of those could have changed, such as after changing locale,
+ * restoring a backup from a different device, or when flags controlling available features
+ * may have flipped.
+ */
+ public static void updatePinnedShortcuts(Context context) {
+ ShortcutManager sm = checkNotNull(context.getSystemService(ShortcutManager.class));
+
+ List updates = new ArrayList<>();
+ for (ShortcutInfo info : sm.getPinnedShortcuts()) {
+ ResolveInfo resolvedActivity = resolveActivity(context, info);
+ if (resolvedActivity != null) {
+ // Id is preserved to update an existing shortcut, but the activity it opens might
+ // be different, according to maybeGetReplacingComponent.
+ updates.add(Shortcuts.createShortcutInfo(context, info.getId(), resolvedActivity));
+ }
+ }
+ if (!updates.isEmpty()) {
+ sm.updateShortcuts(updates);
+ }
+ }
+
+ @Nullable
+ private static ResolveInfo resolveActivity(Context context, ShortcutInfo shortcut) {
+ if (!shortcut.getId().startsWith(SHORTCUT_ID_PREFIX)) {
+ return null;
+ }
+
+ ComponentName cn = ComponentName.unflattenFromString(
+ shortcut.getId().substring(SHORTCUT_ID_PREFIX.length()));
+ if (cn == null) {
+ return null;
+ }
+
+ // Check if the componentName is obsolete and has been replaced by a different one.
+ cn = maybeGetReplacingComponent(context, cn);
+ PackageManager pm = context.getPackageManager();
+ return pm.resolveActivity(new Intent(SHORTCUT_PROBE).setComponent(cn), 0);
+ }
+
+ @NonNull
+ private static ComponentName maybeGetReplacingComponent(Context context, ComponentName cn) {
+ // ZenModeSettingsActivity is replaced by ModesSettingsActivity and will be deleted
+ // soon (so we shouldn't use ZenModeSettingsActivity.class).
+ if (Flags.modesApi() && Flags.modesUi()
+ && cn.getClassName().endsWith("Settings$ZenModeSettingsActivity")) {
+ return new ComponentName(context, Settings.ModesSettingsActivity.class);
+ }
+
+ return cn;
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java
index 9727dd13848..8442a37873b 100644
--- a/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/shortcut/CreateShortcutPreferenceControllerTest.java
@@ -16,7 +16,7 @@
package com.android.settings.shortcut;
-import static com.android.settings.shortcut.CreateShortcutPreferenceController.SHORTCUT_ID_PREFIX;
+import static com.android.settings.shortcut.Shortcuts.SHORTCUT_ID_PREFIX;
import static com.google.common.truth.Truth.assertThat;
@@ -101,10 +101,10 @@ public class CreateShortcutPreferenceControllerTest {
when(mShortcutManager.createShortcutResultIntent(any(ShortcutInfo.class)))
.thenReturn(new Intent().putExtra("d1", "d2"));
- final Intent intent = new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE)
+ final Intent intent = new Intent(Shortcuts.SHORTCUT_PROBE)
.setClass(mContext, Settings.ManageApplicationsActivity.class);
final ResolveInfo ri = mContext.getPackageManager().resolveActivity(intent, 0);
- final Intent result = mController.createResultIntent(intent, ri, "mock");
+ final Intent result = mController.createResultIntent(ri);
assertThat(result.getStringExtra("d1")).isEqualTo("d2");
assertThat((Object) result.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT)).isNotNull();
@@ -131,7 +131,7 @@ public class CreateShortcutPreferenceControllerTest {
ri2.activityInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
mPackageManager.setResolveInfosForIntent(
- new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE),
+ new Intent(Shortcuts.SHORTCUT_PROBE),
Arrays.asList(ri1, ri2));
doReturn(false).when(mController).canShowWifiHotspot();
@@ -158,7 +158,7 @@ public class CreateShortcutPreferenceControllerTest {
ri2.activityInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
mPackageManager.setResolveInfosForIntent(
- new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE),
+ new Intent(Shortcuts.SHORTCUT_PROBE),
Arrays.asList(ri1, ri2));
doReturn(false).when(mController).canShowWifiHotspot();
@@ -276,7 +276,7 @@ public class CreateShortcutPreferenceControllerTest {
ri.activityInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
mPackageManager.setResolveInfosForIntent(
- new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE),
+ new Intent(Shortcuts.SHORTCUT_PROBE),
Arrays.asList(ri));
}
}
diff --git a/tests/robotests/src/com/android/settings/shortcut/ShortcutsTest.java b/tests/robotests/src/com/android/settings/shortcut/ShortcutsTest.java
new file mode 100644
index 00000000000..a347ff9e0db
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/shortcut/ShortcutsTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.shortcut;
+
+import static com.android.settings.shortcut.Shortcuts.SHORTCUT_PROBE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ResolveInfo;
+import android.content.pm.ShortcutInfo;
+
+import com.android.settings.Settings;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class ShortcutsTest {
+
+ private Context mContext;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.getApplication();
+ }
+
+ @Test
+ public void shortcutsUpdateTask() {
+ final Intent intent = new Intent(SHORTCUT_PROBE)
+ .setClass(mContext, Settings.ManageApplicationsActivity.class);
+ final ResolveInfo ri = mContext.getPackageManager().resolveActivity(intent, 0);
+ assertThat(ri).isNotNull();
+
+ ShortcutInfo shortcut = Shortcuts.createShortcutInfo(mContext, ri);
+
+ assertThat(shortcut.getLabel()).isNotNull();
+ assertThat(shortcut.getLabel().toString()).isEqualTo("App info");
+
+ assertThat(shortcut.getIntent()).isNotNull();
+ assertThat(shortcut.getIntent().getAction()).isEqualTo(Intent.ACTION_MAIN);
+ assertThat(shortcut.getIntent().getCategories()).contains("com.android.settings.SHORTCUT");
+ assertThat(shortcut.getIntent().getComponent()).isEqualTo(
+ new ComponentName(mContext, Settings.ManageApplicationsActivity.class));
+ assertThat(shortcut.getIcon()).isNotNull();
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/shortcut/ShortcutsUpdateTaskTest.java b/tests/robotests/src/com/android/settings/shortcut/ShortcutsUpdaterTest.java
similarity index 52%
rename from tests/robotests/src/com/android/settings/shortcut/ShortcutsUpdateTaskTest.java
rename to tests/robotests/src/com/android/settings/shortcut/ShortcutsUpdaterTest.java
index 8352e7a9634..5324ff50f43 100644
--- a/tests/robotests/src/com/android/settings/shortcut/ShortcutsUpdateTaskTest.java
+++ b/tests/robotests/src/com/android/settings/shortcut/ShortcutsUpdaterTest.java
@@ -16,30 +16,30 @@
package com.android.settings.shortcut;
-import static com.android.settings.shortcut.CreateShortcutPreferenceController.SHORTCUT_ID_PREFIX;
+import static com.android.settings.shortcut.Shortcuts.SHORTCUT_ID_PREFIX;
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import android.app.Flags;
import android.content.ComponentName;
import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
+import android.platform.test.annotations.DisableFlags;
+import android.platform.test.annotations.EnableFlags;
+import android.platform.test.flag.junit.SetFlagsRule;
import com.android.settings.Settings;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -48,17 +48,17 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
-import org.robolectric.shadow.api.Shadow;
-import org.robolectric.shadows.ShadowPackageManager;
import java.util.Arrays;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
-public class ShortcutsUpdateTaskTest {
+public class ShortcutsUpdaterTest {
private Context mContext;
- private ShadowPackageManager mPackageManager;
+
+ @Rule
+ public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
@Mock
private ShortcutManager mShortcutManager;
@@ -68,29 +68,12 @@ public class ShortcutsUpdateTaskTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
- mContext = RuntimeEnvironment.application;
- mPackageManager = Shadow.extract(mContext.getPackageManager());
+ mContext = spy(RuntimeEnvironment.application);
+ doReturn(mShortcutManager).when(mContext).getSystemService(eq(Context.SHORTCUT_SERVICE));
}
@Test
- public void shortcutsUpdateTask() {
- mContext = spy(RuntimeEnvironment.application);
- doReturn(mShortcutManager).when(mContext).getSystemService(eq(Context.SHORTCUT_SERVICE));
- final Intent shortcut1 = new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE)
- .setComponent(new ComponentName(
- mContext, Settings.ManageApplicationsActivity.class));
- final ResolveInfo ri1 = mock(ResolveInfo.class);
- ri1.nonLocalizedLabel = "label1";
-
- final Intent shortcut2 = new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE)
- .setComponent(new ComponentName(
- mContext, Settings.SoundSettingsActivity.class));
- final ResolveInfo ri2 = mock(ResolveInfo.class);
- ri2.nonLocalizedLabel = "label2";
-
- mPackageManager.addResolveInfoForIntent(shortcut1, ri1);
- mPackageManager.addResolveInfoForIntent(shortcut2, ri2);
-
+ public void updatePinnedShortcuts_updatesAllShortcuts() {
final List pinnedShortcuts = Arrays.asList(
makeShortcut("d1"),
makeShortcut("d2"),
@@ -99,7 +82,7 @@ public class ShortcutsUpdateTaskTest {
makeShortcut(Settings.SoundSettingsActivity.class));
when(mShortcutManager.getPinnedShortcuts()).thenReturn(pinnedShortcuts);
- new ShortcutsUpdateTask(mContext).doInBackground();
+ ShortcutsUpdater.updatePinnedShortcuts(mContext);
verify(mShortcutManager, times(1)).updateShortcuts(mListCaptor.capture());
@@ -108,6 +91,52 @@ public class ShortcutsUpdateTaskTest {
assertThat(updates).hasSize(2);
assertThat(pinnedShortcuts.get(2).getId()).isEqualTo(updates.get(0).getId());
assertThat(pinnedShortcuts.get(4).getId()).isEqualTo(updates.get(1).getId());
+ assertThat(updates.get(0).getShortLabel().toString()).isEqualTo("App info");
+ assertThat(updates.get(1).getShortLabel().toString()).isEqualTo("Sound & vibration");
+ }
+
+ @Test
+ @EnableFlags(Flags.FLAG_MODES_UI)
+ public void updatePinnedShortcuts_withModesFlag_replacesDndByModes() {
+ List shortcuts = List.of(
+ makeShortcut(Settings.ZenModeSettingsActivity.class));
+ when(mShortcutManager.getPinnedShortcuts()).thenReturn(shortcuts);
+
+ ShortcutsUpdater.updatePinnedShortcuts(mContext);
+
+ verify(mShortcutManager, times(1)).updateShortcuts(mListCaptor.capture());
+ final List updates = mListCaptor.getValue();
+ assertThat(updates).hasSize(1);
+
+ // Id hasn't changed, but intent and label has.
+ ComponentName zenCn = new ComponentName(mContext, Settings.ZenModeSettingsActivity.class);
+ ComponentName modesCn = new ComponentName(mContext, Settings.ModesSettingsActivity.class);
+ assertThat(updates.get(0).getId()).isEqualTo(
+ SHORTCUT_ID_PREFIX + zenCn.flattenToShortString());
+ assertThat(updates.get(0).getIntent().getComponent()).isEqualTo(modesCn);
+ assertThat(updates.get(0).getShortLabel().toString()).isEqualTo("Modes");
+ }
+
+ @Test
+ @DisableFlags(Flags.FLAG_MODES_UI)
+ public void updatePinnedShortcuts_withoutModesFlag_leavesDndAlone() {
+ List shortcuts = List.of(
+ makeShortcut(Settings.ZenModeSettingsActivity.class));
+ when(mShortcutManager.getPinnedShortcuts()).thenReturn(shortcuts);
+
+ ShortcutsUpdater.updatePinnedShortcuts(mContext);
+
+ verify(mShortcutManager, times(1)).updateShortcuts(mListCaptor.capture());
+ final List updates = mListCaptor.getValue();
+ assertThat(updates).hasSize(1);
+
+ // Nothing has changed.
+ ComponentName zenCn = new ComponentName(mContext, Settings.ZenModeSettingsActivity.class);
+ assertThat(updates.get(0).getId()).isEqualTo(
+ SHORTCUT_ID_PREFIX + zenCn.flattenToShortString());
+ assertThat(updates.get(0).getIntent().getComponent()).isEqualTo(zenCn);
+ assertThat(updates.get(0).getShortLabel().toString()).isEqualTo("Do Not Disturb");
+
}
private ShortcutInfo makeShortcut(Class> className) {