restart file observer when permisson are granted

This commit is contained in:
Daniel Gultsch 2018-06-24 20:54:01 +02:00
parent 9d69b38147
commit 0fffc7a1ac
3 changed files with 33 additions and 2 deletions

View file

@ -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);
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);

View file

@ -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) {
@ -1369,6 +1378,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) {
if (!hasPermissions(REQUEST_START_DOWNLOAD, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

View file

@ -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;