diff --git a/Android/app/release/app-release.apk b/Android/app/release/app-release.apk index d3b54f6..b956d51 100644 Binary files a/Android/app/release/app-release.apk and b/Android/app/release/app-release.apk differ diff --git a/Android/app/release/baselineProfiles/0/app-release.dm b/Android/app/release/baselineProfiles/0/app-release.dm index 005ed0d..202b5df 100644 Binary files a/Android/app/release/baselineProfiles/0/app-release.dm and b/Android/app/release/baselineProfiles/0/app-release.dm differ diff --git a/Android/app/release/baselineProfiles/1/app-release.dm b/Android/app/release/baselineProfiles/1/app-release.dm index 496c1de..5f0568a 100644 Binary files a/Android/app/release/baselineProfiles/1/app-release.dm and b/Android/app/release/baselineProfiles/1/app-release.dm differ diff --git a/Android/app/src/main/AndroidManifest.xml b/Android/app/src/main/AndroidManifest.xml index 4f0c8d5..28c9930 100644 --- a/Android/app/src/main/AndroidManifest.xml +++ b/Android/app/src/main/AndroidManifest.xml @@ -9,6 +9,9 @@ + + + GetStringUTFChars(download_path, nullptr); Init((void *)download_pathStr); diff --git a/Android/app/src/main/java/com/example/scut_router/MainActivity.kt b/Android/app/src/main/java/com/example/scut_router/MainActivity.kt index c633a1e..284b7fd 100644 --- a/Android/app/src/main/java/com/example/scut_router/MainActivity.kt +++ b/Android/app/src/main/java/com/example/scut_router/MainActivity.kt @@ -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 @@ -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) @@ -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() @@ -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) /** * 请求文件读写权限 @@ -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) @@ -115,26 +161,34 @@ class MainActivity : AppCompatActivity() { this, arrayOf( Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE - ), REQUEST_CODE + ), requestAccessFilePermission ) } } } - override fun onRequestPermissionsResult(requestCode: Int, - permissions: Array, grantResults: IntArray) { + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, 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 } diff --git a/Android/app/src/main/java/com/example/scut_router/data/LoginDataSource.kt b/Android/app/src/main/java/com/example/scut_router/data/LoginDataSource.kt deleted file mode 100644 index ef255cc..0000000 --- a/Android/app/src/main/java/com/example/scut_router/data/LoginDataSource.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.scut_router.data - -import com.example.scut_router.data.model.LoggedInUser -import java.io.IOException - -/** - * Class that handles authentication w/ login credentials and retrieves user information. - */ -class LoginDataSource { - - fun login(username: String, password: String): Result { - try { - // TODO: handle loggedInUser authentication - val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), "Jane Doe") - return Result.Success(fakeUser) - } catch (e: Throwable) { - return Result.Error(IOException("Error logging in", e)) - } - } - - fun logout() { - // TODO: revoke authentication - } -} \ No newline at end of file diff --git a/Android/app/src/main/java/com/example/scut_router/data/LoginRepository.kt b/Android/app/src/main/java/com/example/scut_router/data/LoginRepository.kt deleted file mode 100644 index 0f6b58b..0000000 --- a/Android/app/src/main/java/com/example/scut_router/data/LoginRepository.kt +++ /dev/null @@ -1,46 +0,0 @@ -package com.example.scut_router.data - -import com.example.scut_router.data.model.LoggedInUser - -/** - * Class that requests authentication and user information from the remote data source and - * maintains an in-memory cache of login status and user credentials information. - */ - -class LoginRepository(val dataSource: LoginDataSource) { - - // in-memory cache of the loggedInUser object - var user: LoggedInUser? = null - private set - - val isLoggedIn: Boolean - get() = user != null - - init { - // If user credentials will be cached in local storage, it is recommended it be encrypted - // @see https://developer.android.com/training/articles/keystore - user = null - } - - fun logout() { - user = null - dataSource.logout() - } - - fun login(username: String, password: String): Result { - // handle login - val result = dataSource.login(username, password) - - if (result is Result.Success) { - setLoggedInUser(result.data) - } - - return result - } - - private fun setLoggedInUser(loggedInUser: LoggedInUser) { - this.user = loggedInUser - // If user credentials will be cached in local storage, it is recommended it be encrypted - // @see https://developer.android.com/training/articles/keystore - } -} \ No newline at end of file diff --git a/Android/app/src/main/java/com/example/scut_router/data/Result.kt b/Android/app/src/main/java/com/example/scut_router/data/Result.kt deleted file mode 100644 index fdc8554..0000000 --- a/Android/app/src/main/java/com/example/scut_router/data/Result.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.scut_router.data - -/** - * A generic class that holds a value with its loading status. - * @param - */ -sealed class Result { - - data class Success(val data: T) : Result() - data class Error(val exception: Exception) : Result() - - override fun toString(): String { - return when (this) { - is Success<*> -> "Success[data=$data]" - is Error -> "Error[exception=$exception]" - } - } -} \ No newline at end of file diff --git a/Android/app/src/main/java/com/example/scut_router/data/model/LoggedInUser.kt b/Android/app/src/main/java/com/example/scut_router/data/model/LoggedInUser.kt deleted file mode 100644 index 1d0bdc7..0000000 --- a/Android/app/src/main/java/com/example/scut_router/data/model/LoggedInUser.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.scut_router.data.model - -/** - * Data class that captures user information for logged in users retrieved from LoginRepository - */ -data class LoggedInUser( - val userId: String, - val displayName: String -) \ No newline at end of file diff --git a/Android/app/src/main/res/values-zh/strings.xml b/Android/app/src/main/res/values-zh/strings.xml index 15f7529..3ddb109 100644 --- a/Android/app/src/main/res/values-zh/strings.xml +++ b/Android/app/src/main/res/values-zh/strings.xml @@ -17,4 +17,10 @@ 校园网 IP 地址 校园网子网掩码 校园网网关 + 可能未连接路由器 + 是否退出应用程序 + + + 正在加载内核 + 内核加载成功 \ No newline at end of file diff --git a/Android/app/src/main/res/values/strings.xml b/Android/app/src/main/res/values/strings.xml index fdee062..d498545 100644 --- a/Android/app/src/main/res/values/strings.xml +++ b/Android/app/src/main/res/values/strings.xml @@ -19,4 +19,11 @@ gateway DNS 1 DNS 2 + 192.168.0.101 + The router may not be connected + Whether to exit the application + Yes + No + Loading Core + Core Loaded successfully \ No newline at end of file