Merge "Fixes AOSP crash when WPP isn't installed." into udc-dev

This commit is contained in:
TreeHugger Robot
2023-03-23 00:53:08 +00:00
committed by Android (Google) Code Review
3 changed files with 57 additions and 7 deletions

View File

@@ -50,7 +50,7 @@ public class CustomizableLockScreenQuickAffordancesPreferenceController extends
final Preference preference = screen.findPreference(getPreferenceKey());
if (preference != null) {
preference.setOnPreferenceClickListener(preference1 -> {
final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
final Intent intent = CustomizableLockScreenUtils.newIntent();
final String packageName =
mContext.getString(R.string.config_wallpaper_picker_package);
if (!TextUtils.isEmpty(packageName)) {

View File

@@ -18,6 +18,8 @@ package com.android.settings.display;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
@@ -67,6 +69,10 @@ public final class CustomizableLockScreenUtils {
* <p>This is a slow, blocking call that shouldn't be made on the main thread.
*/
public static boolean isFeatureEnabled(Context context) {
if (!isWallpaperPickerInstalled(context)) {
return false;
}
try (Cursor cursor = context.getContentResolver().query(
FLAGS_URI,
null,
@@ -151,4 +157,17 @@ public final class CustomizableLockScreenUtils {
return null;
}
}
/**
* Returns a new {@link Intent} that can be used to start the wallpaper picker
* activity.
*/
public static Intent newIntent() {
return new Intent(Intent.ACTION_SET_WALLPAPER);
}
private static boolean isWallpaperPickerInstalled(Context context) {
final PackageManager packageManager = context.getPackageManager();
return newIntent().resolveActivity(packageManager) != null;
}
}

View File

@@ -21,6 +21,8 @@ import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -28,6 +30,10 @@ import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.MatrixCursor;
import android.text.TextUtils;
@@ -54,6 +60,7 @@ public class CustomizableLockScreenQuickAffordancesPreferenceControllerTest {
@Mock private Context mContext;
@Mock private ContentResolver mContentResolver;
@Mock private PackageManager mPackageManager;
private CustomizableLockScreenQuickAffordancesPreferenceController mUnderTest;
@@ -63,20 +70,28 @@ public class CustomizableLockScreenQuickAffordancesPreferenceControllerTest {
when(mContext.getContentResolver()).thenReturn(mContentResolver);
when(mContext.getResources())
.thenReturn(ApplicationProvider.getApplicationContext().getResources());
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mUnderTest = new CustomizableLockScreenQuickAffordancesPreferenceController(mContext, KEY);
}
@Test
public void getAvailabilityStatus_whenEnabled() {
setEnabled(true);
public void getAvailabilityStatus_whenFeatureEnabled() {
setEnabled(/* isWallpaperPickerInstalled= */ true, /* isFeatureEnabled = */ true);
assertThat(mUnderTest.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_whenNotEnabled() {
setEnabled(false);
public void getAvailabilityStatus_whenWallpaperPickerNotInstalledEnabled() {
setEnabled(/* isWallpaperPickerInstalled= */ false, /* isFeatureEnabled = */ true);
assertThat(mUnderTest.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_whenFeatureNotEnabled() {
setEnabled(/* isWallpaperPickerInstalled= */ true, /* isFeatureEnabled = */ false);
assertThat(mUnderTest.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@@ -127,13 +142,29 @@ public class CustomizableLockScreenQuickAffordancesPreferenceControllerTest {
assertThat(TextUtils.equals(mUnderTest.getSummary(), "one, two")).isTrue();
}
private void setEnabled(boolean isEnabled) {
private void setEnabled(boolean isWallpaperPickerInstalled, boolean isFeatureEnabled) {
if (isWallpaperPickerInstalled) {
final ResolveInfo resolveInfo = new ResolveInfo();
final ActivityInfo activityInfo = new ActivityInfo();
final ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.packageName = "com.fake.name";
activityInfo.applicationInfo = applicationInfo;
activityInfo.name = "someName";
resolveInfo.activityInfo = activityInfo;
when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
} else {
when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(null);
}
final MatrixCursor cursor = new MatrixCursor(
new String[] {
CustomizableLockScreenUtils.NAME,
CustomizableLockScreenUtils.VALUE
});
cursor.addRow(new Object[] { CustomizableLockScreenUtils.ENABLED_FLAG, isEnabled ? 1 : 0 });
cursor.addRow(
new Object[] {
CustomizableLockScreenUtils.ENABLED_FLAG, isFeatureEnabled ? 1 : 0
});
when(
mContentResolver.query(
CustomizableLockScreenUtils.FLAGS_URI, null, null, null))