Skip to content

Commit

Permalink
fix: receiver refresh
Browse files Browse the repository at this point in the history
[release: 1.2.2]
  • Loading branch information
Zxilly committed Oct 14, 2022
1 parent c34bfe6 commit 183547e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 1 addition & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down Expand Up @@ -67,7 +66,6 @@
android:name=".service.ReceiverService"
android:enabled="true"
android:exported="true"
android:process=":push"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ object WebSocket : Channel {
get() = "Websocket"

override fun init(context: Context) {
context.packageManager.setComponentEnabledSetting(
ComponentName(context, ReceiverService::class.java),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
)
context.packageManager.setComponentEnabledSetting(
ComponentName(context, ReceiverService::class.java),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
Expand Down
38 changes: 24 additions & 14 deletions app/src/main/kotlin/top/learningman/push/service/ReceiverService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package top.learningman.push.service

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.SystemClock
import android.service.notification.NotificationListenerService
import android.util.Log
import com.microsoft.appcenter.crashes.Crashes
Expand All @@ -22,7 +19,6 @@ import kotlinx.coroutines.channels.onFailure
import kotlinx.coroutines.channels.onSuccess
import kotlinx.coroutines.sync.Mutex
import kotlinx.serialization.json.Json
import top.learningman.push.BuildConfig
import top.learningman.push.Constant
import top.learningman.push.data.Repo
import top.learningman.push.entity.JSONMessageItem
Expand All @@ -33,12 +29,16 @@ import java.util.concurrent.atomic.AtomicInteger

class ReceiverService : NotificationListenerService() {
init {
Log.d(TAG, "ReceiverService init")
Log.d("ReceiverService", "ReceiverService init")
}

private lateinit var manager: WebsocketSessionManager
private val id = UUID.randomUUID().toString()

@Suppress("PrivatePropertyName")
private val TAG
get() = "Recv-${id.substring(0, 8)}"

override fun onCreate() {
super.onCreate()
manager = WebsocketSessionManager(this)
Expand Down Expand Up @@ -112,6 +112,7 @@ class ReceiverService : NotificationListenerService() {
override fun onListenerConnected() {
super.onListenerConnected()
Log.d(TAG, "onListenerConnected")
manager.tryResume()
}

override fun onDestroy() {
Expand All @@ -135,6 +136,9 @@ class ReceiverService : NotificationListenerService() {
private class WebsocketSessionManager(private val service: ReceiverService) :
CoroutineScope by CoroutineScope(context = newSingleThreadContext("WebsocketSessionManager")) {

private val TAG
get() = "Recv-${service.id.substring(0, 8)}-Mgr"

private var job: Job? = null
private var jobLock = Mutex()
private val repo by lazy { Repo.getInstance(service) }
Expand Down Expand Up @@ -214,7 +218,7 @@ class ReceiverService : NotificationListenerService() {
suspend fun tryCancelJob() {
jobLock.lock()
try {
if(job != null) {
if (job != null) {
job?.cancelAndJoin()
job = null
Log.i(TAG, "job cancelled in ${service.id}")
Expand All @@ -235,14 +239,21 @@ class ReceiverService : NotificationListenerService() {

fun tryResume() {
Log.d(TAG, "tryResume with status ${Status.valueOf(status.get())} at ${Date()}")
if (status.compareAndSet(Status.WAIT_START, Status.CONNECTING)) {
launch { connect() }
Log.d(TAG, "tryResume: start websocket from WAIT_START, initial startup")
} else if (status.compareAndSet(Status.WAIT_RECONNECT, Status.CONNECTING)) {
if (status.compareAndSet(Status.WAIT_START, Status.CONNECTING) || status.compareAndSet(
Status.WAIT_RECONNECT,
Status.CONNECTING
)
) {
launch { connect() }
Log.d(TAG, "tryResume: start websocket from WAIT_RECONNECT")
Log.d(
TAG,
"tryResume: start websocket from ${Status.valueOf(status.get())}, initial startup"
)
} else {
Log.d(TAG, "tryResume: not start websocket from status ${status.get()}")
Log.d(
TAG,
"tryResume: not start websocket from status ${Status.valueOf(status.get())}"
)
}
}

Expand Down Expand Up @@ -385,7 +396,7 @@ class ReceiverService : NotificationListenerService() {
}.fold({
Log.d(TAG, "prepare to send notification")
val notificationMessage = it.toMessage()
notifyMessage(notificationMessage, from = (service as ReceiverService).id)
notifyMessage(notificationMessage, from = service.id)
repo.setLastMessageTime(notificationMessage.createdAt)
}, {
Log.e(TAG, "Error parsing message", it)
Expand All @@ -410,7 +421,6 @@ class ReceiverService : NotificationListenerService() {
}

companion object {
const val TAG = "ReceiverService"

const val INTENT_USERID_KEY = "nextUserID"

Expand Down

0 comments on commit 183547e

Please sign in to comment.