From 21ddcc478f9e8423a5f8e61026cc797592e10858 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Wed, 12 Feb 2020 20:37:32 +0100 Subject: set filter to MediaBrowserActivity to show only images and videos or all media files fixes #457 --- .../pixart/messenger/persistance/FileBackend.java | 6 +- .../pixart/messenger/ui/MediaBrowserActivity.java | 124 +++++++++++++++++++-- src/main/res/menu/media_browser.xml | 12 ++ src/main/res/values/defaults.xml | 1 + src/main/res/values/strings.xml | 1 + 5 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 src/main/res/menu/media_browser.xml (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java index be9488205..402ea50a1 100644 --- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java @@ -305,12 +305,12 @@ public class FileBackend { return true; } - public List convertToAttachments(List relativeFilePaths) { - List attachments = new ArrayList<>(); + public List convertToAttachments(final List relativeFilePaths) { + final List attachments = new ArrayList<>(); for (DatabaseBackend.FilePath relativeFilePath : relativeFilePaths) { final String mime = MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(relativeFilePath.path)); final File file = getFileForPath(relativeFilePath.path, mime); - if (file.exists() && mime != null && (mime.startsWith("image/") || mime.startsWith("video/"))) { + if (file.exists()) { attachments.add(Attachment.of(relativeFilePath.uuid, file, mime)); } } diff --git a/src/main/java/de/pixart/messenger/ui/MediaBrowserActivity.java b/src/main/java/de/pixart/messenger/ui/MediaBrowserActivity.java index fb9ec31ef..2ed416c17 100644 --- a/src/main/java/de/pixart/messenger/ui/MediaBrowserActivity.java +++ b/src/main/java/de/pixart/messenger/ui/MediaBrowserActivity.java @@ -3,11 +3,14 @@ package de.pixart.messenger.ui; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import androidx.appcompat.widget.Toolbar; import androidx.databinding.DataBindingUtil; +import java.util.ArrayList; import java.util.List; import de.pixart.messenger.R; @@ -19,14 +22,21 @@ import de.pixart.messenger.ui.adapter.MediaAdapter; import de.pixart.messenger.ui.interfaces.OnMediaLoaded; import de.pixart.messenger.ui.util.Attachment; import de.pixart.messenger.ui.util.GridManager; +import de.pixart.messenger.utils.MenuDoubleTabUtil; import rocks.xmpp.addr.Jid; public class MediaBrowserActivity extends XmppActivity implements OnMediaLoaded { private ActivityMediaBrowserBinding binding; - private MediaAdapter mMediaAdapter; + private boolean OnlyImagesVideos = false; + ArrayList allAttachments = new ArrayList<>(); + ArrayList filteredAttachments = new ArrayList<>(); + private String mSavedInstanceAccount; + private String mSavedInstanceJid; + private String account; + private String jid; public static void launch(Context context, Contact contact) { launch(context, contact.getAccount(), contact.getJid().asBareJid().toEscapedString()); @@ -46,6 +56,10 @@ public class MediaBrowserActivity extends XmppActivity implements OnMediaLoaded @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + if (savedInstanceState != null) { + this.mSavedInstanceAccount = savedInstanceState.getString("account"); + this.mSavedInstanceJid = savedInstanceState.getString("jid"); + } this.binding = DataBindingUtil.setContentView(this, R.layout.activity_media_browser); setSupportActionBar((Toolbar) binding.toolbar); configureActionBar(getSupportActionBar()); @@ -54,18 +68,76 @@ public class MediaBrowserActivity extends XmppActivity implements OnMediaLoaded GridManager.setupLayoutManager(this, this.binding.media, R.dimen.browser_media_size); this.binding.noMedia.setVisibility(View.GONE); this.binding.progressbar.setVisibility(View.VISIBLE); + this.OnlyImagesVideos = getPreferences().getBoolean("show_videos_images_only", this.getResources().getBoolean(R.bool.show_videos_images_only)); + } + + @Override + public void onSaveInstanceState(final Bundle savedInstanceState) { + savedInstanceState.putString("account", account); + savedInstanceState.putString("jid", jid); + super.onSaveInstanceState(savedInstanceState); } @Override protected void refreshUiReal() { + mMediaAdapter.notifyDataSetChanged(); + } + @Override + public boolean onPrepareOptionsMenu(final Menu menu) { + MenuItem showImagesVideosOnly = menu.findItem(R.id.show_videos_images_only); + showImagesVideosOnly.setChecked(OnlyImagesVideos); + return true; + } + + @Override + public boolean onCreateOptionsMenu(final Menu menu) { + getMenuInflater().inflate(R.menu.media_browser, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(final MenuItem menuItem) { + if (MenuDoubleTabUtil.shouldIgnoreTap()) { + return false; + } + switch (menuItem.getItemId()) { + case android.R.id.home: + finish(); + break; + case R.id.show_videos_images_only: + this.OnlyImagesVideos = !menuItem.isChecked(); + menuItem.setChecked(this.OnlyImagesVideos); + getPreferences().edit().putBoolean("show_videos_images_only", OnlyImagesVideos).apply(); + filter(OnlyImagesVideos); + invalidateOptionsMenu(); + refreshUiReal(); + break; + } + return super.onOptionsItemSelected(menuItem); } @Override void onBackendConnected() { - Intent intent = getIntent(); - String account = intent == null ? null : intent.getStringExtra("account"); - String jid = intent == null ? null : intent.getStringExtra("jid"); + final Intent intent = getIntent(); + if (mSavedInstanceAccount != null) { + try { + account = mSavedInstanceAccount; + } catch (Exception e) { + account = intent == null ? null : intent.getStringExtra("account"); + } + } else { + account = intent == null ? null : intent.getStringExtra("account"); + } + if (mSavedInstanceJid != null) { + try { + jid = mSavedInstanceJid; + } catch (Exception e) { + jid = intent == null ? null : intent.getStringExtra("jid"); + } + } else { + jid = intent == null ? null : intent.getStringExtra("jid"); + } if (account != null && jid != null) { xmppConnectionService.getAttachments(account, Jid.of(jid), 0, this); } @@ -73,15 +145,47 @@ public class MediaBrowserActivity extends XmppActivity implements OnMediaLoaded @Override public void onMediaLoaded(List attachments) { + allAttachments.addAll(attachments); runOnUiThread(() -> { - if (attachments.size() > 0) { - mMediaAdapter.setAttachments(attachments); - this.binding.noMedia.setVisibility(View.GONE); - this.binding.progressbar.setVisibility(View.GONE); + if (OnlyImagesVideos) { + filter(OnlyImagesVideos); } else { - this.binding.noMedia.setVisibility(View.VISIBLE); - this.binding.progressbar.setVisibility(View.GONE); + loadAttachments(allAttachments); } }); } + + private void loadAttachments(List attachments) { + if (attachments.size() > 0) { + mMediaAdapter.setAttachments(attachments); + this.binding.noMedia.setVisibility(View.GONE); + this.binding.progressbar.setVisibility(View.GONE); + } else { + this.binding.noMedia.setVisibility(View.VISIBLE); + this.binding.progressbar.setVisibility(View.GONE); + } + } + + protected void filter(boolean needle) { + if (xmppConnectionServiceBound) { + filterAttachments(needle); + } + } + + protected void filterAttachments(boolean needle) { + if (allAttachments.size() > 0) { + final ArrayList attachments = new ArrayList<>(allAttachments); + filteredAttachments.clear(); + if (needle) { + for (Attachment attachment : attachments) { + if (attachment.getMime() != null && (attachment.getMime().startsWith("image/") || attachment.getMime().startsWith("video/"))) { + filteredAttachments.add(attachment); + } + } + } else { + filteredAttachments.addAll(allAttachments); + } + loadAttachments(filteredAttachments); + } + } } \ No newline at end of file diff --git a/src/main/res/menu/media_browser.xml b/src/main/res/menu/media_browser.xml new file mode 100644 index 000000000..8d8674c74 --- /dev/null +++ b/src/main/res/menu/media_browser.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index c32c092f4..ff7696134 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -74,6 +74,7 @@ false false false + false pix-art.de diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index f61da74b9..51609a777 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -1002,4 +1002,5 @@ Alternative voice recording method Colorize nicknames in group chats or channels like default avatar backgrounds. Colorize nicknames + Only images/videos -- cgit v1.2.3