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;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2014-03-02 12:17:03 +00:00
|
|
|
import android.os.Build;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
2013-02-04 02:41:34 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2013-05-21 17:32:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class AttachmentManager {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final View attachmentView;
|
|
|
|
private final ImageView thumbnail;
|
|
|
|
private final Button removeButton;
|
|
|
|
private final SlideDeck slideDeck;
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public AttachmentManager(Activity view) {
|
|
|
|
this.attachmentView = (View)view.findViewById(R.id.attachment_editor);
|
|
|
|
this.thumbnail = (ImageView)view.findViewById(R.id.attachment_thumbnail);
|
|
|
|
this.removeButton = (Button)view.findViewById(R.id.remove_image_button);
|
|
|
|
this.slideDeck = new SlideDeck();
|
2013-02-04 02:41:34 +00:00
|
|
|
this.context = view;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
this.removeButton.setOnClickListener(new RemoveButtonListener());
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void clear() {
|
|
|
|
slideDeck.clear();
|
|
|
|
attachmentView.setVisibility(View.GONE);
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2013-05-21 17:32:48 +00:00
|
|
|
public void setImage(Uri image) throws IOException, BitmapDecodingException {
|
2011-12-20 18:20:44 +00:00
|
|
|
ImageSlide slide = new ImageSlide(context, image);
|
|
|
|
slideDeck.addSlide(slide);
|
2013-09-09 01:19:05 +00:00
|
|
|
thumbnail.setImageDrawable(slide.getThumbnail(345, 261));
|
2011-12-20 18:20:44 +00:00
|
|
|
attachmentView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void setVideo(Uri video) throws IOException, MediaTooLargeException {
|
|
|
|
VideoSlide slide = new VideoSlide(context, video);
|
|
|
|
slideDeck.addSlide(slide);
|
2013-09-09 01:19:05 +00:00
|
|
|
thumbnail.setImageDrawable(slide.getThumbnail(thumbnail.getWidth(), thumbnail.getHeight()));
|
2011-12-20 18:20:44 +00:00
|
|
|
attachmentView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2013-02-04 02:41:34 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void setAudio(Uri audio)throws IOException, MediaTooLargeException {
|
|
|
|
AudioSlide slide = new AudioSlide(context, audio);
|
|
|
|
slideDeck.addSlide(slide);
|
2013-09-09 01:19:05 +00:00
|
|
|
thumbnail.setImageDrawable(slide.getThumbnail(thumbnail.getWidth(), thumbnail.getHeight()));
|
2011-12-20 18:20:44 +00:00
|
|
|
attachmentView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAttachmentPresent() {
|
|
|
|
return attachmentView.getVisibility() == View.VISIBLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SlideDeck getSlideDeck() {
|
|
|
|
return slideDeck;
|
|
|
|
}
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static void selectMediaType(Activity activity, String type, int requestCode) {
|
2014-03-02 12:17:03 +00:00
|
|
|
final Intent intent;
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
|
|
} else {
|
|
|
|
intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
intent.setType(type);
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
}
|
2013-02-04 02:41:34 +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) {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|