Skip to content

Commit

Permalink
backport: 20230402
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 17, 2023
1 parent 9c8b285 commit 61c17e1
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 31 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />

<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/nekohasekai/sagernet/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,6 @@ object Action {
const val SERVICE = "io.nekohasekai.sagernet.SERVICE"
const val CLOSE = "io.nekohasekai.sagernet.CLOSE"
const val RELOAD = "io.nekohasekai.sagernet.RELOAD"
const val SWITCH_WAKE_LOCK = "io.nekohasekai.sagernet.SWITCH_WAKELOCK"
// const val SWITCH_WAKE_LOCK = "io.nekohasekai.sagernet.SWITCH_WAKELOCK"
const val RESET_UPSTREAM_CONNECTIONS = "moe.nb4a.RESET_UPSTREAM_CONNECTIONS"
}
25 changes: 19 additions & 6 deletions app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import libcore.AppStats
import libcore.Libcore
import libcore.TrafficListener
import moe.matsuri.nya.Protocols
import moe.matsuri.nya.utils.Util
import java.net.UnknownHostException
import com.github.shadowsocks.plugin.PluginManager as ShadowsocksPluginPluginManager
import io.nekohasekai.sagernet.aidl.AppStats as AidlAppStats
Expand Down Expand Up @@ -78,11 +79,20 @@ class BaseService {
var proxy: ProxyInstance? = null
var notification: ServiceNotification? = null

val receiver = broadcastReceiver { _, intent ->
val receiver = broadcastReceiver { ctx, intent ->
when (intent.action) {
Intent.ACTION_SHUTDOWN -> service.persistStats()
Action.RELOAD -> service.forceLoad()
Action.SWITCH_WAKE_LOCK -> service.switchWakeLock()
// Action.SWITCH_WAKE_LOCK -> service.switchWakeLock()
Action.RESET_UPSTREAM_CONNECTIONS -> runOnDefaultDispatcher {
Libcore.resetAllConnections(true)
runOnMainDispatcher {
Util.collapseStatusBar(ctx)
Toast.makeText(ctx, "Reset upstream connections done", Toast.LENGTH_SHORT)
.show()
}
}

else -> service.stopRunner()
}
}
Expand Down Expand Up @@ -113,8 +123,10 @@ class BaseService {
// statsListeners: TODO add links
// bandwidthListeners: only for UI update, don't save data

private val bandwidthListeners = mutableMapOf<IBinder, Long>() // the binder is the real identifier
private val statsListeners = mutableMapOf<IBinder, Long>() // the binder is the real identifier
private val bandwidthListeners =
mutableMapOf<IBinder, Long>() // the binder is the real identifier
private val statsListeners =
mutableMapOf<IBinder, Long>() // the binder is the real identifier
override val coroutineContext = Dispatchers.Main.immediate + Job()
private var looper: Job? = null
private var statsLooper: Job? = null
Expand Down Expand Up @@ -196,7 +208,8 @@ class BaseService {
val delayMs = statsListeners.values.minOrNull()
if (delayMs == 0L) return
val queryTime = System.currentTimeMillis()
val sinceLastQueryInSeconds = ((queryTime - lastQueryTime).toDouble() / 1000).toLong()
val sinceLastQueryInSeconds =
((queryTime - lastQueryTime).toDouble() / 1000).toLong()
lastQueryTime = queryTime

appStats.clear()
Expand Down Expand Up @@ -495,7 +508,7 @@ class BaseService {
addAction(Action.RELOAD)
addAction(Intent.ACTION_SHUTDOWN)
addAction(Action.CLOSE)
addAction(Action.SWITCH_WAKE_LOCK)
addAction(Action.RESET_UPSTREAM_CONNECTIONS)
}, "$packageName.SERVICE", null)
data.closeReceiverRegistered = true
}
Expand Down
48 changes: 25 additions & 23 deletions app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import io.nekohasekai.sagernet.utils.Theme
* See also: https://github.com/aosp-mirror/platform_frameworks_base/commit/070d142993403cc2c42eca808ff3fafcee220ac4
*/
class ServiceNotification(
private val service: BaseService.Interface, profileName: String,
private val service: BaseService.Interface, title: String,
channel: String, visible: Boolean = false,
) : BroadcastReceiver() {
companion object {
Expand Down Expand Up @@ -127,7 +127,7 @@ class ServiceNotification(
}

override fun updateWakeLockStatus(acquired: Boolean) {
updateActions(acquired)
updateActions()
builder.priority =
if (acquired) NotificationCompat.PRIORITY_HIGH else NotificationCompat.PRIORITY_LOW
update()
Expand All @@ -139,7 +139,7 @@ class ServiceNotification(
private val builder = NotificationCompat.Builder(service as Context, channel)
.setWhen(0)
.setTicker(service.getString(R.string.forward_success))
.setContentTitle(profileName)
.setContentTitle(title)
.setOnlyAlertOnce(true)
.setContentIntent(SagerNet.configureIntent(service))
.setSmallIcon(R.drawable.ic_service_active)
Expand All @@ -148,7 +148,7 @@ class ServiceNotification(

init {
service as Context
updateActions(false)
updateActions()

Theme.apply(app)
Theme.apply(service)
Expand All @@ -162,41 +162,43 @@ class ServiceNotification(
show()
}

fun updateActions(wakeLockAcquired: Boolean) {
private fun updateActions() {
service as Context

builder.clearActions()

val closeAction = NotificationCompat.Action.Builder(
0, service.getText(R.string.stop), PendingIntent.getBroadcast(
service, 0, Intent(Action.CLOSE).setPackage(service.packageName), flags
)
).apply {
setShowsUserInterface(false)
}.build()
).setShowsUserInterface(false).build()
builder.addAction(closeAction)

val switchAction = NotificationCompat.Action.Builder(
0, service.getString(R.string.action_switch), PendingIntent.getActivity(
service, 0, Intent(service, SwitchActivity::class.java), flags
)
).apply {
setShowsUserInterface(false)
}.build()
).setShowsUserInterface(false).build()
builder.addAction(switchAction)

val wakeLockAction = NotificationCompat.Action.Builder(
0,
service.getText(if (!wakeLockAcquired) R.string.acquire_wake_lock else R.string.release_wake_lock),
val resetUpstreamAction = NotificationCompat.Action.Builder(
0, service.getString(R.string.reset_connections),
PendingIntent.getBroadcast(
service,
0,
Intent(Action.SWITCH_WAKE_LOCK).setPackage(service.packageName),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
service, 0, Intent(Action.RESET_UPSTREAM_CONNECTIONS), flags
)
).apply {
setShowsUserInterface(false)
}.build()
builder.addAction(wakeLockAction)
).setShowsUserInterface(false).build()
builder.addAction(resetUpstreamAction)

// val wakeLockAction = NotificationCompat.Action.Builder(
// 0,
// service.getText(if (!wakeLockAcquired) R.string.acquire_wake_lock else R.string.release_wake_lock),
// PendingIntent.getBroadcast(
// service,
// 0,
// Intent(Action.SWITCH_WAKE_LOCK).setPackage(service.packageName),
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
// )
// ).setShowsUserInterface(false).build()
// builder.addAction(wakeLockAction)
}

override fun onReceive(context: Context, intent: Intent) {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/moe/matsuri/nya/TempDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ abstract class TempDatabase : RoomDatabase() {
private val instance by lazy {
Room.inMemoryDatabaseBuilder(SagerNet.application, TempDatabase::class.java)
.allowMainThreadQueries()
.enableMultiInstanceInvalidation()
.fallbackToDestructiveMigration()
.setQueryExecutor { GlobalScope.launch { it.run() } }
.build()
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/moe/matsuri/nya/utils/Util.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package moe.matsuri.nya.utils

import android.annotation.SuppressLint
import android.content.Context
import android.util.Base64
import java.io.ByteArrayOutputStream
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -121,4 +122,14 @@ object Util {
return sdf1.format(Date(t))
}

@SuppressLint("WrongConstant")
fun collapseStatusBar(context: Context) {
try {
val statusBarManager = context.getSystemService("statusbar")
val collapse = statusBarManager.javaClass.getMethod("collapsePanels")
collapse.invoke(statusBarManager)
} catch (_: Exception) {
}
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,5 @@
<string name="ads">推广</string>
<string name="share_subscription">分享订阅</string>
<string name="show_group_in_notification">在通知中显示组名</string>
<string name="reset_connections">重置连接</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,6 @@ Anyone can write advanced plugins, which can control Matsuri. please download an
<string name="ads">Ads</string>
<string name="share_subscription">Share Subscription</string>
<string name="show_group_in_notification">Show group name in in notification</string>
<string name="reset_connections">Reset Connections</string>

</resources>

0 comments on commit 61c17e1

Please sign in to comment.