Skip to content

Commit

Permalink
Merge pull request #135 from jeancsanchez/issue/134
Browse files Browse the repository at this point in the history
Upgrading sdk version and adding pending intent support for api 31
  • Loading branch information
jeancsanchez authored Aug 30, 2022
2 parents 3b3cbd0 + 25a6a15 commit a0a2cad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ task clean(type: Delete) {

ext {
// sdk and tools
compileSdkVersion = 30
compileSdkVersion = 31
minSdkVersion = 14
targetSdkVersion = 30
targetSdkVersion = 31
buildToolsVersion = '29.0.3'


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import java.lang.ref.WeakReference
* @date 12/07/16.
* Jesus loves you.
*/
class JcNotificationPlayer private constructor(private val context: Context) : JcPlayerManagerListener {
class JcNotificationPlayer private constructor(private val context: Context) :
JcPlayerManagerListener {

private var title: String? = null
private var time = "00:00"
Expand Down Expand Up @@ -71,18 +72,29 @@ class JcNotificationPlayer private constructor(private val context: Context) : J
openUi.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP

notification = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL)
.setSmallIcon(iconResourceResource)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, iconResourceResource))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContent(createNotificationPlayerView())
.setSound(null)
.setContentIntent(PendingIntent.getActivity(context, NOTIFICATION_ID, openUi, PendingIntent.FLAG_CANCEL_CURRENT))
.setAutoCancel(false)
.build()
.setSmallIcon(iconResourceResource)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, iconResourceResource))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContent(createNotificationPlayerView())
.setSound(null)
.setContentIntent(
PendingIntent.getActivity(
context,
NOTIFICATION_ID,
openUi,
buildIntentFlags()
)
)
.setAutoCancel(false)
.build()

@RequiresApi(Build.VERSION_CODES.O)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_CHANNEL, NotificationManager.IMPORTANCE_LOW)
val channel = NotificationChannel(
NOTIFICATION_CHANNEL,
NOTIFICATION_CHANNEL,
NotificationManager.IMPORTANCE_LOW
)
channel.lockscreenVisibility = VISIBILITY_PUBLIC
channel.enableLights(false)
channel.enableVibration(false)
Expand All @@ -104,26 +116,50 @@ class JcNotificationPlayer private constructor(private val context: Context) : J

if (JcPlayerManager.getInstance(context).get()?.isPaused() == true) {
remoteView = RemoteViews(context.packageName, R.layout.layout_paused_notification)
remoteView.setOnClickPendingIntent(R.id.btn_play_notification, buildPendingIntent(PLAY, PLAY_ID))
remoteView.setOnClickPendingIntent(
R.id.btn_play_notification,
buildPendingIntent(PLAY, PLAY_ID)
)
} else {
remoteView = RemoteViews(context.packageName, R.layout.layout_playing_notification)
remoteView.setOnClickPendingIntent(R.id.btn_pause_notification, buildPendingIntent(PAUSE, PAUSE_ID))
remoteView.setOnClickPendingIntent(
R.id.btn_pause_notification,
buildPendingIntent(PAUSE, PAUSE_ID)
)
}

remoteView.setTextViewText(R.id.txt_current_music_notification, title)
remoteView.setTextViewText(R.id.txt_duration_notification, time)
remoteView.setImageViewResource(R.id.icon_player, iconResource)
remoteView.setOnClickPendingIntent(R.id.btn_next_notification, buildPendingIntent(NEXT, NEXT_ID))
remoteView.setOnClickPendingIntent(R.id.btn_prev_notification, buildPendingIntent(PREVIOUS, PREVIOUS_ID))
remoteView.setOnClickPendingIntent(
R.id.btn_next_notification,
buildPendingIntent(NEXT, NEXT_ID)
)
remoteView.setOnClickPendingIntent(
R.id.btn_prev_notification,
buildPendingIntent(PREVIOUS, PREVIOUS_ID)
)

return remoteView
}
private fun buildIntentFlags(): Int =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}

private fun buildPendingIntent(action: String, id: Int): PendingIntent {
val playIntent = Intent(context.applicationContext, JcPlayerNotificationReceiver::class.java)
val playIntent =
Intent(context.applicationContext, JcPlayerNotificationReceiver::class.java)
playIntent.putExtra(ACTION, action)

return PendingIntent.getBroadcast(context.applicationContext, id, playIntent, PendingIntent.FLAG_UPDATE_CURRENT)
return PendingIntent.getBroadcast(
context.applicationContext,
id,
playIntent,
buildIntentFlags()
)
}

override fun onPreparedAudio(status: JcStatus) {
Expand Down
4 changes: 3 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.jean.jcplayersample.MainActivity">
<activity
android:name="com.example.jean.jcplayersample.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down

0 comments on commit a0a2cad

Please sign in to comment.