Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
permission_handler
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
songyanzhi
permission_handler
Commits
6874f12c
Commit
6874f12c
authored
Jul 24, 2018
by
Maurits van Beusekom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Format Dart code
parent
1c81b9a7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
71 additions
and
50 deletions
+71
-50
example/lib/main.dart
+12
-8
lib/permission_enums.dart
+32
-16
lib/permission_handler.dart
+16
-18
lib/utils/codec.dart
+11
-8
No files found.
example/lib/main.dart
View file @
6874f12c
...
@@ -27,14 +27,17 @@ class _MyAppState extends State<MyApp> {
...
@@ -27,14 +27,17 @@ class _MyAppState extends State<MyApp> {
// Platform messages may fail, so we use a try/catch PlatformException.
// Platform messages may fail, so we use a try/catch PlatformException.
try
{
try
{
permissionStatus
=
await
PermissionHandler
.
checkPermissionStatus
(
PermissionGroup
.
calendar
);
permissionStatus
=
await
PermissionHandler
.
checkPermissionStatus
(
PermissionGroup
.
calendar
);
if
(
permissionStatus
!=
PermissionStatus
.
granted
){
if
(
permissionStatus
!=
PermissionStatus
.
granted
)
{
final
shouldShowRationale
=
await
PermissionHandler
.
shouldShowRequestPermissionRationale
(
PermissionGroup
.
calendar
);
final
shouldShowRationale
=
await
PermissionHandler
.
shouldShowRequestPermissionRationale
(
PermissionGroup
.
calendar
);
if
(
shouldShowRationale
)
{
var
permissions
=
await
PermissionHandler
.
requestPermissions
([
PermissionGroup
.
calendar
]);
if
(
shouldShowRationale
)
{
if
(
permissions
.
containsKey
(
PermissionGroup
.
calendar
))
{
var
permissions
=
await
PermissionHandler
.
requestPermissions
([
PermissionGroup
.
calendar
]);
if
(
permissions
.
containsKey
(
PermissionGroup
.
calendar
))
{
permissionStatus
=
permissions
[
PermissionGroup
.
calendar
];
permissionStatus
=
permissions
[
PermissionGroup
.
calendar
];
}
}
}
}
...
@@ -66,7 +69,8 @@ class _MyAppState extends State<MyApp> {
...
@@ -66,7 +69,8 @@ class _MyAppState extends State<MyApp> {
new
Text
(
'Running on:
$_permissionStatus
\n
'
),
new
Text
(
'Running on:
$_permissionStatus
\n
'
),
new
RaisedButton
(
new
RaisedButton
(
child:
new
Text
(
"Open settings"
),
child:
new
Text
(
"Open settings"
),
onPressed:
()
async
=>
await
PermissionHandler
.
openAppSettings
(),
onPressed:
()
async
=>
await
PermissionHandler
.
openAppSettings
(),
),
),
],
],
),
),
...
...
lib/permission_enums.dart
View file @
6874f12c
/// Defines the state of a permission group
/// Defines the state of a permission group
enum
PermissionStatus
enum
PermissionStatus
{
{
/// Permission to access the requested feature is denied by the user.
/// Permission to access the requested feature is denied by the user.
denied
,
denied
,
/// The feature is disabled (or not available) on the device.
/// The feature is disabled (or not available) on the device.
disabled
,
disabled
,
/// Permission to access the requested feature is granted by the user.
granted
,
/// Permission to access the requested feature is granted by the user.
/// The user granted restricted access to the requested feature (only on iOS).
granted
,
restricted
,
/// Permission is in an unknown state
/// The user granted restricted access to the requested feature (only on iOS).
unknown
restricted
,
/// Permission is in an unknown state
unknown
}
}
/// Defines the permission groups for which permissions can be checked or requested.
/// Defines the permission groups for which permissions can be checked or requested.
enum
PermissionGroup
enum
PermissionGroup
{
{
/// The unknown permission only used for return type, never requested
/// The unknown permission only used for return type, never requested
unknown
,
unknown
,
/// Android: Calendar
/// Android: Calendar
/// iOS: Calendar (Events)
/// iOS: Calendar (Events)
calendar
,
calendar
,
/// Android: Camera
/// Android: Camera
/// iOS: Photos (Camera Roll and Camera)
/// iOS: Photos (Camera Roll and Camera)
camera
,
camera
,
/// Android: Contacts
/// Android: Contacts
/// iOS: AddressBook
/// iOS: AddressBook
contacts
,
contacts
,
/// Android: Fine and Coarse Location
/// Android: Fine and Coarse Location
/// iOS: CoreLocation (Always and WhenInUse)
/// iOS: CoreLocation (Always and WhenInUse)
location
,
location
,
/// Android: Microphone
/// Android: Microphone
/// iOS: Microphone
/// iOS: Microphone
microphone
,
microphone
,
/// Android: Phone
/// Android: Phone
/// iOS: Nothing
/// iOS: Nothing
phone
,
phone
,
/// Android: Nothing
/// Android: Nothing
/// iOS: Photos
/// iOS: Photos
photos
,
photos
,
/// Android: Nothing
/// Android: Nothing
/// iOS: Reminders
/// iOS: Reminders
reminders
,
reminders
,
/// Android: Body Sensors
/// Android: Body Sensors
/// iOS: CoreMotion
/// iOS: CoreMotion
sensors
,
sensors
,
/// Android: Sms
/// Android: Sms
/// iOS: Nothing
/// iOS: Nothing
sms
,
sms
,
/// Android: External Storage
/// Android: External Storage
/// iOS: Nothing
/// iOS: Nothing
storage
,
storage
,
/// Android: Microphone
/// Android: Microphone
/// iOS: Speech
/// iOS: Speech
speech
,
speech
,
/// Android: Fine and Coarse Location
/// Android: Fine and Coarse Location
/// iOS: CoreLocation - Always
/// iOS: CoreLocation - Always
locationAlways
,
locationAlways
,
/// Android: Fine and Coarse Location
/// Android: Fine and Coarse Location
/// iOS: CoreLocation - WhenInUse
/// iOS: CoreLocation - WhenInUse
locationWhenInUse
,
locationWhenInUse
,
/// Android: None
/// Android: None
/// iOS: MPMediaLibrary
/// iOS: MPMediaLibrary
mediaLibrary
mediaLibrary
}
}
\ No newline at end of file
lib/permission_handler.dart
View file @
6874f12c
...
@@ -10,39 +10,37 @@ class PermissionHandler {
...
@@ -10,39 +10,37 @@ class PermissionHandler {
const
MethodChannel
(
'flutter.baseflow.com/permissions/methods'
);
const
MethodChannel
(
'flutter.baseflow.com/permissions/methods'
);
/// Returns a [Future] containing the current permission status for the supplied [PermissionGroup].
/// Returns a [Future] containing the current permission status for the supplied [PermissionGroup].
static
Future
<
PermissionStatus
>
checkPermissionStatus
(
PermissionGroup
permission
)
async
{
static
Future
<
PermissionStatus
>
checkPermissionStatus
(
PermissionGroup
permission
)
async
{
final
status
=
await
_channel
.
invokeMethod
(
final
status
=
await
_channel
.
invokeMethod
(
'checkPermissionStatus'
,
'checkPermissionStatus'
,
Codec
.
encodePermissionGroup
(
permission
));
Codec
.
encodePermissionGroup
(
permission
));
return
Codec
.
decodePermissionStatus
(
status
);
return
Codec
.
decodePermissionStatus
(
status
);
}
}
/// Open the App settings page.
/// Open the App settings page.
///
///
/// Returns [true] if the app settings page could be opened, otherwise [false] is returned.
/// Returns [true] if the app settings page could be opened, otherwise [false] is returned.
static
Future
<
bool
>
openAppSettings
()
async
=>
static
Future
<
bool
>
openAppSettings
()
async
=>
await
_channel
.
invokeMethod
(
"openAppSettings"
);
await
_channel
.
invokeMethod
(
"openAppSettings"
);
/// Request the user for access to the supplied list of permissiongroups.
/// Request the user for access to the supplied list of permissiongroups.
///
///
/// Returns a [Map] containing the status per requested permissiongroup.
/// Returns a [Map] containing the status per requested permissiongroup.
static
Future
<
Map
<
PermissionGroup
,
PermissionStatus
>>
requestPermissions
(
List
<
PermissionGroup
>
permissions
)
async
{
static
Future
<
Map
<
PermissionGroup
,
PermissionStatus
>>
requestPermissions
(
List
<
PermissionGroup
>
permissions
)
async
{
final
jsonData
=
Codec
.
encodePermissionGroups
(
permissions
);
final
jsonData
=
Codec
.
encodePermissionGroups
(
permissions
);
final
status
=
await
_channel
.
invokeMethod
(
final
status
=
await
_channel
.
invokeMethod
(
'requestPermissions'
,
jsonData
);
'requestPermissions'
,
jsonData
);
return
Codec
.
decodePermissionRequestResult
(
status
);
return
Codec
.
decodePermissionRequestResult
(
status
);
}
}
/// Request to see if you should show a rationale for requesting permission.
/// Request to see if you should show a rationale for requesting permission.
///
///
/// This method is only implemented on Android, calling this on iOS always
/// This method is only implemented on Android, calling this on iOS always
/// returns [false].
/// returns [false].
static
Future
<
bool
>
shouldShowRequestPermissionRationale
(
PermissionGroup
permission
)
async
=>
static
Future
<
bool
>
shouldShowRequestPermissionRationale
(
await
_channel
.
invokeMethod
(
PermissionGroup
permission
)
async
=>
'shouldShowRequestPermissionRationale'
,
await
_channel
.
invokeMethod
(
'shouldShowRequestPermissionRationale'
,
Codec
.
encodePermissionGroup
(
permission
));
Codec
.
encodePermissionGroup
(
permission
));
}
}
lib/utils/codec.dart
View file @
6874f12c
...
@@ -6,30 +6,34 @@ class Codec {
...
@@ -6,30 +6,34 @@ class Codec {
static
PermissionStatus
decodePermissionStatus
(
dynamic
value
)
{
static
PermissionStatus
decodePermissionStatus
(
dynamic
value
)
{
final
permission
=
json
.
decode
(
value
);
final
permission
=
json
.
decode
(
value
);
return
PermissionStatus
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
permission
);
return
PermissionStatus
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
permission
);
}
}
static
Map
<
PermissionGroup
,
PermissionStatus
>
decodePermissionRequestResult
(
dynamic
value
)
{
static
Map
<
PermissionGroup
,
PermissionStatus
>
decodePermissionRequestResult
(
dynamic
value
)
{
final
jsonObject
=
json
.
decode
(
value
);
final
jsonObject
=
json
.
decode
(
value
);
final
permissionResults
=
Map
<
PermissionGroup
,
PermissionStatus
>();
final
permissionResults
=
Map
<
PermissionGroup
,
PermissionStatus
>();
jsonObject
.
forEach
((
key
,
value
)
{
jsonObject
.
forEach
((
key
,
value
)
{
final
permissionGroup
=
PermissionGroup
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
key
.
toString
());
final
permissionGroup
=
PermissionGroup
.
values
final
permissionStatus
=
PermissionStatus
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
value
.
toString
());
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
key
.
toString
());
final
permissionStatus
=
PermissionStatus
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
value
.
toString
());
permissionResults
[
permissionGroup
]
=
permissionStatus
;
permissionResults
[
permissionGroup
]
=
permissionStatus
;
});
});
return
permissionResults
;
return
permissionResults
;
}
}
static
String
encodePermissionGroup
(
PermissionGroup
permissionGroup
)
=>
static
String
encodePermissionGroup
(
PermissionGroup
permissionGroup
)
=>
json
.
encode
(
_encodeEnum
(
permissionGroup
));
json
.
encode
(
_encodeEnum
(
permissionGroup
));
static
String
encodePermissionGroups
(
List
<
PermissionGroup
>
permissions
)
=>
static
String
encodePermissionGroups
(
List
<
PermissionGroup
>
permissions
)
=>
json
.
encode
(
permissions
.
map
((
p
)
=>
_encodeEnum
(
p
)).
toList
());
json
.
encode
(
permissions
.
map
((
p
)
=>
_encodeEnum
(
p
)).
toList
());
static
String
_encodeEnum
(
dynamic
value
)
{
static
String
_encodeEnum
(
dynamic
value
)
{
return
value
.
toString
().
split
(
'.'
).
last
;
return
value
.
toString
().
split
(
'.'
).
last
;
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment