From bd128335d35a2d8f1877981abf5a1b97f7dc650d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 17 Sep 2018 21:24:25 +0200 Subject: [PATCH] do not show media overview in contact/conference details when missing storage permission --- .../siacs/conversations/persistance/FileBackend.java | 4 +++- .../services/AbstractConnectionManager.java | 6 +----- .../services/XmppConnectionService.java | 2 +- .../conversations/ui/ConferenceDetailsActivity.java | 10 +++++++--- .../conversations/ui/ContactDetailsActivity.java | 12 +++++++++--- .../eu/siacs/conversations/ui/util/GridManager.java | 5 +++++ .../eu/siacs/conversations/utils/Compatibility.java | 6 ++++++ 7 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 501dd0bab..3382aa164 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -259,7 +259,9 @@ public class FileBackend { bitmap = withGifOverlay; } } - cache.put(key, bitmap); + if (bitmap != null) { + cache.put(key, bitmap); + } return bitmap; } diff --git a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java index f777b1bf8..677d7dd70 100644 --- a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java @@ -110,11 +110,7 @@ public class AbstractConnectionManager { } public boolean hasStoragePermission() { - if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; - } else { - return true; - } + return Compatibility.hasStoragePermission(mXmppConnectionService); } public void updateConversationUi(boolean force) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 0b1d5ad71..69a8e1abb 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -992,7 +992,7 @@ public class XmppConnectionService extends Service { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { startContactObserver(); } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + if (Compatibility.hasStoragePermission(this)) { Log.d(Config.LOGTAG, "starting file observer"); new Thread(fileObserver::startWatching).start(); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java index a5339b9ba..ac6d627a2 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java @@ -57,6 +57,7 @@ import eu.siacs.conversations.ui.util.MenuDoubleTabUtil; import eu.siacs.conversations.ui.util.MucDetailsContextMenuHelper; import eu.siacs.conversations.ui.util.MyLinkify; import eu.siacs.conversations.ui.util.SoftKeyboardUtils; +import eu.siacs.conversations.utils.Compatibility; import eu.siacs.conversations.utils.EmojiWrapper; import eu.siacs.conversations.utils.StringUtils; import eu.siacs.conversations.utils.StylingHelper; @@ -290,6 +291,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers if (this.mTheme != theme) { recreate(); } + binding.mediaWrapper.setVisibility(Compatibility.hasStoragePermission(this) ? View.VISIBLE : View.GONE); } @Override @@ -486,9 +488,11 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers if (uuid != null) { this.mConversation = xmppConnectionService.findConversationByUuid(uuid); if (this.mConversation != null) { - final int limit = GridManager.getCurrentColumnCount(this.binding.media); - xmppConnectionService.getAttachments(this.mConversation, limit, this); - this.binding.showMedia.setOnClickListener((v)->MediaBrowserActivity.launch(this,mConversation)); + if (Compatibility.hasStoragePermission(this)) { + final int limit = GridManager.getCurrentColumnCount(this.binding.media); + xmppConnectionService.getAttachments(this.mConversation, limit, this); + this.binding.showMedia.setOnClickListener((v) -> MediaBrowserActivity.launch(this, mConversation)); + } updateView(); } } diff --git a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java index 45165d391..a72299cd3 100644 --- a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.ui; +import android.Manifest; import android.content.ActivityNotFoundException; import android.content.DialogInterface; import android.content.Intent; @@ -11,6 +12,7 @@ import android.preference.PreferenceManager; import android.provider.ContactsContract.CommonDataKinds; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Intents; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; @@ -45,6 +47,7 @@ import eu.siacs.conversations.ui.interfaces.OnMediaLoaded; import eu.siacs.conversations.ui.util.Attachment; import eu.siacs.conversations.ui.util.GridManager; import eu.siacs.conversations.ui.util.MenuDoubleTabUtil; +import eu.siacs.conversations.utils.Compatibility; import eu.siacs.conversations.utils.IrregularUnicodeDetector; import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.XmppUri; @@ -217,6 +220,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp this.showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS, false); this.showLastSeen = preferences.getBoolean("last_activity", false); } + binding.mediaWrapper.setVisibility(Compatibility.hasStoragePermission(this) ? View.VISIBLE : View.GONE); mMediaAdapter.setAttachments(Collections.emptyList()); } @@ -489,9 +493,11 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp mPendingFingerprintVerificationUri = null; } - final int limit = GridManager.getCurrentColumnCount(this.binding.media); - xmppConnectionService.getAttachments(account, contact.getJid().asBareJid(), limit, this); - this.binding.showMedia.setOnClickListener((v)->MediaBrowserActivity.launch(this,contact)); + if (Compatibility.hasStoragePermission(this)) { + final int limit = GridManager.getCurrentColumnCount(this.binding.media); + xmppConnectionService.getAttachments(account, contact.getJid().asBareJid(), limit, this); + this.binding.showMedia.setOnClickListener((v) -> MediaBrowserActivity.launch(this, contact)); + } populateView(); } } diff --git a/src/main/java/eu/siacs/conversations/ui/util/GridManager.java b/src/main/java/eu/siacs/conversations/ui/util/GridManager.java index 467126f51..092e582ec 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/GridManager.java +++ b/src/main/java/eu/siacs/conversations/ui/util/GridManager.java @@ -22,6 +22,11 @@ public class GridManager { @Override public void onGlobalLayout() { recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); + final int availableWidth = recyclerView.getMeasuredWidth(); + if (availableWidth == 0) { + Log.e(Config.LOGTAG,"GridManager: available width was 0; probably because layout was hidden"); + return; + } final ColumnInfo columnInfo = calculateColumnCount(context, recyclerView.getMeasuredWidth(), desiredSize); Log.d(Config.LOGTAG, "final count " + columnInfo.count); if (recyclerView.getAdapter().getItemCount() != 0) { diff --git a/src/main/java/eu/siacs/conversations/utils/Compatibility.java b/src/main/java/eu/siacs/conversations/utils/Compatibility.java index caa592ceb..487886bea 100644 --- a/src/main/java/eu/siacs/conversations/utils/Compatibility.java +++ b/src/main/java/eu/siacs/conversations/utils/Compatibility.java @@ -2,12 +2,14 @@ package eu.siacs.conversations.utils; import android.content.Context; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.os.Build; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceGroup; import android.preference.PreferenceManager; import android.support.annotation.BoolRes; +import android.support.v4.content.ContextCompat; import java.util.Arrays; import java.util.Collections; @@ -28,6 +30,10 @@ public class Compatibility { private static final List UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings"); + public static boolean hasStoragePermission(Context context) { + return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; + } + public static boolean twentySix() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; }