From 48ffde965642111303e103cbfd742807f36f63a6 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 21 Oct 2023 19:47:39 +0200 Subject: [PATCH] toggle foreground service to set correct type when gaining permissions --- .../services/XmppConnectionService.java | 15 +++++++++++++++ .../conversations/ui/ConversationFragment.java | 5 +++++ .../ui/ConversationsActivity.java | 5 ++++- .../conversations/ui/EditAccountActivity.java | 4 ++++ .../conversations/utils/PermissionUtils.java | 18 ++++++++++++++++-- 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 71fd9844c..a202a480c 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -126,6 +126,7 @@ import eu.siacs.conversations.persistance.DatabaseBackend; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.UnifiedPushDatabase; import eu.siacs.conversations.ui.ChooseAccountForProfilePictureActivity; +import eu.siacs.conversations.ui.ConversationsActivity; import eu.siacs.conversations.ui.RtpSessionActivity; import eu.siacs.conversations.ui.SettingsActivity; import eu.siacs.conversations.ui.UiCallback; @@ -5149,4 +5150,18 @@ public class XmppConnectionService extends Service { return Objects.hashCode(id, media, reconnecting); } } + + public static void toggleForegroundService(final XmppConnectionService service) { + if (service == null) { + return; + } + service.toggleForegroundService(); + } + + public static void toggleForegroundService(final ConversationsActivity activity) { + if (activity == null) { + return; + } + toggleForegroundService(activity.xmppConnectionService); + } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 096a704cd..a25c4d3c3 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -4,6 +4,8 @@ import static eu.siacs.conversations.ui.XmppActivity.EXTRA_ACCOUNT; import static eu.siacs.conversations.ui.XmppActivity.REQUEST_INVITE_TO_CONVERSATION; import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.hideSoftKeyboard; import static eu.siacs.conversations.utils.PermissionUtils.allGranted; +import static eu.siacs.conversations.utils.PermissionUtils.audioGranted; +import static eu.siacs.conversations.utils.PermissionUtils.cameraGranted; import static eu.siacs.conversations.utils.PermissionUtils.getFirstDenied; import static eu.siacs.conversations.utils.PermissionUtils.writeGranted; @@ -1869,6 +1871,9 @@ public class ConversationFragment extends XmppFragment } refresh(); } + if (cameraGranted(grantResults, permissions) || audioGranted(grantResults, permissions)) { + XmppConnectionService.toggleForegroundService(activity); + } } public void startDownloadable(Message message) { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index ba231c584..94801c688 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -63,6 +63,7 @@ import org.openintents.openpgp.util.OpenPgpApi; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import eu.siacs.conversations.Config; @@ -312,14 +313,16 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio } } - private void handleActivityResult(ActivityResult activityResult) { + private void handleActivityResult(final ActivityResult activityResult) { if (activityResult.resultCode == Activity.RESULT_OK) { handlePositiveActivityResult(activityResult.requestCode, activityResult.data); } else { handleNegativeActivityResult(activityResult.requestCode); } if (activityResult.requestCode == REQUEST_BATTERY_OP) { + // the result code is always 0 even when battery permission were granted requestNotificationPermissionIfNeeded(); + XmppConnectionService.toggleForegroundService(xmppConnectionService); } } diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index f15d18b3c..62150d9c2 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -472,6 +472,10 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) { updateAccountInformation(mAccount == null); } + if (requestCode == REQUEST_BATTERY_OP) { + // the result code is always 0 even when battery permission were granted + XmppConnectionService.toggleForegroundService(xmppConnectionService); + } if (requestCode == REQUEST_CHANGE_STATUS) { PresenceTemplate template = mPendingPresenceTemplate.pop(); if (template != null && resultCode == Activity.RESULT_OK) { diff --git a/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java b/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java index 004676156..21d7f42a1 100644 --- a/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/PermissionUtils.java @@ -24,9 +24,23 @@ public class PermissionUtils { return true; } - public static boolean writeGranted(int[] grantResults, String[] permission) { + public static boolean writeGranted(final int[] grantResults, final String[] permissions) { + return permissionGranted( + Manifest.permission.WRITE_EXTERNAL_STORAGE, grantResults, permissions); + } + + public static boolean audioGranted(final int[] grantResults, final String[] permissions) { + return permissionGranted(Manifest.permission.RECORD_AUDIO, grantResults, permissions); + } + + public static boolean cameraGranted(final int[] grantResults, final String[] permissions) { + return permissionGranted(Manifest.permission.CAMERA, grantResults, permissions); + } + + private static boolean permissionGranted( + final String permission, final int[] grantResults, final String[] permissions) { for (int i = 0; i < grantResults.length; ++i) { - if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission[i])) { + if (permission.equals(permissions[i])) { return grantResults[i] == PackageManager.PERMISSION_GRANTED; } }