diff --git a/.gitignore b/.gitignore index 25a707b0..e3be8f6a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ # Build output **/dist/ **/build/ -**/android/ # Dependencies node_modules diff --git a/capacitor/.gitignore b/capacitor/.gitignore index a1c47b76..10421520 100644 --- a/capacitor/.gitignore +++ b/capacitor/.gitignore @@ -1,7 +1,9 @@ /node_modules/ android/* +!android/variables.gradle !android/app android/app/* +!android/app/build.gradle !android/app/src android/app/src/* !android/app/src/main diff --git a/capacitor/android/app/build.gradle b/capacitor/android/app/build.gradle new file mode 100644 index 00000000..2294d04f --- /dev/null +++ b/capacitor/android/app/build.gradle @@ -0,0 +1,65 @@ +apply plugin: 'com.android.application' + +def verCode = 0 + +def jsonFile = file('../../../electron/package.json') +def parsedJson = new groovy.json.JsonSlurper().parseText(jsonFile.text) +def verName = parsedJson.version +def versions = verName.tokenize('.') + +versions.each (code) -> { + verCode = (verCode*100) + Integer.parseInt(code) +} + +android { + namespace "watch.miru" + compileSdkVersion rootProject.ext.compileSdkVersion + defaultConfig { + applicationId "watch.miru" + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode verCode + versionName verName + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + aaptOptions { + // Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps. + // Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61 + ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~' + } + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +repositories { + flatDir{ + dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs' + } +} + +dependencies { + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" + implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion" + implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion" + implementation project(':capacitor-android') + testImplementation "junit:junit:$junitVersion" + androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" + implementation project(':capacitor-cordova-android-plugins') +} + +apply from: 'capacitor.build.gradle' + +try { + def servicesJSON = file('google-services.json') + if (servicesJSON.text) { + apply plugin: 'com.google.gms.google-services' + } +} catch(Exception e) { + logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work") +} diff --git a/capacitor/android/variables.gradle b/capacitor/android/variables.gradle new file mode 100644 index 00000000..5946adab --- /dev/null +++ b/capacitor/android/variables.gradle @@ -0,0 +1,16 @@ +ext { + minSdkVersion = 22 + compileSdkVersion = 33 + targetSdkVersion = 33 + androidxActivityVersion = '1.7.0' + androidxAppCompatVersion = '1.6.1' + androidxCoordinatorLayoutVersion = '1.2.0' + androidxCoreVersion = '1.10.0' + androidxFragmentVersion = '1.5.6' + coreSplashScreenVersion = '1.0.0' + androidxWebkitVersion = '1.6.1' + junitVersion = '4.13.2' + androidxJunitVersion = '1.1.5' + androidxEspressoCoreVersion = '3.5.1' + cordovaAndroidVersion = '10.1.1' +} \ No newline at end of file diff --git a/capacitor/package.json b/capacitor/package.json index c2daef3b..29641801 100644 --- a/capacitor/package.json +++ b/capacitor/package.json @@ -31,6 +31,7 @@ "@capacitor/cli": "^5.5.1", "@capacitor/core": "^5.5.1", "@capacitor/ios": "^5.5.1", + "@capacitor/local-notifications": "5.0.8", "@capacitor/status-bar": "^5.0.6", "capacitor-nodejs": "https://github.com/funniray/Capacitor-NodeJS/releases/download/nodejs-18/capacitor-nodejs-1.0.0-beta.6.tgz", "capacitor-plugin-safe-area": "^2.0.5", diff --git a/capacitor/src/capacitor.js b/capacitor/src/capacitor.js index 57a34e48..1afb1f06 100644 --- a/capacitor/src/capacitor.js +++ b/capacitor/src/capacitor.js @@ -3,12 +3,43 @@ import { StatusBar, Style } from '@capacitor/status-bar' import { SafeArea } from 'capacitor-plugin-safe-area' import { App } from '@capacitor/app' import { Browser } from '@capacitor/browser' +import { LocalNotifications } from '@capacitor/local-notifications' import IPC from './ipc.js' IPC.on('open', url => Browser.open({ url })) App.addListener('appUrlOpen', ({ url }) => handleProtocol(url)) +let canShowNotifications = false + +LocalNotifications.checkPermissions().then(async value => { + if (value) { + try { + await LocalNotifications.requestPermissions() + canShowNotifications = true + } catch (e) { + console.error(e) + } + } +}) + +let id = 0 +IPC.on('notification', noti => { + /** @type {import('@capacitor/local-notifications').LocalNotificationSchema} */ + const notification = { + title: noti.title, + body: noti.body, + id: id++, + attachments: [ + { + id: '' + id++, + url: noti.icon + } + ] + } + if (canShowNotifications) LocalNotifications.schedule({ notifications: [notification] }) +}) + // schema: miru://key/value const protocolMap = { auth: token => sendToken(token), diff --git a/capacitor/src/ipc.js b/capacitor/src/ipc.js index 08461d62..8e9a0c18 100644 --- a/capacitor/src/ipc.js +++ b/capacitor/src/ipc.js @@ -1,3 +1,4 @@ +import { App } from '@capacitor/app' import { NodeJS } from 'capacitor-nodejs' import EventEmitter from 'events' @@ -25,6 +26,10 @@ const [_platform, arch] = navigator.platform.split(' ') globalThis.version = { platform: globalThis.cordova?.platformId, - arch, - version: globalThis.cordova?.version + arch } + +main.once('version', async () => { + const { version } = await App.getInfo() + main.emit('version', version) +}) diff --git a/common/components/Menubar.svelte b/common/components/Menubar.svelte index 766372d8..ba8ff306 100644 --- a/common/components/Menubar.svelte +++ b/common/components/Menubar.svelte @@ -13,12 +13,12 @@