feat: use browser to download apk on android

This commit is contained in:
NoCrypt 2024-08-15 23:31:37 +07:00
parent c50a5be3d0
commit 8b81730ddb
8 changed files with 41 additions and 89 deletions

View file

@ -9,11 +9,10 @@ android {
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {
implementation project(':capacitor-community-file-opener')
implementation project(':capacitor-community-screen-brightness')
implementation project(':capacitor-app')
implementation project(':capacitor-app-launcher')
implementation project(':capacitor-browser')
implementation project(':capacitor-filesystem')
implementation project(':capacitor-local-notifications')
implementation project(':capacitor-status-bar')
implementation project(':capacitor-nodejs')

View file

@ -53,7 +53,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-feature android:name="android.software.leanback" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
</manifest>

View file

@ -2,21 +2,18 @@
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../../node_modules/@capacitor/android/capacitor')
include ':capacitor-community-file-opener'
project(':capacitor-community-file-opener').projectDir = new File('../../node_modules/@capacitor-community/file-opener/android')
include ':capacitor-community-screen-brightness'
project(':capacitor-community-screen-brightness').projectDir = new File('../../node_modules/@capacitor-community/screen-brightness/android')
include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../../node_modules/@capacitor/app/android')
include ':capacitor-app-launcher'
project(':capacitor-app-launcher').projectDir = new File('../../node_modules/@capacitor/app-launcher/android')
include ':capacitor-browser'
project(':capacitor-browser').projectDir = new File('../../node_modules/@capacitor/browser/android')
include ':capacitor-filesystem'
project(':capacitor-filesystem').projectDir = new File('../../node_modules/@capacitor/filesystem/android')
include ':capacitor-local-notifications'
project(':capacitor-local-notifications').projectDir = new File('../../node_modules/@capacitor/local-notifications/android')

View file

@ -26,14 +26,13 @@
"webpack-merge": "^5.10.0"
},
"dependencies": {
"@capacitor-community/file-opener": "^6.0.0",
"@capacitor-community/screen-brightness": "^6.0.0",
"@capacitor/android": "^6.1.1",
"@capacitor/app": "^6.0.0",
"@capacitor/app-launcher": "^6.0.2",
"@capacitor/browser": "^6.0.1",
"@capacitor/cli": "^6.1.1",
"@capacitor/core": "^6.1.1",
"@capacitor/filesystem": "^6.0.0",
"@capacitor/ios": "^6.1.1",
"@capacitor/local-notifications": "^6.0.0",
"@capacitor/status-bar": "^6.0.0",

View file

@ -1,12 +1,12 @@
import { App } from '@capacitor/app';
import { Filesystem, Directory } from '@capacitor/filesystem';
import { FileOpener } from '@capacitor-community/file-opener';
import { AppLauncher } from '@capacitor/app-launcher';
export class AutoUpdater {
constructor(githubApiUrl) {
this.githubApiUrl = githubApiUrl;
this.currentVersion = null;
this.appInfo = null;
this.cpuArchitecture = null;
}
async initialize() {
@ -16,32 +16,23 @@ export class AutoUpdater {
this.cpuArchitecture = await this.getCPUArchitecture();
}
async removeCachedAPKs(){
const list = await Filesystem.readdir({path: './', directory: Directory.Cache})
for (const file of list.files) {
if (file.name.endsWith('.apk')) {
await Filesystem.deleteFile({path: file.name, directory: Directory.Cache})
}
}
}
async getCPUArchitecture() {
const versionMap = {'arm64-v8a': 1, 'armeabi-v7a': 2, 'x86': 3, 'universal': 4}; //5: debug
const {build} = this.appInfo;
if (build.length === 7) {
const architectureCode = parseInt(build.substring(0, 1));
console.log(architectureCode)
if (architectureCode < 5) {
for (const [arch, code] of Object.entries(versionMap)) {
if (code === architectureCode) {
return arch;
}
}
}
} else if (architectureCode === 5) {
return 'arm64-v8a';
}
}
// If version code doesn't match expected format or no match found
return 'universal';
}
@ -50,10 +41,9 @@ export class AutoUpdater {
try {
const response = await fetch(this.githubApiUrl);
const releaseInfo = await response.json();
const latestVersion = releaseInfo.tag_name.replace('v', '');
return this.isNewerVersion(latestVersion, this.currentVersion);
// return true
} catch (error) {
console.error('Error checking for update:', error);
return false;
@ -63,58 +53,34 @@ export class AutoUpdater {
isNewerVersion(latestVersion, currentVersion) {
const latest = latestVersion.split('.').map(Number);
const current = currentVersion.split('.').map(Number);
for (let i = 0; i < latest.length; i++) {
if (latest[i] > current[i]) return true;
if (latest[i] < current[i]) return false;
}
return false;
}
async downloadUpdate() {
await this.removeCachedAPKs();
async openUpdateUrl() {
try {
const response = await fetch(this.githubApiUrl);
const releaseInfo = await response.json();
const assetName = `android-Migu-${releaseInfo.tag_name}-${this.cpuArchitecture}.apk`;
const asset = releaseInfo.assets.find(a => a.name === assetName);
if (!asset) {
console.error('Update file not found', assetName);
return null;
return;
}
const fileName = `update-${releaseInfo.tag_name}.apk`;
const result = await Filesystem.downloadFile({
url: asset.browser_download_url,
path: fileName,
directory: Directory.Cache,
});
return result.path;
// Open the specific asset download URL using AppLauncher
await AppLauncher.openUrl({ url: asset.browser_download_url });
} catch (error) {
console.error('Error downloading update:', error);
return null;
}
}
async installUpdate(filePath) {
try {
await FileOpener.open({
filePath,
contentType: 'application/vnd.android.package-archive'
});
} catch (error) {
console.error('Error installing update:', error);
console.error('Error opening update URL:', error);
}
}
async performUpdate() {
const filePath = await this.downloadUpdate();
if (filePath) {
await this.installUpdate(filePath);
}
await this.openUpdateUrl();
}
}
}

View file

@ -137,7 +137,7 @@ export const defaults = {
slowSeeding: true,
disableStartupVideo: true,
amoledTheme: true,
enableAutoUpdate: true,
enableAutoUpdate: !SUPPORTS.isAndroid,
sortByEco: true,
torrentPersist: false,
torrentDHT: false,

View file

@ -44,7 +44,12 @@
Export Settings To Clipboard
</button>
{#if SUPPORTS.update}
<SettingCard title='Enable auto update' description='Enables auto updater upon startup. Automatically update to a new version of Migu if possible after exiting. Disable this if you have issues with it.'>
<SettingCard title='Enable auto update' description='Check updates upon startup. Disable this if you have issues with it.'>
{#if SUPPORTS.isAndroid}
<div class='font-weight-bold'>
<p class="pre-wrap text-muted">This way of updating was served directly from the GitHub release. If you have downloaded this app from F-Droid or IzzyOnDroid, please be aware that updating this way did not go through the additional screening process typically performed by these platforms.</p>
</div>
{/if}
<div class='custom-switch'>
<input type='checkbox' id='enable-auto-updater' bind:checked={settings.enableAutoUpdate} />
<label for='enable-auto-updater'>{settings.enableAutoUpdate ? 'On' : 'Off'}</label>

View file

@ -63,9 +63,6 @@ importers:
capacitor:
dependencies:
'@capacitor-community/file-opener':
specifier: ^6.0.0
version: 6.0.0(@capacitor/core@6.1.1)
'@capacitor-community/screen-brightness':
specifier: ^6.0.0
version: 6.0.0(@capacitor/core@6.1.1)
@ -75,6 +72,9 @@ importers:
'@capacitor/app':
specifier: ^6.0.0
version: 6.0.0(@capacitor/core@6.1.1)
'@capacitor/app-launcher':
specifier: ^6.0.2
version: 6.0.2(@capacitor/core@6.1.1)
'@capacitor/browser':
specifier: ^6.0.1
version: 6.0.1(@capacitor/core@6.1.1)
@ -84,9 +84,6 @@ importers:
'@capacitor/core':
specifier: ^6.1.1
version: 6.1.1
'@capacitor/filesystem':
specifier: ^6.0.0
version: 6.0.0(@capacitor/core@6.1.1)
'@capacitor/ios':
specifier: ^6.1.1
version: 6.1.1(@capacitor/core@6.1.1)
@ -314,12 +311,6 @@ packages:
resolution: {integrity: sha512-Ja18XcETdEl5mzzACGd+DKgaGJzPTCow7EglgwTmHdwokzDFYh/MHua6lU6DV/hjF2IaOJ4oX2nqnjG7RElKOw==}
engines: {node: '>=6.9.0'}
'@capacitor-community/file-opener@6.0.0':
resolution: {integrity: sha512-nJ9S5rCqnVDBKfqdjDhrYOIO9JLeScFkRfKLs2G+d6Df73vrJMes8dr+dGSEvKiPhyjRhICW5imDJEbzaD8KpA==}
engines: {node: '>=16.0.0', npm: '>=8.0.0'}
peerDependencies:
'@capacitor/core': ^6.0.0
'@capacitor-community/screen-brightness@6.0.0':
resolution: {integrity: sha512-8yU2Epwym7IKJ3Ae8LDlo6RDbZuo4x2B2M1oKT04kaVjWRxHzx6wETpzLJqrwix1NyqbXIx5TPPBpk0Kxmv45w==}
peerDependencies:
@ -330,6 +321,11 @@ packages:
peerDependencies:
'@capacitor/core': ^6.1.0
'@capacitor/app-launcher@6.0.2':
resolution: {integrity: sha512-g1hLHTnb7240HYcM28TBN3HbCkc6HV4242sj5/xxQyQ4N4R61DfJLpZDLtp0Wq9WkLlFCJ2B2svHU7ZvA3Tvgg==}
peerDependencies:
'@capacitor/core': ^6.0.0
'@capacitor/app@6.0.0':
resolution: {integrity: sha512-X5UGd90Jh5p9rmoPyMqFyFWqOypdJgVJhYcM5X1YyDVJJGzmJ5MuYv1+ajj5DW9Qyh+5a3th9WYptdGby8jidA==}
peerDependencies:
@ -359,11 +355,6 @@ packages:
'@capacitor/core@6.1.1':
resolution: {integrity: sha512-b1s4WDMy3Y1/owTG2/XNGCI6G5SDpF2NT1JMAgcShue13fiAuHA+Ans7Wfei6McvR0KdopAE6tMHmDAfL5PM8Q==}
'@capacitor/filesystem@6.0.0':
resolution: {integrity: sha512-GnC4CBfky7fvG9zSV/aQnZaGs6ZJ90AaQorr53z81ArTCqcrSUeBMuCxWmvti9HrdXLhBavyA1UOjvRGObOFjg==}
peerDependencies:
'@capacitor/core': ^6.0.0
'@capacitor/ios@6.1.1':
resolution: {integrity: sha512-he6+Fhj6x1dSnOzM98xaPvioOU8MNO+qpodCJwnHE3mIRonTpFutXJK8DP8fnNhjDPk2km9VafLSfOeiZXNv3Q==}
peerDependencies:
@ -5713,10 +5704,6 @@ snapshots:
dependencies:
regenerator-runtime: 0.14.1
'@capacitor-community/file-opener@6.0.0(@capacitor/core@6.1.1)':
dependencies:
'@capacitor/core': 6.1.1
'@capacitor-community/screen-brightness@6.0.0(@capacitor/core@6.1.1)':
dependencies:
'@capacitor/core': 6.1.1
@ -5725,6 +5712,10 @@ snapshots:
dependencies:
'@capacitor/core': 6.1.1
'@capacitor/app-launcher@6.0.2(@capacitor/core@6.1.1)':
dependencies:
'@capacitor/core': 6.1.1
'@capacitor/app@6.0.0(@capacitor/core@6.1.1)':
dependencies:
'@capacitor/core': 6.1.1
@ -5804,10 +5795,6 @@ snapshots:
dependencies:
tslib: 2.6.3
'@capacitor/filesystem@6.0.0(@capacitor/core@6.1.1)':
dependencies:
'@capacitor/core': 6.1.1
'@capacitor/ios@6.1.1(@capacitor/core@6.1.1)':
dependencies:
'@capacitor/core': 6.1.1