2014-08-12 19:11:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2014 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.content.DialogInterface;
|
2014-12-03 09:25:32 +00:00
|
|
|
import android.graphics.Bitmap;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.net.Uri;
|
2014-12-03 09:25:32 +00:00
|
|
|
import android.opengl.GLES20;
|
|
|
|
import android.os.AsyncTask;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.widget.ImageView;
|
2014-12-03 09:25:32 +00:00
|
|
|
import android.widget.TextView;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-12-12 09:03:24 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2014-08-12 19:11:23 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-12-24 22:48:04 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient.RecipientModifiedListener;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2014-12-03 09:25:32 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2014-08-12 19:11:23 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DateUtils;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
|
|
|
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
|
|
|
|
import org.thoughtcrime.securesms.util.SaveAttachmentTask.Attachment;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import uk.co.senab.photoview.PhotoViewAttacher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity for displaying media attachments in-app
|
|
|
|
*/
|
2014-06-29 03:40:57 +00:00
|
|
|
public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity {
|
2014-08-12 19:11:23 +00:00
|
|
|
private final static String TAG = MediaPreviewActivity.class.getSimpleName();
|
|
|
|
|
|
|
|
public final static String MASTER_SECRET_EXTRA = "master_secret";
|
|
|
|
public final static String RECIPIENT_EXTRA = "recipient";
|
|
|
|
public final static String DATE_EXTRA = "date";
|
|
|
|
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
|
2014-12-03 09:25:32 +00:00
|
|
|
private View loadingView;
|
|
|
|
private TextView errorText;
|
2014-08-12 19:11:23 +00:00
|
|
|
private ImageView image;
|
|
|
|
private PhotoViewAttacher imageAttacher;
|
|
|
|
private Uri mediaUri;
|
|
|
|
private String mediaType;
|
|
|
|
private Recipient recipient;
|
|
|
|
private long date;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle bundle) {
|
2014-11-25 06:55:10 +00:00
|
|
|
this.setTheme(R.style.TextSecure_DarkTheme);
|
|
|
|
dynamicLanguage.onCreate(this);
|
|
|
|
|
|
|
|
super.onCreate(bundle);
|
2014-08-12 19:11:23 +00:00
|
|
|
setFullscreenIfPossible();
|
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
|
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
2014-11-25 06:55:10 +00:00
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
setContentView(R.layout.media_preview_activity);
|
2014-11-25 06:55:10 +00:00
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
initializeResources();
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:40:57 +00:00
|
|
|
@TargetApi(VERSION_CODES.JELLY_BEAN)
|
2014-08-12 19:11:23 +00:00
|
|
|
private void setFullscreenIfPossible() {
|
2014-06-29 03:40:57 +00:00
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
|
2014-08-12 19:11:23 +00:00
|
|
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
|
2014-12-24 22:48:04 +00:00
|
|
|
final long recipientId = getIntent().getLongExtra(RECIPIENT_EXTRA, -1);
|
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
masterSecret = getIntent().getParcelableExtra(MASTER_SECRET_EXTRA);
|
|
|
|
mediaUri = getIntent().getData();
|
|
|
|
mediaType = getIntent().getType();
|
|
|
|
date = getIntent().getLongExtra(DATE_EXTRA, -1);
|
|
|
|
|
2014-12-24 22:48:04 +00:00
|
|
|
if (recipientId > -1) {
|
|
|
|
recipient = RecipientFactory.getRecipientForId(this, recipientId, true);
|
|
|
|
recipient.addListener(new RecipientModifiedListener() {
|
|
|
|
@Override
|
|
|
|
public void onModified(Recipient recipient) {
|
|
|
|
initializeActionBar();
|
|
|
|
}
|
|
|
|
});
|
2014-08-12 19:11:23 +00:00
|
|
|
} else {
|
2014-12-24 22:48:04 +00:00
|
|
|
recipient = null;
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
2014-12-24 22:48:04 +00:00
|
|
|
|
|
|
|
initializeActionBar();
|
2014-08-12 19:11:23 +00:00
|
|
|
|
|
|
|
if (!isContentTypeSupported(mediaType)) {
|
|
|
|
Log.w(TAG, "Unsupported media type sent to MediaPreviewActivity, finishing.");
|
2014-12-15 14:44:41 +00:00
|
|
|
Toast.makeText(getApplicationContext(), R.string.MediaPreviewActivity_unssuported_media_type, Toast.LENGTH_LONG).show();
|
2014-08-12 19:11:23 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2014-12-03 09:25:32 +00:00
|
|
|
Log.w(TAG, "Loading Part URI: " + mediaUri);
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2014-12-03 09:25:32 +00:00
|
|
|
if (mediaType != null && mediaType.startsWith("image/")) {
|
|
|
|
displayImage();
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 22:48:04 +00:00
|
|
|
private void initializeActionBar() {
|
|
|
|
final CharSequence relativeTimeSpan;
|
|
|
|
if (date > 0) {
|
|
|
|
relativeTimeSpan = DateUtils.getRelativeTimeSpanString(date,
|
|
|
|
System.currentTimeMillis(),
|
|
|
|
DateUtils.MINUTE_IN_MILLIS);
|
|
|
|
} else {
|
|
|
|
relativeTimeSpan = null;
|
|
|
|
}
|
|
|
|
getSupportActionBar().setTitle(recipient == null ? getString(R.string.MediaPreviewActivity_you) : recipient.getName());
|
|
|
|
getSupportActionBar().setSubtitle(relativeTimeSpan);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
private InputStream getMediaInputStream() throws IOException {
|
|
|
|
return PartAuthority.getPartStream(this, masterSecret, mediaUri);
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
2014-12-03 09:25:32 +00:00
|
|
|
loadingView = findViewById(R.id.loading_indicator);
|
|
|
|
errorText = (TextView) findViewById(R.id.error);
|
2014-08-12 19:11:23 +00:00
|
|
|
image = (ImageView) findViewById(R.id.image);
|
|
|
|
imageAttacher = new PhotoViewAttacher(image);
|
|
|
|
}
|
|
|
|
|
2014-12-03 09:25:32 +00:00
|
|
|
private void displayImage() {
|
|
|
|
new AsyncTask<Void,Void,Bitmap>() {
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
int[] maxTextureSizeParams = new int[1];
|
|
|
|
GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxTextureSizeParams, 0);
|
|
|
|
int maxTextureSize = Math.max(maxTextureSizeParams[0], 2048);
|
|
|
|
Log.w(TAG, "reported GL_MAX_TEXTURE_SIZE: " + maxTextureSize);
|
2014-12-12 09:03:24 +00:00
|
|
|
return BitmapUtil.createScaledBitmap(getMediaInputStream(),
|
|
|
|
getMediaInputStream(),
|
2014-12-03 09:25:32 +00:00
|
|
|
maxTextureSize, maxTextureSize);
|
|
|
|
} catch (IOException | BitmapDecodingException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
loadingView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap bitmap) {
|
|
|
|
loadingView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
if (bitmap == null) {
|
|
|
|
errorText.setText(R.string.MediaPreviewActivity_cant_display);
|
|
|
|
errorText.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
image.setImageBitmap(bitmap);
|
|
|
|
image.setVisibility(View.VISIBLE);
|
|
|
|
imageAttacher.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void saveToDisk() {
|
|
|
|
SaveAttachmentTask.showWarningDialog(this, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(MediaPreviewActivity.this, masterSecret);
|
|
|
|
saveTask.execute(new Attachment(mediaUri, mediaType, date));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
|
|
|
|
menu.clear();
|
2014-06-29 03:40:57 +00:00
|
|
|
MenuInflater inflater = this.getMenuInflater();
|
2014-08-12 19:11:23 +00:00
|
|
|
inflater.inflate(R.menu.media_preview, menu);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.save: saveToDisk(); return true;
|
|
|
|
case android.R.id.home: finish(); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isContentTypeSupported(final String contentType) {
|
|
|
|
return contentType != null && contentType.startsWith("image/");
|
|
|
|
}
|
|
|
|
}
|