From 0fffc7a1ac17c6e6bb5f16c707cc0002a28708f1 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 24 Jun 2018 20:54:01 +0200 Subject: [PATCH] restart file observer when permisson are granted --- .../services/XmppConnectionService.java | 13 ++++++++++++- .../conversations/ui/ConversationFragment.java | 17 ++++++++++++++++- .../utils/ConversationsFileObserver.java | 5 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index b30d31fb4..813f1eec4 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -9,6 +9,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.database.ContentObserver; import android.graphics.Bitmap; import android.media.AudioManager; @@ -29,6 +30,7 @@ import android.security.KeyChain; import android.support.annotation.BoolRes; import android.support.annotation.IntegerRes; import android.support.v4.app.RemoteInput; +import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.util.DisplayMetrics; import android.util.Log; @@ -64,6 +66,7 @@ import java.util.concurrent.atomic.AtomicLong; import javax.security.auth.callback.Callback; import eu.siacs.conversations.Config; +import eu.siacs.conversations.Manifest; import eu.siacs.conversations.R; import eu.siacs.conversations.crypto.OmemoSetting; import eu.siacs.conversations.crypto.PgpDecryptionService; @@ -970,7 +973,10 @@ public class XmppConnectionService extends Service { restoreFromDatabase(); getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver); - new Thread(fileObserver::startWatching).start(); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + Log.d(Config.LOGTAG,"starting file observer"); + new Thread(fileObserver::startWatching).start(); + } if (Config.supportOpenPgp()) { this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() { @Override @@ -1024,6 +1030,11 @@ public class XmppConnectionService extends Service { super.onDestroy(); } + public void restartFileObserver() { + Log.d(Config.LOGTAG,"restarting file observer"); + new Thread(fileObserver::restartWatching).start(); + } + public void toggleScreenEventReceiver() { if (awayWhenScreenOff() && !manuallyChangePresence()) { final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 91381e47f..7d844c4ed 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -499,6 +499,15 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return true; } + private static boolean writeGranted(int[] grantResults, String[] permission) { + for(int i = 0; i < grantResults.length; ++i) { + if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission[i])) { + return grantResults[i] == PackageManager.PERMISSION_GRANTED; + } + } + return false; + } + private static String getFirstDenied(int[] grantResults, String[] permissions) { for (int i = 0; i < grantResults.length; ++i) { if (grantResults[i] == PackageManager.PERMISSION_DENIED) { @@ -1343,7 +1352,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { - if (grantResults.length > 0) + if (grantResults.length > 0) { if (allGranted(grantResults)) { if (requestCode == REQUEST_START_DOWNLOAD) { if (this.mPendingDownloadableMessage != null) { @@ -1368,6 +1377,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } Toast.makeText(getActivity(), res, Toast.LENGTH_SHORT).show(); } + } + if (writeGranted(grantResults, permissions)) { + if (activity != null && activity.xmppConnectionService != null) { + activity.xmppConnectionService.restartFileObserver(); + } + } } public void startDownloadable(Message message) { diff --git a/src/main/java/eu/siacs/conversations/utils/ConversationsFileObserver.java b/src/main/java/eu/siacs/conversations/utils/ConversationsFileObserver.java index aab0e146f..33f08d90b 100644 --- a/src/main/java/eu/siacs/conversations/utils/ConversationsFileObserver.java +++ b/src/main/java/eu/siacs/conversations/utils/ConversationsFileObserver.java @@ -75,6 +75,11 @@ public abstract class ConversationsFileObserver { abstract public void onEvent(int event, String path); + public void restartWatching() { + stopWatching(); + startWatching(); + } + private class SingleFileObserver extends FileObserver { private final String path;