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 2, 2024
2 parents 461227f + 45692fd commit 305aa5c
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 32 deletions.
7 changes: 5 additions & 2 deletions Android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ android {
applicationId = "com.krxkli.scut_router"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
versionCode = 2
versionName = "1.1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

// Golang 库为下列架构
ndk {
abiFilters.add("arm64-v8a")
abiFilters.add("armeabi-v7a")
abiFilters.add("x86_64")
// abiFilters.add("x86") // 暂时不支持
}
}

Expand Down
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.
4 changes: 2 additions & 2 deletions Android/app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0",
"versionCode": 2,
"versionName": "1.1.0",
"outputFile": "app-release.apk"
}
],
Expand Down
4 changes: 4 additions & 0 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<!-- 安卓 11 开始引入-->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<!-- 兼容 安卓 10 之前版本-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:requestLegacyExternalStorage="true"
Expand Down
5 changes: 3 additions & 2 deletions Android/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ cmake_minimum_required(VERSION 3.22.1)
# build script scope).
project("scut_router")

# 输出正在编译的架构
message(STATUS "ANDROID_ABI: ${ANDROID_ABI}")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
Expand All @@ -32,10 +34,9 @@ add_library(${CMAKE_PROJECT_NAME} SHARED

# 注意加上 ${CMAKE_SOURCE_DIR} 环境变量,否则会找不到头文件
set(HEADER_PATH ${CMAKE_SOURCE_DIR}/../../../../../Compatibility/out/)
include_directories(${HEADER_PATH}) # 包含 libSSHCommand.h

# 将库添加到 jniLibs 目录下,保证会自动拷贝到 apk 包
set(Compatibility_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/arm64-v8a)
set(Compatibility_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}) # 跨平台编译
add_library(Compatibility SHARED IMPORTED)
set_property(TARGET Compatibility PROPERTY IMPORTED_NO_SONAME 1)
set_target_properties(Compatibility PROPERTIES IMPORTED_LOCATION ${Compatibility_DIR}/libSSHCommand.so)
Expand Down
42 changes: 35 additions & 7 deletions Android/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
#include <jni.h>
#include <string>
#include "libSSHCommand.h"

// 检测 Android 架构
#if defined(__ANDROID__)
#if defined(__x86__)
#define ARCHITECTURE "x86"
#include "../jniLibs/x86/libSSHCommand.h"
#elif defined(__x86_64__)
#define ARCHITECTURE "x86_64"
#include "../jniLibs/x86_64/libSSHCommand.h"
#elif defined(__arm__)
#define ARCHITECTURE "ARM"
#include "../jniLibs/armeabi-v7a/libSSHCommand.h"
#elif defined(__aarch64__)
#define ARCHITECTURE "ARM64"
#include "../jniLibs/arm64-v8a/libSSHCommand.h"
#else
#define ARCHITECTURE "Unknown Architecture"
#endif
#else
#define ARCHITECTURE "Not an Android Platform"
#endif

extern "C"
JNIEXPORT void JNICALL
Expand Down Expand Up @@ -68,12 +88,8 @@ Java_com_example_scut_1router_InternetActivity_setNetwork(JNIEnv *env, jobject t
env->ReleaseStringUTFChars(gateway, gatewayStr);

}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_scut_1router_MainActivity_00024Companion_initLibSSHCommand(JNIEnv *env,
jobject thiz) {
Init();
}



extern "C"
JNIEXPORT void JNICALL
Expand All @@ -92,4 +108,16 @@ extern "C"
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) {

const char* download_pathStr = env->GetStringUTFChars(download_path, nullptr);

Init((void *)download_pathStr);

env->ReleaseStringUTFChars(download_path, download_pathStr);
}
79 changes: 77 additions & 2 deletions Android/app/src/main/java/com/example/scut_router/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.example.scut_router

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.scut_router.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

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

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

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

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

Expand Down Expand Up @@ -62,13 +73,77 @@ class MainActivity : AppCompatActivity() {
private external fun destroyLibSSHCommand()

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

}

/**
* 请求文件读写权限
*/
private fun requestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android 11 及更高版本,检查 MANAGE_EXTERNAL_STORAGE 权限
if (Environment.isExternalStorageManager()) {
// 权限已被授予,可以进行文件操作
Toast.makeText(this, "权限已成功获取,若首次授予请重启应用以正常运行", Toast.LENGTH_SHORT).show()
} else {
// 权限未被授予,提示用户去设置中开启
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
}
} else {
// Android 10 及更低版本,检查 READ 和 WRITE 权限
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this, arrayOf<String>(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
), REQUEST_CODE
)
}
}
}

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

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

// Add other 'when' lines to check for other
// permissions this app might request.
else -> {
// Ignore all other requests.
}
}
}
}
91 changes: 91 additions & 0 deletions Android/app/src/main/jniLibs/arm64-v8a/libSSHCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package command-line-arguments */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h>

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments. */




/* End of preamble from import "C" comments. */


/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef size_t GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
#ifdef _MSC_VER
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif

/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus
extern "C" {
#endif

extern void Init(void* downloadPath);
extern void Destroy();
extern void Reboot();
extern void WiredLogin();
extern void WirelessLogin();
extern void SyncTime();
extern void AutoLogin();
extern void CancelAutoLogin();
extern void SetScutInfo(void* username, void* password);
extern void SetAcInfo(void* acIP, void* acName);
extern void SetNetwork(void* ip, void* dnsArr, void* netmask, void* gateway);

#ifdef __cplusplus
}
#endif
Binary file modified Android/app/src/main/jniLibs/arm64-v8a/libSSHCommand.so
Binary file not shown.
Loading

0 comments on commit 305aa5c

Please sign in to comment.