Commit 1c9f1f8f authored by Mygod's avatar Mygod

Do not restart service if it was stopped before

parent bd9f6140
...@@ -43,6 +43,10 @@ class BootReceiver : BroadcastReceiver() { ...@@ -43,6 +43,10 @@ class BootReceiver : BroadcastReceiver() {
} }
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
if (!DataStore.persistAcrossReboot) { // sanity check
enabled = false
return
}
val doStart = when (intent.action) { val doStart = when (intent.action) {
Intent.ACTION_BOOT_COMPLETED -> !DataStore.directBootAware Intent.ACTION_BOOT_COMPLETED -> !DataStore.directBootAware
Intent.ACTION_LOCKED_BOOT_COMPLETED -> DataStore.directBootAware Intent.ACTION_LOCKED_BOOT_COMPLETED -> DataStore.directBootAware
......
...@@ -29,6 +29,7 @@ import android.util.Log ...@@ -29,6 +29,7 @@ import android.util.Log
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import com.crashlytics.android.Crashlytics import com.crashlytics.android.Crashlytics
import com.github.shadowsocks.BootReceiver
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
import com.github.shadowsocks.Core.app import com.github.shadowsocks.Core.app
import com.github.shadowsocks.aidl.IShadowsocksService import com.github.shadowsocks.aidl.IShadowsocksService
...@@ -36,6 +37,7 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback ...@@ -36,6 +37,7 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.aidl.TrafficStats import com.github.shadowsocks.aidl.TrafficStats
import com.github.shadowsocks.core.R import com.github.shadowsocks.core.R
import com.github.shadowsocks.plugin.PluginManager import com.github.shadowsocks.plugin.PluginManager
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Action import com.github.shadowsocks.utils.Action
import com.github.shadowsocks.utils.broadcastReceiver import com.github.shadowsocks.utils.broadcastReceiver
import com.github.shadowsocks.utils.printLog import com.github.shadowsocks.utils.printLog
...@@ -285,7 +287,10 @@ object BaseService { ...@@ -285,7 +287,10 @@ object BaseService {
data.changeState(State.Stopped, msg) data.changeState(State.Stopped, msg)
// stop the service if nothing has bound to it // stop the service if nothing has bound to it
if (restart) startRunner() else stopSelf() if (restart) startRunner() else {
BootReceiver.enabled = false
stopSelf()
}
} }
} }
...@@ -313,6 +318,7 @@ object BaseService { ...@@ -313,6 +318,7 @@ object BaseService {
data.proxy = proxy data.proxy = proxy
data.udpFallback = if (fallback == null) null else ProxyInstance(fallback, profile.route) data.udpFallback = if (fallback == null) null else ProxyInstance(fallback, profile.route)
BootReceiver.enabled = DataStore.persistAcrossReboot
if (!data.closeReceiverRegistered) { if (!data.closeReceiverRegistered) {
registerReceiver(data.closeReceiver, IntentFilter().apply { registerReceiver(data.closeReceiver, IntentFilter().apply {
addAction(Action.RELOAD) addAction(Action.RELOAD)
......
...@@ -29,7 +29,6 @@ import com.github.shadowsocks.preference.DataStore ...@@ -29,7 +29,6 @@ import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot import com.github.shadowsocks.utils.DirectBoot
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.lang.IllegalStateException
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.ByteOrder import java.nio.ByteOrder
......
...@@ -22,6 +22,7 @@ package com.github.shadowsocks.preference ...@@ -22,6 +22,7 @@ package com.github.shadowsocks.preference
import android.os.Binder import android.os.Binder
import androidx.preference.PreferenceDataStore import androidx.preference.PreferenceDataStore
import com.github.shadowsocks.BootReceiver
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
import com.github.shadowsocks.database.PrivateDatabase import com.github.shadowsocks.database.PrivateDatabase
import com.github.shadowsocks.database.PublicDatabase import com.github.shadowsocks.database.PublicDatabase
...@@ -61,6 +62,8 @@ object DataStore : OnPreferenceDataStoreChangeListener { ...@@ -61,6 +62,8 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var profileId: Long var profileId: Long
get() = publicStore.getLong(Key.id) ?: 0 get() = publicStore.getLong(Key.id) ?: 0
set(value) = publicStore.putLong(Key.id, value) set(value) = publicStore.putLong(Key.id, value)
val persistAcrossReboot get() = publicStore.getBoolean(Key.persistAcrossReboot)
?: BootReceiver.enabled.also { publicStore.putBoolean(Key.persistAcrossReboot, it) }
val canToggleLocked: Boolean get() = publicStore.getBoolean(Key.directBootAware) == true val canToggleLocked: Boolean get() = publicStore.getBoolean(Key.directBootAware) == true
val directBootAware: Boolean get() = Core.directBootSupported && canToggleLocked val directBootAware: Boolean get() = Core.directBootSupported && canToggleLocked
val tcpFastOpen: Boolean get() = TcpFastOpen.sendEnabled && DataStore.publicStore.getBoolean(Key.tfo, true) val tcpFastOpen: Boolean get() = TcpFastOpen.sendEnabled && DataStore.publicStore.getBoolean(Key.tfo, true)
...@@ -101,6 +104,7 @@ object DataStore : OnPreferenceDataStoreChangeListener { ...@@ -101,6 +104,7 @@ object DataStore : OnPreferenceDataStoreChangeListener {
* Initialize settings that have complicated default values. * Initialize settings that have complicated default values.
*/ */
fun initGlobal() { fun initGlobal() {
persistAcrossReboot
if (publicStore.getBoolean(Key.tfo) == null) publicStore.putBoolean(Key.tfo, tcpFastOpen) if (publicStore.getBoolean(Key.tfo) == null) publicStore.putBoolean(Key.tfo, tcpFastOpen)
if (publicStore.getString(Key.portProxy) == null) portProxy = portProxy if (publicStore.getString(Key.portProxy) == null) portProxy = portProxy
if (publicStore.getString(Key.portLocalDns) == null) portLocalDns = portLocalDns if (publicStore.getString(Key.portLocalDns) == null) portLocalDns = portLocalDns
......
...@@ -43,7 +43,7 @@ object Key { ...@@ -43,7 +43,7 @@ object Key {
const val route = "route" const val route = "route"
const val isAutoConnect = "isAutoConnect" const val persistAcrossReboot = "isAutoConnect"
const val directBootAware = "directBootAware" const val directBootAware = "directBootAware"
const val proxyApps = "isProxyApps" const val proxyApps = "isProxyApps"
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
<string name="bypass_apps">Bypass</string> <string name="bypass_apps">Bypass</string>
<string name="bypass_apps_summary">Enable this option to bypass selected apps</string> <string name="bypass_apps_summary">Enable this option to bypass selected apps</string>
<string name="auto_connect">Auto Connect</string> <string name="auto_connect">Auto Connect</string>
<string name="auto_connect_summary">Enable Shadowsocks on startup</string> <string name="auto_connect_summary">Enable Shadowsocks on startup/app update if it was running before</string>
<string name="direct_boot_aware">Allow Toggling in Lock Screen</string> <string name="direct_boot_aware">Allow Toggling in Lock Screen</string>
<string name="direct_boot_aware_summary">Your selected profile information will be less protected</string> <string name="direct_boot_aware_summary">Your selected profile information will be less protected</string>
<string name="tcp_fastopen_summary">Toggling might require ROOT permission</string> <string name="tcp_fastopen_summary">Toggling might require ROOT permission</string>
......
...@@ -37,7 +37,7 @@ class GlobalSettingsPreferenceFragment : PreferenceFragmentCompat() { ...@@ -37,7 +37,7 @@ class GlobalSettingsPreferenceFragment : PreferenceFragmentCompat() {
preferenceManager.preferenceDataStore = DataStore.publicStore preferenceManager.preferenceDataStore = DataStore.publicStore
DataStore.initGlobal() DataStore.initGlobal()
addPreferencesFromResource(R.xml.pref_global) addPreferencesFromResource(R.xml.pref_global)
val boot = findPreference(Key.isAutoConnect) as SwitchPreference val boot = findPreference(Key.persistAcrossReboot) as SwitchPreference
boot.setOnPreferenceChangeListener { _, value -> boot.setOnPreferenceChangeListener { _, value ->
BootReceiver.enabled = value as Boolean BootReceiver.enabled = value as Boolean
true true
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
android:key="settings_screen" android:key="settings_screen"
app:initialExpandedChildrenCount="3"> app:initialExpandedChildrenCount="3">
<SwitchPreference android:key="isAutoConnect" <SwitchPreference android:key="isAutoConnect"
android:persistent="false"
android:summary="@string/auto_connect_summary" android:summary="@string/auto_connect_summary"
android:title="@string/auto_connect"/> android:title="@string/auto_connect"/>
<SwitchPreference <SwitchPreference
......
...@@ -161,7 +161,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo ...@@ -161,7 +161,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo
stats = findPreference(Key.controlStats) stats = findPreference(Key.controlStats)
controlImport = findPreference(Key.controlImport) controlImport = findPreference(Key.controlImport)
(findPreference(Key.isAutoConnect) as SwitchPreference).apply { (findPreference(Key.persistAcrossReboot) as SwitchPreference).apply {
setOnPreferenceChangeListener { _, value -> setOnPreferenceChangeListener { _, value ->
BootReceiver.enabled = value as Boolean BootReceiver.enabled = value as Boolean
true true
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
app:initialExpandedChildrenCount="2"> app:initialExpandedChildrenCount="2">
<SwitchPreference <SwitchPreference
android:key="isAutoConnect" android:key="isAutoConnect"
android:persistent="false"
android:summary="@string/auto_connect_summary" android:summary="@string/auto_connect_summary"
android:title="@string/auto_connect"/> android:title="@string/auto_connect"/>
<SwitchPreference <SwitchPreference
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment