Skip to content

Commit

Permalink
更换文件服务器为andserver
Browse files Browse the repository at this point in the history
  • Loading branch information
aiselp committed Oct 31, 2023
1 parent e8b672d commit 32c4d11
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 33 deletions.
21 changes: 7 additions & 14 deletions apkbuilder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ android {
}

dependencies {
implementation("com.squareup.okhttp3:okhttp:4.10.0")
implementation("androidx.core:core-ktx:1.8.0")
}
dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude("junit", "junit")
}
androidTestImplementation("androidx.test.espresso:espresso-core:3.1.1-alpha01") {
exclude(group = "com.android.support", module = "support-annotations")
}
testImplementation("junit:junit:4.13.2")
api(fileTree("libs") { include("*.jar") })

implementation(libs.okhttp)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
testImplementation(libs.junit)
api(files("libs/tiny-sign-0.9.jar"))
api(files("libs/commons-io-2.5.jar"))
implementation("androidx.core:core-ktx:1.8.0")
api(libs.commons.io)
implementation(libs.core.ktx)
}
repositories {
mavenCentral()
Expand Down
Binary file removed apkbuilder/libs/commons-io-2.5.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.stardust.autojs.apkbuilder;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -19,7 +19,7 @@ public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
Context appContext = InstrumentationRegistry.getInstrumentation().getContext();

assertEquals("com.stardust.autojs.apkbuilder.test", appContext.getPackageName());
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/raw/licenses.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@
<license>Apache Software License 2.0</license>
</notice>
<notice>
<name>nanohttpd</name>
<copyright>Copyright (c) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias All rights reserved.</copyright>
<url>https://github.com/NanoHttpd/nanohttpd</url>
<license>BSD 3-Clause License</license>
<name>AndServer</name>
<copyright />
<url>https://github.com/yanzhenjie/AndServer/</url>
<license>Apache Software License 2.0</license>
</notice>
<notice>
<name>vscode-mobile</name>
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ buildscript {
classpath(kotlin("gradle-plugin", version = "$kotlin_version"))
classpath("com.jakewharton:butterknife-gradle-plugin:10.2.3")
classpath("org.codehaus.groovy:groovy-json:3.0.8")
classpath("com.yanzhenjie.andserver:plugin:2.1.12")
}
}

Expand Down
4 changes: 4 additions & 0 deletions codeeditor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import java.net.URL
plugins {
id("com.android.library")
id("kotlin-android")
id("com.yanzhenjie.andserver")
id("kotlin-kapt")
}

android {
Expand Down Expand Up @@ -35,6 +37,8 @@ android {

dependencies {

implementation(libs.andserver.api)
kapt(libs.andserver.processor)
implementation(libs.kotlinx.coroutines.android)
api(libs.nanohttpd.webserver)
api(libs.androidx.webkit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class EditorAppManager(val context: Activity) {
private val jsBridge = JsBridge(webView)
private val fileHttpServer = FileHttpServer(
context, File(
context.filesDir,
"$WEB_PUBLIC_PATH/dist"
context.filesDir, "$WEB_PUBLIC_PATH/dist"
)
)
private val pluginManager = PluginManager(jsBridge, coroutineScope)
Expand All @@ -51,7 +50,7 @@ class EditorAppManager(val context: Activity) {
async { initWebResources() }.await()
delay(300)
withContext(Dispatchers.Main) {
webView.loadUrl("http://${fileHttpServer.hostname}:${fileHttpServer.port}")
webView.loadUrl(fileHttpServer.getAddress())
// webView.loadUrl("http://192.168.10.10:8009")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
package com.aiselp.autojs.codeeditor.web

import android.content.Context
import android.os.Build
import android.util.Log
import fi.iki.elonen.SimpleWebServer
import androidx.annotation.RequiresApi
import com.yanzhenjie.andserver.AndServer
import com.yanzhenjie.andserver.Server
import com.yanzhenjie.andserver.annotation.Config
import com.yanzhenjie.andserver.framework.config.WebConfig
import com.yanzhenjie.andserver.framework.website.StorageWebsite
import java.io.File
import java.io.IOException
import java.net.ServerSocket
import java.util.concurrent.TimeUnit
import kotlin.random.Random

class FileHttpServer(context: Context,path:File) :
SimpleWebServer("127.0.0.1", SPort, path, false) {

class FileHttpServer(context: Context, path: File) {
companion object {
var SPort = 42201
fun isPortAvailable(port: Int): Boolean {
try {
ServerSocket(port).use {
return true // 端口可用
}
} catch (e: IOException) {
return false // 端口已被占用
}
}
}

private val port = getPort()
val server: Server = AndServer.webServer(context)
.port(port)
.timeout(10, TimeUnit.SECONDS)
.build();

private fun getPort(): Int {
while (true) {
val port = Random.nextInt(2000, 50000)
if (isPortAvailable(port)) {
return port
}
}
}
val port = SPort
init {
SPort++
Log.d("FileHttpServer", "FileHttpServer init host: ${this.hostname} port: $port")
fun getAddress(): String {
return "http://127.0.0.1:$port"
}
fun start(){
Log.d("FileHttpServer", "FileHttpServer init host: 127.0.0.1 port: $port")
server.startup()
}
fun stop(){
server.shutdown()
}
}

@RequiresApi(Build.VERSION_CODES.M)
@Config
class AppConfig : WebConfig {
override fun onConfig(context: Context, delegate: WebConfig.Delegate) {
// 增加一个位于/sdcard/Download/AndServer/目录的网站
delegate.addWebsite(
StorageWebsite(
File(
context.filesDir, "${EditorAppManager.WEB_PUBLIC_PATH}/dist"
).path
)
)
}
}
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[versions]
andserver = "2.1.12"
core-ktx = "1.8.0"
google-gson = "2.9.1"
junit = "4.13.2"
Expand All @@ -12,6 +13,7 @@ material = "1.9.0"

[libraries]

commons-io = "commons-io:commons-io:2.6"
eventbus = "org.greenrobot:eventbus:3.3.1"
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines-android" }
nanohttpd-webserver = "org.nanohttpd:nanohttpd-webserver:2.3.1"
Expand All @@ -33,7 +35,8 @@ androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }

andserver-processor = { module = "com.yanzhenjie.andserver:processor", version.ref = "andserver" }
andserver-api = { module = "com.yanzhenjie.andserver:api", version.ref = "andserver" }
[plugins]

[bundles]
Expand Down

0 comments on commit 32c4d11

Please sign in to comment.