Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 245ac03c authored by Ben Lin's avatar Ben Lin
Browse files

Check device for config_enableWallpaperService before calling service.

On some devices, it is possible that config_enableWallpaperService is
false, in which then there is no wallpaper service on device. Calling
WallpaperManager#getWallpaperId will then result in an NPE. We should
just do a check and return false.

Bug: 62387789
Test: RoboSettingsTest
Change-Id: I31db6845f06533d92140bf91d0f7fc7e7bedc5c5
parent f52ff285
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -59,7 +59,8 @@ public class WallpaperSuggestionActivity extends Activity {
    @VisibleForTesting
    public static boolean isSuggestionComplete(Context context) {
        final WallpaperManagerWrapper manager = new WallpaperManagerWrapper(context);
        return manager.getWallpaperId(WallpaperManager.FLAG_SYSTEM) > 0;
        return manager.isWallpaperServiceEnabled() ? manager.getWallpaperId(
                WallpaperManager.FLAG_SYSTEM) > 0 : false;
    }

}
+12 −1
Original line number Diff line number Diff line
@@ -22,12 +22,23 @@ import android.content.Context;
public class WallpaperManagerWrapper {

    private final WallpaperManager mWallpaperManager;
    private final boolean mWallpaperServiceEnabled;

    public WallpaperManagerWrapper(Context context) {
        mWallpaperManager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
        mWallpaperServiceEnabled = context.getResources().getBoolean(
                com.android.internal.R.bool.config_enableWallpaperService);
        mWallpaperManager = mWallpaperServiceEnabled ? (WallpaperManager) context.getSystemService(
                Context.WALLPAPER_SERVICE) : null;
    }

    public boolean isWallpaperServiceEnabled() {
        return mWallpaperServiceEnabled;
    }

    public int getWallpaperId(int which) {
        if (!mWallpaperServiceEnabled) {
            throw new RuntimeException("This device does not have wallpaper service enabled.");
        }
        return mWallpaperManager.getWallpaperId(which);
    }
}
+27 −4
Original line number Diff line number Diff line
@@ -17,12 +17,14 @@
package com.android.settings.wallpaper;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;

import com.android.settings.SubSettings;
import com.android.settings.TestConfig;
@@ -42,16 +44,16 @@ import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowActivity;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
        shadows = {
                WallpaperSuggestionActivityTest.ShadowWallpaperManagerWrapper.class
        })
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WallpaperSuggestionActivityTest {

    @Mock
    private Context mContext;
    @Mock
    private PackageManager mPackageManager;
    @Mock
    private Resources mResources;

    private ActivityController<WallpaperSuggestionActivity> mController;

    @Before
@@ -72,6 +74,17 @@ public class WallpaperSuggestionActivityTest {
    }

    @Test
    public void wallpaperServiceEnabled_no_shouldReturnFalse() {
        when(mContext.getResources()).thenReturn(mResources);
        when(mResources.getBoolean(
                com.android.internal.R.bool.config_enableWallpaperService)).thenReturn(false);

        assertThat(WallpaperSuggestionActivity.isSuggestionComplete(mContext))
                .isFalse();
    }

    @Test
    @Config(shadows = WallpaperSuggestionActivityTest.ShadowWallpaperManagerWrapper.class)
    public void hasWallpaperSet_no_shouldReturnFalse() {
        ShadowWallpaperManagerWrapper.setWallpaperId(0);

@@ -80,6 +93,7 @@ public class WallpaperSuggestionActivityTest {
    }

    @Test
    @Config(shadows = WallpaperSuggestionActivityTest.ShadowWallpaperManagerWrapper.class)
    public void hasWallpaperSet_yes_shouldReturnTrue() {
        ShadowWallpaperManagerWrapper.setWallpaperId(100);

@@ -100,6 +114,15 @@ public class WallpaperSuggestionActivityTest {
            sWallpaperId = 0;
        }

        public void __constructor__(Context context) {

        }

        @Implementation
        public boolean isWallpaperServiceEnabled() {
            return true;
        }

        @Implementation
        public int getWallpaperId(int which) {
            return sWallpaperId;