From bc8cea7bd8f177ad17e965d6088d0baf1740c8fa Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Fri, 6 Dec 2019 00:37:28 +0800 Subject: [PATCH] clean up code --- .../github/kr328/clash/ClashStartService.kt | 39 +-------- .../com/github/kr328/clash/MainActivity.kt | 36 +------- .../kr328/clash/SettingApplicationActivity.kt | 87 +++++++++++-------- .../github/kr328/clash/utils/ServiceUtils.kt | 39 +++++++++ core/src/main/golang/server/event.go | 4 +- .../kr328/clash/service/ClashService.kt | 18 ++-- 6 files changed, 105 insertions(+), 118 deletions(-) create mode 100644 app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt diff --git a/app/src/main/java/com/github/kr328/clash/ClashStartService.kt b/app/src/main/java/com/github/kr328/clash/ClashStartService.kt index ad1d6fb..6997842 100644 --- a/app/src/main/java/com/github/kr328/clash/ClashStartService.kt +++ b/app/src/main/java/com/github/kr328/clash/ClashStartService.kt @@ -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() } diff --git a/app/src/main/java/com/github/kr328/clash/MainActivity.kt b/app/src/main/java/com/github/kr328/clash/MainActivity.kt index e7f9a07..0f1a8c3 100644 --- a/app/src/main/java/com/github/kr328/clash/MainActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MainActivity.kt @@ -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) } } } diff --git a/app/src/main/java/com/github/kr328/clash/SettingApplicationActivity.kt b/app/src/main/java/com/github/kr328/clash/SettingApplicationActivity.kt index af5b3b1..51059f3 100644 --- a/app/src/main/java/com/github/kr328/clash/SettingApplicationActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/SettingApplicationActivity.kt @@ -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(KEY_START_ON_BOOT)?.isChecked = - status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED + findPreference(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) } + + } \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt b/app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt new file mode 100644 index 0000000..977b9de --- /dev/null +++ b/app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt @@ -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 + } + } +} \ No newline at end of file diff --git a/core/src/main/golang/server/event.go b/core/src/main/golang/server/event.go index cc15ce2..a9387df 100644 --- a/core/src/main/golang/server/event.go +++ b/core/src/main/golang/server/event.go @@ -98,9 +98,7 @@ func handlePullBandwidthEvent(client *net.UnixConn) { return writeCommandPacket(client, buf.Bytes()) } - if tick() != nil { - return - } + tick() for { select { diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index 615c6df..85c3d4a 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -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)