toggle foreground service to set correct type when gaining permissions

This commit is contained in:
Daniel Gultsch 2023-10-21 19:47:39 +02:00
parent cec8a7ec55
commit 48ffde9656
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
5 changed files with 44 additions and 3 deletions

View file

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

View file

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

View file

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

View file

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

View file

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