diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/persistance/FileBackend.java | 6 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/MediaBrowserActivity.java | 124 | ||||
-rw-r--r-- | src/main/res/menu/media_browser.xml | 12 | ||||
-rw-r--r-- | src/main/res/values/defaults.xml | 1 | ||||
-rw-r--r-- | src/main/res/values/strings.xml | 1 |
5 files changed, 131 insertions, 13 deletions
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<Attachment> convertToAttachments(List<DatabaseBackend.FilePath> relativeFilePaths) { - List<Attachment> attachments = new ArrayList<>(); + public List<Attachment> convertToAttachments(final List<DatabaseBackend.FilePath> relativeFilePaths) { + final List<Attachment> 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<Attachment> allAttachments = new ArrayList<>(); + ArrayList<Attachment> 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<Attachment> 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<Attachment> 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<Attachment> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + <item + android:id="@+id/show_videos_images_only" + android:checkable="true" + android:checked="false" + android:orderInCategory="85" + android:title="@string/show_videos_images_only" + app:showAsAction="never" /> +</menu>
\ 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 @@ <bool name="use_unicolored_chatbg">false</bool> <bool name="alternative_voice_settings">false</bool> <bool name="use_colored_muc_names">false</bool> + <bool name="show_videos_images_only">false</bool> <string-array name="domains"> <item>pix-art.de</item> 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 @@ <string name="pref_alternative_voice_settings">Alternative voice recording method</string> <string name="pref_use_colored_muc_names_summary">Colorize nicknames in group chats or channels like default avatar backgrounds.</string> <string name="pref_use_colored_muc_names">Colorize nicknames</string> + <string name="show_videos_images_only">Only images/videos</string> </resources> |