summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Mark Renouf <mrenouf@google.com> 2023-03-21 14:45:25 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2023-03-21 14:45:25 +0000
commit2bc270acca73cd6b6b0db20cc9e58b2d7bc93d93 (patch)
treee14fc1781b29d518b4b6863b4bfc696acf9bb1a2
parent1f22f537128ab5143269834fc9f605e574f48278 (diff)
parent430b00015051f6158848589165eb1390edfac702 (diff)
Merge "Avoid crash when no work profile files app is set" into udc-dev
-rw-r--r--packages/SystemUI/src/com/android/systemui/screenshot/WorkProfileMessageController.kt8
-rw-r--r--packages/SystemUI/tests/src/com/android/systemui/screenshot/WorkProfileMessageControllerTest.java50
2 files changed, 38 insertions, 20 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/WorkProfileMessageController.kt b/packages/SystemUI/src/com/android/systemui/screenshot/WorkProfileMessageController.kt
index 236213cb023f..798c4908e467 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/WorkProfileMessageController.kt
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/WorkProfileMessageController.kt
@@ -19,6 +19,7 @@ package com.android.systemui.screenshot
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
+import android.content.pm.PackageManager.ComponentInfoFlags
import android.graphics.drawable.Drawable
import android.os.UserHandle
import android.os.UserManager
@@ -53,12 +54,9 @@ constructor(
var badgedIcon: Drawable? = null
var label: CharSequence? = null
val fileManager = fileManagerComponentName()
+ ?: return WorkProfileFirstRunData(defaultFileAppName(), null)
try {
- val info =
- packageManager.getActivityInfo(
- fileManager,
- PackageManager.ComponentInfoFlags.of(0)
- )
+ val info = packageManager.getActivityInfo(fileManager, ComponentInfoFlags.of(0L))
val icon = packageManager.getActivityIcon(fileManager)
badgedIcon = packageManager.getUserBadgedIcon(icon, userHandle)
label = info.loadLabel(packageManager)
diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenshot/WorkProfileMessageControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/screenshot/WorkProfileMessageControllerTest.java
index 3440f91c1237..31f7771bb939 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/screenshot/WorkProfileMessageControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/screenshot/WorkProfileMessageControllerTest.java
@@ -45,7 +45,6 @@ import com.android.systemui.util.FakeSharedPreferences;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -58,8 +57,9 @@ import kotlin.Unit;
@SmallTest
@RunWith(AndroidTestingRunner.class)
public class WorkProfileMessageControllerTest extends SysuiTestCase {
- private static final String DEFAULT_LABEL = "default label";
- private static final String APP_LABEL = "app label";
+ private static final String FILES_APP_COMPONENT = "com.android.test/.FilesComponent";
+ private static final String FILES_APP_LABEL = "Custom Files App";
+ private static final String DEFAULT_FILES_APP_LABEL = "Files";
private static final UserHandle NON_WORK_USER = UserHandle.of(0);
private static final UserHandle WORK_USER = UserHandle.of(10);
@@ -88,14 +88,21 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
when(mMockContext.getSharedPreferences(
eq(WorkProfileMessageController.SHARED_PREFERENCES_NAME),
eq(Context.MODE_PRIVATE))).thenReturn(mSharedPreferences);
- when(mMockContext.getString(ArgumentMatchers.anyInt())).thenReturn(DEFAULT_LABEL);
- when(mPackageManager.getActivityIcon(any(ComponentName.class)))
+ when(mMockContext.getString(R.string.config_sceenshotWorkProfileFilesApp))
+ .thenReturn(FILES_APP_COMPONENT);
+ when(mMockContext.getString(R.string.screenshot_default_files_app_name))
+ .thenReturn(DEFAULT_FILES_APP_LABEL);
+ when(mPackageManager.getActivityIcon(
+ eq(ComponentName.unflattenFromString(FILES_APP_COMPONENT))))
.thenReturn(mActivityIcon);
- when(mPackageManager.getUserBadgedIcon(
- any(), any())).thenReturn(mBadgedActivityIcon);
- when(mPackageManager.getActivityInfo(any(),
- any(PackageManager.ComponentInfoFlags.class))).thenReturn(mActivityInfo);
- when(mActivityInfo.loadLabel(eq(mPackageManager))).thenReturn(APP_LABEL);
+ when(mPackageManager.getUserBadgedIcon(any(), any()))
+ .thenReturn(mBadgedActivityIcon);
+ when(mPackageManager.getActivityInfo(
+ eq(ComponentName.unflattenFromString(FILES_APP_COMPONENT)),
+ any(PackageManager.ComponentInfoFlags.class)))
+ .thenReturn(mActivityInfo);
+ when(mActivityInfo.loadLabel(eq(mPackageManager)))
+ .thenReturn(FILES_APP_LABEL);
mSharedPreferences.edit().putBoolean(
WorkProfileMessageController.PREFERENCE_KEY, false).apply();
@@ -120,14 +127,15 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
@Test
public void testOnScreenshotTaken_packageNotFound()
throws PackageManager.NameNotFoundException {
- when(mPackageManager.getActivityInfo(any(),
+ when(mPackageManager.getActivityInfo(
+ eq(ComponentName.unflattenFromString(FILES_APP_COMPONENT)),
any(PackageManager.ComponentInfoFlags.class))).thenThrow(
new PackageManager.NameNotFoundException());
WorkProfileMessageController.WorkProfileFirstRunData data =
mMessageController.onScreenshotTaken(WORK_USER);
- assertEquals(DEFAULT_LABEL, data.getAppName());
+ assertEquals(DEFAULT_FILES_APP_LABEL, data.getAppName());
assertNull(data.getIcon());
}
@@ -136,16 +144,28 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
WorkProfileMessageController.WorkProfileFirstRunData data =
mMessageController.onScreenshotTaken(WORK_USER);
- assertEquals(APP_LABEL, data.getAppName());
+ assertEquals(FILES_APP_LABEL, data.getAppName());
assertEquals(mBadgedActivityIcon, data.getIcon());
}
@Test
+ public void testOnScreenshotTaken_noFilesAppComponentDefined() {
+ when(mMockContext.getString(R.string.config_sceenshotWorkProfileFilesApp))
+ .thenReturn("");
+
+ WorkProfileMessageController.WorkProfileFirstRunData data =
+ mMessageController.onScreenshotTaken(WORK_USER);
+
+ assertEquals(DEFAULT_FILES_APP_LABEL, data.getAppName());
+ assertNull(data.getIcon());
+ }
+
+ @Test
public void testPopulateView() throws InterruptedException {
ViewGroup layout = (ViewGroup) LayoutInflater.from(mContext).inflate(
R.layout.screenshot_work_profile_first_run, null);
WorkProfileMessageController.WorkProfileFirstRunData data =
- new WorkProfileMessageController.WorkProfileFirstRunData(APP_LABEL,
+ new WorkProfileMessageController.WorkProfileFirstRunData(FILES_APP_LABEL,
mBadgedActivityIcon);
final CountDownLatch countdown = new CountDownLatch(1);
mMessageController.populateView(layout, data, () -> {
@@ -157,7 +177,7 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
assertEquals(mBadgedActivityIcon, image.getDrawable());
TextView text = layout.findViewById(R.id.screenshot_message_content);
// The app name is used in a template, but at least validate that it was inserted.
- assertTrue(text.getText().toString().contains(APP_LABEL));
+ assertTrue(text.getText().toString().contains(FILES_APP_LABEL));
// Validate that clicking the dismiss button calls back properly.
assertEquals(1, countdown.getCount());