2025-03-28 04:59:59 +00:00
|
|
|
/**
|
|
|
|
* Sample React Native App
|
|
|
|
* https://github.com/facebook/react-native
|
|
|
|
*
|
2025-03-31 05:11:57 +00:00
|
|
|
* Generated with the TypeScript template
|
|
|
|
* https://github.com/react-native-community/react-native-template-typescript
|
|
|
|
*
|
2025-03-28 04:59:59 +00:00
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
|
2025-04-24 06:23:45 +00:00
|
|
|
import React, {useEffect} from 'react';
|
2025-03-28 04:59:59 +00:00
|
|
|
import {
|
|
|
|
SafeAreaView,
|
|
|
|
StatusBar,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
View,
|
2025-03-31 05:11:57 +00:00
|
|
|
Switch,
|
|
|
|
Button,
|
|
|
|
PermissionsAndroid,
|
|
|
|
// ToastAndroid,
|
2025-04-24 06:23:45 +00:00
|
|
|
// NativeModules,
|
2025-03-28 04:59:59 +00:00
|
|
|
} from 'react-native';
|
|
|
|
|
2025-03-31 05:11:57 +00:00
|
|
|
import Geolocation, {
|
|
|
|
GeolocationError,
|
2025-04-24 06:23:45 +00:00
|
|
|
// GeolocationResponse,
|
2025-03-31 05:11:57 +00:00
|
|
|
} from '@react-native-community/geolocation';
|
|
|
|
|
2025-04-24 06:23:45 +00:00
|
|
|
import NativeTaskRunner from './specs/NativeTaskRunner';
|
|
|
|
|
|
|
|
// const {ForegroundHeadlessModule} = NativeModules;
|
2025-03-31 05:11:57 +00:00
|
|
|
|
|
|
|
const Colors = {
|
|
|
|
gold: '#fedd1e',
|
|
|
|
black: '#000',
|
|
|
|
white: '#fff',
|
|
|
|
lightGrey: '#ccc',
|
|
|
|
blue: '#337AB7',
|
|
|
|
brick: '#973920',
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Util class for handling fetch-event peristence in AsyncStorage.
|
2025-04-24 06:23:45 +00:00
|
|
|
// import Event from './src/Event';
|
2025-03-31 05:11:57 +00:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
2025-04-24 06:23:45 +00:00
|
|
|
import * as Location from 'expo-location';
|
2025-03-31 05:11:57 +00:00
|
|
|
|
|
|
|
Geolocation.setRNConfiguration({
|
|
|
|
skipPermissionRequests: true,
|
|
|
|
locationProvider: 'auto',
|
|
|
|
});
|
|
|
|
|
2025-04-24 06:23:45 +00:00
|
|
|
/*
|
|
|
|
function geoLocationPromise(): Promise<GeolocationResponse | GeolocationError> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Geolocation.getCurrentPosition(
|
|
|
|
info => {
|
|
|
|
console.log(info);
|
|
|
|
resolve(info);
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
reject(error);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
timeout: 20000,
|
|
|
|
maximumAge: 0,
|
|
|
|
// enableHighAccuracy: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
2025-03-31 05:11:57 +00:00
|
|
|
const requestPermissions = async () => {
|
|
|
|
try {
|
|
|
|
const granted = await PermissionsAndroid.request(
|
|
|
|
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
|
|
|
|
{
|
|
|
|
title: 'Need permission to access location',
|
|
|
|
message:
|
|
|
|
'Location access is needed ' + 'so we can track your location!',
|
|
|
|
buttonNegative: 'Cancel',
|
|
|
|
buttonPositive: 'OK',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const granted2 = await PermissionsAndroid.request(
|
|
|
|
PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION,
|
|
|
|
{
|
|
|
|
title: 'Need permission to access location',
|
|
|
|
message:
|
|
|
|
'Location access is needed ' + 'so we can track your location!',
|
|
|
|
buttonNegative: 'Cancel',
|
|
|
|
buttonPositive: 'OK',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
granted === PermissionsAndroid.RESULTS.GRANTED &&
|
|
|
|
granted2 === PermissionsAndroid.RESULTS.GRANTED
|
|
|
|
) {
|
|
|
|
console.log('You can use the location');
|
|
|
|
} else {
|
|
|
|
console.log('Location permission denied');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
const [enabled, setEnabled] = React.useState(false);
|
|
|
|
|
2025-04-24 06:23:45 +00:00
|
|
|
useEffect(() => {
|
|
|
|
async function getCurrentLocation() {
|
|
|
|
let location = await Location.getLastKnownPositionAsync({});
|
|
|
|
console.log(location);
|
2025-03-31 05:11:57 +00:00
|
|
|
}
|
2025-04-24 06:23:45 +00:00
|
|
|
async function xy() {
|
|
|
|
try {
|
|
|
|
await requestPermissions();
|
|
|
|
const x = await AsyncStorage.getItem('locationtask');
|
|
|
|
if (x === '1') {
|
|
|
|
setEnabled(true);
|
|
|
|
} else {
|
|
|
|
setEnabled(false);
|
2025-03-31 05:11:57 +00:00
|
|
|
}
|
|
|
|
|
2025-04-24 06:23:45 +00:00
|
|
|
getCurrentLocation();
|
|
|
|
} catch (error) {
|
|
|
|
console.log('Something went wrong', error);
|
2025-03-31 05:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2025-04-24 06:23:45 +00:00
|
|
|
|
|
|
|
xy();
|
|
|
|
}, []);
|
2025-03-31 05:11:57 +00:00
|
|
|
|
|
|
|
async function onLocPress(): Promise<void> {
|
|
|
|
try {
|
|
|
|
// const locInfo = (await geoLocationPromise()) as GeolocationResponse;
|
|
|
|
// ToastAndroid.show(
|
|
|
|
// `lat-${locInfo.coords.latitude};long-${locInfo.coords.longitude}`,
|
|
|
|
// 5000,
|
|
|
|
// );
|
2025-04-24 06:23:45 +00:00
|
|
|
if (enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// ForegroundHeadlessModule.startService();
|
|
|
|
NativeTaskRunner.startService();
|
|
|
|
setEnabled(true);
|
|
|
|
AsyncStorage.setItem('locationtask', '1');
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2025-03-31 05:11:57 +00:00
|
|
|
console.log('Did it run?');
|
|
|
|
} catch (error) {
|
|
|
|
const g = error as GeolocationError;
|
|
|
|
if (g.message) {
|
|
|
|
console.log(g.message);
|
|
|
|
} else {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLocStop() {
|
2025-04-24 06:23:45 +00:00
|
|
|
try {
|
|
|
|
// ForegroundHeadlessModule.stopService();
|
|
|
|
NativeTaskRunner.stopService();
|
|
|
|
setEnabled(false);
|
|
|
|
AsyncStorage.setItem('locationtask', '0');
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2025-03-31 05:11:57 +00:00
|
|
|
}
|
|
|
|
|
2025-03-28 04:59:59 +00:00
|
|
|
return (
|
2025-03-31 05:11:57 +00:00
|
|
|
<SafeAreaView style={{flex: 1, backgroundColor: Colors.gold}}>
|
|
|
|
<StatusBar barStyle={'light-content'}></StatusBar>
|
|
|
|
<View style={styles.container}>
|
|
|
|
<View style={styles.toolbar}>
|
|
|
|
<Text style={styles.title}>BGFetch Demo</Text>
|
|
|
|
<Button title="Loc" onPress={onLocPress} />
|
|
|
|
<Button title="Stop" onPress={onLocStop} />
|
2025-03-28 04:59:59 +00:00
|
|
|
</View>
|
2025-03-31 05:11:57 +00:00
|
|
|
<View style={styles.toolbar}>
|
2025-04-24 06:23:45 +00:00
|
|
|
<Switch value={enabled} />
|
2025-03-31 05:11:57 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
2025-03-28 04:59:59 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
2025-03-31 05:11:57 +00:00
|
|
|
};
|
2025-03-28 04:59:59 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2025-03-31 05:11:57 +00:00
|
|
|
container: {
|
|
|
|
flexDirection: 'column',
|
|
|
|
flex: 1,
|
2025-03-28 04:59:59 +00:00
|
|
|
},
|
2025-03-31 05:11:57 +00:00
|
|
|
title: {
|
2025-03-28 04:59:59 +00:00
|
|
|
fontSize: 24,
|
2025-03-31 05:11:57 +00:00
|
|
|
flex: 1,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
color: Colors.black,
|
|
|
|
},
|
|
|
|
eventList: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
},
|
|
|
|
event: {
|
|
|
|
padding: 10,
|
|
|
|
borderBottomWidth: 1,
|
|
|
|
borderColor: Colors.lightGrey,
|
|
|
|
},
|
|
|
|
taskId: {
|
|
|
|
color: Colors.blue,
|
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
},
|
|
|
|
headless: {
|
|
|
|
fontWeight: 'bold',
|
|
|
|
},
|
|
|
|
remark: {
|
|
|
|
color: Colors.brick,
|
2025-03-28 04:59:59 +00:00
|
|
|
},
|
2025-03-31 05:11:57 +00:00
|
|
|
timestamp: {
|
|
|
|
color: Colors.black,
|
2025-03-28 04:59:59 +00:00
|
|
|
},
|
2025-03-31 05:11:57 +00:00
|
|
|
toolbar: {
|
|
|
|
height: 57,
|
|
|
|
flexDirection: 'row',
|
|
|
|
paddingLeft: 10,
|
|
|
|
paddingRight: 10,
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: Colors.gold,
|
2025-03-28 04:59:59 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default App;
|