-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix android Generalize android permissions (might support most permissions in the future)
- Loading branch information
1 parent
b9761b7
commit 9e02e39
Showing
19 changed files
with
238 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react' | ||
import { Button, StyleSheet, Text, View } from 'react-native' | ||
import { Location } from './src' | ||
|
||
class LocationExample extends React.PureComponent { | ||
|
||
state = { | ||
isLoading: false, | ||
} | ||
|
||
render() { | ||
|
||
const { test, locationPermission, requestLocationPermission } = this.props | ||
const { isLoading } = this.state | ||
|
||
return ( | ||
<View style={ styles.container }> | ||
<Text> | ||
{ 'havePermission: ' + locationPermission } | ||
</Text> | ||
<Text> | ||
{ test } | ||
</Text> | ||
<Button | ||
title={ locationPermission === Location.Permission.RESULT.GRANTED ? 'continue' : 'request permisssion' } | ||
style={ { alignSelf: 'center', marginTop: 50 } } | ||
onPress={ requestLocationPermission } | ||
/> | ||
</View> | ||
) | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
justifyContent: 'space-between', | ||
alignItems: 'center', | ||
marginVertical: 150, | ||
} | ||
}) | ||
|
||
export default Location.WithPermission(LocationExample) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { AppRegistry } from 'react-native'; | ||
import App from './App'; | ||
import App from './example'; | ||
|
||
AppRegistry.registerComponent('rnPermissions', () => App); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import LocationPermission, { WithLocationPermission } from './location' | ||
import NotificationPermission, { WithNotificationPermission } from './notification' | ||
import RESULT from './util/results' | ||
|
||
|
||
export const Location = { | ||
Permission: LocationPermission, | ||
Permission: { | ||
...LocationPermission, | ||
RESULT, | ||
}, | ||
WithPermission: WithLocationPermission, | ||
} | ||
|
||
export const Notification = { | ||
Permission: NotificationPermission, | ||
Permission: { | ||
...NotificationPermission, | ||
RESULT, | ||
}, | ||
WithPermission: WithNotificationPermission, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,14 @@ | ||
import { PermissionsAndroid } from 'react-native' | ||
import AsyncStorage, { ASYNC_STORAGE_KEYS } from '../../AsyncStorage' | ||
import RNOpenSettings from 'react-native-open-settings' | ||
import Constants from './constants' | ||
|
||
let ongoingRequest | ||
import PermissionsAndroid from '../permissions-android' | ||
|
||
const LocationPermissions = { | ||
|
||
...Constants, | ||
|
||
async request() { | ||
|
||
const permission = await | ||
LocationPermissions.check() | ||
|
||
if (permission === Constants.RESULTS.GRANTED) { | ||
return | ||
} | ||
|
||
if (permission === Constants.RESULTS.DENIED) { | ||
return RNOpenSettings.openSettings() | ||
} | ||
|
||
const response = await | ||
PermissionsAndroid.request( | ||
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, | ||
) | ||
|
||
await | ||
AsyncStorage.setItem( | ||
ASYNC_STORAGE_KEYS.LOCATION_PERMISSION_NEVER_ASK_AGAIN, | ||
response === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN, | ||
) | ||
|
||
switch (response) { | ||
case PermissionsAndroid.RESULTS.GRANTED: | ||
return Constants.RESULTS.GRANTED | ||
case PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN: | ||
return Constants.RESULTS.DENIED | ||
default: | ||
return Constants.RESULTS.UNDEFINED | ||
} | ||
}, | ||
|
||
/** | ||
* Returns a promise that resolve to either "true" or "false" | ||
*/ | ||
async check() { | ||
const haveClickedNeverAskAgain = await AsyncStorage.getItem( | ||
ASYNC_STORAGE_KEYS.LOCATION_PERMISSION_NEVER_ASK_AGAIN, | ||
false | ||
) | ||
|
||
const permission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION) | ||
|
||
if (permission) { | ||
return Constants.RESULTS.GRANTED | ||
} | ||
|
||
if (haveClickedNeverAskAgain) { | ||
return Constants.RESULTS.DENIED | ||
} | ||
async request() { | ||
return PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION) | ||
}, | ||
|
||
return Constants.RESULTS.UNDEFINED | ||
}, | ||
async check() { | ||
return PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION) | ||
}, | ||
} | ||
|
||
export default LocationPermissions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
import { Linking } from 'react-native' | ||
import AsyncStorage, { ASYNC_STORAGE_KEYS } from '../../AsyncStorage' | ||
import Constants from './constants' | ||
import AsyncStorage, { ASYNC_STORAGE_KEYS } from '../util/async-storage' | ||
import RESULT from '../util/results' | ||
|
||
const LocationPermissions = { | ||
|
||
...Constants, | ||
RESULT, | ||
|
||
async request() { | ||
const permission = await LocationPermissions.check() | ||
async request() { | ||
const permission = await LocationPermissions.check() | ||
|
||
if (permission === Constants.RESULTS.GRANTED) { | ||
return | ||
} | ||
if (permission === RESULT.GRANTED) { | ||
return | ||
} | ||
|
||
AsyncStorage.setItem(ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION, true) | ||
AsyncStorage.setItem(ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION, true) | ||
|
||
navigator.geolocation.requestAuthorization() | ||
}, | ||
navigator.geolocation.requestAuthorization() | ||
}, | ||
|
||
openSettings() { | ||
Linking.openURL('app-settings:') | ||
}, | ||
openSettings() { | ||
Linking.openURL('app-settings:') | ||
}, | ||
|
||
/** | ||
* Returns a promise that resolve to either "true", "false" or | ||
* PERMISSION_UNDEFINED if we haven't asked yet | ||
*/ | ||
async check() { | ||
const haveAsked = await AsyncStorage.getItem( | ||
ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION, | ||
false, | ||
) | ||
/** | ||
* Returns a promise that resolve to either "true", "false" or | ||
* PERMISSION_UNDEFINED if we haven't asked yet | ||
*/ | ||
async check() { | ||
const haveAsked = await AsyncStorage.getItem( | ||
ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION, | ||
false, | ||
) | ||
|
||
if (!haveAsked) { | ||
return Constants.RESULTS.UNDEFINED | ||
} | ||
if (!haveAsked) { | ||
return RESULT.UNDEFINED | ||
} | ||
|
||
const permission = await new Promise((resolve) => { | ||
navigator.geolocation.getCurrentPosition( | ||
position => resolve(true), | ||
error => resolve(error.code !== error.PERMISSION_DENIED), | ||
{ timeout: 100 } | ||
) | ||
}) | ||
const permission = await new Promise((resolve) => { | ||
navigator.geolocation.getCurrentPosition( | ||
position => resolve(true), | ||
error => resolve(error.code !== error.PERMISSION_DENIED), | ||
{ timeout: 100 } | ||
) | ||
}) | ||
|
||
return permission ? Constants.RESULTS.GRANTED : Constants.RESULTS.DENIED | ||
} | ||
return permission ? RESULT.GRANTED : RESULT.DENIED | ||
} | ||
} | ||
|
||
export default LocationPermissions |
Oops, something went wrong.