@ -1,5 +1,6 @@
package sushi.hardcore.aira.background_service
import android.annotation.SuppressLint
import android.app.*
import android.content.Context
import android.content.Intent
@ -22,6 +23,7 @@ import java.io.IOException
import java.net.*
import java.nio.channels.*
@SuppressLint ( " UnspecifiedImmutableFlag " )
class AIRAService : Service ( ) {
private external fun releaseIdentity ( )
@ -36,6 +38,7 @@ class AIRAService : Service() {
const val MESSAGE _SEND _NAME = 3
const val MESSAGE _SEND _AVATAR = 4
const val MESSAGE _CANCEL _FILE _TRANSFER = 5
const val FLAG _PENDING _INTENT = PendingIntent . FLAG _UPDATE _CURRENT
var isServiceRunning = false
}
@ -360,7 +363,7 @@ class AIRAService : Service() {
}
private fun sendNotification ( sessionId : Int , msgContent : ByteArray ) {
val notification Builder = NotificationCompat . Builder ( this , MESSAGES _NOTIFICATION _CHANNEL _ID )
val notification = NotificationCompat . Builder ( this , MESSAGES _NOTIFICATION _CHANNEL _ID )
. setCategory ( NotificationCompat . CATEGORY _MESSAGE )
. setSmallIcon ( R . drawable . ic _launcher )
. setContentTitle ( getNameOf ( sessionId ) )
@ -374,49 +377,35 @@ class AIRAService : Service() {
. setContentIntent (
PendingIntent . getActivity ( this , 0 , Intent ( this , ChatActivity :: class . java ) . apply {
putExtra ( " sessionId " , sessionId )
} , 0 )
} , FLAG _PENDING _INTENT )
)
. setAutoCancel ( true )
. setDefaults ( Notification . DEFAULT _ALL )
. apply {
priority = NotificationCompat . PRIORITY _HIGH
}
if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . KITKAT _WATCH ) {
val markReadIntent = PendingIntent . getBroadcast ( this , 0 ,
Intent ( this , NotificationBroadcastReceiver :: class . java ) . apply {
val bundle = Bundle ( )
bundle . putBinder ( " binder " , AIRABinder ( ) )
bundle . putInt ( " sessionId " , sessionId )
putExtra ( " bundle " , bundle )
action = NotificationBroadcastReceiver . ACTION _MARK _READ
} , PendingIntent . FLAG _UPDATE _CURRENT )
notificationBuilder . addAction (
NotificationCompat . Action (
R . drawable . ic _launcher ,
getString ( R . string . mark _read ) ,
markReadIntent
. addAction ( NotificationCompat . Action (
R . drawable . ic _launcher ,
getString ( R . string . mark _read ) ,
newActionPendingIntent {
it . putBinder ( sessionId )
it . action = NotificationBroadcastReceiver . ACTION _MARK _READ
}
) )
. addAction (
NotificationCompat . Action . Builder ( R . drawable . ic _launcher , getString ( R . string . reply ) , newActionPendingIntent {
it . putBinder ( sessionId )
it . action = NotificationBroadcastReceiver . ACTION _REPLY
} )
. addRemoteInput (
RemoteInput . Builder ( NotificationBroadcastReceiver . KEY _TEXT _REPLY )
. setLabel ( getString ( R . string . reply ) )
. build ( )
)
)
val replyPendingIntent : PendingIntent =
PendingIntent . getBroadcast ( this , 0 ,
Intent ( this , NotificationBroadcastReceiver :: class . java ) . apply {
val bundle = Bundle ( )
bundle . putBinder ( " binder " , AIRABinder ( ) )
bundle . putInt ( " sessionId " , sessionId )
putExtra ( " bundle " , bundle )
action = NotificationBroadcastReceiver . ACTION _REPLY
} , PendingIntent . FLAG _UPDATE _CURRENT )
notificationBuilder . addAction (
NotificationCompat . Action . Builder ( R . drawable . ic _launcher , getString ( R . string . reply ) , replyPendingIntent )
. addRemoteInput (
RemoteInput . Builder ( NotificationBroadcastReceiver . KEY _TEXT _REPLY )
. setLabel ( getString ( R . string . reply ) )
. build ( )
)
. build ( )
)
}
notificationManager . notify ( notificationIdManager . getMessageNotificationId ( sessionId ) , notificationBuilder . build ( ) )
. build ( )
)
. build ( )
notificationManager . notify ( notificationIdManager . getMessageNotificationId ( sessionId ) , notification )
}
private fun initFileTransferNotification ( sessionId : Int , fileTransferNotification : FileTransferNotification , file : PendingFile ) {
@ -425,15 +414,19 @@ class AIRAService : Service() {
file . fileName ,
file . fileSize . toInt ( ) ,
Intent ( this , NotificationBroadcastReceiver :: class . java ) . apply {
val bundle = Bundle ( )
bundle . putBinder ( " binder " , AIRABinder ( ) )
bundle . putInt ( " sessionId " , sessionId )
putExtra ( " bundle " , bundle )
putBinder ( sessionId )
action = NotificationBroadcastReceiver . ACTION _CANCEL _FILE _TRANSFER
}
)
}
private fun Intent . putBinder ( sessionId : Int ) {
val bundle = Bundle ( )
bundle . putBinder ( " binder " , AIRABinder ( ) )
bundle . putInt ( " sessionId " , sessionId )
putExtra ( " bundle " , bundle )
}
private fun saveMsg ( sessionId : Int , timestamp : Long , msg : ByteArray ) {
var msgSaved = false
contacts [ sessionId ] ?. uuid ?. let { uuid ->
@ -768,7 +761,7 @@ class AIRAService : Service() {
. setContentIntent (
PendingIntent . getActivity ( this , 0 , Intent ( this , ChatActivity :: class . java ) . apply {
putExtra ( " sessionId " , sessionId )
} , 0 )
} , FLAG _PENDING _INTENT )
)
. setDefaults ( Notification . DEFAULT _ALL )
. apply {
@ -875,6 +868,7 @@ class AIRAService : Service() {
}
}
}
keys . clear ( )
}
for ( session in sessions . values ) {
session . close ( )
@ -928,21 +922,37 @@ class AIRAService : Service() {
)
}
private fun newActionPendingIntent ( intentBuilder : ( Intent ) -> Unit ) : PendingIntent {
return PendingIntent . getBroadcast ( this , 0 ,
Intent ( this , NotificationBroadcastReceiver :: class . java ) . apply {
intentBuilder ( this )
} , FLAG _PENDING _INTENT
)
}
override fun onStartCommand ( intent : Intent ? , flags : Int , startId : Int ) : Int {
notificationManager = NotificationManagerCompat . from ( this )
val notificationBuilder = if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . O ) {
if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . O ) {
createNotificationChannels ( )
Notification . Builder ( this , SERVICE _NOTIFICATION _CHANNEL _ID )
} else {
@Suppress ( " Deprecation " )
Notification . Builder ( this )
}
val notificationBuilder = NotificationCompat . Builder ( this , SERVICE _NOTIFICATION _CHANNEL _ID )
val notification : Notification = notificationBuilder
. setContentTitle ( getString ( R . string . background _service ) )
. setSmallIcon ( R . drawable . ic _launcher )
. setContentIntent (
PendingIntent . getActivity ( this , 0 , Intent ( this , MainActivity :: class . java ) , 0 )
PendingIntent . getActivity ( this , 0 ,
Intent ( this , MainActivity :: class . java ) . apply {
setFlags ( Intent . FLAG _ACTIVITY _NEW _TASK or Intent . FLAG _ACTIVITY _CLEAR _TASK or Intent . FLAG _ACTIVITY _CLEAR _TOP )
} ,
FLAG _PENDING _INTENT )
)
. addAction ( NotificationCompat . Action (
R . drawable . ic _launcher ,
getString ( R . string . stop ) ,
newActionPendingIntent {
it . action = NotificationBroadcastReceiver . ACTION _LOGOUT
}
) )
. build ( )
startForeground ( 1 , notification )