diff options
Diffstat (limited to 'src/main/java/de/pixart')
-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 |
2 files changed, 117 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 |