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> {
// Platform messages may fail, so we use a try/catch PlatformException.
try
{
permissionStatus
=
await
PermissionHandler
.
checkPermissionStatus
(
PermissionGroup
.
calendar
);
permissionStatus
=
await
PermissionHandler
.
checkPermissionStatus
(
PermissionGroup
.
calendar
);
if
(
permissionStatus
!=
PermissionStatus
.
granted
){
final
shouldShowRationale
=
await
PermissionHandler
.
shouldShowRequestPermissionRationale
(
PermissionGroup
.
calendar
);
if
(
shouldShowRationale
)
{
var
permissions
=
await
PermissionHandler
.
requestPermissions
([
PermissionGroup
.
calendar
]);
if
(
permissions
.
containsKey
(
PermissionGroup
.
calendar
))
{
if
(
permissionStatus
!=
PermissionStatus
.
granted
)
{
final
shouldShowRationale
=
await
PermissionHandler
.
shouldShowRequestPermissionRationale
(
PermissionGroup
.
calendar
);
if
(
shouldShowRationale
)
{
var
permissions
=
await
PermissionHandler
.
requestPermissions
([
PermissionGroup
.
calendar
]);
if
(
permissions
.
containsKey
(
PermissionGroup
.
calendar
))
{
permissionStatus
=
permissions
[
PermissionGroup
.
calendar
];
}
}
...
...
@@ -66,7 +69,8 @@ class _MyAppState extends State<MyApp> {
new
Text
(
'Running on:
$_permissionStatus
\n
'
),
new
RaisedButton
(
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
enum
PermissionStatus
{
/// Permission to access the requested feature is denied by the user.
denied
,
/// The feature is disabled (or not available) on the device.
disabled
,
/// Permission to access the requested feature is granted by the user.
granted
,
/// The user granted restricted access to the requested feature (only on iOS).
restricted
,
/// Permission is in an unknown state
unknown
enum
PermissionStatus
{
/// Permission to access the requested feature is denied by the user.
denied
,
/// The feature is disabled (or not available) on the device.
disabled
,
/// Permission to access the requested feature is granted by the user.
granted
,
/// The user granted restricted access to the requested feature (only on iOS).
restricted
,
/// Permission is in an unknown state
unknown
}
/// 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
unknown
,
/// Android: Calendar
/// iOS: Calendar (Events)
calendar
,
/// Android: Camera
/// iOS: Photos (Camera Roll and Camera)
camera
,
/// Android: Contacts
/// iOS: AddressBook
contacts
,
/// Android: Fine and Coarse Location
/// iOS: CoreLocation (Always and WhenInUse)
location
,
/// Android: Microphone
/// iOS: Microphone
microphone
,
/// Android: Phone
/// iOS: Nothing
phone
,
/// Android: Nothing
/// iOS: Photos
photos
,
/// Android: Nothing
/// iOS: Reminders
reminders
,
/// Android: Body Sensors
/// iOS: CoreMotion
sensors
,
/// Android: Sms
/// iOS: Nothing
sms
,
/// Android: External Storage
/// iOS: Nothing
storage
,
/// Android: Microphone
/// iOS: Speech
speech
,
/// Android: Fine and Coarse Location
/// iOS: CoreLocation - Always
locationAlways
,
/// Android: Fine and Coarse Location
/// iOS: CoreLocation - WhenInUse
locationWhenInUse
,
/// Android: None
/// iOS: MPMediaLibrary
mediaLibrary
}
\ No newline at end of file
}
lib/permission_handler.dart
View file @
6874f12c
...
...
@@ -10,39 +10,37 @@ class PermissionHandler {
const
MethodChannel
(
'flutter.baseflow.com/permissions/methods'
);
/// 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
(
'checkPermissionStatus'
,
Codec
.
encodePermissionGroup
(
permission
));
'checkPermissionStatus'
,
Codec
.
encodePermissionGroup
(
permission
));
return
Codec
.
decodePermissionStatus
(
status
);
}
/// Open the App settings page.
///
///
/// Returns [true] if the app settings page could be opened, otherwise [false] is returned.
static
Future
<
bool
>
openAppSettings
()
async
=>
await
_channel
.
invokeMethod
(
"openAppSettings"
);
await
_channel
.
invokeMethod
(
"openAppSettings"
);
/// Request the user for access to the supplied list of permissiongroups.
///
///
/// 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
status
=
await
_channel
.
invokeMethod
(
'requestPermissions'
,
jsonData
);
final
status
=
await
_channel
.
invokeMethod
(
'requestPermissions'
,
jsonData
);
return
Codec
.
decodePermissionRequestResult
(
status
);
}
/// 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].
static
Future
<
bool
>
shouldShowRequestPermissionRationale
(
PermissionGroup
permission
)
async
=>
await
_channel
.
invokeMethod
(
'shouldShowRequestPermissionRationale'
,
Codec
.
encodePermissionGroup
(
permission
));
static
Future
<
bool
>
shouldShowRequestPermissionRationale
(
PermissionGroup
permission
)
async
=>
await
_channel
.
invokeMethod
(
'shouldShowRequestPermissionRationale'
,
Codec
.
encodePermissionGroup
(
permission
));
}
lib/utils/codec.dart
View file @
6874f12c
...
...
@@ -6,30 +6,34 @@ class Codec {
static
PermissionStatus
decodePermissionStatus
(
dynamic
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
permissionResults
=
Map
<
PermissionGroup
,
PermissionStatus
>();
jsonObject
.
forEach
((
key
,
value
)
{
final
permissionGroup
=
PermissionGroup
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
key
.
toString
());
final
permissionStatus
=
PermissionStatus
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
value
.
toString
());
final
permissionGroup
=
PermissionGroup
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
key
.
toString
());
final
permissionStatus
=
PermissionStatus
.
values
.
firstWhere
((
e
)
=>
e
.
toString
().
split
(
'.'
).
last
==
value
.
toString
());
permissionResults
[
permissionGroup
]
=
permissionStatus
;
});
return
permissionResults
;
}
static
String
encodePermissionGroup
(
PermissionGroup
permissionGroup
)
=>
json
.
encode
(
_encodeEnum
(
permissionGroup
));
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
)
{
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