Skip to content

Commit

Permalink
Add missing notifications for Location Services (Path-Check#64)
Browse files Browse the repository at this point in the history
* Add missing notifications for Location Services

Several conditions cause Private Kit to stop failing when it cannot access
GPS info.  We should notify the user when this has happened.  That includes:
* The app doesn't have proper permissions
* The permissions have been revoked
* The location service has been shut down

Still to do is a notification to show the user that the background service
is not doing anything when the user turns off location services.

* Fix auto-format errors

The automatic formatting done in "Javascript" style instead of
"React Native"

* use 5 minute time interval

Co-authored-by: Abhishek Singh <[email protected]>
  • Loading branch information
2 people authored and vitorpamplona committed Mar 20, 2020
1 parent ccb28d9 commit 67a4a41
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
57 changes: 47 additions & 10 deletions app/services/LocationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SetStoreData
} from '../helpers/General';
import BackgroundGeolocation from '@mauron85/react-native-background-geolocation';
import { Alert } from 'react-native';

var instanceCount = 0;
var lastPointCount = 0;
Expand Down Expand Up @@ -64,17 +65,17 @@ export default class LocationServices {
desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
stationaryRadius: 50,
distanceFilter: 50,
notificationTitle: 'PrivateKit Enabled',
notificationText: 'PrivateKit is recording path information on this device.',
notificationTitle: 'Private Kit Enabled',
notificationText: 'Private Kit is securely storing your GPS coordinates once every five minutes on this device.',
debug: false, // when true, it beeps every time a loc is read
startOnBoot: false,
stopOnTerminate: false,
locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,

// DEBUG: Use these to get a faster output
// interval: 2000,
// fastestInterval: 2000, // Time (in milliseconds) between location information polls. E.g. 60000*5 = 5 minutes
// activitiesInterval: 2000,
/*interval: 2000,
fastestInterval: 2000, // Time (in milliseconds) between location information polls. E.g. 60000*5 = 5 minutes
activitiesInterval: 2000,*/

interval: 20000,
fastestInterval: 60000 * 5, // Time (in milliseconds) between location information polls. E.g. 60000*5 = 5 minutes
Expand Down Expand Up @@ -120,7 +121,7 @@ export default class LocationServices {
BackgroundGeolocation.on('stationary', (stationaryLocation) => {
// handle stationary locations here
// Actions.sendLocation(stationaryLocation);
BackgroundGeolocation.startTask(taskKey => {
BackgroundGeolocation.startTask(taskKey => {
// execute long running task
// eg. ajax post location
// IMPORTANT: task has to be ended by endTask
Expand All @@ -144,10 +145,11 @@ export default class LocationServices {

BackgroundGeolocation.on('authorization', (status) => {
console.log('[INFO] BackgroundGeolocation authorization status: ' + status);

if (status !== BackgroundGeolocation.AUTHORIZED) {
// we need to set delay or otherwise alert may not be shown
setTimeout(() =>
Alert.alert('App requires location tracking permission', 'Would you like to open app settings?', [{
Alert.alert('Private Kit requires access to location information', 'Would you like to open app settings?', [{
text: 'Yes',
onPress: () => BackgroundGeolocation.showAppSettings()
},
Expand All @@ -158,6 +160,12 @@ export default class LocationServices {
}
]), 1000);
}
else {
BackgroundGeolocation.start(); //triggers start on start event

// TODO: We reach this point on Android when location services are toggled off/on.
// When this fires, check if they are off and show a Notification in the tray
}
});

BackgroundGeolocation.on('background', () => {
Expand Down Expand Up @@ -195,10 +203,39 @@ export default class LocationServices {
console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled);
console.log('[INFO] BackgroundGeolocation auth status: ' + status.authorization);

// you don't need to check status before start (this is just the example)
if (!status.isRunning) {
BackgroundGeolocation.start(); //triggers start on start event
BackgroundGeolocation.start(); //triggers start on start event

if (!status.locationServicesEnabled) {
// we need to set delay or otherwise alert may not be shown
setTimeout(() =>
Alert.alert('Private Kit requires location services to be enabled', 'Would you like to open location settings?', [{
text: 'Yes',
onPress: () => BackgroundGeolocation.showLocationSettings()
},
{
text: 'No',
onPress: () => console.log('No Pressed'),
style: 'cancel'
}
]), 1000);
}
else if (!status.authorization) {
// we need to set delay or otherwise alert may not be shown
setTimeout(() =>
Alert.alert('Private Kit requires access to location information', 'Would you like to open app settings?', [{
text: 'Yes',
onPress: () => BackgroundGeolocation.showAppSettings()
},
{
text: 'No',
onPress: () => console.log('No Pressed'),
style: 'cancel'
}
]), 1000);
}
else if (!status.isRunning) {
}

});

// you can also just start without checking for status
Expand Down
11 changes: 5 additions & 6 deletions app/views/Export.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Linking,
View,
Text,
Alert,
Image
} from 'react-native';

Expand Down Expand Up @@ -46,10 +45,10 @@ class ExportScreen extends Component {

b64Data = base64.encode(JSON.stringify(locationData));
Share.open({
url: "data:string/txt;base64," + b64Data
}).then(res => {
console.log(res);
})
url: "data:string/txt;base64," + b64Data
}).then(res => {
console.log(res);
})
.catch(err => {
console.log(err.message, err.code);
})
Expand Down Expand Up @@ -79,7 +78,7 @@ class ExportScreen extends Component {
<View style={styles.block}>
<Button onPress={this.onShare} title="Share" />
</View>
<Text style={styles.mainText}>Points to be shared: { LocationServices.getPointCount() }</Text>
<Text style={styles.mainText}>Points to be shared: {LocationServices.getPointCount()}</Text>
</View>

</SafeAreaView>
Expand Down
18 changes: 9 additions & 9 deletions app/views/Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import {
ScrollView,
Linking,
View,
Text,
Alert
Text
} from 'react-native';

import colors from "../constants/colors";
import WebView from 'react-native-webview';
import Button from "../components/Button";

import {SearchAndImport} from '../helpers/GoogleTakeOutAutoImport';
import {
SearchAndImport
} from '../helpers/GoogleTakeOutAutoImport';

class ImportScreen extends Component {
constructor(props) {
Expand All @@ -29,8 +30,7 @@ class ImportScreen extends Component {

}

componentWillUnmount() {
}
componentWillUnmount() { }

backToMain() {
this.props.navigation.navigate('LocationTrackingScreen', {})
Expand All @@ -47,13 +47,13 @@ class ImportScreen extends Component {

<View style={styles.main}>
<View style={styles.subHeaderTitle}>
<Text style={styles.sectionDescription, { fontSize: 18, marginTop: 8}}>1. Login to your Google Account and Download your Location History</Text>
<Text style={styles.sectionDescription, { fontSize: 18, marginTop: 8}}>2. After downloaded, open this screen again. The data will import automatically.</Text>
<Text style={styles.sectionDescription, { fontSize: 18, marginTop: 8 }}>1. Login to your Google Account and Download your Location History</Text>
<Text style={styles.sectionDescription, { fontSize: 18, marginTop: 8 }}>2. After downloaded, open this screen again. The data will import automatically.</Text>
</View>
<View style={styles.web}>
<WebView
source= {{ uri: 'https://takeout.google.com/settings/takeout/custom/location_history' }}
style= {{ marginTop: 15 }}
source={{ uri: 'https://takeout.google.com/settings/takeout/custom/location_history' }}
style={{ marginTop: 15 }}
/>
</View>
</View>
Expand Down
7 changes: 3 additions & 4 deletions app/views/News.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
ScrollView,
Linking,
View,
Text,
Alert
Text
} from 'react-native';

import colors from "../constants/colors";
Expand All @@ -35,8 +34,8 @@ class NewsScreen extends Component {
<Text style={styles.sectionDescription}>Latest News</Text>
</View>
<WebView
source= {{ uri: 'https://privatekit.mit.edu/views' }}
style= {{ marginTop: 15}}
source={{ uri: 'https://privatekit.mit.edu/views' }}
style={{ marginTop: 15 }}
/>
</SafeAreaView>
)
Expand Down

0 comments on commit 67a4a41

Please sign in to comment.