2017-10-16 20:11:42 +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;
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.SuppressLint;
|
2011-12-20 18:20:44 +00:00
|
|
|
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;
|
2017-03-28 19:05:30 +00:00
|
|
|
import android.database.Cursor;
|
2015-12-18 22:37:11 +00:00
|
|
|
import android.graphics.Bitmap;
|
2018-07-20 21:24:04 +00:00
|
|
|
import android.graphics.PorterDuff;
|
2011-12-20 18:20:44 +00:00
|
|
|
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;
|
2017-03-28 19:05:30 +00:00
|
|
|
import android.provider.OpenableColumns;
|
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;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-03-20 18:27:11 +00:00
|
|
|
import android.util.Pair;
|
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
|
|
|
|
2015-12-18 22:37:11 +00:00
|
|
|
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
|
|
|
|
import com.google.android.gms.common.GooglePlayServicesRepairableException;
|
|
|
|
import com.google.android.gms.location.places.ui.PlacePicker;
|
|
|
|
|
2015-12-11 09:39:55 +00:00
|
|
|
import org.thoughtcrime.securesms.MediaPreviewActivity;
|
2013-02-04 02:41:34 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2018-03-20 18:27:11 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2015-10-21 22:32:29 +00:00
|
|
|
import org.thoughtcrime.securesms.components.AudioView;
|
2017-03-28 19:05:30 +00:00
|
|
|
import org.thoughtcrime.securesms.components.DocumentView;
|
2016-12-08 22:20:38 +00:00
|
|
|
import org.thoughtcrime.securesms.components.RemovableEditableMediaView;
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.components.ThumbnailView;
|
2016-01-04 21:02:22 +00:00
|
|
|
import org.thoughtcrime.securesms.components.location.SignalMapView;
|
2015-12-18 22:37:11 +00:00
|
|
|
import org.thoughtcrime.securesms.components.location.SignalPlace;
|
2016-10-17 02:05:07 +00:00
|
|
|
import org.thoughtcrime.securesms.giph.ui.GiphyActivity;
|
2017-11-25 06:00:30 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
2016-12-08 22:20:38 +00:00
|
|
|
import org.thoughtcrime.securesms.scribbles.ScribbleActivity;
|
2015-12-18 22:37:11 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2015-07-15 20:42:59 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2018-07-20 21:24:04 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ThemeUtil;
|
2015-10-15 21:40:45 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2015-12-18 22:37:11 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
|
2015-11-17 02:38:36 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture.Listener;
|
2018-07-25 15:30:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SettableFuture;
|
2017-01-18 20:27:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.views.Stub;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2015-11-30 19:16:30 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
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
|
|
|
|
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
|
|
|
|
2016-12-08 22:20:38 +00:00
|
|
|
private final @NonNull Context context;
|
2017-01-18 20:27:48 +00:00
|
|
|
private final @NonNull Stub<View> attachmentViewStub;
|
2016-12-08 22:20:38 +00:00
|
|
|
private final @NonNull AttachmentListener attachmentListener;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2017-01-18 20:27:48 +00:00
|
|
|
private RemovableEditableMediaView removableMediaView;
|
|
|
|
private ThumbnailView thumbnail;
|
|
|
|
private AudioView audioView;
|
2017-03-28 19:05:30 +00:00
|
|
|
private DocumentView documentView;
|
2017-01-18 20:27:48 +00:00
|
|
|
private SignalMapView mapView;
|
|
|
|
|
2015-11-30 19:16:30 +00:00
|
|
|
private @NonNull List<Uri> garbage = new LinkedList<>();
|
|
|
|
private @NonNull Optional<Slide> slide = Optional.absent();
|
2015-10-15 21:40:45 +00:00
|
|
|
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.context = activity;
|
2014-04-15 10:43:14 +00:00
|
|
|
this.attachmentListener = listener;
|
2017-01-18 20:27:48 +00:00
|
|
|
this.attachmentViewStub = ViewUtil.findStubById(activity, R.id.attachment_editor_stub);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void inflateStub() {
|
|
|
|
if (!attachmentViewStub.resolved()) {
|
|
|
|
View root = attachmentViewStub.get();
|
|
|
|
|
|
|
|
this.thumbnail = ViewUtil.findById(root, R.id.attachment_thumbnail);
|
|
|
|
this.audioView = ViewUtil.findById(root, R.id.attachment_audio);
|
2017-03-28 19:05:30 +00:00
|
|
|
this.documentView = ViewUtil.findById(root, R.id.attachment_document);
|
2017-01-18 20:27:48 +00:00
|
|
|
this.mapView = ViewUtil.findById(root, R.id.attachment_location);
|
|
|
|
this.removableMediaView = ViewUtil.findById(root, R.id.removable_media_view);
|
|
|
|
|
|
|
|
removableMediaView.setRemoveClickListener(new RemoveButtonListener());
|
|
|
|
removableMediaView.setEditClickListener(new EditButtonListener());
|
|
|
|
thumbnail.setOnClickListener(new ThumbnailClickListener());
|
2018-07-20 21:24:04 +00:00
|
|
|
documentView.getBackground().setColorFilter(ThemeUtil.getThemedColor(context, R.attr.conversation_item_bubble_background), PorterDuff.Mode.MULTIPLY);
|
2017-01-18 20:27:48 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
public void clear(@NonNull GlideRequests glideRequests, boolean animate) {
|
2017-01-18 20:27:48 +00:00
|
|
|
if (attachmentViewStub.resolved()) {
|
2015-06-17 18:54:12 +00:00
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
if (animate) {
|
|
|
|
ViewUtil.fadeOut(attachmentViewStub.get(), 200).addListener(new Listener<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(Boolean result) {
|
2017-10-16 20:11:42 +00:00
|
|
|
thumbnail.clear(glideRequests);
|
2017-04-22 23:29:26 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.GONE);
|
|
|
|
attachmentListener.onAttachmentChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(ExecutionException e) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-10-16 20:11:42 +00:00
|
|
|
thumbnail.clear(glideRequests);
|
2017-04-22 23:29:26 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.GONE);
|
|
|
|
attachmentListener.onAttachmentChanged();
|
|
|
|
}
|
2015-11-30 19:16:30 +00:00
|
|
|
|
2017-01-18 20:27:48 +00:00
|
|
|
markGarbage(getSlideUri());
|
|
|
|
slide = Optional.absent();
|
|
|
|
|
|
|
|
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();
|
2015-11-30 19:16:30 +00:00
|
|
|
|
|
|
|
Iterator<Uri> iterator = garbage.listIterator();
|
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
cleanup(iterator.next());
|
|
|
|
iterator.remove();
|
|
|
|
}
|
2015-10-15 21:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanup(final @Nullable Uri uri) {
|
|
|
|
if (uri != null && PersistentBlobProvider.isAuthority(context, uri)) {
|
|
|
|
Log.w(TAG, "cleaning up " + uri);
|
2018-01-25 03:17:44 +00:00
|
|
|
PersistentBlobProvider.getInstance(context).delete(context, uri);
|
2015-10-15 21:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 19:16:30 +00:00
|
|
|
private void markGarbage(@Nullable Uri uri) {
|
|
|
|
if (uri != null && PersistentBlobProvider.isAuthority(context, uri)) {
|
|
|
|
Log.w(TAG, "Marking garbage that needs cleaning: " + uri);
|
|
|
|
garbage.add(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:40:45 +00:00
|
|
|
private void setSlide(@NonNull Slide slide) {
|
2015-11-24 01:56:41 +00:00
|
|
|
if (getSlideUri() != null) cleanup(getSlideUri());
|
|
|
|
if (captureUri != null && !captureUri.equals(slide.getUri())) cleanup(captureUri);
|
2015-10-15 21:40:45 +00:00
|
|
|
|
|
|
|
this.captureUri = null;
|
|
|
|
this.slide = Optional.of(slide);
|
2015-05-18 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:30:48 +00:00
|
|
|
public ListenableFuture<Boolean> setLocation(@NonNull final SignalPlace place,
|
|
|
|
@NonNull final MediaConstraints constraints)
|
2015-12-18 22:37:11 +00:00
|
|
|
{
|
2017-01-18 20:27:48 +00:00
|
|
|
inflateStub();
|
|
|
|
|
2018-07-25 15:30:48 +00:00
|
|
|
SettableFuture<Boolean> returnResult = new SettableFuture<>();
|
|
|
|
ListenableFuture<Bitmap> future = mapView.display(place);
|
2015-12-18 22:37:11 +00:00
|
|
|
|
2017-01-18 20:27:48 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.VISIBLE);
|
2016-12-08 22:20:38 +00:00
|
|
|
removableMediaView.display(mapView, false);
|
2015-12-18 22:37:11 +00:00
|
|
|
|
|
|
|
future.addListener(new AssertedSuccessListener<Bitmap>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(@NonNull Bitmap result) {
|
|
|
|
byte[] blob = BitmapUtil.toByteArray(result);
|
2015-11-21 07:18:19 +00:00
|
|
|
Uri uri = PersistentBlobProvider.getInstance(context)
|
2018-01-25 03:17:44 +00:00
|
|
|
.create(context, blob, MediaUtil.IMAGE_PNG, null);
|
2016-01-04 21:02:22 +00:00
|
|
|
LocationSlide locationSlide = new LocationSlide(context, uri, blob.length, place);
|
2015-12-18 22:37:11 +00:00
|
|
|
|
|
|
|
setSlide(locationSlide);
|
|
|
|
attachmentListener.onAttachmentChanged();
|
2018-07-25 15:30:48 +00:00
|
|
|
returnResult.set(true);
|
2015-12-18 22:37:11 +00:00
|
|
|
}
|
|
|
|
});
|
2018-07-25 15:30:48 +00:00
|
|
|
|
|
|
|
return returnResult;
|
2015-12-18 22:37:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
2018-07-25 15:30:48 +00:00
|
|
|
public ListenableFuture<Boolean> setMedia(@NonNull final GlideRequests glideRequests,
|
|
|
|
@NonNull final Uri uri,
|
|
|
|
@NonNull final MediaType mediaType,
|
|
|
|
@NonNull final MediaConstraints constraints,
|
|
|
|
final int width,
|
|
|
|
final int height)
|
2017-05-08 22:32:59 +00:00
|
|
|
{
|
2017-01-18 20:27:48 +00:00
|
|
|
inflateStub();
|
|
|
|
|
2018-07-25 15:30:48 +00:00
|
|
|
final SettableFuture<Boolean> result = new SettableFuture<>();
|
|
|
|
|
2018-03-18 21:52:49 +00:00
|
|
|
new AsyncTask<Void, Void, Slide>() {
|
2015-10-01 23:48:57 +00:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
2017-10-16 20:11:42 +00:00
|
|
|
thumbnail.clear(glideRequests);
|
2015-09-05 00:33:22 +00:00
|
|
|
thumbnail.showProgressSpinner();
|
2017-01-18 20:27:48 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.VISIBLE);
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
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
|
|
|
try {
|
2017-03-28 19:05:30 +00:00
|
|
|
if (PartAuthority.isLocalUri(uri)) {
|
2018-03-20 18:27:11 +00:00
|
|
|
return getManuallyCalculatedSlideInfo(uri, width, height);
|
2017-03-28 19:05:30 +00:00
|
|
|
} else {
|
2018-03-20 18:27:11 +00:00
|
|
|
Slide result = getContentResolverSlideInfo(uri, width, height);
|
2017-03-28 19:05:30 +00:00
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
if (result == null) return getManuallyCalculatedSlideInfo(uri, width, height);
|
2017-04-02 16:29:26 +00:00
|
|
|
else return result;
|
2017-03-28 19:05:30 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
2017-04-02 16:29:26 +00:00
|
|
|
return null;
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2017-01-18 20:27:48 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.GONE);
|
2015-09-05 00:33:22 +00:00
|
|
|
Toast.makeText(context,
|
|
|
|
R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
2018-07-25 15:30:48 +00:00
|
|
|
result.set(false);
|
2018-01-25 03:17:44 +00:00
|
|
|
} else if (!areConstraintsSatisfied(context, slide, constraints)) {
|
2017-01-18 20:27:48 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.GONE);
|
2015-09-05 00:33:22 +00:00
|
|
|
Toast.makeText(context,
|
|
|
|
R.string.ConversationActivity_attachment_exceeds_size_limits,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
2018-07-25 15:30:48 +00:00
|
|
|
result.set(false);
|
2015-09-05 00:33:22 +00:00
|
|
|
} else {
|
2015-10-15 21:40:45 +00:00
|
|
|
setSlide(slide);
|
2017-01-18 20:27:48 +00:00
|
|
|
attachmentViewStub.get().setVisibility(View.VISIBLE);
|
2015-10-21 22:32:29 +00:00
|
|
|
|
|
|
|
if (slide.hasAudio()) {
|
2018-01-25 03:17:44 +00:00
|
|
|
audioView.setAudio((AudioSlide) slide, false);
|
2016-12-08 22:20:38 +00:00
|
|
|
removableMediaView.display(audioView, false);
|
2018-07-25 15:30:48 +00:00
|
|
|
result.set(true);
|
2017-03-28 19:05:30 +00:00
|
|
|
} else if (slide.hasDocument()) {
|
2017-04-02 16:29:26 +00:00
|
|
|
documentView.setDocument((DocumentSlide) slide, false);
|
2017-03-28 19:05:30 +00:00
|
|
|
removableMediaView.display(documentView, false);
|
2018-07-25 15:30:48 +00:00
|
|
|
result.set(true);
|
2015-10-21 22:32:29 +00:00
|
|
|
} else {
|
2018-03-20 18:27:11 +00:00
|
|
|
Attachment attachment = slide.asAttachment();
|
2018-07-25 15:30:48 +00:00
|
|
|
result.deferTo(thumbnail.setImageResource(glideRequests, slide, false, true, attachment.getWidth(), attachment.getHeight()));
|
2016-12-08 22:20:38 +00:00
|
|
|
removableMediaView.display(thumbnail, mediaType == MediaType.IMAGE);
|
2015-10-21 22:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
attachmentListener.onAttachmentChanged();
|
|
|
|
}
|
|
|
|
}
|
2017-04-02 16:29:26 +00:00
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
private @Nullable Slide getContentResolverSlideInfo(Uri uri, int width, int height) {
|
2017-04-02 16:29:26 +00:00
|
|
|
Cursor cursor = null;
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = context.getContentResolver().query(uri, null, null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
String fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
|
|
|
|
long fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
|
|
|
|
String mimeType = context.getContentResolver().getType(uri);
|
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
Pair<Integer, Integer> dimens = MediaUtil.getDimensions(context, mimeType, uri);
|
|
|
|
width = dimens.first;
|
|
|
|
height = dimens.second;
|
|
|
|
}
|
|
|
|
|
2017-04-02 16:29:26 +00:00
|
|
|
Log.w(TAG, "remote slide with size " + fileSize + " took " + (System.currentTimeMillis() - start) + "ms");
|
2018-03-20 18:27:11 +00:00
|
|
|
return mediaType.createSlide(context, uri, fileName, mimeType, fileSize, width, height);
|
2017-04-02 16:29:26 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
private @NonNull Slide getManuallyCalculatedSlideInfo(Uri uri, int width, int height) throws IOException {
|
2017-05-10 22:21:52 +00:00
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
Long mediaSize = null;
|
|
|
|
String fileName = null;
|
|
|
|
String mimeType = null;
|
|
|
|
|
|
|
|
if (PartAuthority.isLocalUri(uri)) {
|
2018-01-25 03:17:44 +00:00
|
|
|
mediaSize = PartAuthority.getAttachmentSize(context, uri);
|
|
|
|
fileName = PartAuthority.getAttachmentFileName(context, uri);
|
|
|
|
mimeType = PartAuthority.getAttachmentContentType(context, uri);
|
2017-05-10 22:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaSize == null) {
|
2018-01-25 03:17:44 +00:00
|
|
|
mediaSize = MediaUtil.getMediaSize(context, uri);
|
2017-05-10 22:21:52 +00:00
|
|
|
}
|
2017-04-02 16:29:26 +00:00
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
if (mimeType == null) {
|
|
|
|
mimeType = MediaUtil.getMimeType(context, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
Pair<Integer, Integer> dimens = MediaUtil.getDimensions(context, mimeType, uri);
|
|
|
|
width = dimens.first;
|
|
|
|
height = dimens.second;
|
|
|
|
}
|
|
|
|
|
2017-04-02 16:29:26 +00:00
|
|
|
Log.w(TAG, "local slide with size " + mediaSize + " took " + (System.currentTimeMillis() - start) + "ms");
|
2018-03-20 18:27:11 +00:00
|
|
|
return mediaType.createSlide(context, uri, fileName, mimeType, mediaSize, width, height);
|
2017-04-02 16:29:26 +00:00
|
|
|
}
|
2017-10-23 20:03:32 +00:00
|
|
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2018-07-25 15:30:48 +00:00
|
|
|
|
|
|
|
return result;
|
2014-10-28 15:36:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public boolean isAttachmentPresent() {
|
2017-01-18 20:27:48 +00:00
|
|
|
return attachmentViewStub.resolved() && attachmentViewStub.get().getVisibility() == View.VISIBLE;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-04-18 23:33:03 +00:00
|
|
|
public static void selectDocument(Activity activity, int requestCode) {
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(activity)
|
|
|
|
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(activity.getString(R.string.AttachmentManager_signal_requires_the_external_storage_permission_in_order_to_attach_photos_videos_or_audio))
|
|
|
|
.onAllGranted(() -> selectMediaType(activity, "*/*", null, requestCode))
|
|
|
|
.execute();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2017-04-18 23:33:03 +00:00
|
|
|
public static void selectGallery(Activity activity, int requestCode) {
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(activity)
|
|
|
|
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(activity.getString(R.string.AttachmentManager_signal_requires_the_external_storage_permission_in_order_to_attach_photos_videos_or_audio))
|
|
|
|
.onAllGranted(() -> selectMediaType(activity, "image/*", new String[] {"image/*", "video/*"}, requestCode))
|
|
|
|
.execute();
|
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 selectAudio(Activity activity, int requestCode) {
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(activity)
|
|
|
|
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(activity.getString(R.string.AttachmentManager_signal_requires_the_external_storage_permission_in_order_to_attach_photos_videos_or_audio))
|
|
|
|
.onAllGranted(() -> selectMediaType(activity, "audio/*", null, requestCode))
|
|
|
|
.execute();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2014-06-03 23:24:44 +00:00
|
|
|
public static void selectContactInfo(Activity activity, int requestCode) {
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(activity)
|
|
|
|
.request(Manifest.permission.WRITE_CONTACTS)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(activity.getString(R.string.AttachmentManager_signal_requires_contacts_permission_in_order_to_attach_contact_information))
|
|
|
|
.onAllGranted(() -> {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
})
|
|
|
|
.execute();
|
2014-06-03 23:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 22:37:11 +00:00
|
|
|
public static void selectLocation(Activity activity, int requestCode) {
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(activity)
|
|
|
|
.request(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(activity.getString(R.string.AttachmentManager_signal_requires_location_information_in_order_to_attach_a_location))
|
|
|
|
.onAllGranted(() -> {
|
|
|
|
try {
|
|
|
|
activity.startActivityForResult(new PlacePicker.IntentBuilder().build(activity), requestCode);
|
|
|
|
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.execute();
|
2015-12-18 22:37:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 19:58:47 +00:00
|
|
|
public static void selectGif(Activity activity, int requestCode, boolean isForMms) {
|
2016-10-17 02:05:07 +00:00
|
|
|
Intent intent = new Intent(activity, GiphyActivity.class);
|
2016-12-14 19:58:47 +00:00
|
|
|
intent.putExtra(GiphyActivity.EXTRA_IS_MMS, isForMms);
|
2016-10-17 02:05:07 +00:00
|
|
|
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-11-24 01:56:41 +00:00
|
|
|
public void capturePhoto(Activity activity, int requestCode) {
|
2017-11-25 06:00:30 +00:00
|
|
|
Permissions.with(activity)
|
|
|
|
.request(Manifest.permission.CAMERA)
|
|
|
|
.ifNecessary()
|
|
|
|
.withPermanentDenialDialog(activity.getString(R.string.AttachmentManager_signal_requires_the_camera_permission_in_order_to_take_photos_but_it_has_been_permanently_denied))
|
|
|
|
.onAllGranted(() -> {
|
|
|
|
try {
|
|
|
|
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
|
if (captureIntent.resolveActivity(activity.getPackageManager()) != null) {
|
|
|
|
if (captureUri == null) {
|
2018-01-25 03:17:44 +00:00
|
|
|
captureUri = PersistentBlobProvider.getInstance(context).createForExternal(context, MediaUtil.IMAGE_JPEG);
|
2017-11-25 06:00:30 +00:00
|
|
|
}
|
|
|
|
Log.w(TAG, "captureUri path is " + captureUri.getPath());
|
|
|
|
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);
|
|
|
|
activity.startActivityForResult(captureIntent, requestCode);
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.execute();
|
2015-07-11 01:45:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 02:06:00 +00:00
|
|
|
private static void selectMediaType(Activity activity, @NonNull String type, @Nullable String[] extraMimeType, 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
|
|
|
|
2017-04-20 02:06:00 +00:00
|
|
|
if (extraMimeType != null && Build.VERSION.SDK_INT >= 19) {
|
|
|
|
intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeType);
|
|
|
|
}
|
|
|
|
|
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);
|
2017-04-20 02:06:00 +00:00
|
|
|
|
2014-10-16 08:48:59 +00:00
|
|
|
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 @Nullable Slide slide,
|
|
|
|
final @NonNull MediaConstraints constraints)
|
|
|
|
{
|
2018-01-25 03:17:44 +00:00
|
|
|
return slide == null ||
|
|
|
|
constraints.isSatisfied(context, slide.asAttachment()) ||
|
2015-10-13 01:25:05 +00:00
|
|
|
constraints.canResize(slide.asAttachment());
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 09:39:55 +00:00
|
|
|
private void previewImageDraft(final @NonNull Slide slide) {
|
2016-12-11 21:37:27 +00:00
|
|
|
if (MediaPreviewActivity.isContentTypeSupported(slide.getContentType()) && slide.getUri() != null) {
|
2015-12-11 09:39:55 +00:00
|
|
|
Intent intent = new Intent(context, MediaPreviewActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
2016-12-14 18:21:14 +00:00
|
|
|
intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, slide.asAttachment().getSize());
|
2017-10-06 00:20:28 +00:00
|
|
|
intent.putExtra(MediaPreviewActivity.OUTGOING_EXTRA, true);
|
2015-12-11 09:39:55 +00:00
|
|
|
intent.setDataAndType(slide.getUri(), slide.getContentType());
|
|
|
|
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ThumbnailClickListener implements View.OnClickListener {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (slide.isPresent()) previewImageDraft(slide.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2017-10-16 20:11:42 +00:00
|
|
|
clear(GlideApp.with(context.getApplicationContext()), true);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:20:38 +00:00
|
|
|
private class EditButtonListener implements View.OnClickListener {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(context, ScribbleActivity.class);
|
|
|
|
intent.setData(getSlideUri());
|
|
|
|
((Activity)context).startActivityForResult(intent, ScribbleActivity.SCRIBBLE_REQUEST_CODE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-05-17 06:40:14 +00:00
|
|
|
IMAGE, GIF, AUDIO, VIDEO, DOCUMENT, VCARD;
|
2015-09-05 00:33:22 +00:00
|
|
|
|
2017-03-28 19:05:30 +00:00
|
|
|
public @NonNull Slide createSlide(@NonNull Context context,
|
|
|
|
@NonNull Uri uri,
|
|
|
|
@Nullable String fileName,
|
|
|
|
@Nullable String mimeType,
|
2018-03-20 18:27:11 +00:00
|
|
|
long dataSize,
|
|
|
|
int width,
|
|
|
|
int height)
|
2015-09-05 00:33:22 +00:00
|
|
|
{
|
2017-03-28 19:05:30 +00:00
|
|
|
if (mimeType == null) {
|
|
|
|
mimeType = "application/octet-stream";
|
|
|
|
}
|
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
switch (this) {
|
2018-03-20 18:27:11 +00:00
|
|
|
case IMAGE: return new ImageSlide(context, uri, dataSize, width, height);
|
|
|
|
case GIF: return new GifSlide(context, uri, dataSize, width, height);
|
2017-05-12 05:46:35 +00:00
|
|
|
case AUDIO: return new AudioSlide(context, uri, dataSize, false);
|
2017-03-28 19:05:30 +00:00
|
|
|
case VIDEO: return new VideoSlide(context, uri, dataSize);
|
2018-05-17 06:40:14 +00:00
|
|
|
case VCARD:
|
2017-03-28 19:05:30 +00:00
|
|
|
case DOCUMENT: return new DocumentSlide(context, uri, mimeType, dataSize, fileName);
|
|
|
|
default: throw new AssertionError("unrecognized enum");
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 20:54:40 +00:00
|
|
|
|
|
|
|
public static @Nullable MediaType from(final @Nullable String mimeType) {
|
2017-05-08 22:32:59 +00:00
|
|
|
if (TextUtils.isEmpty(mimeType)) return null;
|
|
|
|
if (MediaUtil.isGif(mimeType)) return GIF;
|
|
|
|
if (MediaUtil.isImageType(mimeType)) return IMAGE;
|
|
|
|
if (MediaUtil.isAudioType(mimeType)) return AUDIO;
|
|
|
|
if (MediaUtil.isVideoType(mimeType)) return VIDEO;
|
2018-05-17 06:40:14 +00:00
|
|
|
if (MediaUtil.isVcard(mimeType)) return VCARD;
|
2017-05-10 22:21:52 +00:00
|
|
|
|
|
|
|
return DOCUMENT;
|
2015-11-18 20:54:40 +00:00
|
|
|
}
|
2017-03-28 19:05:30 +00:00
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|