-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* android permissions util * improve location permission message
- Loading branch information
1 parent
c83d8f7
commit 688fc77
Showing
6 changed files
with
146 additions
and
75 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
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,59 @@ | ||
import { PermissionsAndroid, Platform } from 'react-native'; | ||
|
||
const defaultFineLocationParams = { | ||
title: 'Location Permission', | ||
message: 'Stripe Terminal needs access to your location', | ||
buttonPositive: 'Accept', | ||
}; | ||
|
||
type PermissionsProps = { | ||
accessFineLocation?: { | ||
title: string; | ||
message: string; | ||
buttonPositive: string; | ||
}; | ||
}; | ||
|
||
const isAndroid12orHigher = () => | ||
Platform.OS === 'android' && Platform.Version >= 31; | ||
|
||
export async function requestNeededAndroidPermissions({ | ||
accessFineLocation = defaultFineLocationParams, | ||
}: PermissionsProps | undefined = {}): Promise<boolean> { | ||
let hasGrantedLocationPermissions = false; | ||
let hasGrantedBluetoothPermissions = false; | ||
|
||
const grantedFineLocation = await PermissionsAndroid.request( | ||
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | ||
accessFineLocation || defaultFineLocationParams | ||
); | ||
|
||
if (hasGrantedPermission(grantedFineLocation)) { | ||
hasGrantedLocationPermissions = true; | ||
|
||
if (isAndroid12orHigher()) { | ||
const grantedBT = await PermissionsAndroid.request( | ||
// BLUETOOTH_CONNECT doesn't support customization | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT | ||
); | ||
|
||
const grantedBTScan = await PermissionsAndroid.request( | ||
// BLUETOOTH_SCAN doesn't support customization | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN | ||
); | ||
if ( | ||
hasGrantedPermission(grantedBT) && | ||
hasGrantedPermission(grantedBTScan) | ||
) { | ||
hasGrantedBluetoothPermissions = true; | ||
} | ||
} else { | ||
hasGrantedBluetoothPermissions = true; | ||
} | ||
} | ||
return hasGrantedBluetoothPermissions && hasGrantedLocationPermissions; | ||
} | ||
|
||
const hasGrantedPermission = (status: string) => { | ||
return status === PermissionsAndroid.RESULTS.GRANTED; | ||
}; |