From ba2b9bf1a030a430109b8ea7a99c36206e987d32 Mon Sep 17 00:00:00 2001 From: Mark Harman Date: Wed, 8 May 2024 22:26:49 +0100 Subject: [PATCH] Fix crash for tiles for targetting Android 14. --- .../net/sourceforge/opencamera/MyTileService.java | 14 +++++++++++++- .../opencamera/MyTileServiceFrontCamera.java | 14 +++++++++++++- .../sourceforge/opencamera/MyTileServiceVideo.java | 14 +++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/net/sourceforge/opencamera/MyTileService.java b/app/src/main/java/net/sourceforge/opencamera/MyTileService.java index 45f9c111..4954cb3a 100644 --- a/app/src/main/java/net/sourceforge/opencamera/MyTileService.java +++ b/app/src/main/java/net/sourceforge/opencamera/MyTileService.java @@ -1,5 +1,6 @@ package net.sourceforge.opencamera; +import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.service.quicksettings.TileService; @@ -46,6 +47,17 @@ public class MyTileService extends TileService { Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setAction(TILE_ID); - startActivityAndCollapse(intent); // use this instead of startActivity() so that the notification panel doesn't remain pulled down + // use startActivityAndCollapse() instead of startActivity() so that the notification panel doesn't remain pulled down + if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE ) { + // startActivityAndCollapse(Intent) throws UnsupportedOperationException on Android 14+ + // FLAG_IMMUTABLE needed for PendingIntents on Android 12+ + PendingIntent pending_intent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); + startActivityAndCollapse(pending_intent); + } + else { + // still get warning for startActivityAndCollapse being deprecated, but startActivityAndCollapse(PendingIntent) requires Android 14+ + // and only seems possible to disable the warning for the function, not this statement + startActivityAndCollapse(intent); + } } } diff --git a/app/src/main/java/net/sourceforge/opencamera/MyTileServiceFrontCamera.java b/app/src/main/java/net/sourceforge/opencamera/MyTileServiceFrontCamera.java index fe176538..3bcb684a 100644 --- a/app/src/main/java/net/sourceforge/opencamera/MyTileServiceFrontCamera.java +++ b/app/src/main/java/net/sourceforge/opencamera/MyTileServiceFrontCamera.java @@ -1,5 +1,6 @@ package net.sourceforge.opencamera; +import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.service.quicksettings.TileService; @@ -46,6 +47,17 @@ public class MyTileServiceFrontCamera extends TileService { Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setAction(TILE_ID); - startActivityAndCollapse(intent); // use this instead of startActivity() so that the notification panel doesn't remain pulled down + // use startActivityAndCollapse() instead of startActivity() so that the notification panel doesn't remain pulled down + if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE ) { + // startActivityAndCollapse(Intent) throws UnsupportedOperationException on Android 14+ + // FLAG_IMMUTABLE needed for PendingIntents on Android 12+ + PendingIntent pending_intent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); + startActivityAndCollapse(pending_intent); + } + else { + // still get warning for startActivityAndCollapse being deprecated, but startActivityAndCollapse(PendingIntent) requires Android 14+ + // and only seems possible to disable the warning for the function, not this statement + startActivityAndCollapse(intent); + } } } diff --git a/app/src/main/java/net/sourceforge/opencamera/MyTileServiceVideo.java b/app/src/main/java/net/sourceforge/opencamera/MyTileServiceVideo.java index f00a55a1..c3da602a 100644 --- a/app/src/main/java/net/sourceforge/opencamera/MyTileServiceVideo.java +++ b/app/src/main/java/net/sourceforge/opencamera/MyTileServiceVideo.java @@ -1,5 +1,6 @@ package net.sourceforge.opencamera; +import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.service.quicksettings.TileService; @@ -46,6 +47,17 @@ public class MyTileServiceVideo extends TileService { Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setAction(TILE_ID); - startActivityAndCollapse(intent); // use this instead of startActivity() so that the notification panel doesn't remain pulled down + // use startActivityAndCollapse() instead of startActivity() so that the notification panel doesn't remain pulled down + if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE ) { + // startActivityAndCollapse(Intent) throws UnsupportedOperationException on Android 14+ + // FLAG_IMMUTABLE needed for PendingIntents on Android 12+ + PendingIntent pending_intent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); + startActivityAndCollapse(pending_intent); + } + else { + // still get warning for startActivityAndCollapse being deprecated, but startActivityAndCollapse(PendingIntent) requires Android 14+ + // and only seems possible to disable the warning for the function, not this statement + startActivityAndCollapse(intent); + } } }