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;
|
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.annotation.TargetApi;
|
2020-08-31 13:16:15 +00:00
|
|
|
|
2020-09-17 12:41:15 +00:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2018-01-04 19:11:49 +00:00
|
|
|
import android.content.Context;
|
2015-02-10 09:46:43 +00:00
|
|
|
import android.content.Intent;
|
2018-01-04 19:11:49 +00:00
|
|
|
import android.database.Cursor;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.net.Uri;
|
2017-10-23 20:03:32 +00:00
|
|
|
import android.os.AsyncTask;
|
2020-09-10 06:32:13 +00:00
|
|
|
import android.os.Build;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
|
|
|
import android.os.Bundle;
|
2020-08-19 00:06:26 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.loader.app.LoaderManager;
|
|
|
|
import androidx.loader.content.Loader;
|
|
|
|
import androidx.core.util.Pair;
|
|
|
|
import androidx.viewpager.widget.PagerAdapter;
|
|
|
|
import androidx.viewpager.widget.ViewPager;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2018-11-09 07:33:37 +00:00
|
|
|
import android.view.GestureDetector;
|
2018-01-04 19:11:49 +00:00
|
|
|
import android.view.LayoutInflater;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2018-11-09 07:33:37 +00:00
|
|
|
import android.view.MotionEvent;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.view.View;
|
2018-01-04 19:11:49 +00:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.widget.FrameLayout;
|
2018-11-09 07:33:37 +00:00
|
|
|
import android.widget.TextView;
|
2014-08-12 19:11:23 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2021-01-15 05:51:53 +00:00
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment;
|
2018-01-04 19:11:49 +00:00
|
|
|
import org.thoughtcrime.securesms.components.MediaView;
|
2021-01-13 06:11:30 +00:00
|
|
|
import org.session.libsession.messaging.threads.Address;
|
2018-01-04 19:11:49 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase.MediaRecord;
|
|
|
|
import org.thoughtcrime.securesms.database.loaders.PagingMediaLoader;
|
2019-05-02 14:36:20 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-11-09 07:33:37 +00:00
|
|
|
import org.thoughtcrime.securesms.mediapreview.MediaPreviewViewModel;
|
2019-05-02 14:36:20 +00:00
|
|
|
import org.thoughtcrime.securesms.mediapreview.MediaRailAdapter;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2018-01-04 19:11:49 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
2017-11-25 06:00:30 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2021-01-13 06:11:30 +00:00
|
|
|
import org.session.libsession.messaging.threads.recipients.Recipient;
|
|
|
|
import org.session.libsession.messaging.threads.recipients.RecipientModifiedListener;
|
2018-03-15 18:17:40 +00:00
|
|
|
import org.thoughtcrime.securesms.util.AttachmentUtil;
|
2014-08-12 19:11:23 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DateUtils;
|
|
|
|
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 java.io.IOException;
|
2020-12-14 07:16:16 +00:00
|
|
|
import java.util.Locale;
|
2018-01-04 19:11:49 +00:00
|
|
|
import java.util.WeakHashMap;
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2019-07-24 02:30:23 +00:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
/**
|
|
|
|
* Activity for displaying media attachments in-app
|
|
|
|
*/
|
2018-11-09 07:33:37 +00:00
|
|
|
public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity implements RecipientModifiedListener,
|
|
|
|
LoaderManager.LoaderCallbacks<Pair<Cursor, Integer>>,
|
2018-11-20 17:59:23 +00:00
|
|
|
MediaRailAdapter.RailItemListener
|
2018-11-09 07:33:37 +00:00
|
|
|
{
|
2018-01-04 19:11:49 +00:00
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
private final static String TAG = MediaPreviewActivity.class.getSimpleName();
|
|
|
|
|
2018-01-23 20:39:30 +00:00
|
|
|
public static final String ADDRESS_EXTRA = "address";
|
|
|
|
public static final String DATE_EXTRA = "date";
|
|
|
|
public static final String SIZE_EXTRA = "size";
|
2018-11-09 07:33:37 +00:00
|
|
|
public static final String CAPTION_EXTRA = "caption";
|
2018-01-23 20:39:30 +00:00
|
|
|
public static final String OUTGOING_EXTRA = "outgoing";
|
|
|
|
public static final String LEFT_IS_RECENT_EXTRA = "left_is_recent";
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
private ViewPager mediaPager;
|
|
|
|
private View detailsContainer;
|
|
|
|
private TextView caption;
|
|
|
|
private View captionContainer;
|
|
|
|
private RecyclerView albumRail;
|
2018-11-20 17:59:23 +00:00
|
|
|
private MediaRailAdapter albumRailAdapter;
|
2018-11-09 07:33:37 +00:00
|
|
|
private ViewGroup playbackControlsContainer;
|
|
|
|
private Uri initialMediaUri;
|
|
|
|
private String initialMediaType;
|
|
|
|
private long initialMediaSize;
|
|
|
|
private String initialCaption;
|
|
|
|
private Recipient conversationRecipient;
|
|
|
|
private boolean leftIsRecent;
|
|
|
|
private GestureDetector clickDetector;
|
|
|
|
private MediaPreviewViewModel viewModel;
|
2019-05-02 14:36:20 +00:00
|
|
|
private ViewPagerListener viewPagerListener;
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2018-01-16 20:06:55 +00:00
|
|
|
private int restartItem = -1;
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
2014-08-12 19:11:23 +00:00
|
|
|
@Override
|
2018-02-02 03:22:48 +00:00
|
|
|
protected void onCreate(Bundle bundle, boolean ready) {
|
2020-09-17 12:41:15 +00:00
|
|
|
viewModel = new ViewModelProvider(this).get(MediaPreviewViewModel.class);
|
2018-11-09 07:33:37 +00:00
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
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();
|
2018-11-09 07:33:37 +00:00
|
|
|
initializeObservers();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean dispatchTouchEvent(MotionEvent ev) {
|
|
|
|
clickDetector.onTouchEvent(ev);
|
|
|
|
return super.dispatchTouchEvent(ev);
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-08-31 13:16:15 +00:00
|
|
|
Util.runOnMain(this::updateActionBar);
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
@Override
|
|
|
|
public void onRailItemClicked(int distanceFromActive) {
|
|
|
|
mediaPager.setCurrentItem(mediaPager.getCurrentItem() + distanceFromActive);
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:59:23 +00:00
|
|
|
@Override
|
|
|
|
public void onRailItemDeleteClicked(int distanceFromActive) {
|
|
|
|
throw new UnsupportedOperationException("Callback unsupported.");
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
2020-08-31 13:16:15 +00:00
|
|
|
private void updateActionBar() {
|
2018-01-04 19:11:49 +00:00
|
|
|
MediaItem mediaItem = getCurrentMediaItem();
|
2017-10-04 18:35:16 +00:00
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
if (mediaItem != null) {
|
|
|
|
CharSequence relativeTimeSpan;
|
2017-10-04 18:35:16 +00:00
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
if (mediaItem.date > 0) {
|
2020-12-14 07:16:16 +00:00
|
|
|
relativeTimeSpan = DateUtils.getExtendedRelativeTimeSpanString(this, Locale.getDefault(), mediaItem.date);
|
2018-01-04 19:11:49 +00:00
|
|
|
} else {
|
|
|
|
relativeTimeSpan = getString(R.string.MediaPreviewActivity_draft);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaItem.outgoing) getSupportActionBar().setTitle(getString(R.string.MediaPreviewActivity_you));
|
|
|
|
else if (mediaItem.recipient != null) getSupportActionBar().setTitle(mediaItem.recipient.toShortString());
|
|
|
|
else getSupportActionBar().setTitle("");
|
2017-10-04 18:35:16 +00:00
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
getSupportActionBar().setSubtitle(relativeTimeSpan);
|
|
|
|
}
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2018-01-04 19:11:49 +00:00
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
initializeMedia();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
2018-01-16 20:06:55 +00:00
|
|
|
restartItem = cleanupMedia();
|
2015-02-10 09:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
initializeResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeViews() {
|
2018-01-04 19:11:49 +00:00
|
|
|
mediaPager = findViewById(R.id.media_pager);
|
|
|
|
mediaPager.setOffscreenPageLimit(1);
|
2019-05-02 14:36:20 +00:00
|
|
|
|
|
|
|
viewPagerListener = new ViewPagerListener();
|
|
|
|
mediaPager.addOnPageChangeListener(viewPagerListener);
|
2018-11-09 07:33:37 +00:00
|
|
|
|
|
|
|
albumRail = findViewById(R.id.media_preview_album_rail);
|
2018-11-20 17:59:23 +00:00
|
|
|
albumRailAdapter = new MediaRailAdapter(GlideApp.with(this), this, false);
|
2018-11-09 07:33:37 +00:00
|
|
|
|
|
|
|
albumRail.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
|
|
|
|
albumRail.setAdapter(albumRailAdapter);
|
|
|
|
|
|
|
|
detailsContainer = findViewById(R.id.media_preview_details_container);
|
|
|
|
caption = findViewById(R.id.media_preview_caption);
|
|
|
|
captionContainer = findViewById(R.id.media_preview_caption_container);
|
|
|
|
playbackControlsContainer = findViewById(R.id.media_preview_playback_controls_container);
|
2020-08-31 13:16:15 +00:00
|
|
|
|
|
|
|
setSupportActionBar(findViewById(R.id.toolbar));
|
2020-09-17 12:41:15 +00:00
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setHomeButtonEnabled(true);
|
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
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
initialMediaUri = getIntent().getData();
|
|
|
|
initialMediaType = getIntent().getType();
|
|
|
|
initialMediaSize = getIntent().getLongExtra(SIZE_EXTRA, 0);
|
2018-11-09 07:33:37 +00:00
|
|
|
initialCaption = getIntent().getStringExtra(CAPTION_EXTRA);
|
2018-01-23 20:39:30 +00:00
|
|
|
leftIsRecent = getIntent().getBooleanExtra(LEFT_IS_RECENT_EXTRA, false);
|
2018-01-16 20:06:55 +00:00
|
|
|
restartItem = -1;
|
2014-08-12 19:11:23 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (address != null) {
|
2018-01-04 19:11:49 +00:00
|
|
|
conversationRecipient = Recipient.from(this, address, true);
|
2014-08-12 19:11:23 +00:00
|
|
|
} else {
|
2018-01-04 19:11:49 +00:00
|
|
|
conversationRecipient = 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
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
private void initializeObservers() {
|
|
|
|
viewModel.getPreviewData().observe(this, previewData -> {
|
2018-11-27 17:32:56 +00:00
|
|
|
if (previewData == null || mediaPager == null || mediaPager.getAdapter() == null) {
|
2018-11-09 07:33:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
View playbackControls = ((MediaItemAdapter) mediaPager.getAdapter()).getPlaybackControls(mediaPager.getCurrentItem());
|
|
|
|
|
|
|
|
if (previewData.getAlbumThumbnails().isEmpty() && previewData.getCaption() == null && playbackControls == null) {
|
|
|
|
detailsContainer.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
detailsContainer.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
albumRail.setVisibility(previewData.getAlbumThumbnails().isEmpty() ? View.GONE : View.VISIBLE);
|
2018-11-20 17:59:23 +00:00
|
|
|
albumRailAdapter.setMedia(previewData.getAlbumThumbnails(), previewData.getActivePosition());
|
2018-11-09 07:33:37 +00:00
|
|
|
albumRail.smoothScrollToPosition(previewData.getActivePosition());
|
|
|
|
|
|
|
|
captionContainer.setVisibility(previewData.getCaption() == null ? View.GONE : View.VISIBLE);
|
|
|
|
caption.setText(previewData.getCaption());
|
|
|
|
|
|
|
|
if (playbackControls != null) {
|
|
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
|
playbackControls.setLayoutParams(params);
|
|
|
|
|
|
|
|
playbackControlsContainer.removeAllViews();
|
|
|
|
playbackControlsContainer.addView(playbackControls);
|
|
|
|
} else {
|
|
|
|
playbackControlsContainer.removeAllViews();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
clickDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onSingleTapUp(MotionEvent e) {
|
|
|
|
if (e.getY() < detailsContainer.getTop()) {
|
|
|
|
detailsContainer.setVisibility(detailsContainer.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
|
|
|
|
}
|
|
|
|
return super.onSingleTapUp(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-10 09:46:43 +00:00
|
|
|
private void initializeMedia() {
|
2018-01-04 19:11:49 +00:00
|
|
|
if (!isContentTypeSupported(initialMediaType)) {
|
2014-08-12 19:11:23 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Loading Part URI: " + initialMediaUri);
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
if (conversationRecipient != null) {
|
2018-01-16 20:06:55 +00:00
|
|
|
getSupportLoaderManager().restartLoader(0, null, this);
|
2018-01-04 19:11:49 +00:00
|
|
|
} else {
|
2018-01-25 03:17:44 +00:00
|
|
|
mediaPager.setAdapter(new SingleItemPagerAdapter(this, GlideApp.with(this), getWindow(), initialMediaUri, initialMediaType, initialMediaSize));
|
2018-11-09 07:33:37 +00:00
|
|
|
|
|
|
|
if (initialCaption != null) {
|
|
|
|
detailsContainer.setVisibility(View.VISIBLE);
|
|
|
|
captionContainer.setVisibility(View.VISIBLE);
|
|
|
|
caption.setText(initialCaption);
|
|
|
|
}
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 20:06:55 +00:00
|
|
|
private int cleanupMedia() {
|
|
|
|
int restartItem = mediaPager.getCurrentItem();
|
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
mediaPager.removeAllViews();
|
|
|
|
mediaPager.setAdapter(null);
|
2018-01-16 20:06:55 +00:00
|
|
|
|
|
|
|
return restartItem;
|
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);
|
2018-01-04 19:11:49 +00:00
|
|
|
intent.putExtra(MediaOverviewActivity.ADDRESS_EXTRA, conversationRecipient.getAddress());
|
2016-09-20 16:53:44 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:03:03 +00:00
|
|
|
private void forward() {
|
2018-01-04 19:11:49 +00:00
|
|
|
MediaItem mediaItem = getCurrentMediaItem();
|
|
|
|
|
|
|
|
if (mediaItem != null) {
|
|
|
|
Intent composeIntent = new Intent(this, ShareActivity.class);
|
|
|
|
composeIntent.putExtra(Intent.EXTRA_STREAM, mediaItem.uri);
|
|
|
|
composeIntent.setType(mediaItem.type);
|
|
|
|
startActivity(composeIntent);
|
|
|
|
}
|
2016-11-02 23:03:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
@SuppressWarnings("CodeBlock2Expr")
|
|
|
|
@SuppressLint("InlinedApi")
|
2014-08-12 19:11:23 +00:00
|
|
|
private void saveToDisk() {
|
2018-01-04 19:11:49 +00:00
|
|
|
MediaItem mediaItem = getCurrentMediaItem();
|
2020-09-10 06:32:13 +00:00
|
|
|
if (mediaItem == null) return;
|
|
|
|
|
|
|
|
SaveAttachmentTask.showWarningDialog(this, (dialogInterface, i) -> {
|
|
|
|
Permissions.with(this)
|
|
|
|
.request(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
.maxSdkVersion(Build.VERSION_CODES.P)
|
|
|
|
.withPermanentDenialDialog(getString(R.string.MediaPreviewActivity_signal_needs_the_storage_permission_in_order_to_write_to_external_storage_but_it_has_been_permanently_denied))
|
|
|
|
.onAnyDenied(() -> Toast.makeText(this, R.string.MediaPreviewActivity_unable_to_write_to_external_storage_without_permission, Toast.LENGTH_LONG).show())
|
|
|
|
.onAllGranted(() -> {
|
|
|
|
SaveAttachmentTask saveTask = new SaveAttachmentTask(MediaPreviewActivity.this);
|
|
|
|
long saveDate = (mediaItem.date > 0) ? mediaItem.date : System.currentTimeMillis();
|
|
|
|
saveTask.executeOnExecutor(
|
|
|
|
AsyncTask.THREAD_POOL_EXECUTOR,
|
|
|
|
new Attachment(mediaItem.uri, mediaItem.type, saveDate, null));
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
});
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:24:42 +00:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
private void deleteMedia() {
|
|
|
|
MediaItem mediaItem = getCurrentMediaItem();
|
|
|
|
if (mediaItem == null || mediaItem.attachment == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setIconAttribute(R.attr.dialog_alert_icon);
|
|
|
|
builder.setTitle(R.string.MediaPreviewActivity_media_delete_confirmation_title);
|
|
|
|
builder.setMessage(R.string.MediaPreviewActivity_media_delete_confirmation_message);
|
|
|
|
builder.setCancelable(true);
|
|
|
|
|
|
|
|
builder.setPositiveButton(R.string.delete, (dialogInterface, which) -> {
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
if (mediaItem.attachment == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-03-15 18:17:40 +00:00
|
|
|
AttachmentUtil.deleteAttachment(MediaPreviewActivity.this.getApplicationContext(),
|
|
|
|
mediaItem.attachment);
|
2018-03-14 01:24:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
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);
|
2018-03-14 01:24:42 +00:00
|
|
|
|
|
|
|
if (!isMediaInDb()) {
|
|
|
|
menu.findItem(R.id.media_preview__overview).setVisible(false);
|
|
|
|
menu.findItem(R.id.delete).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;
|
2018-03-14 01:24:42 +00:00
|
|
|
case R.id.delete: deleteMedia(); return true;
|
2016-09-20 16:53:44 +00:00
|
|
|
case android.R.id.home: finish(); return true;
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-14 01:24:42 +00:00
|
|
|
private boolean isMediaInDb() {
|
|
|
|
return conversationRecipient != null;
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:11:49 +00:00
|
|
|
private @Nullable MediaItem getCurrentMediaItem() {
|
|
|
|
MediaItemAdapter adapter = (MediaItemAdapter)mediaPager.getAdapter();
|
|
|
|
|
|
|
|
if (adapter != null) {
|
|
|
|
return adapter.getMediaItemFor(mediaPager.getCurrentItem());
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 19:11:23 +00:00
|
|
|
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
|
|
|
}
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public @NonNull Loader<Pair<Cursor, Integer>> onCreateLoader(int id, Bundle args) {
|
2018-01-23 20:39:30 +00:00
|
|
|
return new PagingMediaLoader(this, conversationRecipient, initialMediaUri, leftIsRecent);
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public void onLoadFinished(@NonNull Loader<Pair<Cursor, Integer>> loader, @Nullable Pair<Cursor, Integer> data) {
|
2018-01-04 19:11:49 +00:00
|
|
|
if (data != null) {
|
|
|
|
@SuppressWarnings("ConstantConditions")
|
2018-01-25 03:17:44 +00:00
|
|
|
CursorPagerAdapter adapter = new CursorPagerAdapter(this, GlideApp.with(this), getWindow(), data.first, data.second, leftIsRecent);
|
2018-01-04 19:11:49 +00:00
|
|
|
mediaPager.setAdapter(adapter);
|
|
|
|
adapter.setActive(true);
|
2018-01-16 20:06:55 +00:00
|
|
|
|
2018-11-20 17:59:23 +00:00
|
|
|
viewModel.setCursor(this, data.first, leftIsRecent);
|
2018-11-09 07:33:37 +00:00
|
|
|
|
2019-05-02 14:36:20 +00:00
|
|
|
int item = restartItem >= 0 ? restartItem : data.second;
|
|
|
|
mediaPager.setCurrentItem(item);
|
|
|
|
|
|
|
|
if (item == 0) {
|
|
|
|
viewPagerListener.onPageSelected(0);
|
|
|
|
}
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public void onLoaderReset(@NonNull Loader<Pair<Cursor, Integer>> loader) {
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:11:49 +00:00
|
|
|
private class ViewPagerListener implements ViewPager.OnPageChangeListener {
|
|
|
|
|
|
|
|
private int currentPage = -1;
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
2020-11-30 09:11:49 +00:00
|
|
|
if (currentPage != -1 && currentPage != position) onPageUnselected(currentPage);
|
|
|
|
currentPage = position;
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
MediaItemAdapter adapter = (MediaItemAdapter)mediaPager.getAdapter();
|
|
|
|
|
|
|
|
if (adapter != null) {
|
|
|
|
MediaItem item = adapter.getMediaItemFor(position);
|
|
|
|
if (item.recipient != null) item.recipient.addListener(MediaPreviewActivity.this);
|
2018-11-09 07:33:37 +00:00
|
|
|
viewModel.setActiveAlbumRailItem(MediaPreviewActivity.this, position);
|
2020-08-31 13:16:15 +00:00
|
|
|
updateActionBar();
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void onPageUnselected(int position) {
|
|
|
|
MediaItemAdapter adapter = (MediaItemAdapter)mediaPager.getAdapter();
|
|
|
|
|
|
|
|
if (adapter != null) {
|
|
|
|
MediaItem item = adapter.getMediaItemFor(position);
|
|
|
|
if (item.recipient != null) item.recipient.removeListener(MediaPreviewActivity.this);
|
|
|
|
|
|
|
|
adapter.pause(position);
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:11:49 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int state) {
|
|
|
|
|
|
|
|
}
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class SingleItemPagerAdapter extends PagerAdapter implements MediaItemAdapter {
|
|
|
|
|
|
|
|
private final GlideRequests glideRequests;
|
|
|
|
private final Window window;
|
|
|
|
private final Uri uri;
|
|
|
|
private final String mediaType;
|
|
|
|
private final long size;
|
|
|
|
|
|
|
|
private final LayoutInflater inflater;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
SingleItemPagerAdapter(@NonNull Context context, @NonNull GlideRequests glideRequests,
|
|
|
|
@NonNull Window window, @NonNull Uri uri, @NonNull String mediaType,
|
|
|
|
long size)
|
2018-01-04 19:11:49 +00:00
|
|
|
{
|
|
|
|
this.glideRequests = glideRequests;
|
|
|
|
this.window = window;
|
|
|
|
this.uri = uri;
|
|
|
|
this.mediaType = mediaType;
|
|
|
|
this.size = size;
|
|
|
|
this.inflater = LayoutInflater.from(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
|
|
|
|
return view == object;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NonNull Object instantiateItem(@NonNull ViewGroup container, int position) {
|
|
|
|
View itemView = inflater.inflate(R.layout.media_view_page, container, false);
|
|
|
|
MediaView mediaView = itemView.findViewById(R.id.media_view);
|
|
|
|
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
mediaView.set(glideRequests, window, uri, mediaType, size, true);
|
2018-01-04 19:11:49 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
container.addView(itemView);
|
|
|
|
|
|
|
|
return itemView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
|
|
|
|
MediaView mediaView = ((FrameLayout)object).findViewById(R.id.media_view);
|
|
|
|
mediaView.cleanup();
|
|
|
|
|
|
|
|
container.removeView((FrameLayout)object);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MediaItem getMediaItemFor(int position) {
|
2018-03-14 01:24:42 +00:00
|
|
|
return new MediaItem(null, null, uri, mediaType, -1, true);
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void pause(int position) {
|
|
|
|
|
|
|
|
}
|
2018-11-09 07:33:37 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public @Nullable View getPlaybackControls(int position) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class CursorPagerAdapter extends PagerAdapter implements MediaItemAdapter {
|
|
|
|
|
|
|
|
private final WeakHashMap<Integer, MediaView> mediaViews = new WeakHashMap<>();
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final GlideRequests glideRequests;
|
|
|
|
private final Window window;
|
|
|
|
private final Cursor cursor;
|
2018-01-23 20:39:30 +00:00
|
|
|
private final boolean leftIsRecent;
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
private boolean active;
|
|
|
|
private int autoPlayPosition;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
CursorPagerAdapter(@NonNull Context context, @NonNull GlideRequests glideRequests,
|
|
|
|
@NonNull Window window, @NonNull Cursor cursor, int autoPlayPosition,
|
|
|
|
boolean leftIsRecent)
|
2018-01-04 19:11:49 +00:00
|
|
|
{
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
this.glideRequests = glideRequests;
|
|
|
|
this.window = window;
|
|
|
|
this.cursor = cursor;
|
|
|
|
this.autoPlayPosition = autoPlayPosition;
|
2018-01-23 20:39:30 +00:00
|
|
|
this.leftIsRecent = leftIsRecent;
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setActive(boolean active) {
|
|
|
|
this.active = active;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
if (!active) return 0;
|
|
|
|
else return cursor.getCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
|
|
|
|
return view == object;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NonNull Object instantiateItem(@NonNull ViewGroup container, int position) {
|
|
|
|
View itemView = LayoutInflater.from(context).inflate(R.layout.media_view_page, container, false);
|
|
|
|
MediaView mediaView = itemView.findViewById(R.id.media_view);
|
|
|
|
boolean autoplay = position == autoPlayPosition;
|
2018-01-23 20:39:30 +00:00
|
|
|
int cursorPosition = getCursorPosition(position);
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
autoPlayPosition = -1;
|
|
|
|
|
2018-01-23 20:39:30 +00:00
|
|
|
cursor.moveToPosition(cursorPosition);
|
2018-01-04 19:11:49 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
MediaRecord mediaRecord = MediaRecord.from(context, cursor);
|
2018-01-04 19:11:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
//noinspection ConstantConditions
|
2018-11-09 07:33:37 +00:00
|
|
|
mediaView.set(glideRequests, window, mediaRecord.getAttachment().getDataUri(),
|
|
|
|
mediaRecord.getAttachment().getContentType(), mediaRecord.getAttachment().getSize(), autoplay);
|
2018-01-04 19:11:49 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaViews.put(position, mediaView);
|
|
|
|
container.addView(itemView);
|
|
|
|
|
|
|
|
return itemView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
|
|
|
|
MediaView mediaView = ((FrameLayout)object).findViewById(R.id.media_view);
|
|
|
|
mediaView.cleanup();
|
|
|
|
|
|
|
|
mediaViews.remove(position);
|
|
|
|
container.removeView((FrameLayout)object);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MediaItem getMediaItemFor(int position) {
|
2018-01-23 20:39:30 +00:00
|
|
|
cursor.moveToPosition(getCursorPosition(position));
|
2018-01-25 03:17:44 +00:00
|
|
|
MediaRecord mediaRecord = MediaRecord.from(context, cursor);
|
2018-01-04 19:11:49 +00:00
|
|
|
Address address = mediaRecord.getAddress();
|
|
|
|
|
|
|
|
if (mediaRecord.getAttachment().getDataUri() == null) throw new AssertionError();
|
|
|
|
|
|
|
|
return new MediaItem(address != null ? Recipient.from(context, address,true) : null,
|
2018-03-14 01:24:42 +00:00
|
|
|
mediaRecord.getAttachment(),
|
2018-01-04 19:11:49 +00:00
|
|
|
mediaRecord.getAttachment().getDataUri(),
|
|
|
|
mediaRecord.getContentType(),
|
|
|
|
mediaRecord.getDate(),
|
|
|
|
mediaRecord.isOutgoing());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void pause(int position) {
|
|
|
|
MediaView mediaView = mediaViews.get(position);
|
|
|
|
if (mediaView != null) mediaView.pause();
|
|
|
|
}
|
2018-01-23 20:39:30 +00:00
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
@Override
|
|
|
|
public @Nullable View getPlaybackControls(int position) {
|
|
|
|
MediaView mediaView = mediaViews.get(position);
|
|
|
|
if (mediaView != null) return mediaView.getPlaybackControls();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:39:30 +00:00
|
|
|
private int getCursorPosition(int position) {
|
|
|
|
if (leftIsRecent) return position;
|
|
|
|
else return cursor.getCount() - 1 - position;
|
|
|
|
}
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class MediaItem {
|
2018-03-14 01:24:42 +00:00
|
|
|
private final @Nullable Recipient recipient;
|
|
|
|
private final @Nullable DatabaseAttachment attachment;
|
|
|
|
private final @NonNull Uri uri;
|
|
|
|
private final @NonNull String type;
|
|
|
|
private final long date;
|
|
|
|
private final boolean outgoing;
|
|
|
|
|
|
|
|
private MediaItem(@Nullable Recipient recipient,
|
|
|
|
@Nullable DatabaseAttachment attachment,
|
|
|
|
@NonNull Uri uri,
|
|
|
|
@NonNull String type,
|
|
|
|
long date,
|
|
|
|
boolean outgoing)
|
|
|
|
{
|
|
|
|
this.recipient = recipient;
|
|
|
|
this.attachment = attachment;
|
|
|
|
this.uri = uri;
|
|
|
|
this.type = type;
|
|
|
|
this.date = date;
|
|
|
|
this.outgoing = outgoing;
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MediaItemAdapter {
|
|
|
|
MediaItem getMediaItemFor(int position);
|
|
|
|
void pause(int position);
|
2018-11-09 07:33:37 +00:00
|
|
|
@Nullable View getPlaybackControls(int position);
|
2018-01-04 19:11:49 +00:00
|
|
|
}
|
2014-08-12 19:11:23 +00:00
|
|
|
}
|