index.uts
7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* plus.android 的 Android 原生实现
* 使用 Kotlin 语法直接调用 Android SDK
*
* 注意:在 uni-app x 中,需要通过特定方式获取 Context/Activity
* 实际使用时,请根据 uni-app x 的 API 文档获取当前 Activity
*/
import { AndroidInterface, InstalledAppInfo } from "../interface";
/**
* Android 实现类
*/
class AndroidImpl implements AndroidInterface {
// #ifdef APP-ANDROID
/**
* 获取当前 Context/Activity
*
* 在 uni-app x 中,获取 Context 的方式可能包括:
* 1. 通过 uni-app x 提供的 API(如 getUniActivity())
* 2. 通过插件生命周期回调获取
* 3. 通过其他 uni-app x 特定方式
*
* 这里提供一个占位实现,实际使用时需要替换为正确的 API
*/
private getContext(): any {
// TODO: 替换为 uni-app x 获取 Context 的实际 API
// 示例(需要根据实际 API 调整):
// return getUniActivity(); // 或其他 uni-app x API
return null;
}
// #endif
/**
* 获取运行时主 Activity
* 对应原代码:plus.android.runtimeMainActivity()
*/
getRuntimeMainActivity(): any {
// #ifdef APP-ANDROID
return this.getContext();
// #endif
// #ifndef APP-ANDROID
return null;
// #endif
}
/**
* 打开系统设置
* 对应原代码:plus.android 调用 Intent 打开系统设置
*/
openSystemSettings(): void {
// #ifdef APP-ANDROID
const context = this.getContext();
if (context == null) {
console.warn("[Android] 无法获取 Context,请实现 getContext() 方法");
return;
}
// 原生代码实现(UTS 会编译为 Kotlin)
// 注意:以下代码需要在获取到真实 Context 后才能工作
/*
try {
// 创建 Intent 打开系统设置
const intent = new android.content.Intent(android.provider.Settings.ACTION_SETTINGS);
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
console.log("[Android] 成功打开系统设置");
} catch (e: any) {
console.error(`[Android] 打开系统设置失败: ${String(e)}`);
}
*/
console.warn("[Android] openSystemSettings 需要实现 getContext() 方法");
// #endif
// #ifndef APP-ANDROID
console.warn("[Android] 非 Android 平台不支持");
// #endif
}
/**
* 打开应用设置
* 对应原代码:plus.android 调用 Intent 打开应用详情设置
*/
openAppSettings(): void {
// #ifdef APP-ANDROID
const context = this.getContext();
if (context == null) {
console.warn("[Android] 无法获取 Context,请实现 getContext() 方法");
return;
}
// 原生代码实现(UTS 会编译为 Kotlin)
// 注意:以下代码需要在获取到真实 Context 后才能工作
/*
try {
// 创建 Intent 打开应用详情设置
const intent = new android.content.Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
const uri = android.net.Uri.fromParts("package", context.getPackageName(), null);
intent.setData(uri);
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
console.log("[Android] 成功打开应用设置");
} catch (e: any) {
console.error(`[Android] 打开应用设置失败: ${String(e)}`);
}
*/
console.warn("[Android] openAppSettings 需要实现 getContext() 方法");
// #endif
// #ifndef APP-ANDROID
console.warn("[Android] 非 Android 平台不支持");
// #endif
}
/**
* 获取已安装应用列表
* 对应原代码:plus.android 调用 PackageManager 获取应用列表
*/
getInstalledPackages(): Array<InstalledAppInfo> {
// #ifdef APP-ANDROID
const context = this.getContext();
if (context == null) {
console.warn("[Android] 无法获取 Context,请实现 getContext() 方法");
return [];
}
// 原生代码实现(UTS 会编译为 Kotlin)
// 注意:以下代码需要在获取到真实 Context 后才能工作
/*
const result: InstalledAppInfo[] = [];
try {
const packageManager = context.getPackageManager();
const packages = packageManager.getInstalledPackages(0);
const total = packages.size();
for (let i = 0; i < total; i++) {
const packageInfo = packages.get(i);
const appInfo = packageInfo.applicationInfo;
// 过滤系统应用(FLAG_SYSTEM != 0)
if ((appInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0) {
continue;
}
const appName = packageManager.getApplicationLabel(appInfo).toString();
result.push({
packName: packageInfo.packageName,
versionName: packageInfo.versionName || "",
versionCode: packageInfo.versionCode,
appName: appName
});
}
console.log(`[Android] 获取到 ${result.length} 个已安装应用`);
} catch (e: any) {
console.error(`[Android] 获取已安装应用列表失败: ${String(e)}`);
}
return result;
*/
console.warn("[Android] getInstalledPackages 需要实现 getContext() 方法");
return [];
// #endif
// #ifndef APP-ANDROID
return [];
// #endif
}
/**
* 获取应用外部存储目录路径
* 对应原代码:plus.android.runtimeMainActivity().getExternalFilesDir(null).getPath()
*/
getExternalFilesDir(folderName: string): string {
// #ifdef APP-ANDROID
const context = this.getContext();
if (context == null) {
console.warn("[Android] 无法获取 Context,请实现 getContext() 方法");
return "";
}
// 原生代码实现(UTS 会编译为 Kotlin)
// 注意:以下代码需要在获取到真实 Context 后才能工作
/*
try {
let baseDir: any = null;
// 检查外部存储是否可用
if (android.os.Environment.MEDIA_MOUNTED == android.os.Environment.getExternalStorageState() ||
!android.os.Environment.isExternalStorageRemovable()) {
baseDir = context.getExternalFilesDir(null);
}
// 如果外部存储不可用,使用内部存储
if (baseDir == null) {
baseDir = context.getFilesDir();
}
if (baseDir != null) {
const fullPath = baseDir.getAbsolutePath() + "/" + folderName;
console.log(`[Android] 外部存储目录: ${fullPath}`);
return fullPath;
}
} catch (e: any) {
console.error(`[Android] 获取外部存储目录失败: ${String(e)}`);
}
*/
console.warn("[Android] getExternalFilesDir 需要实现 getContext() 方法");
return "";
// #endif
// #ifndef APP-ANDROID
return "";
// #endif
}
/**
* 获取应用内部存储目录路径
* 对应原代码:plus.android.runtimeMainActivity().getFilesDir().getPath()
*/
getFilesDir(folderName: string): string {
// #ifdef APP-ANDROID
const context = this.getContext();
if (context == null) {
console.warn("[Android] 无法获取 Context,请实现 getContext() 方法");
return "";
}
// 原生代码实现(UTS 会编译为 Kotlin)
// 注意:以下代码需要在获取到真实 Context 后才能工作
/*
try {
const filesDir = context.getFilesDir();
if (filesDir != null) {
const fullPath = filesDir.getAbsolutePath() + "/" + folderName;
console.log(`[Android] 内部存储目录: ${fullPath}`);
return fullPath;
}
} catch (e: any) {
console.error(`[Android] 获取内部存储目录失败: ${String(e)}`);
}
*/
console.warn("[Android] getFilesDir 需要实现 getContext() 方法");
return "";
// #endif
// #ifndef APP-ANDROID
return "";
// #endif
}
}
// 导出单例
export default new AndroidImpl();