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

Commit 3e776a87 authored by Tony Wickham's avatar Tony Wickham
Browse files

Wallpaper preview parallax matches the actual wallpaper's.

Cherry picking the following changes from ub-launcher3-master:
Change-Id: I41c5bbbfdabfeb4e20d77e9b5804842a03211edf
Change-Id: I69afa3f8cc59c77e9c4c25e29e8db8c4beb87462
Change-Id: I82b7ba506d51ee4b3812af5fbdf95d3303b37aef
Change-Id: Id7c2b5483c5535d59be2f8a459ce7788e3c8318a

Bug: 23568800
Change-Id: I343169b9fdc5ceaab3d4b39044627d78b7267868
parent 01251b0d
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 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.
-->

<!-- startOffset is the same as the duration of the wallpaper_enter animation. We have this delay so
    that we don't see the wallpaper changing before fading back to the home screen. -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:startOffset="@android:integer/config_longAnimTime"
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromAlpha="1"
    android:toAlpha="0"/>
+19 −15
Original line number Diff line number Diff line
@@ -45,6 +45,10 @@ public class BitmapCropTask extends AsyncTask<Void, Void, Boolean> {
        public void onBitmapCropped(byte[] imageBytes);
    }

    public interface OnEndCropHandler {
        public void run(boolean cropSucceeded);
    }

    private static final int DEFAULT_COMPRESS_QUALITY = 90;
    private static final String LOGTAG = "BitmapCropTask";

@@ -59,56 +63,56 @@ public class BitmapCropTask extends AsyncTask<Void, Void, Boolean> {
    boolean mSetWallpaper;
    boolean mSaveCroppedBitmap;
    Bitmap mCroppedBitmap;
    Runnable mOnEndRunnable;
    BitmapCropTask.OnEndCropHandler mOnEndCropHandler;
    Resources mResources;
    BitmapCropTask.OnBitmapCroppedHandler mOnBitmapCroppedHandler;
    boolean mNoCrop;

    public BitmapCropTask(Context c, String filePath,
            RectF cropBounds, int rotation, int outWidth, int outHeight,
            boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) {
            boolean setWallpaper, boolean saveCroppedBitmap, OnEndCropHandler onEndCropHandler) {
        mContext = c;
        mInFilePath = filePath;
        init(cropBounds, rotation,
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable);
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndCropHandler);
    }

    public BitmapCropTask(byte[] imageBytes,
            RectF cropBounds, int rotation, int outWidth, int outHeight,
            boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) {
            boolean setWallpaper, boolean saveCroppedBitmap, OnEndCropHandler onEndCropHandler) {
        mInImageBytes = imageBytes;
        init(cropBounds, rotation,
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable);
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndCropHandler);
    }

    public BitmapCropTask(Context c, Uri inUri,
            RectF cropBounds, int rotation, int outWidth, int outHeight,
            boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) {
            boolean setWallpaper, boolean saveCroppedBitmap, OnEndCropHandler onEndCropHandler) {
        mContext = c;
        mInUri = inUri;
        init(cropBounds, rotation,
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable);
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndCropHandler);
    }

    public BitmapCropTask(Context c, Resources res, int inResId,
            RectF cropBounds, int rotation, int outWidth, int outHeight,
            boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) {
            boolean setWallpaper, boolean saveCroppedBitmap, OnEndCropHandler onEndCropHandler) {
        mContext = c;
        mInResId = inResId;
        mResources = res;
        init(cropBounds, rotation,
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable);
                outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndCropHandler);
    }

    private void init(RectF cropBounds, int rotation, int outWidth, int outHeight,
            boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) {
            boolean setWallpaper, boolean saveCroppedBitmap, OnEndCropHandler onEndCropHandler) {
        mCropBounds = cropBounds;
        mRotation = rotation;
        mOutWidth = outWidth;
        mOutHeight = outHeight;
        mSetWallpaper = setWallpaper;
        mSaveCroppedBitmap = saveCroppedBitmap;
        mOnEndRunnable = onEndRunnable;
        mOnEndCropHandler = onEndCropHandler;
    }

    public void setOnBitmapCropped(BitmapCropTask.OnBitmapCroppedHandler handler) {
@@ -119,8 +123,8 @@ public class BitmapCropTask extends AsyncTask<Void, Void, Boolean> {
        mNoCrop = value;
    }

    public void setOnEndRunnable(Runnable onEndRunnable) {
        mOnEndRunnable = onEndRunnable;
    public void setOnEndRunnable(OnEndCropHandler onEndCropHandler) {
        mOnEndCropHandler = onEndCropHandler;
    }

    // Helper to setup input stream
@@ -398,8 +402,8 @@ public class BitmapCropTask extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPostExecute(Boolean result) {
        if (mOnEndRunnable != null) {
            mOnEndRunnable.run();
        if (mOnEndCropHandler != null) {
            mOnEndCropHandler.run(result);
        }
    }
}
 No newline at end of file
+12 −1
Original line number Diff line number Diff line
@@ -189,6 +189,17 @@ public class CropView extends TiledImageView implements OnScaleGestureListener {
    public void onScaleEnd(ScaleGestureDetector detector) {
    }

    /**
     * Offsets wallpaper preview according to the state it will be displayed in upon returning home.
     * @param offset Ranges from 0 to 1, where 0 is the leftmost parallax and 1 is the rightmost.
     */
    public void setParallaxOffset(float offset, RectF crop) {
        offset = Math.max(0, Math.min(offset, 1)); // Make sure the offset is in the correct range.
        float screenWidth = getWidth() / mRenderer.scale;
        mCenterX = screenWidth / 2 + offset * (crop.width() - screenWidth) + crop.left;
        updateCenter();
    }

    public void moveToLeft() {
        if (getWidth() == 0 || getHeight() == 0) {
            final ViewTreeObserver observer = getViewTreeObserver();
+3 −1
Original line number Diff line number Diff line
@@ -54,7 +54,9 @@ public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements
            final ComponentName itemComponentName = new ComponentName(
                    mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name);
            Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
            launchIntent.setComponent(itemComponentName);
            launchIntent.setComponent(itemComponentName)
            .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET,
                    a.getWallpaperParallaxOffset());
            a.startActivityForResultSafely(
                    launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
        }
+40 −18
Original line number Diff line number Diff line
@@ -124,7 +124,11 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
                    @Override
                    public void onClick(View v) {
                        boolean finishActivityWhenDone = true;
                        cropImageAndSetWallpaper(imageUri, null, finishActivityWhenDone);
                        // Never fade on finish because we return to the app that started us (e.g.
                        // Photos), not the home screen.
                        boolean shouldFadeOutOnFinish = false;
                        cropImageAndSetWallpaper(imageUri, null, finishActivityWhenDone,
                                shouldFadeOutOnFinish);
                    }
                });
        mSetWallpaperButton = findViewById(R.id.set_wallpaper_button);
@@ -246,8 +250,14 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
            if (req.moveToLeft) {
                mCropView.moveToLeft();
            }
            if (req.scaleProvider != null) {
                mCropView.setScale(req.scaleProvider.getScale(req.result));
            if (req.scaleAndOffsetProvider != null) {
                TileSource src = req.result;
                Point wallpaperSize = WallpaperUtils.getDefaultWallpaperSize(
                        getResources(), getWindowManager());
                RectF crop = Utils.getMaxCropRect(src.getImageWidth(), src.getImageHeight(),
                        wallpaperSize.x, wallpaperSize.y, false /* leftAligned */);
                mCropView.setScale(req.scaleAndOffsetProvider.getScale(wallpaperSize, crop));
                mCropView.setParallaxOffset(req.scaleAndOffsetProvider.getParallaxOffset(), crop);
            }

            // Free last image
@@ -265,13 +275,13 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
    }

    public final void setCropViewTileSource(BitmapSource bitmapSource, boolean touchEnabled,
            boolean moveToLeft, CropViewScaleProvider scaleProvider, Runnable postExecute) {
            boolean moveToLeft, CropViewScaleAndOffsetProvider scaleProvider, Runnable postExecute) {
        final LoadRequest req = new LoadRequest();
        req.moveToLeft = moveToLeft;
        req.src = bitmapSource;
        req.touchEnabled = touchEnabled;
        req.postExecute = postExecute;
        req.scaleProvider = scaleProvider;
        req.scaleAndOffsetProvider = scaleProvider;
        mCurrentLoadRequest = req;

        // Remove any pending requests
@@ -295,17 +305,21 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
        return getResources().getBoolean(R.bool.allow_rotation);
    }

    protected void setWallpaper(Uri uri, final boolean finishActivityWhenDone) {
    protected void setWallpaper(Uri uri, final boolean finishActivityWhenDone,
                final boolean shouldFadeOutOnFinish) {
        int rotation = BitmapUtils.getRotationFromExif(getContext(), uri);
        BitmapCropTask cropTask = new BitmapCropTask(
                getContext(), uri, null, rotation, 0, 0, true, false, null);
        final Point bounds = cropTask.getImageBounds();
        Runnable onEndCrop = new Runnable() {
            public void run() {
        BitmapCropTask.OnEndCropHandler onEndCrop = new BitmapCropTask.OnEndCropHandler() {
            public void run(boolean cropSucceeded) {
                updateWallpaperDimensions(bounds.x, bounds.y);
                if (finishActivityWhenDone) {
                    setResult(Activity.RESULT_OK);
                    finish();
                    if (cropSucceeded && shouldFadeOutOnFinish) {
                        overridePendingTransition(0, R.anim.fade_out);
                    }
                }
            }
        };
@@ -314,8 +328,8 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
        cropTask.execute();
    }

    protected void cropImageAndSetWallpaper(
            Resources res, int resId, final boolean finishActivityWhenDone) {
    protected void cropImageAndSetWallpaper(Resources res, int resId,
                final boolean finishActivityWhenDone, final boolean shouldFadeOutOnFinish) {
        // crop this image and scale it down to the default wallpaper size for
        // this device
        int rotation = BitmapUtils.getRotationFromExif(res, resId);
@@ -324,14 +338,17 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
                getWindowManager());
        RectF crop = Utils.getMaxCropRect(
                inSize.x, inSize.y, outSize.x, outSize.y, false);
        Runnable onEndCrop = new Runnable() {
            public void run() {
        BitmapCropTask.OnEndCropHandler onEndCrop = new BitmapCropTask.OnEndCropHandler() {
            public void run(boolean cropSucceeded) {
                // Passing 0, 0 will cause launcher to revert to using the
                // default wallpaper size
                updateWallpaperDimensions(0, 0);
                if (finishActivityWhenDone) {
                    setResult(Activity.RESULT_OK);
                    finish();
                    if (cropSucceeded && shouldFadeOutOnFinish) {
                        overridePendingTransition(0, R.anim.fade_out);
                    }
                }
            }
        };
@@ -342,7 +359,8 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    protected void cropImageAndSetWallpaper(Uri uri,
            BitmapCropTask.OnBitmapCroppedHandler onBitmapCroppedHandler, final boolean finishActivityWhenDone) {
            BitmapCropTask.OnBitmapCroppedHandler onBitmapCroppedHandler,
            final boolean finishActivityWhenDone, final boolean shouldFadeOutOnFinish) {
        boolean centerCrop = getResources().getBoolean(R.bool.center_crop);
        // Get the crop
        boolean ltr = mCropView.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
@@ -418,13 +436,16 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
        final int outWidth = (int) Math.round(cropRect.width() * cropScale);
        final int outHeight = (int) Math.round(cropRect.height() * cropScale);

        Runnable onEndCrop = new Runnable() {
            public void run() {
        BitmapCropTask.OnEndCropHandler onEndCrop = new BitmapCropTask.OnEndCropHandler() {
            public void run(boolean cropSucceeded) {
                updateWallpaperDimensions(outWidth, outHeight);
                if (finishActivityWhenDone) {
                    setResult(Activity.RESULT_OK);
                    finish();
                }
                if (cropSucceeded && shouldFadeOutOnFinish) {
                    overridePendingTransition(0, R.anim.fade_out);
                }
            }
        };
        BitmapCropTask cropTask = new BitmapCropTask(getContext(), uri,
@@ -456,12 +477,13 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
        boolean touchEnabled;
        boolean moveToLeft;
        Runnable postExecute;
        CropViewScaleProvider scaleProvider;
        CropViewScaleAndOffsetProvider scaleAndOffsetProvider;

        TileSource result;
    }

    interface CropViewScaleProvider {
        float getScale(TileSource src);
    interface CropViewScaleAndOffsetProvider {
        float getScale(Point wallpaperSize, RectF crop);
        float getParallaxOffset();
    }
}
Loading