pull/32/head
parent
22a8c6a980
commit
f5c2cf40a5
@ -1,72 +1,64 @@ |
|||||||
package io.legado.app.service.notification |
package io.legado.app.service.notification |
||||||
|
|
||||||
import android.app.Service |
|
||||||
import android.graphics.BitmapFactory |
import android.graphics.BitmapFactory |
||||||
import android.support.v4.media.session.MediaSessionCompat |
|
||||||
import androidx.core.app.NotificationCompat |
import androidx.core.app.NotificationCompat |
||||||
import io.legado.app.R |
import io.legado.app.R |
||||||
import io.legado.app.constant.AppConst |
import io.legado.app.constant.AppConst |
||||||
import io.legado.app.help.PendingIntentHelp |
import io.legado.app.help.PendingIntentHelp |
||||||
|
import io.legado.app.service.ReadAloudService |
||||||
|
|
||||||
object ReadAloudNotification { |
object ReadAloudNotification { |
||||||
|
|
||||||
/** |
/** |
||||||
* 更新通知 |
* 更新通知 |
||||||
*/ |
*/ |
||||||
fun upNotification( |
fun upNotification(service: ReadAloudService) { |
||||||
context: Service, |
|
||||||
mediaSessionCompat: MediaSessionCompat?, |
|
||||||
pause: Boolean, |
|
||||||
title: String, |
|
||||||
subtitle: String, |
|
||||||
timeMinute: Int = 0 |
|
||||||
) { |
|
||||||
var nTitle: String = when { |
var nTitle: String = when { |
||||||
pause -> context.getString(R.string.read_aloud_pause) |
service.pause -> service.getString(R.string.read_aloud_pause) |
||||||
timeMinute in 1..60 -> context.getString(R.string.read_aloud_timer, timeMinute) |
service.timeMinute in 1..60 -> service.getString(R.string.read_aloud_timer, service.timeMinute) |
||||||
else -> context.getString(R.string.read_aloud_t) |
else -> service.getString(R.string.read_aloud_t) |
||||||
} |
} |
||||||
nTitle += ": $title" |
nTitle += ": ${service.title}" |
||||||
var nSubtitle = subtitle |
var nSubtitle = service.subtitle |
||||||
if (subtitle.isEmpty()) |
if (service.subtitle.isEmpty()) |
||||||
nSubtitle = context.getString(R.string.read_aloud_s) |
nSubtitle = service.getString(R.string.read_aloud_s) |
||||||
val builder = NotificationCompat.Builder(context, AppConst.channelIdReadAloud) |
val builder = NotificationCompat.Builder(service, AppConst.channelIdReadAloud) |
||||||
.setSmallIcon(R.drawable.ic_volume_up) |
.setSmallIcon(R.drawable.ic_volume_up) |
||||||
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.icon_read_book)) |
.setLargeIcon(BitmapFactory.decodeResource(service.resources, R.drawable.icon_read_book)) |
||||||
.setOngoing(true) |
.setOngoing(true) |
||||||
.setContentTitle(nTitle) |
.setContentTitle(nTitle) |
||||||
.setContentText(nSubtitle) |
.setContentText(nSubtitle) |
||||||
.setContentIntent(PendingIntentHelp.readBookActivityPendingIntent(context)) |
.setContentIntent(PendingIntentHelp.readBookActivityPendingIntent(service)) |
||||||
if (pause) { |
if (service.pause) { |
||||||
builder.addAction( |
builder.addAction( |
||||||
R.drawable.ic_play_24dp, |
R.drawable.ic_play_24dp, |
||||||
context.getString(R.string.resume), |
service.getString(R.string.resume), |
||||||
PendingIntentHelp.aloudServicePendingIntent(context, "resume") |
PendingIntentHelp.aloudServicePendingIntent(service, "resume") |
||||||
) |
) |
||||||
} else { |
} else { |
||||||
builder.addAction( |
builder.addAction( |
||||||
R.drawable.ic_pause_24dp, |
R.drawable.ic_pause_24dp, |
||||||
context.getString(R.string.pause), |
service.getString(R.string.pause), |
||||||
PendingIntentHelp.aloudServicePendingIntent(context, "pause") |
PendingIntentHelp.aloudServicePendingIntent(service, "pause") |
||||||
) |
) |
||||||
} |
} |
||||||
builder.addAction( |
builder.addAction( |
||||||
R.drawable.ic_stop_black_24dp, |
R.drawable.ic_stop_black_24dp, |
||||||
context.getString(R.string.stop), |
service.getString(R.string.stop), |
||||||
PendingIntentHelp.aloudServicePendingIntent(context, "stop") |
PendingIntentHelp.aloudServicePendingIntent(service, "stop") |
||||||
) |
) |
||||||
builder.addAction( |
builder.addAction( |
||||||
R.drawable.ic_time_add_24dp, |
R.drawable.ic_time_add_24dp, |
||||||
context.getString(R.string.set_timer), |
service.getString(R.string.set_timer), |
||||||
PendingIntentHelp.aloudServicePendingIntent(context, "setTimer") |
PendingIntentHelp.aloudServicePendingIntent(service, "setTimer") |
||||||
) |
) |
||||||
builder.setStyle( |
builder.setStyle( |
||||||
androidx.media.app.NotificationCompat.MediaStyle() |
androidx.media.app.NotificationCompat.MediaStyle() |
||||||
.setMediaSession(mediaSessionCompat?.sessionToken) |
.setMediaSession(service.mediaSessionCompat?.sessionToken) |
||||||
.setShowActionsInCompactView(0, 1, 2) |
.setShowActionsInCompactView(0, 1, 2) |
||||||
) |
) |
||||||
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
||||||
val notification = builder.build() |
val notification = builder.build() |
||||||
context.startForeground(112201, notification) |
service.startForeground(112201, notification) |
||||||
} |
} |
||||||
} |
} |
Loading…
Reference in new issue