2013-02-03 18:41:34 -08:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2013-02-03 18:41:34 -08:00
|
|
|
*
|
2011-12-20 10:20:44 -08: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-03 18:41:34 -08:00
|
|
|
*
|
2011-12-20 10:20:44 -08: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 01:48:59 -07:00
|
|
|
import android.content.ActivityNotFoundException;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2014-03-02 13:17:03 +01:00
|
|
|
import android.os.Build;
|
2014-06-04 01:24:44 +02:00
|
|
|
import android.provider.ContactsContract;
|
2015-05-18 10:26:32 -07:00
|
|
|
import android.provider.MediaStore;
|
2015-03-31 15:44:41 -07:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
2014-10-16 01:48:59 -07:00
|
|
|
import android.widget.Toast;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2013-02-03 18:41:34 -08:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-03-31 15:44:41 -07:00
|
|
|
import org.thoughtcrime.securesms.components.ThumbnailView;
|
2013-05-21 10:32:48 -07:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2015-05-18 10:26:32 -07:00
|
|
|
import java.io.File;
|
2013-02-03 18:41:34 -08:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public class AttachmentManager {
|
2014-10-16 01:48:59 -07:00
|
|
|
private final static String TAG = AttachmentManager.class.getSimpleName();
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2015-05-18 10:26:32 -07:00
|
|
|
private final Context context;
|
|
|
|
private final View attachmentView;
|
|
|
|
private final ThumbnailView thumbnail;
|
|
|
|
private final Button removeButton;
|
|
|
|
private final SlideDeck slideDeck;
|
2014-04-15 12:43:14 +02:00
|
|
|
private final AttachmentListener attachmentListener;
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2015-05-18 10:26:32 -07:00
|
|
|
private File captureFile;
|
|
|
|
|
2014-04-15 12:43:14 +02:00
|
|
|
public AttachmentManager(Activity view, AttachmentListener listener) {
|
2015-05-18 10:26:32 -07:00
|
|
|
this.attachmentView = view.findViewById(R.id.attachment_editor);
|
2015-03-31 15:44:41 -07:00
|
|
|
this.thumbnail = (ThumbnailView)view.findViewById(R.id.attachment_thumbnail);
|
2014-04-15 12:43:14 +02:00
|
|
|
this.removeButton = (Button)view.findViewById(R.id.remove_image_button);
|
|
|
|
this.slideDeck = new SlideDeck();
|
|
|
|
this.context = view;
|
|
|
|
this.attachmentListener = listener;
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
this.removeButton.setOnClickListener(new RemoveButtonListener());
|
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public void clear() {
|
|
|
|
slideDeck.clear();
|
|
|
|
attachmentView.setVisibility(View.GONE);
|
2014-04-15 12:43:14 +02:00
|
|
|
attachmentListener.onAttachmentChanged();
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2015-05-18 10:26:32 -07:00
|
|
|
public void cleanup() {
|
|
|
|
if (captureFile != null) captureFile.delete();
|
|
|
|
captureFile = null;
|
|
|
|
}
|
|
|
|
|
2014-12-22 16:25:51 -08:00
|
|
|
public void setImage(Uri image) throws IOException, BitmapDecodingException {
|
2015-03-31 15:44:41 -07:00
|
|
|
setMedia(new ImageSlide(context, image));
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public void setVideo(Uri video) throws IOException, MediaTooLargeException {
|
2014-10-28 10:36:27 -05:00
|
|
|
setMedia(new VideoSlide(context, video));
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2014-10-28 10:36:27 -05:00
|
|
|
public void setAudio(Uri audio) throws IOException, MediaTooLargeException {
|
|
|
|
setMedia(new AudioSlide(context, audio));
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:44:41 -07:00
|
|
|
public void setMedia(final Slide slide) {
|
2014-10-28 10:36:27 -05:00
|
|
|
slideDeck.clear();
|
2011-12-20 10:20:44 -08:00
|
|
|
slideDeck.addSlide(slide);
|
2015-03-31 15:44:41 -07:00
|
|
|
attachmentView.setVisibility(View.VISIBLE);
|
|
|
|
thumbnail.setImageResource(slide);
|
2015-05-18 10:26:32 -07:00
|
|
|
attachmentListener.onAttachmentChanged();
|
2014-10-28 10:36:27 -05:00
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public boolean isAttachmentPresent() {
|
|
|
|
return attachmentView.getVisibility() == View.VISIBLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SlideDeck getSlideDeck() {
|
|
|
|
return slideDeck;
|
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2015-05-18 10:26:32 -07:00
|
|
|
public File getCaptureFile() {
|
|
|
|
return captureFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void capturePhoto(Activity activity, int requestCode) {
|
|
|
|
try {
|
|
|
|
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
|
|
|
|
|
if (captureIntent.resolveActivity(activity.getPackageManager()) != null) {
|
|
|
|
captureFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".jpg", activity.getExternalFilesDir(null));
|
|
|
|
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(captureFile));
|
|
|
|
activity.startActivityForResult(captureIntent, requestCode);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public static void selectVideo(Activity activity, int requestCode) {
|
|
|
|
selectMediaType(activity, "video/*", requestCode);
|
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public static void selectImage(Activity activity, int requestCode) {
|
|
|
|
selectMediaType(activity, "image/*", requestCode);
|
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public static void selectAudio(Activity activity, int requestCode) {
|
|
|
|
selectMediaType(activity, "audio/*", requestCode);
|
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2014-06-04 01:24:44 +02:00
|
|
|
public static void selectContactInfo(Activity activity, int requestCode) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
private static void selectMediaType(Activity activity, String type, int requestCode) {
|
2014-10-16 01:48:59 -07:00
|
|
|
final Intent intent = new Intent();
|
|
|
|
intent.setType(type);
|
2014-03-02 13:17:03 +01:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
2014-10-16 01:48:59 -07: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 13:17:03 +01:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:48:59 -07: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 10:20:44 -08:00
|
|
|
}
|
2013-02-03 18:41:34 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
private class RemoveButtonListener implements View.OnClickListener {
|
2013-02-03 18:41:34 -08:00
|
|
|
@Override
|
2011-12-20 10:20:44 -08:00
|
|
|
public void onClick(View v) {
|
|
|
|
clear();
|
2015-05-18 10:26:32 -07:00
|
|
|
cleanup();
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 12:43:14 +02:00
|
|
|
public interface AttachmentListener {
|
|
|
|
public void onAttachmentChanged();
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|