安卓13权限兼容,增加类型文件

This commit is contained in:
nonameShijian 2024-04-17 22:04:49 +08:00
parent 4ef4af98af
commit 615459efb4
3 changed files with 29 additions and 13 deletions

View File

@ -9,6 +9,8 @@
/// <reference path="./type.d.ts" />
/// <reference path="./MapEx.d.ts" />
/// <reference types="@types/cordova" />
/// <reference path="./cordova-plugin-android-permissions.d.ts" />
/// <reference path="./cordova-plugin-device.d.ts" />
/// <reference path="./cordova-plugin-dialogs.d.ts" />
/// <reference path="./cordova-plugin-file.d.ts" />
/// <reference path="./cordova-plugin-file-transfer.d.ts" />

View File

@ -70,4 +70,6 @@ declare interface Window {
initReadWriteFunction?(game: Game): Promise<void>;
bannedKeyWords: string[];
device: Device;
}

View File

@ -1,10 +1,10 @@
// @ts-nocheck
import { get } from "../get/index.js";
import { lib } from "../library/index.js";
import { game } from "../game/index.js";
import { _status } from "../status/index.js";
import { ui } from "../ui/index.js";
import { nonameInitialized } from "../util/index.js";
import { checkVersion } from "../library/update.js";
export async function cordovaReady() {
if (lib.device == "android") {
@ -49,18 +49,30 @@ export async function cordovaReady() {
if ("cordova" in window && "plugins" in window.cordova && "permissions" in window.cordova.plugins) {
const permissions = cordova.plugins.permissions;
const requests = ["WRITE_EXTERNAL_STORAGE", "READ_EXTERNAL_STORAGE"];
requests.forEach((request) => {
permissions.checkPermission(
permissions[request],
(status) => {
if (!status.hasPermission) {
permissions.requestPermission(
permissions[request],
lib.other.ignore,
lib.other.ignore
);
if (typeof device == 'object') {
// 安卓13或以上
if (checkVersion(device.version, "13") >= 0) {
requests.length = 0;
requests.push('READ_MEDIA_IMAGES', 'READ_MEDIA_VIDEO', 'READ_MEDIA_AUDIO');
}
},
}
Promise.all(requests.map(request => {
return new Promise((resolve, reject) => {
permissions.checkPermission(permissions[request], status => {
resolve({
request: request,
hasPermission: status.hasPermission
});
}, lib.other.ignore);
});
})).then(shouldRequestPermissions => {
return shouldRequestPermissions
.filter(({ hasPermission }) => !hasPermission)
.map(({ request }) => permissions[request]);
}).then(willRequestPermissions => {
permissions.requestPermissions(
willRequestPermissions,
lib.other.ignore,
lib.other.ignore
);
});