clean up code
This commit is contained in:
6 files changed
+105
-118
No files matched your search
@@ -3,16 +3,13 @@ package com.github.kr328.clash
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import android.os.Binder
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
import com.github.kr328.clash.utils.ServiceUtils
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class ClashStartService : Service() {
|
||||
@@ -43,39 +40,7 @@ class ClashStartService : Service() {
|
||||
startForeground(NOTIFICATION_ID, notification)
|
||||
|
||||
thread {
|
||||
getSharedPreferences("application", Context.MODE_PRIVATE).apply {
|
||||
when (getString(
|
||||
MainApplication.KEY_PROXY_MODE,
|
||||
MainApplication.PROXY_MODE_VPN
|
||||
)) {
|
||||
MainApplication.PROXY_MODE_VPN -> {
|
||||
if (VpnService.prepare(this@ClashStartService) == null)
|
||||
startService(
|
||||
Intent(
|
||||
this@ClashStartService,
|
||||
TunService::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
MainApplication.PROXY_MODE_PROXY_ONLY -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(
|
||||
Intent(
|
||||
this@ClashStartService,
|
||||
ClashService::class.java
|
||||
)
|
||||
)
|
||||
} else {
|
||||
startService(
|
||||
Intent(
|
||||
this@ClashStartService,
|
||||
ClashService::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ServiceUtils.startProxyService(this)
|
||||
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.github.kr328.clash.core.model.GeneralPacket
|
||||
import com.github.kr328.clash.core.utils.ByteFormatter
|
||||
import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
import com.github.kr328.clash.utils.ServiceUtils
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.activity_main_clash_status.*
|
||||
import kotlinx.android.synthetic.main.activity_main_profiles.*
|
||||
@@ -57,39 +58,8 @@ class MainActivity : BaseActivity() {
|
||||
it.stop()
|
||||
}
|
||||
else -> runOnUiThread {
|
||||
getSharedPreferences("application", Context.MODE_PRIVATE).apply {
|
||||
when (getString(
|
||||
MainApplication.KEY_PROXY_MODE,
|
||||
MainApplication.PROXY_MODE_VPN
|
||||
)) {
|
||||
MainApplication.PROXY_MODE_VPN -> {
|
||||
VpnService.prepare(this@MainActivity)?.apply {
|
||||
startActivityForResult(this, VPN_REQUEST_CODE)
|
||||
} ?: startService(
|
||||
Intent(
|
||||
this@MainActivity,
|
||||
TunService::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
MainApplication.PROXY_MODE_PROXY_ONLY -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(
|
||||
Intent(
|
||||
this@MainActivity,
|
||||
ClashService::class.java
|
||||
)
|
||||
)
|
||||
} else {
|
||||
startService(
|
||||
Intent(
|
||||
this@MainActivity,
|
||||
ClashService::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
ServiceUtils.startProxyService(this)?.also {
|
||||
startActivityForResult(it, VPN_REQUEST_CODE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package com.github.kr328.clash
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import androidx.preference.CheckBoxPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import com.github.kr328.clash.core.utils.Log
|
||||
import kotlinx.android.synthetic.main.activity_setting_application.*
|
||||
import java.lang.Exception
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class SettingApplicationActivity : BaseActivity() {
|
||||
@@ -14,18 +18,15 @@ class SettingApplicationActivity : BaseActivity() {
|
||||
const val KEY_START_ON_BOOT = "key_application_start_on_boot"
|
||||
}
|
||||
|
||||
class Fragment : PreferenceFragmentCompat(),
|
||||
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
class Fragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.setting_application, rootKey)
|
||||
|
||||
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
|
||||
val status = requireContext().packageManager
|
||||
val status = context.packageManager
|
||||
.getComponentEnabledSetting(
|
||||
ComponentName.createRelative(
|
||||
requireContext(),
|
||||
@@ -33,43 +34,57 @@ class SettingApplicationActivity : BaseActivity() {
|
||||
)
|
||||
)
|
||||
|
||||
findPreference<CheckBoxPreference>(KEY_START_ON_BOOT)?.isChecked =
|
||||
status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
findPreference<CheckBoxPreference>(KEY_START_ON_BOOT)?.also {
|
||||
it.isChecked = status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
it.onPreferenceChangeListener = this
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(
|
||||
sharedPreferences: SharedPreferences?,
|
||||
key: String?
|
||||
) {
|
||||
when (key) {
|
||||
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
|
||||
if ( !isAdded )
|
||||
return false
|
||||
|
||||
when (preference?.key) {
|
||||
KEY_START_ON_BOOT -> {
|
||||
val enabled = sharedPreferences!!.getBoolean(key, false)
|
||||
val enabled = newValue as Boolean? ?: false
|
||||
|
||||
thread {
|
||||
if (enabled) {
|
||||
requireContext().packageManager
|
||||
.setComponentEnabledSetting(
|
||||
ComponentName.createRelative(
|
||||
requireContext(),
|
||||
BootCompleteReceiver::class.java.name
|
||||
),
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||
PackageManager.DONT_KILL_APP
|
||||
)
|
||||
} else {
|
||||
requireContext().packageManager
|
||||
.setComponentEnabledSetting(
|
||||
ComponentName.createRelative(
|
||||
requireContext(),
|
||||
BootCompleteReceiver::class.java.name
|
||||
),
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP
|
||||
)
|
||||
try {
|
||||
setBootCompleteReceiverEnabled(enabled)
|
||||
}
|
||||
catch (e: Exception) {
|
||||
Log.w("Set boot complete failure", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private fun setBootCompleteReceiverEnabled(enabled: Boolean) {
|
||||
if (enabled) {
|
||||
requireActivity().packageManager
|
||||
.setComponentEnabledSetting(
|
||||
ComponentName.createRelative(
|
||||
requireActivity(),
|
||||
BootCompleteReceiver::class.java.name
|
||||
),
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||
PackageManager.DONT_KILL_APP
|
||||
)
|
||||
} else {
|
||||
requireActivity().packageManager
|
||||
.setComponentEnabledSetting(
|
||||
ComponentName.createRelative(
|
||||
requireActivity(),
|
||||
BootCompleteReceiver::class.java.name
|
||||
),
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,4 +94,6 @@ class SettingApplicationActivity : BaseActivity() {
|
||||
|
||||
setSupportActionBar(activity_setting_application_toolbar)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.github.kr328.clash.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import android.os.Build
|
||||
import com.github.kr328.clash.MainActivity
|
||||
import com.github.kr328.clash.MainApplication
|
||||
import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
|
||||
object ServiceUtils {
|
||||
fun startProxyService(context: Context): Intent? {
|
||||
context.getSharedPreferences("application", Context.MODE_PRIVATE).apply {
|
||||
when (getString(
|
||||
MainApplication.KEY_PROXY_MODE,
|
||||
MainApplication.PROXY_MODE_VPN
|
||||
)) {
|
||||
MainApplication.PROXY_MODE_VPN -> {
|
||||
val prepare = VpnService.prepare(context)
|
||||
|
||||
if ( prepare != null )
|
||||
return prepare
|
||||
|
||||
context.startService(Intent(context, TunService::class.java))
|
||||
}
|
||||
MainApplication.PROXY_MODE_PROXY_ONLY -> {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(Intent(context, ClashService::class.java))
|
||||
} else {
|
||||
context.startService(Intent(context, ClashService::class.java))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,9 +98,7 @@ func handlePullBandwidthEvent(client *net.UnixConn) {
|
||||
return writeCommandPacket(client, buf.Bytes())
|
||||
}
|
||||
|
||||
if tick() != nil {
|
||||
return
|
||||
}
|
||||
tick()
|
||||
|
||||
for {
|
||||
select {
|
||||
|
||||
@@ -5,10 +5,7 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Binder
|
||||
import android.os.IBinder
|
||||
import android.os.IInterface
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.os.*
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.core.event.*
|
||||
import com.github.kr328.clash.core.model.GeneralPacket
|
||||
@@ -44,8 +41,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
clash.setSelectProxy(proxy, selected)
|
||||
|
||||
this@ClashService.profileService.setCurrentProfileProxy(proxy, selected)
|
||||
}
|
||||
catch (e: IOException) {
|
||||
} catch (e: IOException) {
|
||||
Log.w("Set proxy failure", e)
|
||||
|
||||
this@ClashService.eventService.performErrorEvent(
|
||||
@@ -96,8 +92,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
this@ClashService.eventService.performErrorEvent(
|
||||
ErrorEvent(ErrorEvent.Type.START_FAILURE, e.toString())
|
||||
)
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
fd.close()
|
||||
}
|
||||
}
|
||||
@@ -150,6 +145,7 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
Event.EVENT_BANDWIDTH ->
|
||||
puller.startBandwidthPull()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun releaseEvent(event: Int) {
|
||||
@@ -251,8 +247,10 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master,
|
||||
Log.i("Loading profile ${active.cache}")
|
||||
|
||||
try {
|
||||
val remove = clash.loadProfile(File(active.cache),
|
||||
profileService.queryProfileSelected(active.id))
|
||||
val remove = clash.loadProfile(
|
||||
File(active.cache),
|
||||
profileService.queryProfileSelected(active.id)
|
||||
)
|
||||
|
||||
profileService.removeCurrentProfileProxy(remove)
|
||||
|
||||
|
||||
Reference in new issue
Block a user