maipulse: 脱敏存档

- Python 后端 + 单页前端 + Android(Chaquopy) 壳
- 曲库 1343 首 / 称号 2800 个
- 机台身份与店铺信息已抹除,字段留空
- 打包脚本含 16KB 页对齐与自动脱敏
This commit is contained in:
shouko committed 2026-10-28 22:27:46 +08:00
commit 8421898626
161 files changed
+50297

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:allowBackup="true"
android:label="maipulse"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="orientation|screenSize|keyboardHidden"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
@@ -0,0 +1,397 @@
package com.maipulse.app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.FileProvider;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* maipulse 的 Android 外壳。
*
* 流程:复制 assets/app -> filesDir/app(assets 只读,Flask 要写 logs 与缓存),
* 起 Chaquopy 跑 Flask 绑 127.0.0.1:8787,端口通了再把 WebView 指过去。
*
* 额外给了网页一个 Maipulse.installApk(url):走系统 DownloadManager 下 APK,
* 下完弹安装器,这样更新不用让用户自己去找文件。
*/
public class MainActivity extends Activity {
private static final String TAG = "maipulse";
private static final int PORT = 8787;
private WebView web;
private TextView tip;
private static final int REQ_PICK_FILE = 1001;
private ValueCallback<Uri[]> filePathCallback;
private long pendingDownloadId = -1;
private BroadcastReceiver downloadReceiver;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout root = new FrameLayout(this);
root.setBackgroundColor(Color.parseColor("#EEF2F9"));
web = new WebView(this);
web.setBackgroundColor(Color.parseColor("#EEF2F9"));
WebSettings s = web.getSettings();
s.setJavaScriptEnabled(true);
s.setDomStorageEnabled(true);
s.setAllowFileAccess(true);
s.setDatabaseEnabled(true);
s.setMediaPlaybackRequiresUserGesture(false);
s.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
s.setCacheMode(WebSettings.LOAD_DEFAULT);
// 禁掉双指缩放与双击放大(网页里能放大就是这里没设)
s.setSupportZoom(false);
s.setBuiltInZoomControls(false);
s.setDisplayZoomControls(false);
s.setTextZoom(100);
// 长按不再弹出「复制/分享」这类浏览器菜单
web.setLongClickable(false);
web.setHapticFeedbackEnabled(false);
web.setWebViewClient(new WebViewClient());
// 网页里的 <input type=file> 走 WebChromeClient.onShowFileChooser,挂在
// WebViewClient 上编译不过,必须单独设一个 chrome client。
web.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> callback,
FileChooserParams params) {
if (filePathCallback != null) filePathCallback.onReceiveValue(null);
filePathCallback = callback;
try {
Intent i = params.createIntent();
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(i, REQ_PICK_FILE);
return true;
} catch (Exception e) {
filePathCallback = null;
return false;
}
}
});
web.addJavascriptInterface(new Bridge(), "Maipulse");
tip = new TextView(this);
tip.setText("正在启动服务…");
tip.setTextColor(Color.parseColor("#5A6A82"));
tip.setTextSize(15);
tip.setPadding(48, 48, 48, 48);
root.addView(web, new FrameLayout.LayoutParams(-1, -1));
root.addView(tip, new FrameLayout.LayoutParams(-1, -1));
setContentView(root);
registerDownloadReceiver();
new Thread(this::bootPython, "maipulse-boot").start();
new Thread(this::waitAndLoad, "maipulse-wait").start();
}
/* ---------------- 网页可调的接口 ---------------- */
public class Bridge {
/** 下载并安装新版本。url 由网页的更新源给。 */
@JavascriptInterface
public void installApk(String url) {
runOnUiThread(() -> startDownload(url));
}
/** 当前 App 版本号,网页可以拿来显示。 */
@JavascriptInterface
public String appVersion() {
try {
return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (Exception e) {
return "1.0";
}
}
}
private void startDownload(String url) {
if (url == null || !url.startsWith("http")) {
Toast.makeText(this, "更新地址不对", Toast.LENGTH_SHORT).show();
return;
}
try {
File dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if (dir != null) {
for (File f : dir.listFiles()) {
if (f.getName().endsWith(".apk")) f.delete();
}
}
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
req.setTitle("maipulse 新版本");
req.setDescription("下载完成后会弹安装");
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
req.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "maipulse-update.apk");
req.setMimeType("application/vnd.android.package-archive");
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
pendingDownloadId = dm.enqueue(req);
Toast.makeText(this, "开始下载,完成后会弹安装", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(TAG, "download failed", e);
Toast.makeText(this, "下载失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private void registerDownloadReceiver() {
downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) return;
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id != pendingDownloadId) return;
File dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if (dir == null) return;
File apk = new File(dir, "maipulse-update.apk");
if (apk.exists()) installApkFile(apk);
}
};
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(downloadReceiver, filter, Context.RECEIVER_EXPORTED);
} else {
registerReceiver(downloadReceiver, filter);
}
}
private void installApkFile(File apk) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& !getPackageManager().canRequestPackageInstalls()) {
// 没有「安装未知应用」权限,跳设置页让用户开
Intent s = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
Uri.parse("package:" + getPackageName()));
startActivity(s);
Toast.makeText(this, "允许安装后回到 App 再点一次更新", Toast.LENGTH_LONG).show();
return;
}
Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", apk);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(uri, "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} catch (Exception e) {
Log.e(TAG, "install failed", e);
Toast.makeText(this, "拉起安装器失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
/* ---------------- Python 启动 ---------------- */
private void bootPython() {
try {
File files = getFilesDir();
File appDir = new File(files, "app");
File stamp = new File(files, "asset.rev");
// 内置资源版本号:打包时由 build_apk.ps1 写进 assets/app/asset.rev,
// 每次打包都会变,所以数据更新后一定能触发重建(手写常量太容易忘)。
String want = "";
try (InputStream in = getAssets().open("app/asset.rev")) {
byte[] b = new byte[128];
int n = in.read(b);
if (n > 0) want = new String(b, 0, n, "UTF-8").trim();
} catch (Exception ignored) {
}
String have = "";
try {
if (stamp.exists()) {
byte[] b = new byte[(int) stamp.length()];
try (InputStream in = new FileInputStream(stamp)) {
int n = in.read(b);
if (n > 0) have = new String(b, 0, n, "UTF-8").trim();
}
}
} catch (Exception ignored) {
}
boolean broken = !appDir.exists()
|| !new File(appDir, "app.py").exists()
|| !new File(appDir, "data/master.json").exists();
if (broken || want.isEmpty() || !want.equals(have)) {
tip("正在释放内置资源");
// 用户导入的机台配置要保住,其余整目录重建(否则旧资源永远不更新)
byte[] conf = null;
File confFile = new File(new File(appDir, "astral"), "config.py");
if (confFile.exists() && confFile.length() > 0) {
conf = new byte[(int) confFile.length()];
try (InputStream in = new FileInputStream(confFile)) {
int n = in.read(conf);
if (n <= 0) conf = null;
}
}
deleteTree(appDir);
copyAssetDir("app", appDir);
if (conf != null) {
File cf = new File(new File(appDir, "astral"), "config.py");
File parent = cf.getParentFile();
if (parent != null && !parent.exists()) parent.mkdirs();
try (OutputStream out = new FileOutputStream(cf)) {
out.write(conf);
}
}
try (OutputStream out = new FileOutputStream(stamp)) {
out.write(want.getBytes("UTF-8"));
}
}
tip("正在加载 Python 运行时…");
if (!Python.isStarted()) {
Python.start(new AndroidPlatform(this));
}
tip("正在启动本地服务…");
Python.getInstance().getModule("android_boot").callAttr("boot", files.getAbsolutePath());
} catch (Throwable t) {
Log.e(TAG, "boot failed", t);
tip("启动失败:" + t);
}
}
private void tip(final String text) {
runOnUiThread(() -> tip.setText(text));
}
private void deleteTree(File f) {
if (f == null || !f.exists()) return;
if (f.isDirectory()) {
File[] kids = f.listFiles();
if (kids != null) {
for (File k : kids) deleteTree(k);
}
}
f.delete();
}
private void copyAssetDir(String src, File dst) throws IOException {
AssetManager am = getAssets();
String[] list = am.list(src);
if (list == null || list.length == 0) {
copyAssetFile(src, dst);
return;
}
if (!dst.exists() && !dst.mkdirs()) {
throw new IOException("mkdirs failed: " + dst);
}
for (String name : list) {
copyAssetDir(src + "/" + name, new File(dst, name));
}
}
private void copyAssetFile(String src, File dst) throws IOException {
// 不跳过已存在文件:整目录重建后旧的残留必须被覆盖
File parent = dst.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
throw new IOException("mkdirs failed: " + parent);
}
try (InputStream in = getAssets().open(src);
OutputStream out = new FileOutputStream(dst)) {
byte[] buf = new byte[64 * 1024];
int n;
while ((n = in.read(buf)) > 0) out.write(buf, 0, n);
}
}
private void waitAndLoad() {
for (int i = 0; i < 200; i++) {
try (Socket sock = new Socket()) {
sock.connect(new InetSocketAddress("127.0.0.1", PORT), 300);
runOnUiThread(() -> {
tip.setVisibility(View.GONE);
web.loadUrl("http://127.0.0.1:" + PORT + "/");
});
return;
} catch (IOException ignored) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
return;
}
}
}
tip("服务没有起来,看看日志");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQ_PICK_FILE) {
Uri[] result = null;
if (resultCode == RESULT_OK && data != null) {
if (data.getDataString() != null) {
result = new Uri[]{Uri.parse(data.getDataString())};
} else if (data.getClipData() != null) {
int n = data.getClipData().getItemCount();
result = new Uri[n];
for (int i = 0; i < n; i++) {
result[i] = data.getClipData().getItemAt(i).getUri();
}
}
}
if (filePathCallback != null) {
filePathCallback.onReceiveValue(result);
filePathCallback = null;
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && web != null && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
try {
if (downloadReceiver != null) unregisterReceiver(downloadReceiver);
} catch (Exception ignored) {
}
if (web != null) {
web.destroy();
web = null;
}
super.onDestroy();
}
}
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
"""Android 启动器:把 filesDir/app 挂进 sys.path,然后跑 Flask。
为什么不在 assets 里直接 import:app.py 顶层会 os.makedirs(logs),
assets 是只读的,必须用 Java 侧复制出来的可写副本。
"""
import os
import sys
PORT = 8787
def boot(files_dir):
base = os.path.join(files_dir, "app")
if base not in sys.path:
sys.path.insert(0, base)
astral = os.path.join(base, "astral")
if astral not in sys.path:
sys.path.insert(0, astral)
os.environ["ASTRAL_DIR"] = astral
os.environ.setdefault("MAIPULSE_PORT", str(PORT))
os.chdir(base)
import app as maipulse_app # noqa: E402
# 绑定 127.0.0.1:只有本机 WebView 能访问
maipulse_app.app.run(host="127.0.0.1", port=PORT, debug=False,
threaded=True, use_reloader=False)
return "exited"
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/iconBg" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" />
</adaptive-icon>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/iconBg" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" />
</adaptive-icon>
Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="iconBg">#FFE9A8</color>
</resources>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">maipulse</string>
</resources>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">#EEF2F9</item>
<item name="android:navigationBarColor">#FFFFFF</item>
<item name="android:windowBackground">#EEF2F9</item>
</style>
</resources>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="downloaded" path="Download/" />
<external-cache-path name="cache" path="." />
</paths>