Skip to content

Commit

Permalink
Merge branch 'feature-android' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
krxkli committed Sep 3, 2024
2 parents 305aa5c + af30558 commit 468b15c
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 122 deletions.
Binary file modified Android/app/release/app-release.apk
Binary file not shown.
Binary file modified Android/app/release/baselineProfiles/0/app-release.dm
Binary file not shown.
Binary file modified Android/app/release/baselineProfiles/1/app-release.dm
Binary file not shown.
3 changes: 3 additions & 0 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<!-- 兼容 安卓 10 之前版本-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- WIFI 状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:requestLegacyExternalStorage="true"
Expand Down
7 changes: 3 additions & 4 deletions Android/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,11 @@ JNIEXPORT void JNICALL
Java_com_example_scut_1router_LoginActivity_cancelAutoLogin(JNIEnv *env, jobject thiz) {
CancelAutoLogin();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_scut_1router_MainActivity_00024Companion_initLibSSHCommand(JNIEnv *env,
jobject thiz,
jstring download_path) {

Java_com_example_scut_1router_MainActivity_initLibSSHCommand(JNIEnv *env, jobject thiz,
jstring download_path) {
const char* download_pathStr = env->GetStringUTFChars(download_path, nullptr);

Init((void *)download_pathStr);
Expand Down
96 changes: 75 additions & 21 deletions Android/app/src/main/java/com/example/scut_router/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.example.scut_router

import android.Manifest
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.text.format.Formatter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
Expand All @@ -17,18 +21,19 @@ import com.example.scut_router.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
val REQUEST_CODE: Int = 100
private val requestAccessFilePermission: Int = 100

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

requestPermissions() // 请求文件管理权限

// 加载布局
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

// 为按钮设置点击事件
var intent : Intent
var intent: Intent
binding.imageButtonAccount.setOnClickListener() {
// Toast.makeText(this, "Account", Toast.LENGTH_SHORT).show()
intent = Intent(this, LoginActivity::class.java)
Expand Down Expand Up @@ -56,8 +61,55 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)

}

// 检查连接
if (!checkIsValidConn()) {
val alertBuilder = AlertDialog.Builder(this)
alertBuilder.setTitle(getString(R.string.validConnTitle))
alertBuilder.setMessage(getString(R.string.validConnMsg))
alertBuilder.setPositiveButton(getString(R.string.validConnYes)) { _, _ ->
finish()
}
alertBuilder.setNegativeButton(getString(R.string.validConnNo)) { dialog, _ ->
dialog.dismiss()
loadNativeLib()
}
alertBuilder.show()
} else {
loadNativeLib()
}
}

private fun loadNativeLib() {
Toast.makeText(this, getString(R.string.loadCore), Toast.LENGTH_SHORT).show()
// 加载 Native 库
System.loadLibrary("scut_router")
// 初始化 SSH 库
val downloadPath: String =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
initLibSSHCommand(downloadPath)

Toast.makeText(this, getString(R.string.core_loaded_successfully), Toast.LENGTH_SHORT).show()
}

private fun checkIsValidConn(): Boolean {
// 检查是否正确连接路由器
val context: Context = this
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
val ipAddr = wifiInfo.ipAddress

val ipStr = Formatter.formatIpAddress(ipAddr)
println("IP 地址: $ipStr")

if (ipStr != this.getString(R.string.default_router_conn)) {
return false
} else {
return true
}
}


override fun onDestroy() {
super.onDestroy()

Expand All @@ -72,17 +124,7 @@ class MainActivity : AppCompatActivity() {

private external fun destroyLibSSHCommand()

companion object {
private external fun initLibSSHCommand(downloadPath: String)
// Used to load the 'scut_router' library on application startup.
init {
System.loadLibrary("scut_router")
// 初始化 SSH 库
val downloadPath : String = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
initLibSSHCommand(downloadPath)
}

}
private external fun initLibSSHCommand(downloadPath: String)

/**
* 请求文件读写权限
Expand All @@ -92,7 +134,11 @@ class MainActivity : AppCompatActivity() {
// Android 11 及更高版本,检查 MANAGE_EXTERNAL_STORAGE 权限
if (Environment.isExternalStorageManager()) {
// 权限已被授予,可以进行文件操作
Toast.makeText(this, "权限已成功获取,若首次授予请重启应用以正常运行", Toast.LENGTH_SHORT).show()
Toast.makeText(
this,
"权限已成功获取,若首次授予请重启应用以正常运行",
Toast.LENGTH_SHORT
).show()
} else {
// 权限未被授予,提示用户去设置中开启
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
Expand All @@ -115,26 +161,34 @@ class MainActivity : AppCompatActivity() {
this, arrayOf<String>(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
), REQUEST_CODE
), requestAccessFilePermission
)
}
}
}

override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQUEST_CODE -> {
requestAccessFilePermission -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
grantResults[0] == PackageManager.PERMISSION_GRANTED)
) {
// Permission is granted. Continue the action or workflow
// in your app.
Toast.makeText(this, "权限已成功获取,若首次授予请重启应用以正常运行", Toast.LENGTH_SHORT).show()
Toast.makeText(
this,
"权限已成功获取,若首次授予请重启应用以正常运行",
Toast.LENGTH_SHORT
).show()

} else {
Toast.makeText(this, "权限被拒绝,无法正常工作,请重启", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "权限被拒绝,无法正常工作,请重启", Toast.LENGTH_SHORT)
.show()
}
return
}
Expand Down

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions Android/app/src/main/java/com/example/scut_router/data/Result.kt

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions Android/app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
<string name="scut_ip">校园网 IP 地址</string>
<string name="netmask">校园网子网掩码</string>
<string name="gateway">校园网网关</string>
<string name="validConnTitle">可能未连接路由器</string>
<string name="validConnMsg">是否退出应用程序</string>
<string name="validConnYes">是</string>
<string name="validConnNo">否</string>
<string name="loadCore">正在加载内核</string>
<string name="core_loaded_successfully">内核加载成功</string>
</resources>
7 changes: 7 additions & 0 deletions Android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
<string name="gateway">gateway</string>
<string name="dns_1" translatable="false">DNS 1</string>
<string name="dns_2" translatable="false">DNS 2</string>
<string name="default_router_conn" translatable="false">192.168.0.101</string>
<string name="validConnTitle">The router may not be connected</string>
<string name="validConnMsg">Whether to exit the application</string>
<string name="validConnYes">Yes</string>
<string name="validConnNo">No</string>
<string name="loadCore">Loading Core</string>
<string name="core_loaded_successfully">Core Loaded successfully</string>
</resources>

0 comments on commit 468b15c

Please sign in to comment.