2017-10-16 20:11:42 +00:00
|
|
|
/*
|
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;
|
2015-02-10 09:46:43 +00:00
|
|
|
import android.content.Intent;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.net.Uri;
|
2017-10-23 20:03:32 +00:00
|
|
|
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;
|
2014-12-15 20:25:55 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-08-12 19:11:23 +00:00
|
|
|
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.Toast;
|
|
|
|
|
2015-06-04 20:21:20 +00:00
|
|
|
import org.thoughtcrime.securesms.components.ZoomingImageView;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2016-11-26 20:10:14 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.VideoSlide;
|
2014-08-12 19:11:23 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
|
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;
|
2017-09-08 18:36:09 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-11-26 20:10:14 +00:00
|
|
|
import org.thoughtcrime.securesms.video.VideoPlayer;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-08-12 19:11:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity for displaying media attachments in-app
|
|
|
|
*/
|
2015-02-10 09:46:43 +00:00
|
|
|
public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity implements RecipientModifiedListener {
|
2014-08-12 19:11:23 +00:00
|
|
|
private final static String TAG = MediaPreviewActivity.class.getSimpleName();
|
|
|
|
|
2017-10-04 18:35:16 +00:00
|
|
|
public static final String ADDRESS_EXTRA = "address";
|
|
|
|
public static final String DATE_EXTRA = "date";
|
|
|
|
public static final String SIZE_EXTRA = "size";
|
|
|
|
public static final String OUTGOING_EXTRA = "outgoing";
|
2014-08-12 19:11:23 +00:00
|
|
|
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
|
2016-12-14 18:21:14 +00:00
|
|
|
private ZoomingImageView image;
|
|
|
|
private VideoPlayer video;
|
|
|
|
|
|
|
|
private Uri mediaUri;
|
|
|
|
private String mediaType;
|
|
|
|
private Recipient recipient;
|
|
|
|
private long date;
|
|
|
|
private long size;
|
2017-10-04 18:35:16 +00:00
|
|
|
private boolean outgoing;
|
2014-08-12 19:11:23 +00:00
|
|
|
|
|
|
|
@Override
|
2014-12-15 20:25:55 +00:00
|
|
|
protected void onCreate(Bundle bundle, @NonNull MasterSecret masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
2014-11-25 06:55:10 +00:00
|
|
|
this.setTheme(R.style.TextSecure_DarkTheme);
|
|
|
|
dynamicLanguage.onCreate(this);
|
|
|
|
|
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
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
initializeViews();
|
2014-08-12 19:11:23 +00:00
|
|
|
initializeResources();
|
2015-02-10 09:46:43 +00:00
|
|
|
initializeActionBar();
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
@Override
|
|
|
|
public void onModified(Recipient recipient) {
|
2017-09-08 18:36:09 +00:00
|
|
|
Util.runOnMain(this::initializeActionBar);
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeActionBar() {
|
|
|
|
final CharSequence relativeTimeSpan;
|
2017-10-04 18:35:16 +00:00
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
if (date > 0) {
|
2016-08-05 20:17:31 +00:00
|
|
|
relativeTimeSpan = DateUtils.getExtendedRelativeTimeSpanString(this,dynamicLanguage.getCurrentLocale(),date);
|
2015-02-10 09:46:43 +00:00
|
|
|
} else {
|
2017-02-10 14:02:48 +00:00
|
|
|
relativeTimeSpan = getString(R.string.MediaPreviewActivity_draft);
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
2017-10-04 18:35:16 +00:00
|
|
|
|
|
|
|
if (outgoing) getSupportActionBar().setTitle(getString(R.string.MediaPreviewActivity_you));
|
|
|
|
else getSupportActionBar().setTitle(recipient.toShortString());
|
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
getSupportActionBar().setSubtitle(relativeTimeSpan);
|
|
|
|
}
|
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicLanguage.onResume(this);
|
2015-02-10 09:46:43 +00:00
|
|
|
if (recipient != null) recipient.addListener(this);
|
|
|
|
initializeMedia();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
if (recipient != null) recipient.removeListener(this);
|
|
|
|
cleanupMedia();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
if (recipient != null) recipient.removeListener(this);
|
|
|
|
setIntent(intent);
|
|
|
|
initializeResources();
|
|
|
|
initializeActionBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeViews() {
|
2017-10-16 20:11:42 +00:00
|
|
|
image = findViewById(R.id.image);
|
|
|
|
video = findViewById(R.id.video_player);
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
private void initializeResources() {
|
2017-07-26 16:59:15 +00:00
|
|
|
Address address = getIntent().getParcelableExtra(ADDRESS_EXTRA);
|
2014-12-24 22:48:04 +00:00
|
|
|
|
2017-10-04 18:35:16 +00:00
|
|
|
mediaUri = getIntent().getData();
|
|
|
|
mediaType = getIntent().getType();
|
|
|
|
date = getIntent().getLongExtra(DATE_EXTRA, -1);
|
|
|
|
size = getIntent().getLongExtra(SIZE_EXTRA, 0);
|
|
|
|
outgoing = getIntent().getBooleanExtra(OUTGOING_EXTRA, false);
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (address != null) {
|
2017-08-22 01:32:38 +00:00
|
|
|
recipient = Recipient.from(this, address, true);
|
2015-02-10 09:46:43 +00:00
|
|
|
recipient.addListener(this);
|
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
|
|
|
}
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
2014-12-24 22:48:04 +00:00
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
private void initializeMedia() {
|
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
|
|
|
|
2016-11-26 20:10:14 +00:00
|
|
|
try {
|
|
|
|
if (mediaType != null && mediaType.startsWith("image/")) {
|
|
|
|
image.setVisibility(View.VISIBLE);
|
|
|
|
video.setVisibility(View.GONE);
|
2017-10-16 20:11:42 +00:00
|
|
|
image.setImageUri(masterSecret, GlideApp.with(this), mediaUri, mediaType);
|
2016-11-26 20:10:14 +00:00
|
|
|
} else if (mediaType != null && mediaType.startsWith("video/")) {
|
|
|
|
image.setVisibility(View.GONE);
|
|
|
|
video.setVisibility(View.VISIBLE);
|
2017-08-01 01:40:46 +00:00
|
|
|
video.setWindow(getWindow());
|
2016-11-26 20:10:14 +00:00
|
|
|
video.setVideoSource(masterSecret, new VideoSlide(this, mediaUri, size));
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
Toast.makeText(getApplicationContext(), R.string.MediaPreviewActivity_unssuported_media_type, Toast.LENGTH_LONG).show();
|
|
|
|
finish();
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
private void cleanupMedia() {
|
2016-12-14 18:21:14 +00:00
|
|
|
image.cleanup();
|
2016-11-26 20:10:14 +00:00
|
|
|
video.cleanup();
|
2014-12-24 22:48:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 16:53:44 +00:00
|
|
|
private void showOverview() {
|
|
|
|
Intent intent = new Intent(this, MediaOverviewActivity.class);
|
2017-09-21 01:14:28 +00:00
|
|
|
intent.putExtra(MediaOverviewActivity.ADDRESS_EXTRA, recipient.getAddress());
|
2016-09-20 16:53:44 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:03:03 +00:00
|
|
|
private void forward() {
|
|
|
|
Intent composeIntent = new Intent(this, ShareActivity.class);
|
|
|
|
composeIntent.putExtra(Intent.EXTRA_STREAM, mediaUri);
|
|
|
|
composeIntent.setType(mediaType);
|
|
|
|
startActivity(composeIntent);
|
|
|
|
}
|
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
private void saveToDisk() {
|
2017-10-16 20:11:42 +00:00
|
|
|
SaveAttachmentTask.showWarningDialog(this, (dialogInterface, i) -> {
|
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(MediaPreviewActivity.this, masterSecret, image);
|
|
|
|
long saveDate = (date > 0) ? date : System.currentTimeMillis();
|
|
|
|
saveTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Attachment(mediaUri, mediaType, saveDate, null));
|
2014-08-12 19:11:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2017-09-21 01:14:28 +00:00
|
|
|
if (recipient == null) menu.findItem(R.id.media_preview__overview).setVisible(false);
|
2014-08-12 19:11:23 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
2016-09-20 16:53:44 +00:00
|
|
|
case R.id.media_preview__overview: showOverview(); return true;
|
|
|
|
case R.id.media_preview__forward: forward(); return true;
|
|
|
|
case R.id.save: saveToDisk(); return true;
|
|
|
|
case android.R.id.home: finish(); return true;
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isContentTypeSupported(final String contentType) {
|
2016-11-26 20:10:14 +00:00
|
|
|
return contentType != null && (contentType.startsWith("image/") || contentType.startsWith("video/"));
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
}
|