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

Commit 8d4c825c authored by Felipe Leme's avatar Felipe Leme
Browse files

Fixes usage of WallpaperManager to make it optional.

As the main javadoc states:
"An app can check whether wallpapers are supported for the current user,
by calling {@link #isWallpaperSupported()}, and whether setting of
wallpapers is allowed, by calling {@link #isSetWallpaperAllowed()}."

Test: manual verification on automotive
Test: atest CtsAppTestCases:android.app.cts.WallpaperManagerTest # on automotive

Bug: 138939803
Bug: 132111956

Change-Id: I88946f92ff54e556d289c41c28e22da3a4b8c8b8
Merged-In: I88946f92ff54e556d289c41c28e22da3a4b8c8b8
(cherry picked from commit 34a861ad)
parent 0537bd33
Loading
Loading
Loading
Loading
+346 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 android.app;

import android.annotation.NonNull;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

/**
 * A no-op implementation of {@link WallpaperManager}.
 */
final class DisabledWallpaperManager extends WallpaperManager {

    private static final String TAG = DisabledWallpaperManager.class.getSimpleName();

    // Don't need to worry about synchronization
    private static DisabledWallpaperManager sInstance;

    // TODO(b/138939803): STOPSHIP changed to false and/or remove it
    private static final boolean DEBUG = true;

    @NonNull
    static DisabledWallpaperManager getInstance() {
        if (sInstance == null) {
            sInstance = new DisabledWallpaperManager();
        }
        return sInstance;
    }

    private DisabledWallpaperManager() {
        super(null, null, null);
    }

    @Override
    public boolean isWallpaperSupported() {
        return false;
    }

    @Override
    public boolean isSetWallpaperAllowed() {
        return false;
    }

    // TODO(b/138939803): STOPSHIP methods below should not be necessary,
    // callers should check if isWallpaperSupported(), consider removing them to keep this class
    // simpler

    private static <T> T unsupported() {
        if (DEBUG) Log.w(TAG, "unsupported method called; returning null", new Exception());
        return null;
    }

    private static boolean unsupportedBoolean() {
        if (DEBUG) Log.w(TAG, "unsupported method called; returning false", new Exception());
        return false;
    }

    @Override
    public Drawable getDrawable() {
        return unsupported();
    }

    @Override
    public Drawable getBuiltInDrawable() {
        return unsupported();
    }

    @Override
    public Drawable getBuiltInDrawable(int which) {
        return unsupported();
    }

    @Override
    public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
            float horizontalAlignment, float verticalAlignment) {
        return unsupported();
    }

    @Override
    public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
            float horizontalAlignment, float verticalAlignment, int which) {
        return unsupported();
    }

    @Override
    public Drawable peekDrawable() {
        return unsupported();
    }

    @Override
    public Drawable getFastDrawable() {
        return unsupported();
    }

    @Override
    public Drawable peekFastDrawable() {
        return unsupported();
    }

    @Override
    public Bitmap getBitmap() {
        return unsupported();
    }

    @Override
    public Bitmap getBitmap(boolean hardware) {
        return unsupported();
    }

    @Override
    public Bitmap getBitmapAsUser(int userId, boolean hardware) {
        return unsupported();
    }

    @Override
    public ParcelFileDescriptor getWallpaperFile(int which) {
        return unsupported();
    }

    @Override
    public void addOnColorsChangedListener(OnColorsChangedListener listener, Handler handler) {
        unsupported();
    }

    @Override
    public void addOnColorsChangedListener(OnColorsChangedListener listener, Handler handler,
            int userId) {
        unsupported();
    }

    @Override
    public void removeOnColorsChangedListener(OnColorsChangedListener callback) {
        unsupported();
    }

    @Override
    public void removeOnColorsChangedListener(OnColorsChangedListener callback, int userId) {
        unsupported();
    }

    @Override
    public WallpaperColors getWallpaperColors(int which) {
        return unsupported();
    }

    @Override
    public WallpaperColors getWallpaperColors(int which, int userId) {
        return unsupported();
    }

    @Override
    public ParcelFileDescriptor getWallpaperFile(int which, int userId) {
        return unsupported();
    }

    @Override
    public void forgetLoadedWallpaper() {
        unsupported();
    }

    @Override
    public WallpaperInfo getWallpaperInfo() {
        return unsupported();
    }

    @Override
    public WallpaperInfo getWallpaperInfo(int userId) {
        return unsupported();
    }

    @Override
    public int getWallpaperId(int which) {
        return unsupported();
    }

    @Override
    public int getWallpaperIdForUser(int which, int userId) {
        return unsupported();
    }

    @Override
    public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
        return unsupported();
    }

    @Override
    public void setResource(int resid) throws IOException {
        unsupported();
    }

    @Override
    public int setResource(int resid, int which) throws IOException {
        return unsupported();
    }

    @Override
    public void setBitmap(Bitmap bitmap) throws IOException {
        unsupported();
    }

    @Override
    public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
            throws IOException {
        return unsupported();
    }

    @Override
    public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int which)
            throws IOException {
        return unsupported();
    }

    @Override
    public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int which,
            int userId) throws IOException {
        return unsupported();
    }

    @Override
    public void setStream(InputStream bitmapData) throws IOException {
        unsupported();
    }

    @Override
    public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
            throws IOException {
        return unsupported();
    }

    @Override
    public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup,
            int which) throws IOException {
        return unsupported();
    }

    @Override
    public boolean hasResourceWallpaper(int resid) {
        return unsupportedBoolean();
    }

    @Override
    public int getDesiredMinimumWidth() {
        return unsupported();
    }

    @Override
    public int getDesiredMinimumHeight() {
        return unsupported();
    }

    @Override
    public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
        unsupported();
    }

    @Override
    public void setDisplayPadding(Rect padding) {
        unsupported();
    }

    @Override
    public void setDisplayOffset(IBinder windowToken, int x, int y) {
        unsupported();
    }

    @Override
    public void clearWallpaper() {
        unsupported();
    }

    @Override
    public void clearWallpaper(int which, int userId) {
        unsupported();
    }

    @Override
    public boolean setWallpaperComponent(ComponentName name) {
        return unsupportedBoolean();
    }

    @Override
    public boolean setWallpaperComponent(ComponentName name, int userId) {
        return unsupportedBoolean();
    }

    @Override
    public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
        unsupported();
    }

    @Override
    public void setWallpaperOffsetSteps(float xStep, float yStep) {
        unsupported();
    }

    @Override
    public void sendWallpaperCommand(IBinder windowToken, String action, int x, int y, int z,
            Bundle extras) {
        unsupported();
    }

    @Override
    public void clearWallpaperOffsets(IBinder windowToken) {
        unsupported();
    }

    @Override
    public void clear() throws IOException {
        unsupported();
    }

    @Override
    public void clear(int which) throws IOException {
        unsupported();
    }

    @Override
    public boolean isWallpaperBackupEligible(int which) {
        return unsupportedBoolean();
    }
}
+16 −5
Original line number Diff line number Diff line
@@ -694,11 +694,22 @@ final class SystemServiceRegistry {
            @Override
            public WallpaperManager createService(ContextImpl ctx)
                    throws ServiceNotFoundException {
                final IBinder b;
                final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
                if (b == null) {
                    // There are 2 reason service can be null:
                    // 1.Device doesn't support it - that's fine
                    // 2.App is running on instant mode - should fail
                    final boolean enabled = Resources.getSystem()
                            .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
                    if (!enabled) {
                        // Life moves on...
                        return DisabledWallpaperManager.getInstance();
                    }
                    if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
                    b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
                } else {
                    b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
                        // Instant app
                        throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
                    }
                    // Bad state - WallpaperManager methods will throw exception
                }
                IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
                return new WallpaperManager(service, ctx.getOuterContext(),
+3 −1
Original line number Diff line number Diff line
@@ -510,8 +510,10 @@ public class WallpaperManager {

    /*package*/ WallpaperManager(IWallpaperManager service, Context context, Handler handler) {
        mContext = context;
        if (service != null) {
            initGlobals(service, context.getMainLooper());
        }
    }

    /**
     * Retrieve a WallpaperManager associated with the given Context.
+4 −0
Original line number Diff line number Diff line
@@ -85,6 +85,10 @@ public class WallpaperBackupHelper extends FileBackupHelperBase implements Backu
     */
    @Override
    public void restoreEntity(BackupDataInputStream data) {
        if (mWpm == null) {
            Slog.w(TAG, "restoreEntity(): no wallpaper service");
            return;
        }
        final String key = data.getKey();
        if (isKeyInList(key, mKeys)) {
            if (key.equals(WALLPAPER_IMAGE_KEY)) {
+4 −2
Original line number Diff line number Diff line
@@ -73,9 +73,11 @@ public class ColorExtractor implements WallpaperManager.OnColorsChangedListener
        }

        mOnColorsChangedListeners = new ArrayList<>();
        if (wallpaperManager.isWallpaperSupported()) {
            wallpaperManager.addOnColorsChangedListener(this, null /* handler */);
            initExtractColors(wallpaperManager, immediately);
        }
    }

    private void initExtractColors(WallpaperManager wallpaperManager, boolean immediately) {
        if (immediately) {
Loading