2013-02-04 02:41:34 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2013-02-04 02:41:34 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-02-04 02:41:34 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2014-10-16 08:48:59 +00:00
|
|
|
import android.content.ActivityNotFoundException;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2015-09-05 00:33:22 +00:00
|
|
|
import android.os.AsyncTask;
|
2014-03-02 12:17:03 +00:00
|
|
|
import android.os.Build;
|
2014-06-03 23:24:44 +00:00
|
|
|
import android.provider.ContactsContract;
|
2015-07-11 01:45:55 +00:00
|
|
|
import android.provider.MediaStore;
|
2015-09-05 00:33:22 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-04-16 05:38:33 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2015-11-18 20:54:40 +00:00
|
|
|
import android.text.TextUtils;
|
2015-03-31 22:44:41 +00:00
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.view.View;
|
2014-10-16 08:48:59 +00:00
|
|
|
import android.widget.Toast;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-04 02:41:34 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-10-21 22:32:29 +00:00
|
|
|
import org.thoughtcrime.securesms.components.AudioView;
|
|
|
|
import org.thoughtcrime.securesms.components.RemovableMediaView;
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.components.ThumbnailView;
|
2015-04-16 05:38:33 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
2015-07-14 00:35:34 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2015-07-15 20:42:59 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2015-11-17 02:38:36 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture.Listener;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2015-11-17 02:38:36 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2015-11-18 20:54:40 +00:00
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class AttachmentManager {
|
2015-10-21 22:32:29 +00:00
|
|
|
|
2014-10-16 08:48:59 +00:00
|
|
|
private final static String TAG = AttachmentManager.class.getSimpleName();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2015-10-21 22:32:29 +00:00
|
|
|
private final @NonNull Context context;
|
|
|
|
private final @NonNull View attachmentView;
|
|
|
|
private final @NonNull RemovableMediaView removableMediaView;
|
|
|
|
private final @NonNull ThumbnailView thumbnail;
|
|
|
|
private final @NonNull AudioView audioView;
|
|
|
|
private final @NonNull AttachmentListener attachmentListener;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
private @NonNull Optional<Slide> slide = Optional.absent();
|
|
|
|
private @Nullable Uri captureUri;
|
2015-05-18 17:26:32 +00:00
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
public AttachmentManager(@NonNull Activity activity, @NonNull AttachmentListener listener) {
|
|
|
|
this.attachmentView = ViewUtil.findById(activity, R.id.attachment_editor);
|
|
|
|
this.thumbnail = ViewUtil.findById(activity, R.id.attachment_thumbnail);
|
|
|
|
this.audioView = ViewUtil.findById(activity, R.id.attachment_audio);
|
|
|
|
this.removableMediaView = ViewUtil.findById(activity, R.id.removable_media_view);
|
|
|
|
this.context = activity;
|
2014-04-15 10:43:14 +00:00
|
|
|
this.attachmentListener = listener;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2015-10-21 22:32:29 +00:00
|
|
|
removableMediaView.setRemoveClickListener(new RemoveButtonListener());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void clear() {
|
2015-11-17 02:38:36 +00:00
|
|
|
ViewUtil.fadeOut(attachmentView, 200).addListener(new Listener<Boolean>() {
|
2015-10-01 23:48:57 +00:00
|
|
|
@Override
|
2015-11-17 02:38:36 +00:00
|
|
|
public void onSuccess(Boolean result) {
|
2015-09-05 00:33:22 +00:00
|
|
|
thumbnail.clear();
|
2015-06-17 18:54:12 +00:00
|
|
|
attachmentView.setVisibility(View.GONE);
|
|
|
|
attachmentListener.onAttachmentChanged();
|
|
|
|
}
|
|
|
|
|
2015-11-17 02:38:36 +00:00
|
|
|
@Override
|
|
|
|
public void onFailure(ExecutionException e) {}
|
|
|
|
});
|
|
|
|
slide = Optional.absent();
|
2015-10-21 22:32:29 +00:00
|
|
|
audioView.cleanup();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2015-05-18 17:26:32 +00:00
|
|
|
public void cleanup() {
|
2015-10-15 21:40:45 +00:00
|
|
|
cleanup(captureUri);
|
|
|
|
cleanup(getSlideUri());
|
|
|
|
|
2015-06-08 18:07:46 +00:00
|
|
|
captureUri = null;
|
2015-10-15 21:40:45 +00:00
|
|
|
slide = Optional.absent();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanup(final @Nullable Uri uri) {
|
|
|
|
if (uri != null && PersistentBlobProvider.isAuthority(context, uri)) {
|
|
|
|
Log.w(TAG, "cleaning up " + uri);
|
|
|
|
PersistentBlobProvider.getInstance(context).delete(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setSlide(@NonNull Slide slide) {
|
|
|
|
if (getSlideUri() != null) cleanup(getSlideUri());
|
|
|
|
if (captureUri != null && slide.getUri() != captureUri) cleanup(captureUri);
|
|
|
|
|
|
|
|
this.captureUri = null;
|
|
|
|
this.slide = Optional.of(slide);
|
2015-05-18 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
public void setMedia(@NonNull final MasterSecret masterSecret,
|
|
|
|
@NonNull final Uri uri,
|
|
|
|
@NonNull final MediaType mediaType,
|
|
|
|
@NonNull final MediaConstraints constraints)
|
2015-07-28 20:17:01 +00:00
|
|
|
{
|
2015-09-05 00:33:22 +00:00
|
|
|
new AsyncTask<Void, Void, Slide>() {
|
2015-10-01 23:48:57 +00:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
2015-09-05 00:33:22 +00:00
|
|
|
thumbnail.clear();
|
|
|
|
thumbnail.showProgressSpinner();
|
|
|
|
attachmentView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2014-10-28 15:36:27 +00:00
|
|
|
|
2015-10-01 23:48:57 +00:00
|
|
|
@Override
|
|
|
|
protected @Nullable Slide doInBackground(Void... params) {
|
2015-09-05 00:33:22 +00:00
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
try {
|
|
|
|
final long mediaSize = MediaUtil.getMediaSize(context, masterSecret, uri);
|
|
|
|
final Slide slide = mediaType.createSlide(context, uri, mediaSize);
|
|
|
|
Log.w(TAG, "slide with size " + mediaSize + " took " + (System.currentTimeMillis() - start) + "ms");
|
|
|
|
return slide;
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 05:38:33 +00:00
|
|
|
|
2015-10-01 23:48:57 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(@Nullable final Slide slide) {
|
2015-09-05 00:33:22 +00:00
|
|
|
if (slide == null) {
|
|
|
|
attachmentView.setVisibility(View.GONE);
|
|
|
|
Toast.makeText(context,
|
|
|
|
R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
} else if (!areConstraintsSatisfied(context, masterSecret, slide, constraints)) {
|
|
|
|
attachmentView.setVisibility(View.GONE);
|
|
|
|
Toast.makeText(context,
|
|
|
|
R.string.ConversationActivity_attachment_exceeds_size_limits,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
2015-10-15 21:40:45 +00:00
|
|
|
setSlide(slide);
|
2015-09-05 00:33:22 +00:00
|
|
|
attachmentView.setVisibility(View.VISIBLE);
|
2015-10-21 22:32:29 +00:00
|
|
|
|
|
|
|
if (slide.hasAudio()) {
|
|
|
|
audioView.setAudio(masterSecret, (AudioSlide)slide, false);
|
|
|
|
removableMediaView.display(audioView);
|
|
|
|
} else {
|
|
|
|
thumbnail.setImageResource(masterSecret, slide, false);
|
|
|
|
removableMediaView.display(thumbnail);
|
|
|
|
}
|
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
attachmentListener.onAttachmentChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
2014-10-28 15:36:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public boolean isAttachmentPresent() {
|
|
|
|
return attachmentView.getVisibility() == View.VISIBLE;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
public @NonNull SlideDeck buildSlideDeck() {
|
|
|
|
SlideDeck deck = new SlideDeck();
|
|
|
|
if (slide.isPresent()) deck.addSlide(slide.get());
|
|
|
|
return deck;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static void selectVideo(Activity activity, int requestCode) {
|
|
|
|
selectMediaType(activity, "video/*", requestCode);
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static void selectImage(Activity activity, int requestCode) {
|
|
|
|
selectMediaType(activity, "image/*", requestCode);
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static void selectAudio(Activity activity, int requestCode) {
|
|
|
|
selectMediaType(activity, "audio/*", requestCode);
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2014-06-03 23:24:44 +00:00
|
|
|
public static void selectContactInfo(Activity activity, int requestCode) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
private @Nullable Uri getSlideUri() {
|
|
|
|
return slide.isPresent() ? slide.get().getUri() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @Nullable Uri getCaptureUri() {
|
2015-07-11 01:45:55 +00:00
|
|
|
return captureUri;
|
|
|
|
}
|
|
|
|
|
2015-07-14 00:35:34 +00:00
|
|
|
public void capturePhoto(Activity activity, Recipients recipients, int requestCode) {
|
2015-07-11 01:45:55 +00:00
|
|
|
try {
|
|
|
|
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
|
if (captureIntent.resolveActivity(activity.getPackageManager()) != null) {
|
2015-10-15 21:40:45 +00:00
|
|
|
if (captureUri == null) {
|
|
|
|
captureUri = PersistentBlobProvider.getInstance(context).createForExternal(recipients);
|
|
|
|
}
|
|
|
|
Log.w(TAG, "captureUri path is " + captureUri.getPath());
|
2015-07-11 01:45:55 +00:00
|
|
|
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);
|
|
|
|
activity.startActivityForResult(captureIntent, requestCode);
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static void selectMediaType(Activity activity, String type, int requestCode) {
|
2014-10-16 08:48:59 +00:00
|
|
|
final Intent intent = new Intent();
|
|
|
|
intent.setType(type);
|
2014-03-02 12:17:03 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
2014-10-16 08:48:59 +00:00
|
|
|
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
|
|
|
|
try {
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
return;
|
|
|
|
} catch (ActivityNotFoundException anfe) {
|
|
|
|
Log.w(TAG, "couldn't complete ACTION_OPEN_DOCUMENT, no activity found. falling back.");
|
|
|
|
}
|
2014-03-02 12:17:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 08:48:59 +00:00
|
|
|
intent.setAction(Intent.ACTION_GET_CONTENT);
|
|
|
|
try {
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
} catch (ActivityNotFoundException anfe) {
|
|
|
|
Log.w(TAG, "couldn't complete ACTION_GET_CONTENT intent, no activity found. falling back.");
|
|
|
|
Toast.makeText(activity, R.string.AttachmentManager_cant_open_media_selection, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
private boolean areConstraintsSatisfied(final @NonNull Context context,
|
|
|
|
final @NonNull MasterSecret masterSecret,
|
|
|
|
final @Nullable Slide slide,
|
|
|
|
final @NonNull MediaConstraints constraints)
|
|
|
|
{
|
2015-10-13 01:25:05 +00:00
|
|
|
return slide == null ||
|
|
|
|
constraints.isSatisfied(context, masterSecret, slide.asAttachment()) ||
|
|
|
|
constraints.canResize(slide.asAttachment());
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class RemoveButtonListener implements View.OnClickListener {
|
2013-02-04 02:41:34 +00:00
|
|
|
@Override
|
2011-12-20 18:20:44 +00:00
|
|
|
public void onClick(View v) {
|
2015-05-18 17:26:32 +00:00
|
|
|
cleanup();
|
2015-10-15 21:40:45 +00:00
|
|
|
clear();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 10:43:14 +00:00
|
|
|
public interface AttachmentListener {
|
2015-06-08 18:07:46 +00:00
|
|
|
void onAttachmentChanged();
|
2014-04-15 10:43:14 +00:00
|
|
|
}
|
2015-09-05 00:33:22 +00:00
|
|
|
|
|
|
|
public enum MediaType {
|
|
|
|
IMAGE, GIF, AUDIO, VIDEO;
|
|
|
|
|
|
|
|
public @NonNull Slide createSlide(@NonNull Context context,
|
|
|
|
@NonNull Uri uri,
|
|
|
|
long dataSize)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
switch (this) {
|
|
|
|
case IMAGE: return new ImageSlide(context, uri, dataSize);
|
|
|
|
case GIF: return new GifSlide(context, uri, dataSize);
|
|
|
|
case AUDIO: return new AudioSlide(context, uri, dataSize);
|
|
|
|
case VIDEO: return new VideoSlide(context, uri, dataSize);
|
|
|
|
default: throw new AssertionError("unrecognized enum");
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 20:54:40 +00:00
|
|
|
|
|
|
|
public static @Nullable MediaType from(final @Nullable String mimeType) {
|
|
|
|
if (TextUtils.isEmpty(mimeType)) return null;
|
|
|
|
if (MediaUtil.isGif(mimeType)) return GIF;
|
|
|
|
if (ContentType.isImageType(mimeType)) return IMAGE;
|
|
|
|
if (ContentType.isAudioType(mimeType)) return AUDIO;
|
|
|
|
if (ContentType.isVideoType(mimeType)) return VIDEO;
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|