1841a502864d307341945eae62569b92cada93e75Sunny Goyal/**
2841a502864d307341945eae62569b92cada93e75Sunny Goyal * Copyright (C) 2015 The Android Open Source Project
3841a502864d307341945eae62569b92cada93e75Sunny Goyal *
4841a502864d307341945eae62569b92cada93e75Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5841a502864d307341945eae62569b92cada93e75Sunny Goyal * you may not use this file except in compliance with the License.
6841a502864d307341945eae62569b92cada93e75Sunny Goyal * You may obtain a copy of the License at
7841a502864d307341945eae62569b92cada93e75Sunny Goyal *
8841a502864d307341945eae62569b92cada93e75Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9841a502864d307341945eae62569b92cada93e75Sunny Goyal *
10841a502864d307341945eae62569b92cada93e75Sunny Goyal * Unless required by applicable law or agreed to in writing, software
11841a502864d307341945eae62569b92cada93e75Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12841a502864d307341945eae62569b92cada93e75Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13841a502864d307341945eae62569b92cada93e75Sunny Goyal * See the License for the specific language governing permissions and
14841a502864d307341945eae62569b92cada93e75Sunny Goyal * limitations under the License.
15841a502864d307341945eae62569b92cada93e75Sunny Goyal */
16841a502864d307341945eae62569b92cada93e75Sunny Goyalpackage com.android.wallpaperpicker.common;
17841a502864d307341945eae62569b92cada93e75Sunny Goyal
18841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.content.Context;
19841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.graphics.Bitmap;
20841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.graphics.Bitmap.CompressFormat;
21841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.graphics.RectF;
22841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.os.AsyncTask;
23841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.util.Log;
24841a502864d307341945eae62569b92cada93e75Sunny Goyalimport android.widget.Toast;
25841a502864d307341945eae62569b92cada93e75Sunny Goyal
26841a502864d307341945eae62569b92cada93e75Sunny Goyalimport com.android.wallpaperpicker.R;
27841a502864d307341945eae62569b92cada93e75Sunny Goyal
28841a502864d307341945eae62569b92cada93e75Sunny Goyalimport java.io.ByteArrayInputStream;
29841a502864d307341945eae62569b92cada93e75Sunny Goyalimport java.io.ByteArrayOutputStream;
30841a502864d307341945eae62569b92cada93e75Sunny Goyalimport java.io.IOException;
31841a502864d307341945eae62569b92cada93e75Sunny Goyal
32841a502864d307341945eae62569b92cada93e75Sunny Goyalpublic class CropAndSetWallpaperTask extends AsyncTask<Integer, Void, Boolean> {
33841a502864d307341945eae62569b92cada93e75Sunny Goyal
34841a502864d307341945eae62569b92cada93e75Sunny Goyal    public interface OnBitmapCroppedHandler {
35841a502864d307341945eae62569b92cada93e75Sunny Goyal        void onBitmapCropped(byte[] imageBytes);
36841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
37841a502864d307341945eae62569b92cada93e75Sunny Goyal
38841a502864d307341945eae62569b92cada93e75Sunny Goyal    public interface OnEndCropHandler {
39841a502864d307341945eae62569b92cada93e75Sunny Goyal        void run(boolean cropSucceeded);
40841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
41841a502864d307341945eae62569b92cada93e75Sunny Goyal
42841a502864d307341945eae62569b92cada93e75Sunny Goyal    private static final int DEFAULT_COMPRESS_QUALITY = 90;
43841a502864d307341945eae62569b92cada93e75Sunny Goyal    private static final String TAG = "CropAndSetWallpaperTask";
44841a502864d307341945eae62569b92cada93e75Sunny Goyal
45841a502864d307341945eae62569b92cada93e75Sunny Goyal    private final InputStreamProvider mStreamProvider;
46841a502864d307341945eae62569b92cada93e75Sunny Goyal    private final Context mContext;
47841a502864d307341945eae62569b92cada93e75Sunny Goyal
48841a502864d307341945eae62569b92cada93e75Sunny Goyal    private final RectF mCropBounds;
49841a502864d307341945eae62569b92cada93e75Sunny Goyal    private int mOutWidth, mOutHeight;
50841a502864d307341945eae62569b92cada93e75Sunny Goyal    private int mRotation;
51841a502864d307341945eae62569b92cada93e75Sunny Goyal    private CropAndSetWallpaperTask.OnEndCropHandler mOnEndCropHandler;
52841a502864d307341945eae62569b92cada93e75Sunny Goyal    private CropAndSetWallpaperTask.OnBitmapCroppedHandler mOnBitmapCroppedHandler;
53841a502864d307341945eae62569b92cada93e75Sunny Goyal
54841a502864d307341945eae62569b92cada93e75Sunny Goyal    public CropAndSetWallpaperTask(InputStreamProvider streamProvider, Context context,
55841a502864d307341945eae62569b92cada93e75Sunny Goyal                                   RectF cropBounds, int rotation, int outWidth, int outHeight,
56841a502864d307341945eae62569b92cada93e75Sunny Goyal                                   OnEndCropHandler onEndCropHandler) {
57841a502864d307341945eae62569b92cada93e75Sunny Goyal        mStreamProvider = streamProvider;
58841a502864d307341945eae62569b92cada93e75Sunny Goyal        mContext = context;
59841a502864d307341945eae62569b92cada93e75Sunny Goyal
60841a502864d307341945eae62569b92cada93e75Sunny Goyal        mCropBounds = cropBounds;
61841a502864d307341945eae62569b92cada93e75Sunny Goyal        mRotation = rotation;
62841a502864d307341945eae62569b92cada93e75Sunny Goyal        mOutWidth = outWidth;
63841a502864d307341945eae62569b92cada93e75Sunny Goyal        mOutHeight = outHeight;
64841a502864d307341945eae62569b92cada93e75Sunny Goyal        mOnEndCropHandler = onEndCropHandler;
65841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
66841a502864d307341945eae62569b92cada93e75Sunny Goyal
67841a502864d307341945eae62569b92cada93e75Sunny Goyal    public void setOnBitmapCropped(CropAndSetWallpaperTask.OnBitmapCroppedHandler handler) {
68841a502864d307341945eae62569b92cada93e75Sunny Goyal        mOnBitmapCroppedHandler = handler;
69841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
70841a502864d307341945eae62569b92cada93e75Sunny Goyal
71841a502864d307341945eae62569b92cada93e75Sunny Goyal    public boolean cropBitmap(int whichWallpaper) {
72841a502864d307341945eae62569b92cada93e75Sunny Goyal        Bitmap crop = mStreamProvider.readCroppedBitmap(
73841a502864d307341945eae62569b92cada93e75Sunny Goyal                mCropBounds, mOutWidth, mOutHeight, mRotation);
74841a502864d307341945eae62569b92cada93e75Sunny Goyal        if (crop == null) {
75841a502864d307341945eae62569b92cada93e75Sunny Goyal            return false;
76841a502864d307341945eae62569b92cada93e75Sunny Goyal        }
77841a502864d307341945eae62569b92cada93e75Sunny Goyal
78841a502864d307341945eae62569b92cada93e75Sunny Goyal        boolean failure = false;
79841a502864d307341945eae62569b92cada93e75Sunny Goyal        // Compress to byte array
80841a502864d307341945eae62569b92cada93e75Sunny Goyal        ByteArrayOutputStream tmpOut = new ByteArrayOutputStream(2048);
81841a502864d307341945eae62569b92cada93e75Sunny Goyal        if (crop.compress(CompressFormat.JPEG, DEFAULT_COMPRESS_QUALITY, tmpOut)) {
82841a502864d307341945eae62569b92cada93e75Sunny Goyal            // Set the wallpaper
83841a502864d307341945eae62569b92cada93e75Sunny Goyal            try {
84841a502864d307341945eae62569b92cada93e75Sunny Goyal                byte[] outByteArray = tmpOut.toByteArray();
85841a502864d307341945eae62569b92cada93e75Sunny Goyal                WallpaperManagerCompat.getInstance(mContext).setStream(
86841a502864d307341945eae62569b92cada93e75Sunny Goyal                        new ByteArrayInputStream(outByteArray),
87841a502864d307341945eae62569b92cada93e75Sunny Goyal                        null, true, whichWallpaper);
88841a502864d307341945eae62569b92cada93e75Sunny Goyal                if (mOnBitmapCroppedHandler != null) {
89841a502864d307341945eae62569b92cada93e75Sunny Goyal                    mOnBitmapCroppedHandler.onBitmapCropped(outByteArray);
90841a502864d307341945eae62569b92cada93e75Sunny Goyal                }
91841a502864d307341945eae62569b92cada93e75Sunny Goyal            } catch (IOException e) {
92841a502864d307341945eae62569b92cada93e75Sunny Goyal                Log.w(TAG, "cannot write stream to wallpaper", e);
93841a502864d307341945eae62569b92cada93e75Sunny Goyal                failure = true;
94841a502864d307341945eae62569b92cada93e75Sunny Goyal            }
95841a502864d307341945eae62569b92cada93e75Sunny Goyal        } else {
96841a502864d307341945eae62569b92cada93e75Sunny Goyal            Log.w(TAG, "cannot compress bitmap");
97841a502864d307341945eae62569b92cada93e75Sunny Goyal            failure = true;
98841a502864d307341945eae62569b92cada93e75Sunny Goyal        }
99841a502864d307341945eae62569b92cada93e75Sunny Goyal        return !failure; // True if any of the operations failed
100841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
101841a502864d307341945eae62569b92cada93e75Sunny Goyal
102841a502864d307341945eae62569b92cada93e75Sunny Goyal    @Override
103841a502864d307341945eae62569b92cada93e75Sunny Goyal    protected Boolean doInBackground(Integer... whichWallpaper) {
104841a502864d307341945eae62569b92cada93e75Sunny Goyal        return cropBitmap(whichWallpaper[0]);
105841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
106841a502864d307341945eae62569b92cada93e75Sunny Goyal
107841a502864d307341945eae62569b92cada93e75Sunny Goyal    @Override
108841a502864d307341945eae62569b92cada93e75Sunny Goyal    protected void onPostExecute(Boolean cropSucceeded) {
109841a502864d307341945eae62569b92cada93e75Sunny Goyal        if (!cropSucceeded) {
110841a502864d307341945eae62569b92cada93e75Sunny Goyal            Toast.makeText(mContext, R.string.wallpaper_set_fail, Toast.LENGTH_SHORT).show();
111841a502864d307341945eae62569b92cada93e75Sunny Goyal        }
112841a502864d307341945eae62569b92cada93e75Sunny Goyal        if (mOnEndCropHandler != null) {
113841a502864d307341945eae62569b92cada93e75Sunny Goyal            mOnEndCropHandler.run(cropSucceeded);
114841a502864d307341945eae62569b92cada93e75Sunny Goyal        }
115841a502864d307341945eae62569b92cada93e75Sunny Goyal    }
116841a502864d307341945eae62569b92cada93e75Sunny Goyal}