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

Commit 9762072e authored by Aurélien Pomini's avatar Aurélien Pomini
Browse files

Refactor WallpaperData constructor, make getWallpaperDir static

Part of the wallpaperManagerService refactor

Bug: 264637309
Test: atest CtsWallpaperTestCases
Change-Id: Ia22fef4245e1858d59cc43951fca8db632266112
parent 250dc7e5
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -16,8 +16,17 @@

package com.android.server.wallpaper;

import static android.app.WallpaperManager.FLAG_LOCK;

import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER;
import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER_CROP;
import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER_LOCK_CROP;
import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER_LOCK_ORIG;
import static com.android.server.wallpaper.WallpaperUtils.getWallpaperDir;

import android.app.IWallpaperManagerCallback;
import android.app.WallpaperColors;
import android.app.WallpaperManager.SetWallpaperFlags;
import android.content.ComponentName;
import android.graphics.Rect;
import android.os.RemoteCallbackList;
@@ -42,8 +51,7 @@ class WallpaperData {
    boolean imageWallpaperPending;

    /**
     * Which wallpaper is set. Flag values are from
     * {@link android.app.WallpaperManager.SetWallpaperFlags}.
     * Which wallpaper is set. Flag values are from {@link SetWallpaperFlags}.
     */
    int mWhich;

@@ -125,6 +133,12 @@ class WallpaperData {
        cropFile = new File(wallpaperDir, cropFileName);
    }

    WallpaperData(int userId, @SetWallpaperFlags int wallpaperType) {
        this(userId, getWallpaperDir(userId),
                (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_ORIG : WALLPAPER,
                (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_CROP : WALLPAPER_CROP);
    }

    // Called during initialization of a given user's wallpaper bookkeeping
    boolean cropExists() {
        return cropFile.exists();
+11 −23
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ import static android.os.ParcelFileDescriptor.MODE_TRUNCATE;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;

import static com.android.server.wallpaper.WallpaperUtils.getWallpaperDir;

import android.annotation.NonNull;
import android.app.ActivityManager;
import android.app.ActivityOptions;
@@ -71,7 +73,6 @@ import android.hardware.display.DisplayManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Debug;
import android.os.Environment;
import android.os.FileObserver;
import android.os.FileUtils;
import android.os.Handler;
@@ -149,6 +150,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
    private static final boolean DEBUG = false;
    private static final boolean DEBUG_LIVE = true;
    private static final boolean DEBUG_CROP = true;

    private static final @NonNull RectF LOCAL_COLOR_BOUNDS =
            new RectF(0, 0, 1, 1);

@@ -1678,10 +1680,6 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        getWallpaperSafeLocked(UserHandle.USER_SYSTEM, FLAG_SYSTEM);
    }

    File getWallpaperDir(int userId) {
        return Environment.getUserSystemDirectory(userId);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
@@ -1822,9 +1820,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
    }

    private void clearWallpaperData(int userID, int wallpaperType) {
        final WallpaperData wallpaper = new WallpaperData(userID, getWallpaperDir(userID),
                (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_ORIG : WALLPAPER,
                (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_CROP : WALLPAPER_CROP);
        final WallpaperData wallpaper = new WallpaperData(userID, wallpaperType);
        if (wallpaper.sourceExists()) {
            wallpaper.wallpaperFile.delete();
        }
@@ -1944,9 +1940,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                    // while locked, so pretend like the component was actually
                    // bound into place
                    wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent;
                    final WallpaperData fallback =
                            new WallpaperData(wallpaper.userId, getWallpaperDir(wallpaper.userId),
                            WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
                    final WallpaperData fallback = new WallpaperData(wallpaper.userId, FLAG_LOCK);
                    ensureSaneWallpaperData(fallback);
                    bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply);
                    mWaitingForUnlock = true;
@@ -2808,8 +2802,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        }

        // We know a-priori that there is no lock-only wallpaper currently
        WallpaperData lockWP = new WallpaperData(userId, getWallpaperDir(userId),
                WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
        WallpaperData lockWP = new WallpaperData(userId, FLAG_LOCK);
        lockWP.wallpaperId = sysWP.wallpaperId;
        lockWP.cropHint.set(sysWP.cropHint);
        lockWP.allowBackup = sysWP.allowBackup;
@@ -3482,16 +3475,14 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            // it now.
            if (wallpaper == null) {
                if (which == FLAG_LOCK) {
                    wallpaper = new WallpaperData(userId, getWallpaperDir(userId),
                            WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
                    wallpaper = new WallpaperData(userId, FLAG_LOCK);
                    mLockWallpaperMap.put(userId, wallpaper);
                    ensureSaneWallpaperData(wallpaper);
                } else {
                    // rationality fallback: we're in bad shape, but establishing a known
                    // valid system+lock WallpaperData will keep us from dying.
                    Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!");
                    wallpaper = new WallpaperData(userId, getWallpaperDir(userId),
                            WALLPAPER, WALLPAPER_CROP);
                    wallpaper = new WallpaperData(userId, FLAG_SYSTEM);
                    mWallpaperMap.put(userId, wallpaper);
                    ensureSaneWallpaperData(wallpaper);
                }
@@ -3510,8 +3501,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
            // Do this once per boot
            migrateFromOld();

            wallpaper = new WallpaperData(userId, getWallpaperDir(userId),
                    WALLPAPER, WALLPAPER_CROP);
            wallpaper = new WallpaperData(userId, FLAG_SYSTEM);
            wallpaper.allowBackup = true;
            mWallpaperMap.put(userId, wallpaper);
            if (!wallpaper.cropExists()) {
@@ -3562,8 +3552,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
                        // keyguard-specific wallpaper for this user
                        WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
                        if (lockWallpaper == null) {
                            lockWallpaper = new WallpaperData(userId, getWallpaperDir(userId),
                                    WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
                            lockWallpaper = new WallpaperData(userId, FLAG_LOCK);
                            mLockWallpaperMap.put(userId, lockWallpaper);
                        }
                        parseWallpaperAttributes(parser, lockWallpaper, false);
@@ -3614,8 +3603,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
        if (mFallbackWallpaper == null) {
            if (DEBUG) Slog.d(TAG, "Initialize fallback wallpaper");
            final int systemUserId = UserHandle.USER_SYSTEM;
            mFallbackWallpaper = new WallpaperData(systemUserId, getWallpaperDir(systemUserId),
                    WALLPAPER, WALLPAPER_CROP);
            mFallbackWallpaper = new WallpaperData(systemUserId, FLAG_SYSTEM);
            mFallbackWallpaper.allowBackup = false;
            mFallbackWallpaper.wallpaperId = makeWallpaperIdLocked();
            bindWallpaperComponentLocked(mImageWallpaper, true, false, mFallbackWallpaper, null);
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.server.wallpaper;

import android.os.Environment;

import java.io.File;

class WallpaperUtils {

    static File getWallpaperDir(int userId) {
        return Environment.getUserSystemDirectory(userId);
    }
}
+23 −18
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER;
import static com.android.server.wallpaper.WallpaperManagerService.WALLPAPER_CROP;

import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
@@ -76,6 +75,7 @@ import androidx.test.filters.FlakyTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.dx.mockito.inline.extended.ExtendedMockito;
import com.android.dx.mockito.inline.extended.StaticMockitoSession;
import com.android.internal.R;
import com.android.modules.utils.TypedXmlPullParser;
@@ -113,6 +113,8 @@ import java.nio.charset.StandardCharsets;
@FlakyTest(bugId = 129797242)
@RunWith(AndroidJUnit4.class)
public class WallpaperManagerServiceTests {

    private static final String TAG = "WallpaperManagerServiceTests";
    private static final int DISPLAY_SIZE_DIMENSION = 100;
    private static StaticMockitoSession sMockitoSession;

@@ -137,6 +139,7 @@ public class WallpaperManagerServiceTests {
    public static void setUpClass() {
        sMockitoSession = mockitoSession()
                .strictness(Strictness.LENIENT)
                .spyStatic(WallpaperUtils.class)
                .spyStatic(LocalServices.class)
                .spyStatic(WallpaperManager.class)
                .startMocking();
@@ -192,6 +195,11 @@ public class WallpaperManagerServiceTests {
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        ExtendedMockito.doAnswer(invocation ->  {
            int userId = (invocation.getArgument(0));
            return getWallpaperTestDir(userId);
        }).when(() -> WallpaperUtils.getWallpaperDir(anyInt()));

        sContext.addMockSystemService(DisplayManager.class, mDisplayManager);

        final Display mockDisplay = mock(Display.class);
@@ -216,27 +224,25 @@ public class WallpaperManagerServiceTests {
        mService = null;
    }

    protected class TestWallpaperManagerService extends WallpaperManagerService {
        private static final String TAG = "TestWallpaperManagerService";

        TestWallpaperManagerService(Context context) {
            super(context);
        }

        @Override
        File getWallpaperDir(int userId) {
    private File getWallpaperTestDir(int userId) {
        File tempDir = mTempDirs.get(userId);
        if (tempDir == null) {
            try {
                tempDir = mFolder.newFolder(String.valueOf(userId));
                mTempDirs.append(userId, tempDir);
            } catch (IOException e) {
                    Log.e(TAG, "getWallpaperDir failed at userId= " + userId);
                Log.e(TAG, "getWallpaperTestDir failed at userId= " + userId);
            }
        }
        return tempDir;
    }

    protected class TestWallpaperManagerService extends WallpaperManagerService {

        TestWallpaperManagerService(Context context) {
            super(context);
        }

        // Always return true for test
        @Override
        public boolean isWallpaperSupported(String callingPackage) {
@@ -402,8 +408,7 @@ public class WallpaperManagerServiceTests {

    @Test
    public void testWallpaperManagerCallbackInRightOrder() throws RemoteException {
        WallpaperData wallpaper = new WallpaperData(
                USER_SYSTEM, mService.getWallpaperDir(USER_SYSTEM), WALLPAPER, WALLPAPER_CROP);
        WallpaperData wallpaper = new WallpaperData(USER_SYSTEM, FLAG_SYSTEM);
        wallpaper.primaryColors = new WallpaperColors(Color.valueOf(Color.RED),
                Color.valueOf(Color.BLUE), null);