Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge #4329
Browse files Browse the repository at this point in the history
4329:  For #4113. Add extension method for creating service `PendingIntent`.  r=rocketsroger a=dector



Co-authored-by: Denys M <dector9@gmail.com>
  • Loading branch information
MozLando and dector committed Sep 9, 2019
2 parents bd35658 + b363aad commit 3c03b07
Showing 4 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions components/lib/crash/build.gradle
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.asForegroundServicePendingIntent

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)
.asForegroundServicePendingIntent(context)

val channel = ensureChannelExists(context)

Original file line number Diff line number Diff line change
@@ -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("createForegroundServicePendingIntent")
fun Intent.asForegroundServicePendingIntent(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)
}
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -72,6 +72,9 @@ permalink: /changelog/
* **feature-pwa**
* Adds the ability to create a basic shortcut with a custom label

* **support-utils**
* `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

* [Commits](https://github.com/mozilla-mobile/android-components/compare/v10.0.0...v11.0.0)

0 comments on commit 3c03b07

Please sign in to comment.