Skip to content

Commit

Permalink
Merge pull request #304 from kyrie25/exp-rpc-update-on-event
Browse files Browse the repository at this point in the history
perf: keep socket healthy & add button to restart service
  • Loading branch information
dead8309 authored Oct 15, 2024
2 parents 2c27a63 + 785d1d1 commit b40d81e
Show file tree
Hide file tree
Showing 28 changed files with 317 additions and 67 deletions.
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-fil/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-hr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>


</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-kr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-mm/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>


</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,7 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>

</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s прошло</string>
<string name="time_left">%1$s осталось</string>
<string name="config_name_empty">Название конфигурации не может быть пустым</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-th/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s đã trôi qua</string>
<string name="time_left">%1$s còn lại</string>
<string name="config_name_empty">Tên cấu hình không được để trống</string>
<string name="restart">Khởi chạy lại</string>
<string name="custom_rpc_running">RPC tuỳ chỉnh đang chạy</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@
<string name="time_elapsed">%1$s elapsed</string>
<string name="time_left">%1$s left</string>
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.util.TreeMap
import javax.inject.Inject

class GetCurrentlyRunningApp @Inject constructor(
@ApplicationContext private val context: Context
@ApplicationContext private val context: Context,
) {
operator fun invoke(beginTime: Long = System.currentTimeMillis() - 10000): CommonRpc {
val usageStatsManager =
Expand All @@ -37,8 +37,6 @@ class GetCurrentlyRunningApp @Inject constructor(
val queryUsageStats = usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_DAILY, beginTime, currentTimeMillis
)
val apps = Prefs[Prefs.ENABLED_APPS, "[]"]
val enabledPackages: ArrayList<String> = Json.decodeFromString(apps)
if (queryUsageStats != null && queryUsageStats.size > 1) {
val treeMap: SortedMap<Long, UsageStats> = TreeMap()
for (usageStats in queryUsageStats) {
Expand All @@ -47,13 +45,14 @@ class GetCurrentlyRunningApp @Inject constructor(
if (!(treeMap.isEmpty() || treeMap[treeMap.lastKey()]?.packageName == "com.my.kizzy" || treeMap[treeMap.lastKey()]?.packageName == "com.discord")) {
val packageName = treeMap[treeMap.lastKey()]!!.packageName
Objects.requireNonNull(packageName)
if (enabledPackages.contains(packageName)) {
return CommonRpc(
name = AppUtils.getAppName(packageName),
largeImage = RpcImage.ApplicationIcon(packageName, context),
packageName = packageName
)
}
return CommonRpc(
name = AppUtils.getAppName(packageName),
details = null,
state = null,
largeImage = RpcImage.ApplicationIcon(packageName, context),
packageName = packageName
)

}
}
return CommonRpc()
Expand Down
4 changes: 2 additions & 2 deletions data/src/main/java/com/my/kizzy/data/rpc/CommonRpc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ data class CommonRpc(
val partyMaxSize: Int? = null,
val largeImage: RpcImage? = null,
val smallImage: RpcImage? = null,
var largeText: String? = "",
var smallText: String? = "",
var largeText: String? = null,
var smallText: String? = null,
val time: Timestamps? = null,
val packageName: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ internal object Constants {
const val CHANNEL_DESCRIPTION = "Background Service notification which runs rpc"
const val NOTIFICATION_ID = 2022_03_04
const val ACTION_STOP_SERVICE = "Stop Service"
const val ACTION_RESTART_SERVICE = "Restart Service"
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ class AppDetectionService : Service() {

private lateinit var pendingIntent: PendingIntent

private var runningPackage = ""
private lateinit var restartPendingIntent: PendingIntent

private var runningPackage = ""
override fun onBind(intent: Intent): IBinder? = null

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.action == Constants.ACTION_STOP_SERVICE) {
stopSelf()
} else if (intent?.action == Constants.ACTION_RESTART_SERVICE) {
stopSelf()
startService(Intent(this, AppDetectionService::class.java))
} else {
handleAppDetection()
}
Expand All @@ -83,12 +86,20 @@ class AppDetectionService : Service() {

val stopIntent = createStopIntent()
pendingIntent = createPendingIntent(stopIntent)

val restartIntent = createRestartIntent()
restartPendingIntent = PendingIntent.getService(
this,
0, restartIntent, PendingIntent.FLAG_IMMUTABLE
)
// Adding action to notification builder here to avoid having multiple Exit buttons
// https://github.com/dead8309/Kizzy/issues/197
notificationBuilder
.setSmallIcon(R.drawable.ic_apps)
.addAction(R.drawable.ic_apps, getString(R.string.restart), restartPendingIntent)
.addAction(R.drawable.ic_apps, getString(R.string.exit), pendingIntent)


startForeground(Constants.NOTIFICATION_ID, createDefaultNotification())

val rpcButtons = getRpcButtons()
Expand Down Expand Up @@ -139,7 +150,7 @@ class AppDetectionService : Service() {
private suspend fun handleValidPackage(
packageName: String,
enabledPackages: List<String>,
rpcButtons: RpcButtons
rpcButtons: RpcButtons,
) {
if (packageName in enabledPackages && packageName != runningPackage) {
handleEnabledPackage(packageName, rpcButtons)
Expand All @@ -155,7 +166,7 @@ class AppDetectionService : Service() {
kizzyRPC.apply {
setName(AppUtils.getAppName(packageName))
setStartTimestamps(System.currentTimeMillis())
setStatus(Prefs[Prefs.CUSTOM_ACTIVITY_STATUS,"dnd"])
setStatus(Prefs[Prefs.CUSTOM_ACTIVITY_STATUS, "dnd"])
setLargeImage(RpcImage.ApplicationIcon(packageName, this@AppDetectionService))
if (Prefs[Prefs.USE_RPC_BUTTONS, false]) {
with(rpcButtons) {
Expand Down Expand Up @@ -187,10 +198,11 @@ class AppDetectionService : Service() {
}

private fun createDefaultNotification(): Notification {
return Notification.Builder(this,Constants.CHANNEL_ID)
return Notification.Builder(this, Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_apps)
.setContentTitle(getString(R.string.service_enabled))
.addAction(R.drawable.ic_apps, getString(R.string.exit), pendingIntent)
.addAction(R.drawable.ic_apps, getString(R.string.restart), restartPendingIntent)
.build()
}

Expand All @@ -200,6 +212,12 @@ class AppDetectionService : Service() {
return stopIntent
}

private fun createRestartIntent(): Intent {
val restartIntent = Intent(this, AppDetectionService::class.java)
restartIntent.action = Constants.ACTION_RESTART_SERVICE
return restartIntent
}

private fun createPendingIntent(stopIntent: Intent): PendingIntent {
return PendingIntent.getService(this, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CustomRpcService : Service() {

startForeground(
Constants.NOTIFICATION_ID, notificationBuilder
.setContentTitle("Custom RPC is running")
.setContentTitle(getString(R.string.custom_rpc_running))
.setContentText(rpcData?.name ?: "")
.setSmallIcon(R.drawable.ic_rpc_placeholder)
.addAction(R.drawable.ic_rpc_placeholder, getString(R.string.exit), pendingIntent)
Expand Down
Loading

0 comments on commit b40d81e

Please sign in to comment.