From 151e63a5348b579750dc576998f1d0b3cb94be69 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 12 Sep 2023 16:37:41 +0200 Subject: [PATCH 1/4] feat(checkstate): add a method to check the location state --- android/build.gradle | 1 + android/gradle.properties | 2 +- .../AndroidLocationEnablerModule.kt | 12 ++++++++++++ example/src/App.tsx | 14 ++++++++++++-- src/index.tsx | 4 ++++ 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 882e81c..a74704f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -98,6 +98,7 @@ dependencies { implementation "com.facebook.react:react-android:+" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "com.google.android.gms:play-services-location:21.0.1" + implementation 'androidx.appcompat:appcompat:1.6.1' implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0') } diff --git a/android/gradle.properties b/android/gradle.properties index 38a9920..50ee582 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,4 +1,4 @@ -AndroidLocationEnabler_kotlinVersion=1.8.10 +AndroidLocationEnabler_kotlinVersion=1.9.0 AndroidLocationEnabler_minSdkVersion=21 AndroidLocationEnabler_targetSdkVersion=31 AndroidLocationEnabler_compileSdkVersion=31 diff --git a/android/src/main/java/com/heanoria/library/reactnative/locationenabler/AndroidLocationEnablerModule.kt b/android/src/main/java/com/heanoria/library/reactnative/locationenabler/AndroidLocationEnablerModule.kt index cfe404d..00d8839 100644 --- a/android/src/main/java/com/heanoria/library/reactnative/locationenabler/AndroidLocationEnablerModule.kt +++ b/android/src/main/java/com/heanoria/library/reactnative/locationenabler/AndroidLocationEnablerModule.kt @@ -7,6 +7,7 @@ import android.content.Intent import android.content.IntentSender import android.location.LocationManager import android.util.Log +import androidx.core.location.LocationManagerCompat import com.facebook.react.bridge.ActivityEventListener import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext @@ -40,6 +41,17 @@ class AndroidLocationEnablerModule(reactContext: ReactApplicationContext) : return NAME } + @ReactMethod + fun isLocationEnabled(promise: Promise) { + if (currentActivity == null) return + + val locationManager = this.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager + + val isLocationEnabled = LocationManagerCompat.isLocationEnabled(locationManager) + + promise.resolve(isLocationEnabled) + } + @ReactMethod fun promptForEnableLocationIfNeeded(params: ReadableMap?, promise: Promise) { if (currentActivity == null) return diff --git a/example/src/App.tsx b/example/src/App.tsx index 29759dd..17aded6 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,10 +1,11 @@ import * as React from 'react'; import { StyleSheet, View, Text, Button } from 'react-native'; -import { promptForEnableLocationIfNeeded } from 'react-native-android-location-enabler'; +import { isLocationEnabled, promptForEnableLocationIfNeeded } from 'react-native-android-location-enabler'; export default function App() { const [result, setResult] = React.useState(); + const [enabled, setEnabled] = React.useState() async function handleEnabledPressed() { try { @@ -18,10 +19,18 @@ export default function App() { } } + async function handleCheckPressed() { + const checkEnabled = await isLocationEnabled(); + console.log('checkEnabled', checkEnabled) + setEnabled(checkEnabled) + } + return ( - {result && Result: {result}} +