Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Anye committed Feb 15, 2023
1 parent e6d8e8c commit b4b7d73
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 90 deletions.
86 changes: 0 additions & 86 deletions saf.js

This file was deleted.

80 changes: 76 additions & 4 deletions 原神切服器.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,90 @@
"ui";
importClass(java.io.File);
let saf = require('./saf.js');
importClass(java.nio.file.Paths);
importClass(java.nio.file.Files);
importClass(java.lang.StringBuilder);
importClass(android.provider.DocumentsContract);
importClass(android.content.Intent);
importClass(android.net.Uri);
importClass(Packages.androidx.documentfile.provider.DocumentFile)

//跳转获取授权
function getPermission(activity) {
let url = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata");
let intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, url);
activity.startActivityForResult(intent, 11);
}

//判断是否有授权
function hasPermission() {
let url = Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata");
let documentFile = DocumentFile.fromTreeUri(activity, url);
return documentFile.canRead() && documentFile.canWrite();
}

//获取data目录的document对象
function get_data_documentFile(activity) {
let url = Uri.parse('content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata');
return DocumentFile.fromTreeUri(activity, url);
}

//path路径转uri对象
function path_to_url(path) {
let paths = path.replace("/storage/emulated/0/Android/data", "").split("/");
let stringBuilder = new StringBuilder("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata");
for (let i = 0; i < paths.length; i++) {
if (paths[i].length === 0) continue;
stringBuilder.append("%2F").append(paths[i]);
}
return Uri.parse(stringBuilder.toString());
}

//拷贝documet对象到文件
function saveDocumentFile(activity, documentFile, path, callback) {
threads.start(function () {
let inp;
try {
inp = getInput(activity, documentFile);
Files.copy(inp, Paths.get(path));
callback(true);
} catch (err) {
log(err);
callback(false);
} finally {
inp.close();
}
});
}

function getInput(activity, documentFile) {
if (documentFile.isFile()) {
return activity.getContentResolver().openInputStream(documentFile.getUri());
}
return null;
}
//获取path指定的文件document对象
function getDocumentFile(activity, path) {
let url = path_to_url(path);
return DocumentFile.fromSingleUri(activity, url);
}

if (device.sdkInt >= 30) {
if (!saf.hasPermission(context)) {
if (!hasPermission(context)) {
dialogs.alert('提示', '从安卓11开始,谷歌限制了Android/data目录的访问权限,' +
'普通应用无法直接使用java.io访问该目录的文件,' +
'但是可以通过安卓提供的SAF框架读取文件,需要跳转页面授权').then(() => {
saf.getPermission(activity);
getPermission(activity);
})
}
}


const dataDir = saf.get_data_documentFile(context);
const dataDir = get_data_documentFile(context);

var hasapp = []
var hasname = []
Expand Down

0 comments on commit b4b7d73

Please sign in to comment.