fix auto restart
This commit is contained in:
24 files changed
+103
-68
No files matched your search
@@ -13,8 +13,8 @@ import com.github.kr328.clash.common.utils.asBytesString
|
||||
import com.github.kr328.clash.core.model.General
|
||||
import com.github.kr328.clash.remote.withClash
|
||||
import com.github.kr328.clash.remote.withProfile
|
||||
import com.github.kr328.clash.utils.startClashService
|
||||
import com.github.kr328.clash.utils.stopClashService
|
||||
import com.github.kr328.clash.service.util.startClashService
|
||||
import com.github.kr328.clash.service.util.stopClashService
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.github.kr328.clash
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.github.kr328.clash.service.Constants
|
||||
import com.github.kr328.clash.utils.startClashService
|
||||
|
||||
class OnBootReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
if (intent?.action != Intent.ACTION_BOOT_COMPLETED || context == null)
|
||||
return
|
||||
|
||||
if (context.cacheDir.resolve(Constants.CLASH_SERVICE_RUNNING_FILE).exists())
|
||||
context.startClashService()
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,8 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
|
||||
commitAsync(id)
|
||||
else
|
||||
ProfileProvider.releaseTemp(self, id)
|
||||
|
||||
Unit
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import android.service.quicksettings.TileService
|
||||
import com.github.kr328.clash.common.Permissions
|
||||
import com.github.kr328.clash.common.ids.Intents
|
||||
import com.github.kr328.clash.remote.RemoteUtils
|
||||
import com.github.kr328.clash.utils.startClashService
|
||||
import com.github.kr328.clash.utils.stopClashService
|
||||
import com.github.kr328.clash.service.util.startClashService
|
||||
import com.github.kr328.clash.service.util.stopClashService
|
||||
|
||||
class TileService : TileService() {
|
||||
private var currentProfile = ""
|
||||
|
||||
@@ -16,7 +16,6 @@ class UiSettings(context: Context) :
|
||||
const val DARK_MODE_DARK = "dark"
|
||||
const val DARK_MODE_LIGHT = "light"
|
||||
|
||||
val ENABLE_VPN = BooleanEntry("enable_vpn", true)
|
||||
val PROXY_GROUP_SORT = StringEntry("proxy_group_sort", PROXY_SORT_DEFAULT)
|
||||
val PROXY_PROXY_SORT = StringEntry("proxy_proxy_sort", PROXY_SORT_DEFAULT)
|
||||
val PROXY_LAST_SELECT_GROUP = StringEntry("proxy_last_select_group", "")
|
||||
|
||||
@@ -2,10 +2,10 @@ package com.github.kr328.clash.settings
|
||||
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import com.github.kr328.clash.OnBootReceiver
|
||||
import com.github.kr328.clash.R
|
||||
import com.github.kr328.clash.common.util.componentName
|
||||
import com.github.kr328.clash.remote.Broadcasts
|
||||
import com.github.kr328.clash.service.RestartReceiver
|
||||
import com.github.kr328.clash.service.settings.ServiceSettings
|
||||
|
||||
class BehaviorFragment : BaseSettingFragment() {
|
||||
@@ -40,7 +40,7 @@ class BehaviorFragment : BaseSettingFragment() {
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED
|
||||
|
||||
requireActivity().packageManager.setComponentEnabledSetting(
|
||||
OnBootReceiver::class.componentName,
|
||||
RestartReceiver::class.componentName,
|
||||
status,
|
||||
PackageManager.DONT_KILL_APP
|
||||
)
|
||||
@@ -48,7 +48,7 @@ class BehaviorFragment : BaseSettingFragment() {
|
||||
|
||||
override fun get(): Any? {
|
||||
val status = requireActivity().packageManager
|
||||
.getComponentEnabledSetting(OnBootReceiver::class.componentName)
|
||||
.getComponentEnabledSetting(RestartReceiver::class.componentName)
|
||||
|
||||
return status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class NetworkFragment : BaseSettingFragment() {
|
||||
|
||||
override fun onCreateDataStore(): SettingsDataStore {
|
||||
return SettingsDataStore().apply {
|
||||
on(KEY_ENABLE_VPN_SERVICE, UiSettings.ENABLE_VPN.asSource(ui))
|
||||
on(KEY_ENABLE_VPN_SERVICE, ServiceSettings.ENABLE_VPN.asSource(service))
|
||||
on(BYPASS_PRIVATE_NETWORK, ServiceSettings.BYPASS_PRIVATE_NETWORK.asSource(service))
|
||||
on(KEY_DNS_HIJACKING, ServiceSettings.DNS_HIJACKING.asSource(service))
|
||||
on(KEY_DNS_OVERRIDE, ServiceSettings.OVERRIDE_DNS.asSource(service))
|
||||
|
||||
@@ -11,22 +11,3 @@ import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
import com.github.kr328.clash.service.util.sendBroadcastSelf
|
||||
|
||||
fun Context.startClashService(): Intent? {
|
||||
val startTun = UiSettings(this).get(UiSettings.ENABLE_VPN)
|
||||
|
||||
if (startTun) {
|
||||
val vpnRequest = VpnService.prepare(this)
|
||||
if (vpnRequest != null)
|
||||
return vpnRequest
|
||||
|
||||
startForegroundServiceCompat(TunService::class.intent)
|
||||
} else {
|
||||
startForegroundServiceCompat(ClashService::class.intent)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun Context.stopClashService() {
|
||||
sendBroadcastSelf(Intent(Intents.INTENT_ACTION_REQUEST_STOP))
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
<string name="application_name">Clash for Android</string>
|
||||
<string name="auto_update">自动更新 (分钟)</string>
|
||||
<string name="behavior">行为</string>
|
||||
<string name="boot">启动</string>
|
||||
<string name="restart">重启</string>
|
||||
<string name="bypass_private_network">绕过私有网络</string>
|
||||
<string name="bypass_private_network_summary">绕过私有网络地址</string>
|
||||
<string name="cancel">取消</string>
|
||||
@@ -94,8 +94,8 @@
|
||||
<string name="show_traffic_summary">在通知中自动刷新流量</string>
|
||||
<string name="sort_group">代理组排序</string>
|
||||
<string name="sort_proxy">代理排序</string>
|
||||
<string name="start_clash_on_system_boot">在系统启动时启动 Clash</string>
|
||||
<string name="start_on_boot">开机时启动</string>
|
||||
<string name="allow_clash_auto_restart">允许 Clash 自动重启</string>
|
||||
<string name="auto_restart">自动重启</string>
|
||||
<string name="start_url_provider_failure">打开 URL 提供程序失败</string>
|
||||
<string name="stopped">已停止</string>
|
||||
<string name="support">支持</string>
|
||||
|
||||
@@ -106,9 +106,9 @@
|
||||
<string name="network">Network</string>
|
||||
<string name="interface_">Interface</string>
|
||||
|
||||
<string name="boot">Boot</string>
|
||||
<string name="start_on_boot">Start on Boot</string>
|
||||
<string name="start_clash_on_system_boot">Start Clash on system boot</string>
|
||||
<string name="restart">Restart</string>
|
||||
<string name="auto_restart">Auto Restart</string>
|
||||
<string name="allow_clash_auto_restart">Allow clash auto restart</string>
|
||||
|
||||
<string name="notification">Notification</string>
|
||||
<string name="show_traffic">Show Traffic</string>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--suppress DeprecatedClassUsageInspection -->
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<PreferenceCategory android:title="@string/boot">
|
||||
<PreferenceCategory android:title="@string/restart">
|
||||
<SwitchPreference
|
||||
android:key="start_on_boot"
|
||||
android:title="@string/start_on_boot"
|
||||
android:summary="@string/start_clash_on_system_boot" />
|
||||
android:title="@string/auto_restart"
|
||||
android:summary="@string/allow_clash_auto_restart" />
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory android:title="@string/notification">
|
||||
<SwitchPreference
|
||||
|
||||
@@ -67,7 +67,11 @@
|
||||
android:name=".ProfileReceiver"
|
||||
android:exported="false"
|
||||
android:process=":background"
|
||||
android:permission="${applicationId}.permission.RECEIVE_BROADCASTS">
|
||||
android:permission="${applicationId}.permission.RECEIVE_BROADCASTS" />
|
||||
<receiver
|
||||
android:enabled="false"
|
||||
android:name=".RestartReceiver"
|
||||
android:process=":background">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.github.kr328.clash.service
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import com.github.kr328.clash.common.util.createLanguageConfigurationContext
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.service.settings.ServiceSettings
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.MainScope
|
||||
|
||||
@@ -8,6 +8,4 @@ object Constants {
|
||||
const val PROFILE_PROVIDER_SUFFIX = ".profiles"
|
||||
const val STATUS_PROVIDER_SUFFIX = ".status"
|
||||
const val SETTING_PROVIDER_SUFFIX = ".settings"
|
||||
|
||||
const val CLASH_SERVICE_RUNNING_FILE = "clash_running"
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.IBinder
|
||||
import android.os.RemoteException
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.service.data.ProfileDao
|
||||
import com.github.kr328.clash.service.ipc.IStreamCallback
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.github.kr328.clash.service
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.github.kr328.clash.service.util.startClashService
|
||||
|
||||
class RestartReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
if (context == null)
|
||||
return
|
||||
|
||||
when (intent?.action) {
|
||||
Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_MY_PACKAGE_REPLACED -> {
|
||||
}
|
||||
else -> return
|
||||
}
|
||||
|
||||
ProfileReceiver.tryInitialize(context)
|
||||
|
||||
if (ServiceStatusProvider.shouldStartClashOnBoot)
|
||||
context.startClashService()
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,17 @@ import com.github.kr328.clash.common.Global
|
||||
class ServiceStatusProvider : ContentProvider() {
|
||||
companion object {
|
||||
const val METHOD_PING_CLASH_SERVICE = "pingClashService"
|
||||
private const val CLASH_SERVICE_RUNNING_FILE = "service_running"
|
||||
|
||||
var serviceRunning: Boolean = false
|
||||
set(value) {
|
||||
field = value
|
||||
|
||||
Global.application.cacheDir.resolve(Constants.CLASH_SERVICE_RUNNING_FILE).apply {
|
||||
shouldStartClashOnBoot = value
|
||||
}
|
||||
var shouldStartClashOnBoot: Boolean
|
||||
get() = Global.application.cacheDir.resolve(CLASH_SERVICE_RUNNING_FILE).exists()
|
||||
set(value) {
|
||||
Global.application.cacheDir.resolve(CLASH_SERVICE_RUNNING_FILE).apply {
|
||||
if (value)
|
||||
createNewFile()
|
||||
else
|
||||
|
||||
@@ -189,6 +189,15 @@ object DatabaseMigrations {
|
||||
}
|
||||
|
||||
database.execSQL("DROP TABLE _profiles")
|
||||
|
||||
val uiSp = Global.application
|
||||
.getSharedPreferences("ui", Context.MODE_PRIVATE)
|
||||
val srvSp = Global.application
|
||||
.getSharedPreferences("service", Context.MODE_PRIVATE)
|
||||
|
||||
srvSp.edit {
|
||||
putBoolean("enable_vpn", uiSp.getBoolean("enable_vpn", true))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("Migration failure", e)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.github.kr328.clash.service.files
|
||||
import android.content.Context
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.provider.DocumentsContract
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.service.R
|
||||
import com.github.kr328.clash.service.data.ProfileDao
|
||||
import com.github.kr328.clash.service.util.resolveProfileFile
|
||||
|
||||
@@ -19,7 +19,7 @@ fun ProfileEntity.toProfileMetadata(context: Context): ProfileMetadata {
|
||||
name = name,
|
||||
type = type,
|
||||
uri = Uri.parse(uri),
|
||||
source = source?.let { Uri.parse(it)},
|
||||
source = source?.let { Uri.parse(it) },
|
||||
active = active,
|
||||
interval = interval,
|
||||
lastModified = lastModified
|
||||
|
||||
@@ -19,7 +19,7 @@ data class ProfileMetadata(
|
||||
val active: Boolean,
|
||||
val interval: Long,
|
||||
val lastModified: Long
|
||||
): Parcelable {
|
||||
) : Parcelable {
|
||||
enum class Type {
|
||||
FILE, URL, EXTERNAL, UNKNOWN
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ class ServiceSettings(preference: SharedPreferences) :
|
||||
const val ACCESS_CONTROL_MODE_BLACKLIST = "access_control_mode_blacklist"
|
||||
const val ACCESS_CONTROL_MODE_WHITELIST = "access_control_mode_whitelist"
|
||||
|
||||
val ENABLE_VPN =
|
||||
BooleanEntry("enable_vpn", true)
|
||||
val LANGUAGE =
|
||||
StringEntry("language", "")
|
||||
val BYPASS_PRIVATE_NETWORK =
|
||||
|
||||
@@ -4,12 +4,12 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.github.kr328.clash.common.Permissions
|
||||
import com.github.kr328.clash.common.ids.Intents
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import java.lang.Exception
|
||||
|
||||
fun Context.sendBroadcastSelf(intent: Intent) {
|
||||
this.sendBroadcast(intent.setPackage(this.packageName),
|
||||
Permissions.PERMISSION_RECEIVE_BROADCASTS)
|
||||
this.sendBroadcast(
|
||||
intent.setPackage(this.packageName),
|
||||
Permissions.PERMISSION_RECEIVE_BROADCASTS
|
||||
)
|
||||
}
|
||||
|
||||
fun Context.broadcastProfileChanged() {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.github.kr328.clash.service.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import com.github.kr328.clash.common.ids.Intents
|
||||
import com.github.kr328.clash.common.util.intent
|
||||
import com.github.kr328.clash.common.util.startForegroundServiceCompat
|
||||
import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
import com.github.kr328.clash.service.settings.ServiceSettings
|
||||
|
||||
fun Context.startClashService(): Intent? {
|
||||
val startTun = ServiceSettings(this).get(ServiceSettings.ENABLE_VPN)
|
||||
|
||||
if (startTun) {
|
||||
val vpnRequest = VpnService.prepare(this)
|
||||
if (vpnRequest != null)
|
||||
return vpnRequest
|
||||
|
||||
startForegroundServiceCompat(TunService::class.intent)
|
||||
} else {
|
||||
startForegroundServiceCompat(ClashService::class.intent)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun Context.stopClashService() {
|
||||
sendBroadcastSelf(Intent(Intents.INTENT_ACTION_REQUEST_STOP))
|
||||
}
|
||||
Reference in new issue
Block a user