feat: 全面 Android API → Python 兼容层 (android_compat.py)
新增 android_compat.py (3552行):
全面映射 Android Java API 到 Python 跨平台实现,覆盖 16 个领域、108 个 API 对象:
android.content: Intent(20+actions), Context, ContentResolver, ContentValues,
BroadcastReceiver, IntentFilter, SharedPreferences
android.net: Uri (parse/fromFile/withAppendedPath)
android.os: Build, Environment, PowerManager, BatteryManager, VibrationEffect,
Bundle, Handler, Looper, ParcelFileDescriptor
android.graphics: Bitmap (PIL后端), Canvas, Color, Paint, Rect, Matrix,
Typeface, BitmapFactory
android.graphics.pdf: PdfRenderer (PyMuPDF/pdf2image后端)
android.app: ActivityManager, PackageManager(包查询), NotificationManager,
AlarmManager, DownloadManager
android.view: View, TextView, EditText, Button, ImageView, ScrollView,
ViewGroup, LinearLayout, FrameLayout, RelativeLayout
android.view.accessibility: AccessibilityNodeInfo (UI树+pyautogui点击),
AccessibilityPage, UINode
android.hardware: SensorManager, CameraManager, BiometricPrompt
android.location: LocationManager(模拟GPS), Geocoder, Location(Haversine)
android.media: MediaPlayer, AudioRecord, AudioManager
android.telephony: TelephonyManager
android.provider: Settings, MediaStore, CalendarContract, ContactsContract
android.net.wifi: WifiManager, WifiInfo + ConnectivityManager
android.os.storage: StorageManager, StorageVolume
java.io: File, FileInputStream/OutputStream, BufferedReader, BufferedWriter
Java桥接: Java.type() 支持直调真实Java类(jpype)或返回存根
系统服务: 10+ 模拟服务 (ActivityManager/PowerManager/etc)
路径映射: /sdcard → ~/Android-compat/sdcard
UI自动化: UINode + pyautogui 桌面降级
绘图: Bitmap/Canvas → PIL/Pillow 后端
集成到 operit_runtime.py: 自动检测并注入108个Android API到脚本命名空间
测试结果:
- 语法检查: android_compat.py ✅, operit_runtime.py ✅
- 路径映射: /sdcard → /root/Android-compat/sdcard ✅
- Intent/Uri/Context/Bitmap/File 全API ✅
- 脚本加载: 38脚本 280工具 ✅
This commit is contained in:
1 parent
dc77dfd304
commit
b245af9063
2 files changed
+3593
No files matched your search
@@ -37,10 +37,19 @@ import zipfile
|
||||
import sqlite3
|
||||
import traceback
|
||||
import inspect
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Any, Dict, List, Optional, Union, Callable
|
||||
|
||||
# 导入 Android 兼容层
|
||||
_ANDROID_COMPAT_AVAILABLE = False
|
||||
try:
|
||||
import android_compat
|
||||
_ANDROID_COMPAT_AVAILABLE = True
|
||||
except ImportError:
|
||||
android_compat = None
|
||||
|
||||
# ── 第三方依赖(可选导入) ──
|
||||
try:
|
||||
import requests
|
||||
@@ -1246,6 +1255,22 @@ class OperitScriptLoader:
|
||||
"requests": requests, "httpx": httpx,
|
||||
}
|
||||
|
||||
# 注入 Android 兼容层(如果可用)
|
||||
if _ANDROID_COMPAT_AVAILABLE and android_compat:
|
||||
android_globals = android_compat.get_android_compat_globals()
|
||||
script_globals.update(android_globals)
|
||||
# 特别注入 context 和 service 实例
|
||||
script_globals["androidContext"] = android_compat.application_context
|
||||
else:
|
||||
# 没有 android_compat 时提供空桩
|
||||
script_globals["Java"] = type("Java", (), {"type": staticmethod(
|
||||
lambda cls: type("Stub", (), {
|
||||
"__getattr__": lambda s, n: s,
|
||||
"__call__": lambda s, *a, **kw: s,
|
||||
"toString": lambda s: "[Android-only]",
|
||||
})())
|
||||
})
|
||||
|
||||
try:
|
||||
with open(script_path, "r", encoding="utf-8") as f:
|
||||
code = f.read()
|
||||
@@ -1417,6 +1442,22 @@ if __name__ == "__main__":
|
||||
print(f" 平台: {platform.system()} {platform.release()}")
|
||||
print(f" Python: {sys.version.split()[0]}")
|
||||
|
||||
# Android 兼容层状态
|
||||
try:
|
||||
import android_compat as _ac
|
||||
ag = _ac.get_android_compat_globals()
|
||||
print(f" Android兼容层: 已加载 ({len(ag)} 个 API)")
|
||||
print(f" ├─ Android核心API: Intent, Context, Uri, ContentResolver, 等")
|
||||
print(f" ├─ Android系统服务: ActivityManager, PackageManager, 等")
|
||||
print(f" ├─ Android图形API: Bitmap, Canvas, Color, Paint, PdfRenderer")
|
||||
print(f" ├─ Android视图体系: View, TextView, UINode, AccessibilityNodeInfo")
|
||||
print(f" ├─ Android媒体/定位/通信: MediaPlayer, LocationManager, TelephonyManager")
|
||||
print(f" ├─ Java桥接: Java.type() - 直接调用Java类 (需jpype)")
|
||||
print(f" ├─ java.io兼容: File, FileInputStream, FileOutputStream")
|
||||
print(f" └─ 路径映射: /sdcard → {_ac._sdcard_root}")
|
||||
except Exception as _e:
|
||||
print(f" Android兼容层: 未安装 (android_compat.py 不可用, {_e})")
|
||||
|
||||
# 如果脚本目录存在,扫描加载
|
||||
if os.path.isdir(loader.scripts_dir):
|
||||
result = loader.scan_directory()
|
||||
|
||||
Reference in new issue
Block a user