From d7747352c3e92a630e06a39d8e0e9d05de0e0c66 Mon Sep 17 00:00:00 2001 From: Denys M Date: Thu, 5 Sep 2019 23:04:14 +0400 Subject: [PATCH 1/2] For #4113. Add extension method for creating service `PendingIntent`. --- components/lib/crash/build.gradle | 1 + .../crash/notification/CrashNotification.kt | 13 +++------- .../components/support/utils/intents.kt | 25 +++++++++++++++++++ docs/changelog.md | 5 ++++ 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt diff --git a/components/lib/crash/build.gradle b/components/lib/crash/build.gradle index bf12e9ec364..53fd044c0bc 100644 --- a/components/lib/crash/build.gradle +++ b/components/lib/crash/build.gradle @@ -37,6 +37,7 @@ dependencies { implementation project(':support-base') implementation project(':support-ktx') + implementation project(':support-utils') // We only compile against Sentry, GeckoView, and Glean. It's up to the app to add those dependencies if it wants to // send crash reports to Socorro (GV) or Sentry. diff --git a/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt b/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt index 06e86b1fe66..b139f7c9495 100644 --- a/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt +++ b/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt @@ -17,6 +17,7 @@ import mozilla.components.lib.crash.R import mozilla.components.lib.crash.prompt.CrashPrompt import mozilla.components.lib.crash.service.SendCrashReportService import mozilla.components.support.base.ids.notify +import mozilla.components.support.utils.asPendingIntentForLaunchService private const val NOTIFICATION_SDK_LEVEL = 29 // On Android Q+ we show a notification instead of a prompt @@ -33,15 +34,9 @@ internal class CrashNotification( context, 0, CrashPrompt.createIntent(context, crash), 0 ) - val reportPendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - PendingIntent.getForegroundService( - context, 0, SendCrashReportService.createReportIntent(context, crash), 0 - ) - } else { - PendingIntent.getService( - context, 0, SendCrashReportService.createReportIntent(context, crash), 0 - ) - } + val reportPendingIntent = SendCrashReportService + .createReportIntent(context, crash) + .asPendingIntentForLaunchService(context) val channel = ensureChannelExists(context) diff --git a/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt b/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt new file mode 100644 index 00000000000..ac9c480ff1f --- /dev/null +++ b/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt @@ -0,0 +1,25 @@ +@file:JvmName("IntentUtils") + +package mozilla.components.support.utils + +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.os.Build + +/** + * Create a [PendingIntent] instance to run a certain service described with the [Intent]. + * + * This method will allow you to launch a service that will be able to overpass + * [background service limitations](https://developer.android.com/about/versions/oreo/background#services) + * introduced in Android Oreo. + * + * @param context an [Intent] to start a service. + */ +@JvmName("createPendingIntentForLaunchService") +fun Intent.asPendingIntentForLaunchService(context: Context): PendingIntent = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + PendingIntent.getForegroundService(context, 0, this, 0) + } else { + PendingIntent.getService(context, 0, this, 0) + } diff --git a/docs/changelog.md b/docs/changelog.md index 34970383671..91c5fa49a33 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -55,6 +55,11 @@ permalink: /changelog/ * `GlobalSyncableStoreProvider.configureStore` now takes a pair of `Pair`, instead of allowing arbitrary string names for engines. * `GlobalSyncableStoreProvider.getStore` is no longer part of the public API. +* **support-utils** + * `Intent.asPendingIntentForLaunchService(Context)` extension method + to create pending intent for service that will play nicely with + background execution limitations introduced in Android O. + # 11.0.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v10.0.0...v11.0.0) From b363aadc1b56282010840569baa7d3c89d9512ff Mon Sep 17 00:00:00 2001 From: Denys M Date: Tue, 10 Sep 2019 00:20:59 +0400 Subject: [PATCH 2/2] For #4113. Add extension method for foreground service `PendingIntent`. --- .../components/lib/crash/notification/CrashNotification.kt | 4 ++-- .../src/main/java/mozilla/components/support/utils/intents.kt | 4 ++-- docs/changelog.md | 4 +--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt b/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt index b139f7c9495..fa2f59485f0 100644 --- a/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt +++ b/components/lib/crash/src/main/java/mozilla/components/lib/crash/notification/CrashNotification.kt @@ -17,7 +17,7 @@ import mozilla.components.lib.crash.R import mozilla.components.lib.crash.prompt.CrashPrompt import mozilla.components.lib.crash.service.SendCrashReportService import mozilla.components.support.base.ids.notify -import mozilla.components.support.utils.asPendingIntentForLaunchService +import mozilla.components.support.utils.asForegroundServicePendingIntent private const val NOTIFICATION_SDK_LEVEL = 29 // On Android Q+ we show a notification instead of a prompt @@ -36,7 +36,7 @@ internal class CrashNotification( val reportPendingIntent = SendCrashReportService .createReportIntent(context, crash) - .asPendingIntentForLaunchService(context) + .asForegroundServicePendingIntent(context) val channel = ensureChannelExists(context) diff --git a/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt b/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt index ac9c480ff1f..bd0f15cf8cc 100644 --- a/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt +++ b/components/support/utils/src/main/java/mozilla/components/support/utils/intents.kt @@ -16,8 +16,8 @@ import android.os.Build * * @param context an [Intent] to start a service. */ -@JvmName("createPendingIntentForLaunchService") -fun Intent.asPendingIntentForLaunchService(context: Context): PendingIntent = +@JvmName("createForegroundServicePendingIntent") +fun Intent.asForegroundServicePendingIntent(context: Context): PendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { PendingIntent.getForegroundService(context, 0, this, 0) } else { diff --git a/docs/changelog.md b/docs/changelog.md index 91c5fa49a33..dc1ebac465a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -56,9 +56,7 @@ permalink: /changelog/ * `GlobalSyncableStoreProvider.getStore` is no longer part of the public API. * **support-utils** - * `Intent.asPendingIntentForLaunchService(Context)` extension method - to create pending intent for service that will play nicely with - background execution limitations introduced in Android O. + * `Intent.asForegroundServicePendingIntent(Context)` extension method to create pending intent for the service that will play nicely with background execution limitations introduced in Android O (e.g. foreground service). # 11.0.0