Refactor shortcut updating, and do it on BOOT_COMPLETE

This CL shuffles quite a bit of code around, but the effective differences are:
* Unified shortcut updating code (language switch & backup restoration).
* Shortcuts are also updated on boot (flagged by MODES_UI which will need this).
* Removed usage of (long obsolete) AsyncTask.

A further CL will add some special-casing for the DND->Modes shortcut transition.

Bug: 365545604
Test: atest com.android.settings.shortcut + manual (switch language, reboot)
Flag: android.app.modes_ui
Change-Id: I30450d13cb05008d2a71ed89d4781eb81e5532b9
This commit is contained in:
Matías Hernández
2024-09-11 18:36:47 +02:00
parent b979edf1f8
commit 939189bde8
10 changed files with 298 additions and 161 deletions

View File

@@ -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));
}
}

View File

@@ -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();
}
}

View File

@@ -16,14 +16,12 @@
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;
@@ -31,9 +29,6 @@ import static org.mockito.Mockito.when;
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;
@@ -48,17 +43,14 @@ 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;
@Mock
private ShortcutManager mShortcutManager;
@@ -69,27 +61,12 @@ public class ShortcutsUpdateTaskTest {
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPackageManager = Shadow.extract(mContext.getPackageManager());
}
@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);
final List<ShortcutInfo> pinnedShortcuts = Arrays.asList(
makeShortcut("d1"),
@@ -99,7 +76,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 +85,8 @@ 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");
}
private ShortcutInfo makeShortcut(Class<?> className) {