From a8302d4d2ea6d077051496e0dd9bde5c53765a18 Mon Sep 17 00:00:00 2001 From: vishnuraghavb Date: Sun, 28 Feb 2021 23:04:27 +0530 Subject: [PATCH] fixed crash with blocked sensor --- app/components/EnRecipes.vue | 9 ++++++-- app/components/Settings.vue | 10 ++++++--- app/shared/utils.js | 43 ++++++++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/app/components/EnRecipes.vue b/app/components/EnRecipes.vue index 82acee80..5b6b9622 100644 --- a/app/components/EnRecipes.vue +++ b/app/components/EnRecipes.vue @@ -347,6 +347,7 @@ import EditRecipe from "./EditRecipe.vue"; import ViewRecipe from "./ViewRecipe.vue"; import ActionDialog from "./modal/ActionDialog.vue"; import ConfirmDialog from "./modal/ConfirmDialog.vue"; +import * as utils from "~/shared/utils.js"; let lastTime = 0; let lastShake = 0; let lastForce = 0; @@ -465,6 +466,7 @@ export default { "setSortTypeAction", "deleteRecipeAction", "deleteRecipesAction", + "setShakeAction", ]), onPageLoad(args) { const page = args.object; @@ -477,8 +479,11 @@ export default { ? this.setComponent("Filtered recipes") : this.setComponent("EnRecipes"); if (!this.selectMode) this.showFAB = true; - if (this.shakeEnabled) - startAccelerometerUpdates((data) => this.onSensorData(data)); + if (this.shakeEnabled) { + if (utils.hasAccelerometer()) + startAccelerometerUpdates((data) => this.onSensorData(data)); + else this.setShakeAction(false); + } if (this.showSearch || this.selectMode) this.hijackLocalBackEvent(); this.showDrawer(); this.closeDrawer(); diff --git a/app/components/Settings.vue b/app/components/Settings.vue index 17db9532..b38f075e 100644 --- a/app/components/Settings.vue +++ b/app/components/Settings.vue @@ -397,9 +397,13 @@ export default { // SHAKE VIEW RANDOM RECIPE toggleShake(args) { let checked = args.object.checked; - // let checked = !this.shakeEnabled - ApplicationSettings.setBoolean("shakeEnabled", checked); - this.setShakeAction(checked); + if (checked && !utils.hasAccelerometer()) { + args.object.checked = false; + Toast.makeText(localize("noAccSensor"), "long").show(); + } else { + ApplicationSettings.setBoolean("shakeEnabled", checked); + this.setShakeAction(checked); + } }, // EXPORT HANDLERS exportCheck() { diff --git a/app/shared/utils.js b/app/shared/utils.js index eab82aea..ba2e7194 100644 --- a/app/shared/utils.js +++ b/app/shared/utils.js @@ -1,15 +1,44 @@ -import {Application} from "@nativescript/core" +import { Application, Utils } from '@nativescript/core' export const restartApp = () => { - const mStartActivity = new android.content.Intent(Application.android.context, Application.android.startActivity.getClass()) + const mStartActivity = new android.content.Intent( + Application.android.context, + Application.android.startActivity.getClass() + ) const mPendingIntentId = parseInt(Math.random() * 100000, 10) - const mPendingIntent = android.app.PendingIntent.getActivity(Application.android.context, mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT) - const mgr = Application.android.context.getSystemService(android.content.Context.ALARM_SERVICE) - mgr.set(android.app.AlarmManager.RTC, java.lang.System.currentTimeMillis() + 100, mPendingIntent) + const mPendingIntent = android.app.PendingIntent.getActivity( + Application.android.context, + mPendingIntentId, + mStartActivity, + android.app.PendingIntent.FLAG_CANCEL_CURRENT + ) + const mgr = Application.android.context.getSystemService( + android.content.Context.ALARM_SERVICE + ) + mgr.set( + android.app.AlarmManager.RTC, + java.lang.System.currentTimeMillis() + 100, + mPendingIntent + ) android.os.Process.killProcess(android.os.Process.myPid()) } export const openAppSettingsPage = () => { - const intent = new android.content.Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + const intent = new android.content.Intent( + android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS + ) intent.addCategory(android.content.Intent.CATEGORY_DEFAULT) - intent.setData(android.net.Uri.parse("package:" + Application.android.context.getPackageName())) + intent.setData( + android.net.Uri.parse( + 'package:' + Application.android.context.getPackageName() + ) + ) Application.android.foregroundActivity.startActivity(intent) } +export const hasAccelerometer = () => { + let context = Utils.ad.getApplicationContext() + let sensorManager = context.getSystemService( + android.content.Context.SENSOR_SERVICE + ) + return sensorManager.getDefaultSensor( + android.hardware.Sensor.TYPE_ACCELEROMETER + ) +}