From 71afe8ed86449aeb3795cef48fe815fd23e388d4 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 9 Jul 2018 21:15:56 +0200 Subject: restart file observer when permisson are granted --- .../pixart/messenger/services/XmppConnectionService.java | 11 ++++++++++- .../java/de/pixart/messenger/ui/ConversationFragment.java | 14 ++++++++++++++ .../pixart/messenger/utils/ConversationsFileObserver.java | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index cca4d5fc2..f45a5ac8e 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -31,6 +31,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; @@ -1102,7 +1103,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 @@ -1161,6 +1165,11 @@ public class XmppConnectionService extends Service { CancelAutomaticExport(true); } + 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/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index fa2ba5327..2458e4967 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -566,6 +566,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) { @@ -1618,6 +1627,11 @@ 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/de/pixart/messenger/utils/ConversationsFileObserver.java b/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java index 72a01a13c..4e275df3f 100644 --- a/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java +++ b/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java @@ -80,6 +80,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; -- cgit v1.2.3