Skip to content

Commit

Permalink
log
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jan 15, 2024
1 parent 8d2b157 commit 8870acf
Show file tree
Hide file tree
Showing 21 changed files with 714 additions and 94 deletions.
13 changes: 13 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id "kotlin-android"
id "kotlin-parcelize"
id "kotlinx-serialization"
id "com.google.devtools.ksp"
id "dev.flutter.flutter-gradle-plugin"
}

Expand Down Expand Up @@ -47,6 +48,12 @@ android {
versionCode gitCommits
versionName version

ksp {
arg("room.schemaLocation", "$projectDir/schemas".toString())
arg("room.incremental", "true")
arg("room.expandProjection", "true")
}

buildConfigField("String", "ALIST_VERSION", "\"${alistVersion}\"")
}

Expand Down Expand Up @@ -135,5 +142,11 @@ dependencies {

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")

// Room
// implementation("androidx.room:room-runtime:$room_version")
// implementation("androidx.room:room-ktx:$room_version")
// ksp("androidx.room:room-compiler:$room_version")
// androidTestImplementation("androidx.room:room-testing:$room_version")


}
34 changes: 33 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_INTERNAL_STORAGE"
tools:ignore="SystemPermissionTypo" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

<application
android:name="com.github.jing332.alistflutter.App"
android:icon="@mipmap/ic_launcher"
android:label="AList">

android:label="AList"
android:networkSecurityConfig="@xml/network_security_config"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"


android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute">
<activity
android:name=".SwitchServerActivity"
android:exported="true" />
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,12 @@ static void setUp(@NonNull BinaryMessenger binaryMessenger, @Nullable AppConfig
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
public interface Android {

void addShortcut();

void startService();

void setAdminPwd(@NonNull String pwd);

@NonNull
Boolean isRunning();

Expand All @@ -277,6 +281,28 @@ public interface Android {
}
/**Sets up an instance of `Android` to handle messages through the `binaryMessenger`. */
static void setUp(@NonNull BinaryMessenger binaryMessenger, @Nullable Android api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.alist_flutter.Android.addShortcut", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
try {
api.addShortcut();
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
Expand All @@ -289,6 +315,30 @@ static void setUp(@NonNull BinaryMessenger binaryMessenger, @Nullable Android ap
api.startService();
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.alist_flutter.Android.setAdminPwd", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String pwdArg = (String) args.get(0);
try {
api.setAdminPwd(pwdArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
Expand Down Expand Up @@ -470,5 +520,25 @@ public void onServiceStatusChanged(@NonNull Boolean isRunningArg, @NonNull VoidR
}
});
}
public void onServerLog(@NonNull Long levelArg, @NonNull String timeArg, @NonNull String logArg, @NonNull VoidResult result) {
final String channelName = "dev.flutter.pigeon.alist_flutter.Event.onServerLog";
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, channelName, getCodec());
channel.send(
new ArrayList<Object>(Arrays.asList(levelArg, timeArg, logArg)),
channelReply -> {
if (channelReply instanceof List) {
List<Object> listReply = (List<Object>) channelReply;
if (listReply.size() > 1) {
result.error(new FlutterError((String) listReply.get(0), (String) listReply.get(1), (String) listReply.get(2)));
} else {
result.success();
}
} else {
result.error(createConnectionError(channelName));
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.github.jing332.alistflutter.bridge.AndroidBridge
import com.github.jing332.alistflutter.bridge.AppConfigBridge
import com.github.jing332.alistflutter.model.ShortCuts
import com.github.jing332.alistflutter.model.alist.Logger
import com.github.jing332.pigeon.GeneratedApi
import com.github.jing332.pigeon.GeneratedApi.VoidResult
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MainActivity : FlutterActivity() {
companion object {
private val TAG = "MainActivity"
const val BRIDGE_CHANNEL = "alistflutter/bridge"
const val CONFIG_CHANNEL = "alistflutter/config"

const val EVENT_CHANNEL = "alistflutter/event"
private const val TAG = "MainActivity"
}

private val receiver by lazy { MyReceiver() }
private var mEvent: GeneratedApi.Event? = null

@OptIn(DelicateCoroutinesApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -43,6 +43,22 @@ class MainActivity : FlutterActivity() {
GeneratedApi.Android.setUp(binaryMessage, AndroidBridge(this))
mEvent = GeneratedApi.Event(binaryMessage)

Logger.addListener(object : Logger.Listener {
override fun onLog(level: Int, time: String, msg: String) {
GlobalScope.launch(Dispatchers.Main) {
mEvent?.onServerLog(level.toLong(), time, msg, object : VoidResult {
override fun success() {

}

override fun error(error: Throwable) {
}

})
}
}

})
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ class SwitchServerActivity : Activity() {
super.onCreate(savedInstanceState)

if (AListService.isRunning) {
toast(R.string.alist_shut_downing)
startService(Intent(this, AListService::class.java).apply {
action = AListService.ACTION_SHUTDOWN
})
} else {
toast(R.string.alist_starting)
startService(Intent(this, AListService::class.java))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ import android.content.Context
import android.content.Intent
import com.github.jing332.alistflutter.AListService
import com.github.jing332.alistflutter.BuildConfig
import com.github.jing332.alistflutter.R
import com.github.jing332.alistflutter.SwitchServerActivity
import com.github.jing332.alistflutter.model.alist.AList
import com.github.jing332.alistflutter.utils.MyTools
import com.github.jing332.alistflutter.utils.ToastUtils.longToast
import com.github.jing332.alistflutter.utils.ToastUtils.toast
import com.github.jing332.pigeon.GeneratedApi

class AndroidBridge(private val context: Context) : GeneratedApi.Android {
override fun addShortcut() {
MyTools.addShortcut(
context,
context.getString(R.string.app_switch),
"alist_flutter_switch",
R.drawable.ic_launcher_foreground,
Intent(context, SwitchServerActivity::class.java)
)
}

override fun startService() {
context.startService(Intent(context, AListService::class.java))
}

override fun setAdminPwd(pwd: String) {
AList.setAdminPassword(pwd)
}

override fun isRunning() = AListService.isRunning
override fun getAListVersion() = BuildConfig.ALIST_VERSION
override fun getVersionName() = BuildConfig.VERSION_NAME
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.jing332.alistflutter.constant

import androidx.annotation.IntDef

@IntDef(
LogLevel.PANIC,
LogLevel.FATAL,
LogLevel.ERROR,
LogLevel.WARN,
LogLevel.INFO,
LogLevel.DEBUG,
LogLevel.TRACE
)
annotation class LogLevel {
companion object {
const val PANIC = 0
const val FATAL = 1
const val ERROR = 2
const val WARN = 3
const val INFO = 4
const val DEBUG = 5
const val TRACE = 6

fun Int.toLevelString(): String {
return when (this) {
PANIC -> "PANIC"
FATAL -> "FATAL"
ERROR -> "ERROR"
WARN -> "WARN"
INFO -> "INFO"
DEBUG -> "DEBUG"
TRACE -> "TRACE"
else -> "UNKNOWN"
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
package com.github.jing332.alistflutter.data
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.github.jing332.alistandroid.data.dao.ServerLogDao
import com.github.jing332.alistflutter.data.entities.ServerLog
import com.github.jing332.alistflutter.App.Companion.app
val appDb by lazy { AppDatabase.create() }
@Database(
version = 2,
entities = [ServerLog::class],
autoMigrations = [
AutoMigration(from = 1, to = 2)
]
)
abstract class AppDatabase : RoomDatabase() {
abstract val serverLogDao: ServerLogDao
companion object {
fun create() = Room.databaseBuilder(
app,
AppDatabase::class.java,
"alistandroid.db"
)
.allowMainThreadQueries()
.build()
}
}*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.jing332.alistflutter.data.entities

import com.github.jing332.alistflutter.constant.LogLevel

data class ServerLog(

@LogLevel val level: Int,
val message: String,
val time: String,
) {
companion object {

@Suppress("RegExpRedundantEscape")
fun String.evalLog(): ServerLog? {
val logPattern = """(\w+)\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] (.+)""".toRegex()
val result = logPattern.find(this)
if (result != null) {
val (level, time, msg) = result.destructured
val l = when (level[0].toString()) {
"D" -> LogLevel.DEBUG
"I" -> LogLevel.INFO
"W" -> LogLevel.WARN
"E" -> LogLevel.ERROR
else -> LogLevel.INFO
}
return ServerLog(level = l, message = msg, time = time)
}
return null
}
}
}
Loading

0 comments on commit 8870acf

Please sign in to comment.