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

Commit 03265022 authored by travp624's avatar travp624 Committed by Roman Birg
Browse files

Frameworks: Lockscreen custom wallpaper (1/2)

kitkat now has code for a custom wallpaper image.

Using that code set a switch case to set the custom background between
  - Custom Wallpaper
  - Default background

This way allows the same overlay shadow thats set on the keyguard when
its the defualt way.

Change-Id: I98fde63ef9f3c0a5f334cd9a97b237a6f99c57f6
parent 3f2e3f75
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -1777,6 +1777,13 @@ public class Intent implements Parcelable, Cloneable {
    public static final String ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE =
        "android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE";

    /**
     * Broadcast Action: The current keyguard wallpaper configuration
     * has changed and should be re-read.
     * {@hide}
     */
    public static final String ACTION_KEYGUARD_WALLPAPER_CHANGED =
            "android.intent.action.KEYGUARD_WALLPAPER_CHANGED";
    /**
     * Broadcast Action:  The current system wallpaper has changed.  See
     * {@link android.app.WallpaperManager} for retrieving the new wallpaper.
+5 −2
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import android.text.TextUtils;
import android.util.AndroidException;
import android.util.Log;

import com.android.internal.util.cm.LockscreenBackgroundUtil;
import com.android.internal.widget.ILockSettings;

import java.net.URISyntaxException;
@@ -3225,10 +3226,12 @@ public final class Settings {
        public static final String QUIET_HOURS_DIM = "quiet_hours_dim";

        /**
         * Sets the lockscreen background style
         * Sets the lockscreen background style. Integer.
         * @see LockscreenBackgroundUtil#LOCKSCREEN_STYLE_DEFAULT
         * @see LockscreenBackgroundUtil#LOCKSCREEN_STYLE_IMAGE
         * @hide
         */
        public static final String LOCKSCREEN_BACKGROUND = "lockscreen_background";
        public static final String LOCKSCREEN_BACKGROUND_STYLE = "lockscreen_background_style";

         /**
         * Action for long-pressing back button on lock screen
+39 −0
Original line number Diff line number Diff line
package com.android.internal.util.cm;

import java.io.File;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings;

/**
 * @hide
 */
public class LockscreenBackgroundUtil {

    public static final int LOCKSCREEN_STYLE_IMAGE = 1;
    public static final int LOCKSCREEN_STYLE_DEFAULT = 0;

    private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
    private static final String LOCKSCREEN_WALLPAPER_FILE_NAME = "lockwallpaper";

    public static File getWallpaperFile(Context ctx) {
        Context settingsContext = null;
        if (ctx.getPackageName().equals(SETTINGS_PACKAGE_NAME)) {
            settingsContext = ctx;
        } else {
            try {
                settingsContext = ctx.createPackageContext(SETTINGS_PACKAGE_NAME, 0);
            } catch (NameNotFoundException e) {
                // Settings package doesn't exist.
                return null;
            }
        }
        return new File(settingsContext.getFilesDir(), LOCKSCREEN_WALLPAPER_FILE_NAME);
    }

    public static int getLockscreenStyle(Context ctx) {
        return Settings.System.getInt(ctx.getContentResolver(),
                Settings.System.LOCKSCREEN_BACKGROUND_STYLE, LOCKSCREEN_STYLE_DEFAULT);
    }
}
+83 −8
Original line number Diff line number Diff line
@@ -16,22 +16,30 @@

package com.android.keyguard;

import java.io.File;

import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.TransitionDrawable;

import com.android.internal.policy.IKeyguardShowCallback;
import com.android.internal.widget.LockPatternUtils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.WallpaperManager;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
@@ -60,6 +68,7 @@ import android.view.ViewManager;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.android.internal.util.cm.LockscreenBackgroundUtil;
import com.android.internal.util.cm.TorchConstants;

/**
@@ -94,12 +103,21 @@ public class KeyguardViewManager {

    private boolean mUnlockKeyDown = false;

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_KEYGUARD_WALLPAPER_CHANGED.equals(intent.getAction())) {
                mKeyguardHost.cacheUserImage();
            }
        }
    };

    private KeyguardUpdateMonitorCallback mBackgroundChanger = new KeyguardUpdateMonitorCallback() {
        @Override
        public void onSetBackground(Bitmap bmp) {
            mKeyguardHost.setCustomBackground(bmp != null ?
                    new BitmapDrawable(mContext.getResources(), bmp) : null);
            updateShowWallpaper(bmp == null);
            updateShowWallpaper(mKeyguardHost.shouldShowWallpaper());
        }
    };

@@ -120,6 +138,10 @@ public class KeyguardViewManager {
        mViewManager = viewManager;
        mViewMediatorCallback = callback;
        mLockPatternUtils = lockPatternUtils;

        context.registerReceiver(mBroadcastReceiver,
                new IntentFilter(Intent.ACTION_KEYGUARD_WALLPAPER_CHANGED),
                android.Manifest.permission.CONTROL_KEYGUARD, null);
    }

    /**
@@ -170,8 +192,10 @@ public class KeyguardViewManager {
    class ViewManagerHost extends FrameLayout {
        private static final int BACKGROUND_COLOR = 0x70000000;

        private Drawable mUserBackground;
        private Drawable mCustomBackground;
        private Configuration mLastConfiguration;
        private int mLockscreenStyle;

        // This is a faster way to draw the background on devices without hardware acceleration
        private final Drawable mBackgroundDrawable = new Drawable() {
@@ -199,6 +223,7 @@ public class KeyguardViewManager {
        public ViewManagerHost(Context context) {
            super(context);
            setBackground(mBackgroundDrawable);
            cacheUserImage();
            mLastConfiguration = new Configuration(context.getResources().getConfiguration());
        }

@@ -220,10 +245,12 @@ public class KeyguardViewManager {

        public void setCustomBackground(Drawable d) {
            if (!ActivityManager.isHighEndGfx() || !mScreenOn) {
                mCustomBackground = d;
                if (d != null) {
                if (d == null) {
                    d = mUserBackground;
                } else {
                    d.setColorFilter(BACKGROUND_COLOR, PorterDuff.Mode.SRC_OVER);
                }
                mCustomBackground = d;
                computeCustomBackgroundBounds(mCustomBackground);
                setBackground(mBackgroundDrawable);
            } else {
@@ -231,16 +258,18 @@ public class KeyguardViewManager {
                if (old == null && d == null) {
                    return;
                }
                boolean newIsNull = false;
                boolean newIsNull = mUserBackground == null;
                if (old == null) {
                    old = new ColorDrawable(BACKGROUND_COLOR);
                }
                if (d == null) {
                    d = mUserBackground;
                }
                // no user wallpaper set
                if (d == null) {
                    d = new ColorDrawable(BACKGROUND_COLOR);
                    newIsNull = true;
                } else {
                    d.setColorFilter(BACKGROUND_COLOR, PorterDuff.Mode.SRC_OVER);
                }
                d.setColorFilter(BACKGROUND_COLOR, PorterDuff.Mode.SRC_OVER);
                computeCustomBackgroundBounds(d);
                Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(b);
@@ -322,6 +351,47 @@ public class KeyguardViewManager {
            }
            return super.dispatchKeyEvent(event);
        }

        private void cacheUserImage() {
            Drawable userDrawable = null;
            mLockscreenStyle = LockscreenBackgroundUtil.getLockscreenStyle(mContext);
            switch (mLockscreenStyle) {
                case LockscreenBackgroundUtil.LOCKSCREEN_STYLE_IMAGE:
                        File imageFile = LockscreenBackgroundUtil.getWallpaperFile(mContext);
                        if (imageFile != null) {
                            Bitmap bitmap = BitmapFactory.decodeFile(imageFile.toString());
                            userDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
                        } else {
                            userDrawable = null;
                        }
                    break;
                case LockscreenBackgroundUtil.LOCKSCREEN_STYLE_DEFAULT:
                default:
                    userDrawable = null;
                    break;
            }
            mUserBackground = userDrawable;
            setCustomBackground(mUserBackground);
        }

        public boolean shouldShowWallpaper(boolean hiding) {
            if (hiding) {
                if (mCustomBackground != null) {
                    return false;
                }
                WallpaperManager wm = WallpaperManager.getInstance(mContext);
                boolean liveWallpaperActive = wm != null && wm.getWallpaperInfo() != null;
                if (liveWallpaperActive) {
                    return false;
                }
            }
            return shouldShowWallpaper();
        }

        public boolean shouldShowWallpaper() {
            return mLockscreenStyle == LockscreenBackgroundUtil.LOCKSCREEN_STYLE_DEFAULT;
        }

    }

    public boolean handleKeyDown(int keyCode, KeyEvent event) {
@@ -655,6 +725,7 @@ public class KeyguardViewManager {

        if (mKeyguardView != null) {
            mKeyguardView.onScreenTurnedOn();
            updateShowWallpaper(mKeyguardHost.shouldShowWallpaper());

            // Caller should wait for this window to be shown before turning
            // on the screen.
@@ -721,7 +792,11 @@ public class KeyguardViewManager {
                            lastView.cleanUp();
                            // Let go of any large bitmaps.
                            mKeyguardHost.setCustomBackground(null);
                            updateShowWallpaper(true);
                            // When turning the screen off and a custom wallpaper is set,
                            // showing the wallpaper will cause the *regular* wallpaper
                            // to briefly flash. This is proper behavior only if no
                            // custom wallpaper is set.
                            updateShowWallpaper(mKeyguardHost.shouldShowWallpaper(true));
                            mKeyguardHost.removeView(lastView);
                            mViewMediatorCallback.keyguardGone();
                        }