From 14644077e7d8b09061b11b87daaa44d151ba5705 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 23 Jan 2018 12:39:30 -0800 Subject: [PATCH] Change media preview swipe direction based on entry point Fixes #7354 --- .../securesms/ConversationItem.java | 1 + .../securesms/MediaGalleryAdapter.java | 1 + .../securesms/MediaPreviewActivity.java | 29 +++++++++++++------ .../RecipientPreferenceActivity.java | 1 + .../database/loaders/PagingMediaLoader.java | 10 ++++--- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationItem.java b/src/org/thoughtcrime/securesms/ConversationItem.java index 47fdc21497..11c559c7f4 100644 --- a/src/org/thoughtcrime/securesms/ConversationItem.java +++ b/src/org/thoughtcrime/securesms/ConversationItem.java @@ -611,6 +611,7 @@ public class ConversationItem extends LinearLayout intent.putExtra(MediaPreviewActivity.OUTGOING_EXTRA, messageRecord.isOutgoing()); intent.putExtra(MediaPreviewActivity.DATE_EXTRA, messageRecord.getTimestamp()); intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, slide.asAttachment().getSize()); + intent.putExtra(MediaPreviewActivity.LEFT_IS_RECENT_EXTRA, false); context.startActivity(intent); } else if (slide.getUri() != null) { diff --git a/src/org/thoughtcrime/securesms/MediaGalleryAdapter.java b/src/org/thoughtcrime/securesms/MediaGalleryAdapter.java index 8fcd2f449b..b8ee0fad56 100644 --- a/src/org/thoughtcrime/securesms/MediaGalleryAdapter.java +++ b/src/org/thoughtcrime/securesms/MediaGalleryAdapter.java @@ -137,6 +137,7 @@ class MediaGalleryAdapter extends StickyHeaderGridAdapter { intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, mediaRecord.getAttachment().getSize()); intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, address); intent.putExtra(MediaPreviewActivity.OUTGOING_EXTRA, mediaRecord.isOutgoing()); + intent.putExtra(MediaPreviewActivity.LEFT_IS_RECENT_EXTRA, true); if (mediaRecord.getAddress() != null) { intent.putExtra(MediaPreviewActivity.ADDRESS_EXTRA, mediaRecord.getAddress()); diff --git a/src/org/thoughtcrime/securesms/MediaPreviewActivity.java b/src/org/thoughtcrime/securesms/MediaPreviewActivity.java index 84a0595c7d..1d38299273 100644 --- a/src/org/thoughtcrime/securesms/MediaPreviewActivity.java +++ b/src/org/thoughtcrime/securesms/MediaPreviewActivity.java @@ -73,10 +73,11 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im private final static String TAG = MediaPreviewActivity.class.getSimpleName(); - 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"; + 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"; + public static final String LEFT_IS_RECENT_EXTRA = "left_is_recent"; private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); @@ -87,6 +88,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im private String initialMediaType; private long initialMediaSize; private Recipient conversationRecipient; + private boolean leftIsRecent; private int restartItem = -1; @@ -179,6 +181,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im initialMediaUri = getIntent().getData(); initialMediaType = getIntent().getType(); initialMediaSize = getIntent().getLongExtra(SIZE_EXTRA, 0); + leftIsRecent = getIntent().getBooleanExtra(LEFT_IS_RECENT_EXTRA, false); restartItem = -1; if (address != null) { @@ -294,14 +297,14 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im @Override public Loader> onCreateLoader(int id, Bundle args) { - return new PagingMediaLoader(this, conversationRecipient, initialMediaUri); + return new PagingMediaLoader(this, conversationRecipient, initialMediaUri, leftIsRecent); } @Override public void onLoadFinished(Loader> loader, @Nullable Pair data) { if (data != null) { @SuppressWarnings("ConstantConditions") - CursorPagerAdapter adapter = new CursorPagerAdapter(this, masterSecret, GlideApp.with(this), getWindow(), data.first, data.second); + CursorPagerAdapter adapter = new CursorPagerAdapter(this, masterSecret, GlideApp.with(this), getWindow(), data.first, data.second, leftIsRecent); mediaPager.setAdapter(adapter); adapter.setActive(true); @@ -423,13 +426,14 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im private final GlideRequests glideRequests; private final Window window; private final Cursor cursor; + private final boolean leftIsRecent; private boolean active; private int autoPlayPosition; CursorPagerAdapter(@NonNull Context context, @NonNull MasterSecret masterSecret, @NonNull GlideRequests glideRequests, @NonNull Window window, - @NonNull Cursor cursor, int autoPlayPosition) + @NonNull Cursor cursor, int autoPlayPosition, boolean leftIsRecent) { this.context = context.getApplicationContext(); this.masterSecret = masterSecret; @@ -437,6 +441,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im this.window = window; this.cursor = cursor; this.autoPlayPosition = autoPlayPosition; + this.leftIsRecent = leftIsRecent; } public void setActive(boolean active) { @@ -460,10 +465,11 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im 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; + int cursorPosition = getCursorPosition(position); autoPlayPosition = -1; - cursor.moveToPosition(position); + cursor.moveToPosition(cursorPosition); MediaRecord mediaRecord = MediaRecord.from(context, masterSecret, cursor); @@ -490,7 +496,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im } public MediaItem getMediaItemFor(int position) { - cursor.moveToPosition(position); + cursor.moveToPosition(getCursorPosition(position)); MediaRecord mediaRecord = MediaRecord.from(context, masterSecret, cursor); Address address = mediaRecord.getAddress(); @@ -508,6 +514,11 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im MediaView mediaView = mediaViews.get(position); if (mediaView != null) mediaView.pause(); } + + private int getCursorPosition(int position) { + if (leftIsRecent) return position; + else return cursor.getCount() - 1 - position; + } } private static class MediaItem { diff --git a/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java b/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java index 9d33817637..b3bf08198c 100644 --- a/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java +++ b/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java @@ -161,6 +161,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi intent.putExtra(MediaPreviewActivity.OUTGOING_EXTRA, mediaRecord.isOutgoing()); intent.putExtra(MediaPreviewActivity.DATE_EXTRA, mediaRecord.getDate()); intent.putExtra(MediaPreviewActivity.SIZE_EXTRA, mediaRecord.getAttachment().getSize()); + intent.putExtra(MediaPreviewActivity.LEFT_IS_RECENT_EXTRA, true); intent.setDataAndType(mediaRecord.getAttachment().getDataUri(), mediaRecord.getContentType()); startActivity(intent); }); diff --git a/src/org/thoughtcrime/securesms/database/loaders/PagingMediaLoader.java b/src/org/thoughtcrime/securesms/database/loaders/PagingMediaLoader.java index 97aac91f13..8846bbd1ff 100644 --- a/src/org/thoughtcrime/securesms/database/loaders/PagingMediaLoader.java +++ b/src/org/thoughtcrime/securesms/database/loaders/PagingMediaLoader.java @@ -22,11 +22,13 @@ public class PagingMediaLoader extends AsyncLoader> { private final Recipient recipient; private final Uri uri; + private final boolean leftIsRecent; - public PagingMediaLoader(@NonNull Context context, @NonNull Recipient recipient, @NonNull Uri uri) { + public PagingMediaLoader(@NonNull Context context, @NonNull Recipient recipient, @NonNull Uri uri, boolean leftIsRecent) { super(context); - this.recipient = recipient; - this.uri = uri; + this.recipient = recipient; + this.uri = uri; + this.leftIsRecent = leftIsRecent; } @Nullable @@ -40,7 +42,7 @@ public class PagingMediaLoader extends AsyncLoader> { Uri attachmentUri = PartAuthority.getAttachmentDataUri(attachmentId); if (attachmentUri.equals(uri)) { - return new Pair<>(cursor, cursor.getPosition()); + return new Pair<>(cursor, leftIsRecent ? cursor.getPosition() : cursor.getCount() - 1 - cursor.getPosition()); } }