Skip to content

Commit

Permalink
Separate android ui and vpn
Browse files Browse the repository at this point in the history
  • Loading branch information
chen08209 committed Jul 13, 2024
1 parent 53af862 commit c640798
Show file tree
Hide file tree
Showing 33 changed files with 860 additions and 521 deletions.
48 changes: 39 additions & 9 deletions android/app/src/main/kotlin/com/follow/clash/GlobalState.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.follow.clash

import android.app.Activity
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.follow.clash.plugins.AppPlugin
import com.follow.clash.plugins.ProxyPlugin
import com.follow.clash.plugins.TilePlugin
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import java.util.Date

enum class RunState {
Expand All @@ -12,16 +17,41 @@ enum class RunState {
STOP
}

class GlobalState {
companion object {
val runState: MutableLiveData<RunState> = MutableLiveData<RunState>(RunState.STOP)
var runTime: Date? = null
var flutterEngine: FlutterEngine? = null
fun getCurrentTilePlugin(): TilePlugin? =
flutterEngine?.plugins?.get(TilePlugin::class.java) as TilePlugin?
object GlobalState {
val runState: MutableLiveData<RunState> = MutableLiveData<RunState>(RunState.STOP)
var flutterEngine: FlutterEngine? = null
private var serviceEngine: FlutterEngine? = null

fun getCurrentAppPlugin(): AppPlugin? =
flutterEngine?.plugins?.get(AppPlugin::class.java) as AppPlugin?
fun getCurrentAppPlugin(): AppPlugin? {
val currentEngine = if (flutterEngine != null) flutterEngine else serviceEngine
return currentEngine?.plugins?.get(AppPlugin::class.java) as AppPlugin?
}

fun getCurrentTitlePlugin(): TilePlugin? {
val currentEngine = if (flutterEngine != null) flutterEngine else serviceEngine
return currentEngine?.plugins?.get(TilePlugin::class.java) as TilePlugin?
}

fun destroyServiceEngine() {
serviceEngine?.destroy()
serviceEngine = null
}

fun initServiceEngine(context: Context) {
if (serviceEngine != null) return
val serviceEngine = FlutterEngine(context)
serviceEngine.plugins.add(ProxyPlugin())
serviceEngine.plugins.add(AppPlugin())
serviceEngine.plugins.add(TilePlugin())
val vpnService = DartExecutor.DartEntrypoint(
FlutterInjector.instance().flutterLoader().findAppBundlePath(),
"vpnService"
)
serviceEngine.dartExecutor.executeDartEntrypoint(
vpnService,
listOf("${flutterEngine == null}")
)
GlobalState.serviceEngine = serviceEngine
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.flutter.embedding.engine.FlutterEngine
class MainActivity : FlutterActivity() {

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GlobalState.flutterEngine?.destroy()
super.configureFlutterEngine(flutterEngine)
flutterEngine.plugins.add(AppPlugin())
flutterEngine.plugins.add(ProxyPlugin())
Expand Down
28 changes: 18 additions & 10 deletions android/app/src/main/kotlin/com/follow/clash/plugins/AppPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Build
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.content.getSystemService
import com.follow.clash.GlobalState
import com.follow.clash.extensions.getBase64
import com.follow.clash.extensions.getProtocol
import com.follow.clash.models.Package
Expand All @@ -21,6 +22,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.Result
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
Expand Down Expand Up @@ -56,14 +58,16 @@ class AppPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware
}

private fun tip(message: String?) {
if (toast != null) {
toast!!.cancel()
if(GlobalState.flutterEngine == null){
if (toast != null) {
toast!!.cancel()
}
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
toast!!.show()
}
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
toast!!.show()
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"moveTaskToBack" -> {
activity?.moveTaskToBack(true)
Expand Down Expand Up @@ -163,13 +167,17 @@ class AppPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware
}
}


private fun updateExcludeFromRecents(value: Boolean?) {
if (context == null) return
val am = getSystemService(context!!, ActivityManager::class.java)
val task = am?.appTasks?.firstOrNull { task ->
task.taskInfo.baseIntent.component?.packageName == context!!.packageName
val task = am?.appTasks?.firstOrNull {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
it.taskInfo.taskId == activity?.taskId
} else {
it.taskInfo.id == activity?.taskId
}
}

when (value) {
true -> task?.setExcludeFromRecents(value)
false -> task?.setExcludeFromRecents(value)
Expand Down Expand Up @@ -220,7 +228,7 @@ class AppPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware
}

override fun onDetachedFromActivityForConfigChanges() {
activity = null;
activity = null
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
Expand All @@ -230,6 +238,6 @@ class AppPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware
override fun onDetachedFromActivity() {
channel.invokeMethod("exit", null)
scope.cancel()
activity = null;
activity = null
}
}
Loading

0 comments on commit c640798

Please sign in to comment.