1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui.mediapicker;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Bitmap;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.BitmapFactory;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Canvas;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Matrix;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.MediaScratchFileProvider;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ContentType;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.SafeAsyncTask;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.exif.ExifInterface;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.exif.ExifTag;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.io.IOException;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.io.OutputStream;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ImagePersistTask extends SafeAsyncTask<Void, Void, Void> {
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String JPEG_EXTENSION = "jpg";
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String TAG = LogUtil.BUGLE_TAG;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mWidth;
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mHeight;
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final float mHeightPercent;
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final byte[] mBytes;
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Context mContext;
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final CameraManager.MediaCallback mCallback;
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private Uri mOutputUri;
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private Exception mException;
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public ImagePersistTask(
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int width,
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int height,
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final float heightPercent,
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final byte[] bytes,
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Context context,
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final CameraManager.MediaCallback callback) {
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.isTrue(heightPercent >= 0 && heightPercent <= 1);
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.notNull(bytes);
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.notNull(context);
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.notNull(callback);
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mWidth = width;
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mHeight = height;
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mHeightPercent = heightPercent;
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mBytes = bytes;
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mContext = context;
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mCallback = callback;
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // TODO: We probably want to store directly in MMS storage to prevent this
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // intermediate step
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mOutputUri = MediaScratchFileProvider.buildMediaScratchSpaceUri(JPEG_EXTENSION);
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected Void doInBackgroundTimed(final Void... params) {
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        OutputStream outputStream = null;
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Bitmap bitmap = null;
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Bitmap clippedBitmap = null;
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        try {
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            outputStream =
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mContext.getContentResolver().openOutputStream(mOutputUri);
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (mHeightPercent != 1.0f) {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                int orientation = android.media.ExifInterface.ORIENTATION_UNDEFINED;
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final ExifInterface exifInterface = new ExifInterface();
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                try {
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    exifInterface.readExif(mBytes);
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final Integer orientationValue =
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            exifInterface.getTagIntValue(ExifInterface.TAG_ORIENTATION);
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    if (orientationValue != null) {
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        orientation = orientationValue.intValue();
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    // The thumbnail is of the full image, but we're cropping it, so just clear
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    // the thumbnail
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    exifInterface.setCompressedThumbnail((byte[]) null);
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                } catch (IOException e) {
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    // Couldn't get exif tags, not the end of the world
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                bitmap = BitmapFactory.decodeByteArray(mBytes, 0, mBytes.length);
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final int clippedWidth;
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final int clippedHeight;
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (ExifInterface.getOrientationParams(orientation).invertDimensions) {
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.equals(mWidth, bitmap.getHeight());
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.equals(mHeight, bitmap.getWidth());
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    clippedWidth = (int) (mHeight * mHeightPercent);
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    clippedHeight = mWidth;
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                } else {
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.equals(mWidth, bitmap.getWidth());
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.equals(mHeight, bitmap.getHeight());
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    clippedWidth = mWidth;
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    clippedHeight = (int) (mHeight * mHeightPercent);
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final int offsetTop = (bitmap.getHeight() - clippedHeight) / 2;
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final int offsetLeft = (bitmap.getWidth() - clippedWidth) / 2;
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mWidth = clippedWidth;
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mHeight = clippedHeight;
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                clippedBitmap = Bitmap.createBitmap(clippedWidth, clippedHeight,
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        Bitmap.Config.ARGB_8888);
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                clippedBitmap.setDensity(bitmap.getDensity());
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final Canvas clippedBitmapCanvas = new Canvas(clippedBitmap);
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final Matrix matrix = new Matrix();
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                matrix.postTranslate(-offsetLeft, -offsetTop);
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                clippedBitmapCanvas.drawBitmap(bitmap, matrix, null /* paint */);
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                clippedBitmapCanvas.save();
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // EXIF data can take a big chunk of the file size and is often cleared by the
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // carrier, only store orientation since that's critical
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                ExifTag orientationTag = exifInterface.getTag(ExifInterface.TAG_ORIENTATION);
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                exifInterface.clearExif();
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                exifInterface.setTag(orientationTag);
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                exifInterface.writeExif(clippedBitmap, outputStream);
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            } else {
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                outputStream.write(mBytes);
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } catch (final IOException e) {
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mOutputUri = null;
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mException = e;
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            LogUtil.e(TAG, "Unable to persist image to temp storage " + e);
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } finally {
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (bitmap != null) {
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                bitmap.recycle();
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (clippedBitmap != null) {
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                clippedBitmap.recycle();
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (outputStream != null) {
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                try {
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    outputStream.flush();
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                } catch (final IOException e) {
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mOutputUri = null;
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mException = e;
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    LogUtil.e(TAG, "error trying to flush and close the outputStream" + e);
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                } finally {
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    try {
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        outputStream.close();
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    } catch (final IOException e) {
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        // Do nothing.
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return null;
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected void onPostExecute(final Void aVoid) {
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mOutputUri != null) {
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mCallback.onMediaReady(mOutputUri, ContentType.IMAGE_JPEG, mWidth, mHeight);
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            Assert.notNull(mException);
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mCallback.onMediaFailed(mException);
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
173