apk包含apk
This commit is contained in:
166
utils/fileUtils.js
Normal file
166
utils/fileUtils.js
Normal file
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* 检查当前平台是否为安卓
|
||||
*/
|
||||
export function isAndroid() {
|
||||
return uni.getSystemInfoSync().platform === 'android'
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保 setTimeout 函数可用
|
||||
*/
|
||||
if (typeof setTimeout === 'undefined') {
|
||||
console.log('setTimeout 未定义,添加 polyfill')
|
||||
global.setTimeout = function(callback, delay) {
|
||||
// 简单的同步实现,适用于不需要真正延迟的情况
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已安装Chrome
|
||||
*/
|
||||
export async function checkChromeInstalled() {
|
||||
return new Promise((resolve) => {
|
||||
if (isAndroid()) {
|
||||
try {
|
||||
const main = plus.android.runtimeMainActivity()
|
||||
const PackageManager = plus.android.importClass('android.content.pm.PackageManager')
|
||||
const pm = main.getPackageManager()
|
||||
|
||||
pm.getPackageInfo("com.android.chrome", PackageManager.GET_ACTIVITIES)
|
||||
resolve(true)
|
||||
} catch (e) {
|
||||
resolve(false)
|
||||
}
|
||||
} else {
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动APK并返回结果
|
||||
*/
|
||||
export async function moveChromeApkToDevice(options = {}) {
|
||||
const config = {
|
||||
sourcePath: '/static/apk/com.android.chrome.apk',
|
||||
targetDir: 'yxd',
|
||||
targetFileName: 'com.android.chrome.apk',
|
||||
...options
|
||||
}
|
||||
|
||||
console.log('开始移动APK文件,配置:', config)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 直接使用 plus.io 的文件系统 API
|
||||
// const srcPath = `_www${config.sourcePath}`
|
||||
const srcPath = plus.io.convertLocalFileSystemURL(config.sourcePath)
|
||||
const targetDirPath = '_downloads/'
|
||||
console.log('源文件路径:', srcPath)
|
||||
|
||||
// 使用内部存储根目录
|
||||
const internalStoragePath = 'file:///storage/emulated/0/'
|
||||
console.log('尝试使用内部存储目录:', internalStoragePath)
|
||||
|
||||
plus.io.resolveLocalFileSystemURL(srcPath, (srcEntry) => {
|
||||
console.log('找到源文件:', srcEntry.fullPath)
|
||||
|
||||
// plus.io.resolveLocalFileSystemURL(internalStoragePath, (rootEntry) => {
|
||||
// console.log('找到内部存储根目录:', rootEntry.fullPath)
|
||||
|
||||
// rootEntry.getDirectory(config.targetDir, {
|
||||
// create: true
|
||||
// }, (targetDirEntry) => {
|
||||
// console.log('创建/找到目标目录:', targetDirEntry.fullPath)
|
||||
|
||||
// // 先检查是否已有同名文件
|
||||
// targetDirEntry.getFile(config.targetFileName, {}, (
|
||||
// existingFile) => {
|
||||
// console.log('找到同名文件,直接返回:', existingFile
|
||||
// .fullPath)
|
||||
// // 通知系统扫描文件
|
||||
// scanFile(existingFile.fullPath)
|
||||
// resolve(existingFile)
|
||||
// }, () => {
|
||||
// // 没有同名文件,执行复制
|
||||
// srcEntry.copyTo(targetDirEntry, config
|
||||
// .targetFileName, (newEntry) => {
|
||||
// console.log('文件复制成功:', newEntry
|
||||
// .fullPath)
|
||||
// // 通知系统扫描新文件
|
||||
// scanFile(newEntry.fullPath)
|
||||
// resolve(newEntry)
|
||||
// }, (copyErr) => {
|
||||
// console.error('复制失败:', copyErr)
|
||||
// reject(
|
||||
// `复制失败: ${JSON.stringify(copyErr)}`)
|
||||
// })
|
||||
// })
|
||||
// }, (dirErr) => {
|
||||
// console.error('创建目标目录失败:', dirErr)
|
||||
// reject(`创建目标目录失败: ${JSON.stringify(dirErr)}`)
|
||||
// })
|
||||
// }, (rootErr) => {
|
||||
// console.error('访问内部存储根目录失败:', rootErr)
|
||||
// reject(`访问内部存储根目录失败: ${JSON.stringify(rootErr)}`)
|
||||
// })
|
||||
// 3. 直接解析下载目录
|
||||
plus.io.resolveLocalFileSystemURL(targetDirPath, (downloadDirEntry) => {
|
||||
console.log('找到下载目录:', downloadDirEntry.fullPath)
|
||||
|
||||
// 4. 检查是否已有同名文件
|
||||
downloadDirEntry.getFile(config.targetFileName, {}, (existingFile) => {
|
||||
console.log('找到已存在的 APK:', existingFile.fullPath)
|
||||
scanFile(existingFile.fullPath)
|
||||
resolve(existingFile)
|
||||
}, () => {
|
||||
// 5. 不存在则进行复制
|
||||
srcEntry.copyTo(downloadDirEntry, config.targetFileName, (
|
||||
newEntry) => {
|
||||
console.log('APK 复制成功:', newEntry.fullPath)
|
||||
scanFile(newEntry.fullPath)
|
||||
resolve(newEntry)
|
||||
}, (copyErr) => {
|
||||
console.error('复制失败:', copyErr)
|
||||
reject(`复制失败: ${JSON.stringify(copyErr)}`)
|
||||
})
|
||||
})
|
||||
}, (dirErr) => {
|
||||
// 兼容处理:如果获取下载目录也失败,尝试使用应用私有目录 (_doc)
|
||||
console.error('获取公共下载目录失败,尝试应用私有目录:', dirErr)
|
||||
saveToPrivateDir(srcEntry, config, resolve, reject)
|
||||
})
|
||||
}, (srcErr) => {
|
||||
console.error('访问源文件失败:', srcErr)
|
||||
reject(`访问源文件失败: ${JSON.stringify(srcErr)}`)
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('执行过程中发生异常:', e)
|
||||
reject(`执行过程中发生异常: ${e}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function scanFile(filePath) {
|
||||
if (!isAndroid()) return
|
||||
try {
|
||||
console.log('开始扫描文件:', filePath)
|
||||
const Intent = plus.android.importClass('android.content.Intent')
|
||||
const Uri = plus.android.importClass('android.net.Uri')
|
||||
const File = plus.android.importClass('java.io.File')
|
||||
const main = plus.android.runtimeMainActivity()
|
||||
const intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
|
||||
intent.setData(Uri.fromFile(new File(filePath)))
|
||||
main.sendBroadcast(intent)
|
||||
console.log('文件扫描成功')
|
||||
} catch (e) {
|
||||
console.error('文件扫描失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
isAndroid,
|
||||
checkChromeInstalled,
|
||||
moveChromeApkToDevice
|
||||
}
|
||||
Reference in New Issue
Block a user