From e3d690e42d46e2bce674adbf43f6d635a0056a68 Mon Sep 17 00:00:00 2001 From: vishnuraghavb Date: Wed, 26 May 2021 21:14:50 +0530 Subject: [PATCH] implemented foreground service for timer alerts --- app/ForegroundService.js | 78 ++++++++----------- app/components/CookingTimer.vue | 73 +++++++++-------- app/components/sub/Timer.vue | 2 +- app/main.ts | 58 ++++++++++++-- .../Android/src/main/AndroidManifest.xml | 4 +- app/shared/utils.ts | 78 +++++++++++-------- package-lock.json | 36 ++++----- 7 files changed, 189 insertions(+), 140 deletions(-) diff --git a/app/ForegroundService.js b/app/ForegroundService.js index dad1177b..3f2ac675 100644 --- a/app/ForegroundService.js +++ b/app/ForegroundService.js @@ -1,51 +1,39 @@ -var ForegroundService = /** @class */ (function(_super) { - __extends(ForegroundService, _super) - function ForegroundService() { - return (_super !== null && _super.apply(this, arguments)) || this - } - ForegroundService.prototype.onStartCommand = function( - intent, - flags, - startId - ) { +import { TimerNotif } from './shared/utils' +const superProto = android.app.Service.prototype +android.app.Service.extend('com.tns.ForegroundService', { + onStartCommand: function(intent, flags, startId) { console.log('onStartCommand') - _super.prototype.onStartCommand.call(this, intent, flags, startId) + superProto.onStartCommand.call(this, intent, flags, startId) return android.app.Service.START_STICKY - } - ForegroundService.prototype.onCreate = function() { + }, + onCreate: function() { console.log('onCreate') - _super.prototype.onCreate.call(this) - this.startForeground(1, this.getNotification()) - } - ForegroundService.prototype.onBind = function(intent) { - return _super.prototype.onBind.call(this, intent) - } - ForegroundService.prototype.onUnbind = function(intent) { - return _super.prototype.onUnbind.call(this, intent) - } - ForegroundService.prototype.onDestroy = function() { + superProto.onCreate.call(this) + this.startForeground(777, this.getNotification()) + }, + onBind: function(intent) { + return superProto.onBind.call(this, intent) + }, + onUnbind: function(intent) { + return superProto.onUnbind.call(this, intent) + }, + onDestroy: function() { console.log('onDestroy') this.stopForeground(true) - } - ForegroundService.prototype.getNotification = function() { - var channel = new android.app.NotificationChannel( - 'channel_01', - 'ForegroundService Channel', - android.app.NotificationManager.IMPORTANCE_DEFAULT + }, + getNotification: function() { + return TimerNotif.getNotification( + { + bID: 'bringToFront', + cID: 'cti', + cName: 'Cooking Timer info', + description: `0 ongoing, 0 paused`, + nID: 777, + priority: -2, + sound: null, + title: 'EnRecipes is running', + }, + this.getApplicationContext() ) - var notificationManager = this.getSystemService( - android.content.Context.NOTIFICATION_SERVICE - ) - notificationManager.createNotificationChannel(channel) - var builder = new android.app.Notification.Builder( - this.getApplicationContext(), - 'channel_01' - ) - return builder.build() - } - ForegroundService = __decorate( - [JavaProxy('com.tns.ForegroundService')], - ForegroundService - ) - return ForegroundService -})(android.app.Service) + }, +}) diff --git a/app/components/CookingTimer.vue b/app/components/CookingTimer.vue index c7367ff3..1f70c6a7 100644 --- a/app/components/CookingTimer.vue +++ b/app/components/CookingTimer.vue @@ -19,7 +19,6 @@ { - if (comp == ct) clearInterval(openTimer); - else { - if (comp == "CTSettings") this.$navigateBack(); - else this.$navigateTo(CookingTimer); - comp = ct; - } - Application.off(Application.launchEvent, this.intentListener); - Application.android.off( - AndroidApplication.activityNewIntentEvent, - this.intentListener - ); - }, 250); - } + if (activeCount <= 0) this.foregroundService(false); }, fireTimer(timer) { console.log("firing"); @@ -230,18 +210,39 @@ export default { console.log(action, "firing"); EventBus.$emit(bID, action); }); + firingTimers.push(timer); + // if (firingTimers.length == 1) { + // this.$showModal(TimerReminder, { + // fullscreen: true, + // props: { + // timers: firingTimers, + // stop: this.stopFiringTimers, + // formattedTime: this.formattedTime, + // }, + // }); + // } }, - startForegroundService() { + stopFiringTimers() { + firingTimers.forEach((e) => utils.TimerNotif.clear(e.id)); + firingTimers = []; + }, + openReminder() {}, + foregroundService(bool) { const ctx = Utils.ad.getApplicationContext(); - const intent = new android.content.Intent(); - intent.setClassName(ctx, "com.tns.ForegroundService"); - ctx.startService(intent); + const intent = new android.content.Intent( + ctx, + com.tns.ForegroundService.class + ); + if (bool) + parseInt(Device.sdkVersion) < 26 + ? ctx.startService(intent) + : ctx.startForegroundService(intent); + else ctx.stopService(intent); }, // DATA HANDLERS addTimer() { - // fgs.class - this.startForegroundService(); + this.foregroundService(true); this.$showModal(TimePickerHMS, { props: { title: "ntmr", @@ -300,6 +301,7 @@ export default { if (!noUndo) { this.showUndoBar("tmrClr") .then(() => { + this.foregroundService(true); this.addActiveTimer({ timer: temp, index, @@ -385,11 +387,6 @@ export default { }, }, created() { - Application.on(Application.launchEvent, this.intentListener); - Application.android.on( - AndroidApplication.activityNewIntentEvent, - this.intentListener - ); this.clearTimerInterval(); }, }; diff --git a/app/components/sub/Timer.vue b/app/components/sub/Timer.vue index aa58df67..d1f5f912 100644 --- a/app/components/sub/Timer.vue +++ b/app/components/sub/Timer.vue @@ -4,9 +4,9 @@ columns="auto, *, auto, auto, auto" class="singleTimer" > +