Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@
</intent-filter>
</activity>

<receiver
android:name=".ChangeModeReceiver"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="${applicationId}.action.CHANGE_MODE" />
</intent-filter>
</receiver>

<service
android:name=".services.FlClashTileService"
android:exported="true"
Expand Down
20 changes: 20 additions & 0 deletions android/app/src/main/java/com/follow/clash/ChangeModeReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.follow.clash

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.follow.clash.extensions.wrapAction

class ChangeModeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val appContext = FlClashApplication.getAppContext()
when (intent.action) {
appContext.wrapAction("CHANGE_MODE") -> {
val mode = intent.getStringExtra("mode")
if (mode != null) {
GlobalState.handleChangeMode(mode)
}
}
}
}
}
4 changes: 4 additions & 0 deletions android/app/src/main/kotlin/com/follow/clash/GlobalState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ object GlobalState {
}
}

fun handleChangeMode(modeKey: String) {
getCurrentAppPlugin()?.changeMode(modeKey)
}

fun handleTryDestroy() {
if (flutterEngine == null) {
destroyServiceEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ class AppPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware
return false
}

fun changeMode(modeKey: String) {
channel.invokeMethod("changeMode", modeKey)
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activityRef = WeakReference(binding.activity)
binding.addActivityResultListener(::onActivityResult)
Expand Down
15 changes: 14 additions & 1 deletion lib/enum/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,20 @@ extension UsedProxyExtension on UsedProxy {
String get value => UsedProxyExtension.valueList[index];
}

enum Mode { rule, global, direct }
enum Mode {
rule,
global,
direct;

static Mode fromString(String mode) {
return switch (mode) {
"rule" => Mode.rule,
"global" => Mode.global,
"direct" => Mode.direct,
String() => throw UnimplementedError(),
};
}
}

enum ViewMode { mobile, laptop, desktop }

Expand Down
11 changes: 11 additions & 0 deletions lib/plugins/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import 'dart:io';
import 'dart:isolate';

import 'package:fl_clash/common/app_localizations.dart';
import 'package:fl_clash/common/print.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/models/models.dart';
import 'package:fl_clash/state.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
Expand All @@ -28,6 +31,14 @@ class App {
} catch (_) {
return "";
}
case "changeMode":
if (call.arguments is String) {
commonPrint.log("change mode to ${call.arguments}");
globalState.appController
.changeMode(Mode.fromString(call.arguments as String));
await globalState.appController.savePreferences();
}
break;
default:
throw MissingPluginException();
}
Expand Down