Skip to content

Commit 7995215

Browse files
committed
fix: new screen off auto refresh featurefor Hisense A9
1 parent dd6ad7a commit 7995215

8 files changed

Lines changed: 130 additions & 3 deletions

File tree

App_Resources/Android/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158
<meta-data android:name="io.sentry.breadcrumbs.system-events" android:value="false" />
159159
<meta-data android:name="io.sentry.breadcrumbs.app-components" android:value="false" />
160160
<meta-data android:name="io.sentry.breadcrumbs.user-interaction" android:value="false" />
161+
<receiver android:name=".RefreshAlarmReceiver" android:enabled="true" android:exported="false">
162+
</receiver>
161163
</application>
162164
<queries>
163165
<package android:name="com.akylas.weather" />

app.webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = (env, params = {}) => {
139139
env.appPath = appPath;
140140
env.appResourcesPath = appResourcesPath;
141141
env.appComponents = env.appComponents || [];
142-
env.appComponents.push('~/services/android/BgService', '~/services/android/BgServiceBinder', '~/android/processtextactivity', '~/android/activity.android');
142+
env.appComponents.push('~/services/android/BgService', '~/services/android/BgServiceBinder', '~/android/processtextactivity', '~/android/activity.android', '~/android/refresh_alarm_receiver');
143143

144144
const ignoredSvelteWarnings = new Set(['a11y-no-onchange', 'a11y-label-has-associated-control', 'illegal-attribute-character']);
145145

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { scheduleRefreshAlarm } from '~/utils/utils';
2+
3+
@JavaProxy('__PACKAGE__.RefreshAlarmReceiver')
4+
@NativeClass
5+
export class RefreshAlarmReceiver extends android.content.BroadcastReceiver {
6+
async onReceive(context: android.content.Context, intent: android.content.Intent) {
7+
DEV_LOG && console.log("AlarmReceiver", "Alarm triggered!")
8+
9+
const broadcastIntent = android.content.Intent(ApplicationSettings.getString('refreshAlarmBroadcast', "com.akylas.A9_REFRESH_SCREEN"));
10+
context.sendBroadcast(broadcastIntent);
11+
12+
scheduleRefreshAlarm();
13+
}
14+
}

app/components/map/Map.svelte

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import { clearTimeout, disableShowWhenLockedAndTurnScreenOn, enableShowWhenLockedAndTurnScreenOn, setTimeout } from '~/utils/utils';
8585
import { colors, screenHeightDips, screenWidthDips, windowInset } from '../../variables';
8686
import MapResultPager from '../search/MapResultPager.svelte';
87+
import { startRefreshAlarm, stopRefreshAlarm } from '~/utils/utils';
8788
8889
$: ({ colorBackground, colorError, colorPrimary } = $colors);
8990
$: ({ bottom: windowInsetBottom, left: windowInsetLeft, right: windowInsetRight, top: windowInsetTop } = $windowInset);
@@ -645,6 +646,46 @@
645646
}, 500);
646647
647648
let appUrlRegistered = false;
649+
let screenOnOffReceiver = null;
650+
function registerScreenOnOff(){
651+
if (__ANDROID__) {
652+
if (!screenOnOffReceiver) {
653+
screenOnOffReceiver = new android.content.BroadcastReceiver({
654+
onReceive: () => {
655+
const action = intent.getAction();
656+
DEV_LOG && console.log('screenOnOffReceiver', action);
657+
import { startRefreshAlarm, stopRefreshAlarm } from '~/utils/utils';
658+
if (action === android.content.Intent.ACTION_SCREEN_ON) {
659+
DEV_LOG && console.log("Screen ON");
660+
stopRefreshAlarm();
661+
} else if (action === android.content.Intent.ACTION_SCREEN_OFF) {
662+
DEV_LOG && console.log("Screen OFF");
663+
startRefreshAlarm();
664+
}
665+
}
666+
});
667+
668+
// Create an IntentFilter to listen for screen on/off events
669+
const filter = new android.content.IntentFilter();
670+
filter.addAction(android.content.Intent.ACTION_SCREEN_ON);
671+
filter.addAction(android.content.Intent.ACTION_SCREEN_OFF);
672+
673+
// Register the receiver
674+
const context = Utils.android.getApplicationContext();
675+
context.registerReceiver(screenOnOffReceiver, filter);
676+
}
677+
}
678+
}
679+
function unregisterScreenOnOff(){
680+
if (__ANDROID__) {
681+
if (screenOnOffReceiver) {
682+
683+
const context = Utils.android.getApplicationContext();
684+
context.unregisterReceiver(screenOnOffReceiver);
685+
screenOnOffReceiver = null;
686+
}
687+
}
688+
}
648689
async function onMainMapReady(e) {
649690
try {
650691
// if (!PRODUCTION) {
@@ -705,6 +746,7 @@
705746
});
706747
}
707748
}
749+
registerScreenOnOff();
708750
} catch (error) {
709751
console.error(error, error.stack);
710752
}
@@ -1802,6 +1844,7 @@
18021844
// });
18031845
onDestroy(() => {
18041846
webserver?.stop();
1847+
unregisterScreenOnOff();
18051848
});
18061849
const showMapMenu = tryCatchFunction(
18071850
async (event) => {
@@ -2110,7 +2153,23 @@
21102153
isSelected: showOnLockscreen,
21112154
tooltip: lc('show_screen_lock'),
21122155
visible: __ANDROID__,
2113-
onTap: switchShowOnLockscreen
2156+
onTap: switchShowOnLockscreen,
2157+
onLongPress: tryCatchFunction(async (event, button) => {
2158+
await showSliderPopover({
2159+
debounceDuration: 100,
2160+
anchor: event.object,
2161+
vertPos: VerticalPosition.BELOW,
2162+
value: ApplicationSettings.getNumber('refreshAlarmInterval', 60 * 1000);,
2163+
onChange(value) {
2164+
ApplicationSettings.setNumber('refreshAlarmInterval', value);
2165+
},
2166+
step: 100,
2167+
min: 0,
2168+
max: 10 * 60 * 1000
2169+
title: lc('refresh_alarm_interval')
2170+
});
2171+
2172+
})
21142173
}
21152174
];
21162175
if ((WITH_BUS_SUPPORT && customLayersModule?.devMode) || customLayersModule?.hasLocalData) {

app/utils/utils/index.android.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from './index.common';
22
export { gClearInterval as clearInterval, gClearTimeout as clearTimeout, gSetInterval as setInterval, gSetTimeout as setTimeout };
33
import { lc } from '@nativescript-community/l';
44
import { request } from '@nativescript-community/perms';
5-
import { Application, Color, Device, File, Frame, Utils, View, path } from '@nativescript/core';
5+
import { Application, ApplicationSettings, Color, Device, File, Frame, Utils, View, path } from '@nativescript/core';
66
import { AndroidActivityResultEventData } from '@nativescript/core/application';
77
import { Dayjs } from 'dayjs';
88
import { clock_24 } from '~/helpers/locale';
@@ -243,6 +243,47 @@ export function moveFileOrFolder(sourceLocationPath: string, targetLocationPath:
243243
}
244244
}
245245

246+
export function startRefreshAlarm() {
247+
const enabled = ApplicationSettings.getBoolean('refreshAlarmEnabled', false);
248+
const interval = ApplicationSettings.getNumber('refreshAlarmInterval', 0);
249+
DEV_LOG && console.log("startRefreshAlarm", enabled, interval);
250+
if (!enabled && interval > 0) {
251+
ApplicationSettings.setBoolean('refreshAlarmEnabled', true);
252+
scheduleRefreshAlarm();
253+
}
254+
}
255+
export function stopRefreshAlarm() {
256+
const enabled = ApplicationSettings.getBoolean('refreshAlarmEnabled', false);
257+
DEV_LOG && console.log("stopRefreshAlarm", enabled);
258+
if (enabled) {
259+
ApplicationSettings.setBoolean('refreshAlarmEnabled', false);
260+
}
261+
}
262+
export function scheduleRefreshAlarm(){
263+
const enabled = ApplicationSettings.getBoolean('refreshAlarmEnabled', false);
264+
DEV_LOG && console.log("scheduleRefreshAlarm", enabled);
265+
if (enabled) {
266+
const context = Utils.android.getApplicationContext();
267+
// Reschedule the alarm
268+
const alarmManager = context.getSystemService(android.content.Context.ALARM_SERVICE) as android.app.AlarmManager;
269+
const triggerAtMillis = android.os.System.currentTimeMillis() + ApplicationSettings.getNumber('refreshAlarmInterval', 60 * 1000); // 15 minutes from now
270+
271+
const pendingIntent = android.content.PendingIntent.getBroadcast(
272+
context,
273+
0,
274+
new android.content.Intent(context, java.lang.Class.forName(__APP_ID__ + '.RefreshAlarmReceiver')),
275+
android.content.PendingIntent.FLAG_UPDATE_CURRENT or android.content.PendingIntent.FLAG_IMMUTABLE
276+
);
277+
278+
// Reschedule using setExactAndAllowWhileIdle
279+
alarmManager.setExactAndAllowWhileIdle(
280+
android.app.AlarmManager.RTC_WAKEUP,
281+
triggerAtMillis,
282+
pendingIntent
283+
);
284+
}
285+
}
286+
246287
const gSetTimeout = setTimeout;
247288
const gSetInterval = setInterval;
248289
const gClearTimeout = clearTimeout;

app/utils/utils/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ export async function pickTime(currentDate: Dayjs): Promise<[number, number]>;
33
export async function pickDate(currentDate: Dayjs): Promise<number>;
44
export function moveFileOrFolder(sourceLocationPath: string, targetLocationPath: string, androidTargetLocationPath?: string);
55
export function restartApp();
6+
export function startRefreshAlarm();
7+
export function stopRefreshAlarm();
8+
export function scheduleRefreshAlarm();

app/utils/utils/index.ios.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ export function moveFileOrFolder(sourceLocationPath: string, targetLocationPath:
9595
}
9696
}
9797

98+
export function startRefreshAlarm() {
99+
}
100+
export function stopRefreshAlarm() {
101+
}
102+
export function scheduleRefreshAlarm(){
103+
}
104+
98105
export const setTimeout = global.__ns__setTimeout;
99106
export const setInterval = global.__ns__setInterval;
100107
export const clearTimeout = global.__ns__clearTimeout;

typings/android.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare namespace akylas {
1212
class VectorTileEventListener extends com.akylas.carto.additions.AKVectorTileEventListener {}
1313
class BgService extends globalAndroid.app.Service {}
1414
class BgServiceBinder extends globalAndroid.os.Binder {}
15+
class RefreshAlarmReceiver extends globalAndroid.content.BroadcastReceiver {}
1516
}
1617
}
1718
}

0 commit comments

Comments
 (0)