Merge changes I28f7e3e6,I30450d13 into main

* changes:
  Fix existing DND Settings shortcuts to point to Modes
  Refactor shortcut updating, and do it on BOOT_COMPLETE
This commit is contained in:
Matías Hernández
2024-09-19 07:38:44 +00:00
committed by Android (Google) Code Review
11 changed files with 433 additions and 215 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,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<ShortcutInfo> 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<ShortcutInfo> shortcuts = List.of(
makeShortcut(Settings.ZenModeSettingsActivity.class));
when(mShortcutManager.getPinnedShortcuts()).thenReturn(shortcuts);
ShortcutsUpdater.updatePinnedShortcuts(mContext);
verify(mShortcutManager, times(1)).updateShortcuts(mListCaptor.capture());
final List<ShortcutInfo> 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<ShortcutInfo> shortcuts = List.of(
makeShortcut(Settings.ZenModeSettingsActivity.class));
when(mShortcutManager.getPinnedShortcuts()).thenReturn(shortcuts);
ShortcutsUpdater.updatePinnedShortcuts(mContext);
verify(mShortcutManager, times(1)).updateShortcuts(mListCaptor.capture());
final List<ShortcutInfo> 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) {