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
7f9322c6
Unverified
Commit
7f9322c6
authored
Feb 28, 2020
by
Maurits van Beusekom
Committed by
GitHub
Feb 28, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #217 from Baseflow/add_missing_api_docs
Document public api
parents
bd094a2d
a7f9b85d
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
68 additions
and
26 deletions
+68
-26
analysis_options.yaml
+6
-1
example/lib/main.dart
+5
-0
lib/src/permission_enums.dart
+19
-8
lib/src/permission_handler.dart
+25
-13
lib/src/utils/codec.dart
+12
-3
pubspec.yaml
+1
-1
No files found.
analysis_options.yaml
View file @
7f9322c6
include
:
package:pedantic/analysis_options.1.7.0.yaml
include
:
package:effective_dart/analysis_options.1.2.0.yaml
linter
:
rules
:
-
public_member_api_docs
\ No newline at end of file
example/lib/main.dart
View file @
7f9322c6
...
@@ -5,6 +5,8 @@ import 'package:permission_handler/permission_handler.dart';
...
@@ -5,6 +5,8 @@ import 'package:permission_handler/permission_handler.dart';
void
main
(
)
=>
runApp
(
MyApp
());
void
main
(
)
=>
runApp
(
MyApp
());
/// Example Flutter Application demonstrating the functionality of the
/// Permission Handler plugin.
class
MyApp
extends
StatelessWidget
{
class
MyApp
extends
StatelessWidget
{
@override
@override
Widget
build
(
BuildContext
context
)
{
Widget
build
(
BuildContext
context
)
{
...
@@ -49,7 +51,10 @@ class MyApp extends StatelessWidget {
...
@@ -49,7 +51,10 @@ class MyApp extends StatelessWidget {
}
}
}
}
/// Permission widget which displays a permission and allows users to request
/// the permissions.
class
PermissionWidget
extends
StatefulWidget
{
class
PermissionWidget
extends
StatefulWidget
{
/// Constructs a [PermissionWidget] for the supplied [PermissionGroup].
const
PermissionWidget
(
this
.
_permissionGroup
);
const
PermissionWidget
(
this
.
_permissionGroup
);
final
PermissionGroup
_permissionGroup
;
final
PermissionGroup
_permissionGroup
;
...
...
lib/src/permission_enums.dart
View file @
7f9322c6
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
class
PermissionStatus
{
class
PermissionStatus
{
const
PermissionStatus
.
_
(
this
.
value
);
const
PermissionStatus
.
_
(
this
.
value
);
/// Integer representation of the [PermissionStatus].
final
int
value
;
final
int
value
;
/// Permission to access the requested feature is denied by the user.
/// Permission to access the requested feature is denied by the user.
...
@@ -13,17 +14,19 @@ class PermissionStatus {
...
@@ -13,17 +14,19 @@ class PermissionStatus {
/// Permission to access the requested feature is granted by the user.
/// Permission to access the requested feature is granted by the user.
static
const
PermissionStatus
granted
=
PermissionStatus
.
_
(
2
);
static
const
PermissionStatus
granted
=
PermissionStatus
.
_
(
2
);
/// Permission to access the requested feature is denied by the OS (only on
iOS).
/// Permission to access the requested feature is denied by the OS (only on
///
The user cannot change this app's status, possibly due to active restrictions such as
///
iOS). The user cannot change this app's status, possibly due to active
/// parental controls being in place.
///
restrictions such as
parental controls being in place.
static
const
PermissionStatus
restricted
=
PermissionStatus
.
_
(
3
);
static
const
PermissionStatus
restricted
=
PermissionStatus
.
_
(
3
);
/// Permission is in an unknown state
/// Permission is in an unknown state
static
const
PermissionStatus
unknown
=
PermissionStatus
.
_
(
4
);
static
const
PermissionStatus
unknown
=
PermissionStatus
.
_
(
4
);
/// Permission to access the requested feature is denied by the user and never show selected (only on Android).
/// Permission to access the requested feature is denied by the user and
/// never show selected (only on Android).
static
const
PermissionStatus
neverAskAgain
=
PermissionStatus
.
_
(
5
);
static
const
PermissionStatus
neverAskAgain
=
PermissionStatus
.
_
(
5
);
/// Returns a list of all possible [PermissionStatus] values.
static
const
List
<
PermissionStatus
>
values
=
<
PermissionStatus
>[
static
const
List
<
PermissionStatus
>
values
=
<
PermissionStatus
>[
denied
,
denied
,
disabled
,
disabled
,
...
@@ -50,6 +53,7 @@ class PermissionStatus {
...
@@ -50,6 +53,7 @@ class PermissionStatus {
class
ServiceStatus
{
class
ServiceStatus
{
const
ServiceStatus
.
_
(
this
.
value
);
const
ServiceStatus
.
_
(
this
.
value
);
/// Integer representation of the [ServiceStatus].
final
int
value
;
final
int
value
;
/// The service for the supplied permission group is disabled.
/// The service for the supplied permission group is disabled.
...
@@ -61,9 +65,11 @@ class ServiceStatus {
...
@@ -61,9 +65,11 @@ class ServiceStatus {
/// There is no service for the supplied permission group.
/// There is no service for the supplied permission group.
static
const
ServiceStatus
notApplicable
=
ServiceStatus
.
_
(
2
);
static
const
ServiceStatus
notApplicable
=
ServiceStatus
.
_
(
2
);
/// The unknown service status indicates the state of the service could not be determined.
/// The unknown service status indicates the state of the service could not
/// be determined.
static
const
ServiceStatus
unknown
=
ServiceStatus
.
_
(
3
);
static
const
ServiceStatus
unknown
=
ServiceStatus
.
_
(
3
);
/// Returns a list of all possible [ServiceStatus] values.
static
const
List
<
ServiceStatus
>
values
=
<
ServiceStatus
>[
static
const
List
<
ServiceStatus
>
values
=
<
ServiceStatus
>[
disabled
,
disabled
,
enabled
,
enabled
,
...
@@ -82,10 +88,12 @@ class ServiceStatus {
...
@@ -82,10 +88,12 @@ class ServiceStatus {
String
toString
()
=>
'ServiceStatus.
${_names[value]}
'
;
String
toString
()
=>
'ServiceStatus.
${_names[value]}
'
;
}
}
/// Defines the permission groups for which permissions can be checked or requested.
/// Defines the permission groups for which permissions can be checked or
/// requested.
class
PermissionGroup
{
class
PermissionGroup
{
const
PermissionGroup
.
_
(
this
.
value
);
const
PermissionGroup
.
_
(
this
.
value
);
/// Integer representation of the [PermissionGroup].
final
int
value
;
final
int
value
;
/// Android: Calendar
/// Android: Calendar
...
@@ -147,7 +155,8 @@ class PermissionGroup {
...
@@ -147,7 +155,8 @@ class PermissionGroup {
static
const
PermissionGroup
speech
=
PermissionGroup
.
_
(
13
);
static
const
PermissionGroup
speech
=
PermissionGroup
.
_
(
13
);
/// Android: External Storage
/// Android: External Storage
/// iOS: Access to folders like `Documents` or `Downloads`. Implicitly granted.
/// iOS: Access to folders like `Documents` or `Downloads`. Implicitly
/// granted.
static
const
PermissionGroup
storage
=
PermissionGroup
.
_
(
14
);
static
const
PermissionGroup
storage
=
PermissionGroup
.
_
(
14
);
/// Android: Ignore Battery Optimizations
/// Android: Ignore Battery Optimizations
...
@@ -158,7 +167,8 @@ class PermissionGroup {
...
@@ -158,7 +167,8 @@ class PermissionGroup {
/// iOS: Notification
/// iOS: Notification
static
const
PermissionGroup
notification
=
PermissionGroup
.
_
(
16
);
static
const
PermissionGroup
notification
=
PermissionGroup
.
_
(
16
);
/// Android: Allows an application to access any geographic locations persisted in the user's shared collection.
/// Android: Allows an application to access any geographic locations
/// persisted in the user's shared collection.
static
const
PermissionGroup
access_media_location
=
PermissionGroup
.
_
(
17
);
static
const
PermissionGroup
access_media_location
=
PermissionGroup
.
_
(
17
);
/// When running on Android Q and above: Activity Recognition
/// When running on Android Q and above: Activity Recognition
...
@@ -169,6 +179,7 @@ class PermissionGroup {
...
@@ -169,6 +179,7 @@ class PermissionGroup {
/// The unknown permission only used for return type, never requested
/// The unknown permission only used for return type, never requested
static
const
PermissionGroup
unknown
=
PermissionGroup
.
_
(
19
);
static
const
PermissionGroup
unknown
=
PermissionGroup
.
_
(
19
);
/// Returns a list of all possible [PermissionGroup] values.
static
const
List
<
PermissionGroup
>
values
=
<
PermissionGroup
>[
static
const
List
<
PermissionGroup
>
values
=
<
PermissionGroup
>[
calendar
,
calendar
,
camera
,
camera
,
...
...
lib/src/permission_handler.dart
View file @
7f9322c6
...
@@ -4,14 +4,21 @@ import 'dart:io';
...
@@ -4,14 +4,21 @@ import 'dart:io';
import
'package:flutter/services.dart'
;
import
'package:flutter/services.dart'
;
import
'package:meta/meta.dart'
;
import
'package:meta/meta.dart'
;
import
'p
ackage:permission_handler/src/p
ermission_enums.dart'
;
import
'permission_enums.dart'
;
import
'
package:permission_handler/src/
utils/codec.dart'
;
import
'utils/codec.dart'
;
/// Provides a cross-platform (iOS, Android) API to request and check permissions.
/// Provides a cross-platform (iOS, Android) API to request and check
/// permissions.
class
PermissionHandler
{
class
PermissionHandler
{
/// Constructs a singleton instance of [Geolocator].
///
/// When a second instance is created, the first instance will not be able to
/// listen to the EventChannel because it is overridden. Forcing the class to
/// be a singleton class can prevent misuse of creating a second instance
/// from a programmer.
factory
PermissionHandler
()
{
factory
PermissionHandler
()
{
if
(
_instance
==
null
)
{
if
(
_instance
==
null
)
{
const
MethodChannel
methodChannel
=
const
methodChannel
=
MethodChannel
(
'flutter.baseflow.com/permissions/methods'
);
MethodChannel
(
'flutter.baseflow.com/permissions/methods'
);
_instance
=
PermissionHandler
.
private
(
methodChannel
);
_instance
=
PermissionHandler
.
private
(
methodChannel
);
...
@@ -19,6 +26,8 @@ class PermissionHandler {
...
@@ -19,6 +26,8 @@ class PermissionHandler {
return
_instance
;
return
_instance
;
}
}
/// This constructor is only used for testing and shouldn't be accessed by
/// users of the plugin.
@visibleForTesting
@visibleForTesting
PermissionHandler
.
private
(
this
.
_methodChannel
);
PermissionHandler
.
private
(
this
.
_methodChannel
);
...
@@ -28,10 +37,11 @@ class PermissionHandler {
...
@@ -28,10 +37,11 @@ class PermissionHandler {
/// Check current permission status.
/// Check current permission status.
///
///
/// Returns a [Future] containing the current permission status for the supplied [PermissionGroup].
/// Returns a [Future] containing the current permission status for the
/// supplied [PermissionGroup].
Future
<
PermissionStatus
>
checkPermissionStatus
(
Future
<
PermissionStatus
>
checkPermissionStatus
(
PermissionGroup
permission
)
async
{
PermissionGroup
permission
)
async
{
final
int
status
=
await
_methodChannel
.
invokeMethod
(
final
status
=
await
_methodChannel
.
invokeMethod
(
'checkPermissionStatus'
,
permission
.
value
);
'checkPermissionStatus'
,
permission
.
value
);
return
Codec
.
decodePermissionStatus
(
status
);
return
Codec
.
decodePermissionStatus
(
status
);
...
@@ -39,7 +49,8 @@ class PermissionHandler {
...
@@ -39,7 +49,8 @@ class PermissionHandler {
/// Check current service status.
/// Check current service status.
///
///
/// Returns a [Future] containing the current service status for the supplied [PermissionGroup].
/// Returns a [Future] containing the current service status for the supplied
/// [PermissionGroup].
///
///
/// Notes about specific PermissionGroups:
/// Notes about specific PermissionGroups:
/// - **PermissionGroup.phone**
/// - **PermissionGroup.phone**
...
@@ -60,7 +71,7 @@ class PermissionHandler {
...
@@ -60,7 +71,7 @@ class PermissionHandler {
/// devices' capability to place & connect phone calls
/// devices' capability to place & connect phone calls
/// as it also depends on the network condition.
/// as it also depends on the network condition.
Future
<
ServiceStatus
>
checkServiceStatus
(
PermissionGroup
permission
)
async
{
Future
<
ServiceStatus
>
checkServiceStatus
(
PermissionGroup
permission
)
async
{
final
int
status
=
await
_methodChannel
.
invokeMethod
(
final
status
=
await
_methodChannel
.
invokeMethod
(
'checkServiceStatus'
,
permission
.
value
);
'checkServiceStatus'
,
permission
.
value
);
return
Codec
.
decodeServiceStatus
(
status
);
return
Codec
.
decodeServiceStatus
(
status
);
...
@@ -68,9 +79,10 @@ class PermissionHandler {
...
@@ -68,9 +79,10 @@ class PermissionHandler {
/// 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.
Future
<
bool
>
openAppSettings
()
async
{
Future
<
bool
>
openAppSettings
()
async
{
final
bool
hasOpened
=
await
_methodChannel
.
invokeMethod
(
'openAppSettings'
);
final
hasOpened
=
await
_methodChannel
.
invokeMethod
(
'openAppSettings'
);
return
hasOpened
;
return
hasOpened
;
}
}
...
@@ -80,8 +92,8 @@ class PermissionHandler {
...
@@ -80,8 +92,8 @@ class PermissionHandler {
/// Returns a [Map] containing the status per requested permissiongroup.
/// Returns a [Map] containing the status per requested permissiongroup.
Future
<
Map
<
PermissionGroup
,
PermissionStatus
>>
requestPermissions
(
Future
<
Map
<
PermissionGroup
,
PermissionStatus
>>
requestPermissions
(
List
<
PermissionGroup
>
permissions
)
async
{
List
<
PermissionGroup
>
permissions
)
async
{
final
List
<
int
>
data
=
Codec
.
encodePermissionGroups
(
permissions
);
final
data
=
Codec
.
encodePermissionGroups
(
permissions
);
final
Map
<
dynamic
,
dynamic
>
status
=
final
status
=
await
_methodChannel
.
invokeMethod
(
'requestPermissions'
,
data
);
await
_methodChannel
.
invokeMethod
(
'requestPermissions'
,
data
);
return
Codec
.
decodePermissionRequestResult
(
Map
<
int
,
int
>.
from
(
status
));
return
Codec
.
decodePermissionRequestResult
(
Map
<
int
,
int
>.
from
(
status
));
...
@@ -97,7 +109,7 @@ class PermissionHandler {
...
@@ -97,7 +109,7 @@ class PermissionHandler {
return
false
;
return
false
;
}
}
final
bool
shouldShowRationale
=
await
_methodChannel
.
invokeMethod
(
final
shouldShowRationale
=
await
_methodChannel
.
invokeMethod
(
'shouldShowRequestPermissionRationale'
,
permission
.
value
);
'shouldShowRequestPermissionRationale'
,
permission
.
value
);
return
shouldShowRationale
;
return
shouldShowRationale
;
...
...
lib/src/utils/codec.dart
View file @
7f9322c6
import
'
package:permission_handler/src
/permission_enums.dart'
;
import
'
..
/permission_enums.dart'
;
/// Provides utility methods for encoding messages that are send on the Flutter
/// message channel.
class
Codec
{
class
Codec
{
/// Converts the supplied integer value into a [PermissionStatus] instance.
static
PermissionStatus
decodePermissionStatus
(
int
value
)
{
static
PermissionStatus
decodePermissionStatus
(
int
value
)
{
return
PermissionStatus
.
values
[
value
];
return
PermissionStatus
.
values
[
value
];
}
}
/// Converts the supplied integer value into a [ServiceStatus] instance.
static
ServiceStatus
decodeServiceStatus
(
int
value
)
{
static
ServiceStatus
decodeServiceStatus
(
int
value
)
{
return
ServiceStatus
.
values
[
value
];
return
ServiceStatus
.
values
[
value
];
}
}
/// Converts the supplied [Map] of integers into a [Map] of
/// [PermissionGroup] key and [PermissionStatus] value instances.
static
Map
<
PermissionGroup
,
PermissionStatus
>
decodePermissionRequestResult
(
static
Map
<
PermissionGroup
,
PermissionStatus
>
decodePermissionRequestResult
(
Map
<
int
,
int
>
value
)
{
Map
<
int
,
int
>
value
)
{
return
value
.
map
((
int
key
,
int
value
)
=>
return
value
.
map
((
key
,
value
)
=>
MapEntry
<
PermissionGroup
,
PermissionStatus
>(
MapEntry
<
PermissionGroup
,
PermissionStatus
>(
PermissionGroup
.
values
[
key
],
PermissionStatus
.
values
[
value
]));
PermissionGroup
.
values
[
key
],
PermissionStatus
.
values
[
value
]));
}
}
/// Converts the supplied [List] containing [PermissionGroup] instances into
/// a [List] containing integers which can be used to send on the Flutter
/// method channel.
static
List
<
int
>
encodePermissionGroups
(
List
<
PermissionGroup
>
permissions
)
{
static
List
<
int
>
encodePermissionGroups
(
List
<
PermissionGroup
>
permissions
)
{
return
permissions
.
map
((
PermissionGroup
it
)
=>
it
.
value
).
toList
();
return
permissions
.
map
((
it
)
=>
it
.
value
).
toList
();
}
}
}
}
pubspec.yaml
View file @
7f9322c6
...
@@ -13,7 +13,7 @@ dependencies:
...
@@ -13,7 +13,7 @@ dependencies:
meta
:
^1.1.6
meta
:
^1.1.6
dev_dependencies
:
dev_dependencies
:
pedantic
:
^1.7.0
effective_dart
:
^1.2.1
flutter
:
flutter
:
plugin
:
plugin
:
...
...
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