Skip to content

Commit

Permalink
admin password
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jan 15, 2024
1 parent 8870acf commit a2a5baa
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 115 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ flutter {
//}
dependencies {
// compileOnly files("$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar")

implementation project(":utils")
//noinspection GradleDependency
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

Expand Down
5 changes: 4 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
tools:ignore="UnusedAttribute">
<activity
android:name=".SwitchServerActivity"
android:exported="true" />
android:exported="true"
android:launchMode="singleTask"
android:taskAffinity="alist.switch"
android:theme="@android:style/Theme.NoDisplay" />
<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 @@ -488,6 +488,44 @@ static void setUp(@NonNull BinaryMessenger binaryMessenger, @Nullable Android ap
}
}
/** Generated class from Pigeon that represents Flutter messages that can be called from Java. */
public static class Flutter {
private final @NonNull BinaryMessenger binaryMessenger;

public Flutter(@NonNull BinaryMessenger argBinaryMessenger) {
this.binaryMessenger = argBinaryMessenger;
}

/** Public interface for sending reply. */
/** The codec used by Flutter. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
public void getLocalIpAddress(@NonNull Result<String> result) {
final String channelName = "dev.flutter.pigeon.alist_flutter.Flutter.getLocalIpAddress";
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, channelName, getCodec());
channel.send(
null,
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 if (listReply.get(0) == null) {
result.error(new FlutterError("null-error", "Flutter api returned null value for non-null return value.", ""));
} else {
@SuppressWarnings("ConstantConditions")
String output = (String) listReply.get(0);
result.success(output);
}
} else {
result.error(createConnectionError(channelName));
}
});
}
}
/** Generated class from Pigeon that represents Flutter messages that can be called from Java. */
public static class Event {
private final @NonNull BinaryMessenger binaryMessenger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.github.jing332.alistflutter.model.alist.AList
import com.github.jing332.alistflutter.BridgeUtils.invokeMethodSync
import com.github.jing332.alistflutter.config.AppConfig
import com.github.jing332.alistflutter.model.alist.AList
import com.github.jing332.alistflutter.utils.AndroidUtils.registerReceiverCompat
import com.github.jing332.alistflutter.utils.ClipboardUtils
import com.github.jing332.alistflutter.utils.ToastUtils.toast
import com.github.jing332.utils.NativeLib
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import splitties.systemservices.powerManager

Expand All @@ -50,6 +49,7 @@ class AListService : Service() {
private val mNotificationReceiver = NotificationActionReceiver()
private val mReceiver = MyReceiver()
private var mWakeLock: PowerManager.WakeLock? = null
private var mLocalAddress: String = ""

override fun onBind(p0: Intent?): IBinder? = null

Expand All @@ -62,7 +62,7 @@ class AListService : Service() {
override fun onCreate() {
super.onCreate()

initNotification()
initOrUpdateNotification()

if (AppConfig.isWakeLockEnabled) {
mWakeLock = powerManager.newWakeLock(
Expand All @@ -73,7 +73,10 @@ class AListService : Service() {
}

LocalBroadcastManager.getInstance(this)
.registerReceiver(mReceiver, IntentFilter(ACTION_STATUS_CHANGED))
.registerReceiver(
mReceiver,
IntentFilter(ACTION_STATUS_CHANGED)
)
registerReceiverCompat(
mNotificationReceiver,
ACTION_SHUTDOWN,
Expand Down Expand Up @@ -117,28 +120,24 @@ class AListService : Service() {

@Suppress("DEPRECATION")
inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == ACTION_STATUS_CHANGED) {
if (!isRunning) {
stopForeground(true)
stopSelf()
override fun onReceive(context: Context?, intent: Intent) {
when (intent.action) {
ACTION_STATUS_CHANGED -> {
if (!isRunning) {
stopForeground(true)
stopSelf()
}
}
}

}
}

private fun httpAddress(): String = runBlocking {
val channel = BridgeUtils.getMethodChannel(this@AListService)
private fun localAddress(): String = NativeLib.getLocalIp()

// return@runBlocking when (val ret = channel.invokeMethodSync("getHttpAddress", null)) {
// is BridgeUtils.MethodChannelResult.Success -> ret.result as String
// else -> ""
// }
return@runBlocking ""
}

@Suppress("DEPRECATION")
private fun initNotification() {
private fun initOrUpdateNotification() {
// Android 12(S)+ 必须指定PendingIntent.FLAG_
val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
PendingIntent.FLAG_IMMUTABLE
Expand Down Expand Up @@ -196,24 +195,23 @@ class AListService : Service() {
val notification = builder
// .setColor(color)
.setContentTitle(getString(R.string.alist_server_running))
.setContentText(httpAddress())
.setContentText(localAddress())
.setSmallIcon(smallIconRes)
.setContentIntent(pendingIntent)
.addAction(0, getString(R.string.shutdown), shutdownAction)
.addAction(0, getString(R.string.copy_address), copyAddressPendingIntent)

.build()

// 前台服务
startForeground(FOREGROUND_ID, notification)
}

inner class NotificationActionReceiver : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
when (intent?.action) {
// ACTION_SHUTDOWN -> /*AList.shutdown()*/

ACTION_COPY_ADDRESS -> {
ClipboardUtils.copyText("AList", httpAddress())
ClipboardUtils.copyText("AList", localAddress())
toast(R.string.address_copied)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,21 @@ class MainActivity : FlutterActivity() {

inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == AListService.ACTION_STATUS_CHANGED) {
Log.e(TAG, "onReceive: ${AListService.isRunning}")
mEvent?.onServiceStatusChanged(AListService.isRunning, object : VoidResult {
override fun success() {
when (intent.action) {
AListService.ACTION_STATUS_CHANGED -> {
Log.d(TAG, "onReceive: ACTION_STATUS_CHANGED")

mEvent?.onServiceStatusChanged(AListService.isRunning, object : VoidResult {
override fun success() {}
override fun error(error: Throwable) {
}
})
}

}

override fun error(error: Throwable) {
}

})
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AndroidBridge(private val context: Context) : GeneratedApi.Android {
context,
context.getString(R.string.app_switch),
"alist_flutter_switch",
R.drawable.ic_launcher_foreground,
R.drawable.alist_switch,
Intent(context, SwitchServerActivity::class.java)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.github.jing332.alistflutter.model.alist

import android.annotation.SuppressLint
import android.util.Log
import com.github.jing332.alistflutter.R
import com.github.jing332.alistflutter.app
import com.github.jing332.alistflutter.constant.LogLevel
import com.github.jing332.alistflutter.data.entities.ServerLog.Companion.evalLog
import com.github.jing332.alistflutter.utils.FileUtils.readAllText
import com.github.jing332.alistflutter.utils.StringUtils.removeAnsiCodes
import com.github.jing332.alistflutter.utils.ToastUtils.longToast
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -36,11 +34,12 @@ object AList {


fun setAdminPassword(pwd: String) {
val log = execWithParams(
execWithParams(
redirect = true,
params = arrayOf("admin", "set", pwd, "--data", dataPath)
).inputStream.readAllText()
// appDb.serverLogDao.insert(ServerLog(level = LogLevel.INFO, message = log.removeAnsiCodes()))
).inputStream.logWatcher {
handleLog(it)
}
}


Expand All @@ -54,14 +53,21 @@ object AList {

private var mProcess: Process? = null

private suspend fun InputStream.logWatcher(onNewLine: (String) -> Unit) {
private fun handleLog(log:String){
log.removeAnsiCodes().evalLog()?.let {
Logger.log(level = it.level, time = it.time, msg = it.message)
} ?: run {
Logger.log(level = LogLevel.INFO, time = "", msg = log)
}
}

private fun InputStream.logWatcher(onNewLine: (String) -> Unit) {
bufferedReader().use {
while (coroutineContext.isActive) {
while (true) {
runCatching {
val line = it.readLine() ?: return@use
onNewLine(line)
}.onFailure {
Log.e(TAG, "logWatcher: ", it)
return@use
}
}
Expand All @@ -72,19 +78,12 @@ object AList {

private val mScope = CoroutineScope(Dispatchers.IO + Job())
private fun initOutput() {
val onNewLine = { msg: String ->
msg.removeAnsiCodes().evalLog()?.let {
Logger.log(level = it.level, time = it.time, msg = it.message)
} ?: run {
Logger.log(level = LogLevel.INFO, time = "", msg = msg)
}
}

mScope.launch {
mProcess?.inputStream?.logWatcher(onNewLine)
mProcess?.inputStream?.logWatcher(::handleLog)
}
mScope.launch {
mProcess?.errorStream?.logWatcher(onNewLine)
mProcess?.errorStream?.logWatcher(::handleLog)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ object MyTools {
iconResId: Int,
launcherIntent: Intent
) {
ctx.longToast("如失败 请手动授予权限")
if (Build.VERSION.SDK_INT < 26) { /* Android8.0 */
ctx.longToast("如失败 请手动授予权限")

val addShortcutIntent = Intent("com.android.launcher.action.INSTALL_SHORTCUT")
// 不允许重复创建
addShortcutIntent.putExtra("duplicate", false) // 经测试不是根据快捷方式的名字判断重复的
Expand Down
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ plugins {
}

include ":app"
include ":utils"
35 changes: 35 additions & 0 deletions android/utils/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}

android {
namespace "com.github.jing332.utils"
compileSdk 34

defaultConfig {
// minSdk

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.22.1"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Loading

0 comments on commit a2a5baa

Please sign in to comment.