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 Doddpackage com.android.messaging.ui.mediapicker;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.Activity;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.Fragment;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Intent;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Bundle;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.Factory;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.PendingAttachmentData;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.UIIntents;
2769ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylorimport com.android.messaging.util.LogUtil;
2869ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylorimport com.android.messaging.util.FileUtil;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ImageUtils;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.SafeAsyncTask;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Wraps around the functionalities to allow the user to pick images from the document
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * picker.  Instances of this class must be tied to a Fragment which is able to delegate activity
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * result callbacks.
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class DocumentImagePicker {
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * An interface for a listener that listens for when a document has been picked.
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public interface SelectionListener {
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        /**
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd         * Called when an document is selected from picker. At this point, the file hasn't been
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd         * actually loaded and staged in the temp directory, so we are passing in a pending
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd         * MessagePartData, which the consumer should use to display a placeholder image.
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd         * @param pendingItem a temporary attachment data for showing the placeholder state.
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd         */
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        void onDocumentSelected(PendingAttachmentData pendingItem);
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // The owning fragment.
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Fragment mFragment;
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // The listener on the picker events.
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final SelectionListener mListener;
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String EXTRA_PHOTO_URL = "photo_url";
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Creates a new instance of DocumentImagePicker.
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param activity The activity that owns the picker, or the activity that hosts the owning
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        fragment.
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public DocumentImagePicker(final Fragment fragment,
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final SelectionListener listener) {
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mFragment = fragment;
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListener = listener;
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Intent out to open an image/video from document picker.
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void launchPicker() {
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        UIIntents.get().launchDocumentImagePicker(mFragment);
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Must be called from the fragment/activity's onActivityResult().
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (requestCode == UIIntents.REQUEST_PICK_IMAGE_FROM_DOCUMENT_PICKER &&
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                resultCode == Activity.RESULT_OK) {
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Sometimes called after media item has been picked from the document picker.
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            String url = data.getStringExtra(EXTRA_PHOTO_URL);
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (url == null) {
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // we're using the builtin photo picker which supplies the return
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // url as it's "data"
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                url = data.getDataString();
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (url == null) {
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final Bundle extras = data.getExtras();
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    if (extras != null) {
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        final Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        if (uri != null) {
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            url = uri.toString();
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        }
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Guard against null uri cases for when the activity returns a null/invalid intent.
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (url != null) {
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final Uri uri = Uri.parse(url);
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                prepareDocumentForAttachment(uri);
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private void prepareDocumentForAttachment(final Uri documentUri) {
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Notify our listener with a PendingAttachmentData containing the metadata.
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Asynchronously get the content type for the picked image since
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // ImageUtils.getContentType() potentially involves I/O and can be expensive.
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        new SafeAsyncTask<Void, Void, String>() {
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            protected String doInBackgroundTimed(final Void... params) {
116a43c5c5f18d966355a63db194c931e02267a2695Tom Taylor                if (FileUtil.isInPrivateDir(documentUri)) {
11769ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                    // hacker sending private app data. Bail out
11869ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                    if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.ERROR)) {
11969ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                        LogUtil.e(LogUtil.BUGLE_TAG, "Aborting attach of private app data ("
12069ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                                + documentUri + ")");
12169ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                    }
12269ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                    return null;
12369ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                }
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return ImageUtils.getContentType(
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        Factory.get().getApplicationContext().getContentResolver(), documentUri);
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            protected void onPostExecute(final String contentType) {
13069ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                if (contentType == null) {
13169ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                    return;     // bad uri on input
13269ed579fb8092395c4ffeb64ff5147622def3d4aTom Taylor                }
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // Ask the listener to create a temporary placeholder item to show the progress.
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final PendingAttachmentData pendingItem =
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        PendingAttachmentData.createPendingAttachmentData(contentType,
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                                documentUri);
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mListener.onDocumentSelected(pendingItem);
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }.executeOnThreadPool();
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
142