Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change checkPermission method return type in phone log plugin. #4

Merged
merged 8 commits into from
Aug 16, 2018
3 changes: 3 additions & 0 deletions packages/phone_log/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.0.3] - August 15th, 2018.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to change the pubspec.yaml file to rev the version to 0.0.3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you for catching this!

Update 'checkPermission' method.

## [0.0.2] - June 11th, 2018.
Update README and several fixes.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ private void requestPermission() {
registrar.activity().requestPermissions(perm, 0);
}

private boolean checkPermission() {
private String checkPermission() {
Log.i("PhoneLogPlugin", "Checking permission : " + Manifest.permission.READ_CALL_LOG);
return PackageManager.PERMISSION_GRANTED
boolean isGranted = PackageManager.PERMISSION_GRANTED
== registrar.activity().checkSelfPermission(Manifest.permission.READ_CALL_LOG);
if (isGranted){
return "granted";
} else if (registrar.activity().shouldShowRequestPermissionRationale(Manifest.permission.READ_CALL_LOG)){
return "denied";
} return "deniedAndCannotRequest";
}

@Override
Expand Down
11 changes: 6 additions & 5 deletions packages/phone_log/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class _MyAppState extends State<MyApp> {
}


requestPermission() async {
void requestPermission() async {
bool res = await phoneLog.requestPermission();
print("permission request result is " + res.toString());
print("permission request result is: " + res.toString());
}

checkPermission() async {
bool res = await phoneLog.checkPermission();
print("permission is " + res.toString());
void checkPermission() async {
PermissionStatus res = await phoneLog.checkPermission();
print("permission is: " + res.toString());
}


@override
Widget build(BuildContext context) {
var children = <Widget>[
Expand Down
25 changes: 20 additions & 5 deletions packages/phone_log/lib/phone_log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import 'package:fixnum/fixnum.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

/// [PermissionStatus.granted] means that the permission is already granted.
/// [PermissionStatus.denied] means that the permission is not granted yet.
/// [PermissionStatus.deniedAndCannotRequest] means that the permission
/// request is denied previously and user has checked 'never ask again' check
/// box. In this case calling [requestPermission] method, the request
/// permission dialog would not pop up.
enum PermissionStatus {granted, denied, deniedAndCannotRequest}

/// Provide methods to access and fetch the phone log.
class PhoneLog {
final MethodChannel _channel;
Expand All @@ -17,13 +25,13 @@ class PhoneLog {
@visibleForTesting
PhoneLog.private(MethodChannel platformChannel):_channel = platformChannel;

/// Check a [permission] and return a [Future] with the result
Future<bool> checkPermission() async {
final bool isGranted = await _channel.invokeMethod("checkPermission", null);
return isGranted;
/// Check a [permission] and return a [Future] of the [PermissionStatus].
Future<PermissionStatus> checkPermission() async {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Please document it in CHANGELOG.md and up the version.

Also update the documentation. "return a Future with the result" is not very informative. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

final String status = await _channel.invokeMethod("checkPermission", null);
return permissionMap[status];
}

/// Request a [permission] and return a [Future] with the result
/// Request a [permission] and return a [Future] of bool.
Future<bool> requestPermission() async {
final bool isGranted =
await _channel.invokeMethod("requestPermission", null);
Expand All @@ -44,6 +52,12 @@ class PhoneLog {
}
}

var permissionMap = <String, PermissionStatus>{
'granted': PermissionStatus.granted,
'denied': PermissionStatus.denied,
'deniedAndCannotRequest': PermissionStatus.deniedAndCannotRequest
};

/// The class that carries all the data for one call history entry.
class CallRecord {
CallRecord({
Expand All @@ -52,6 +66,7 @@ class CallRecord {
this.callType,
this.dateYear,
this.dateMonth,
this.dateDay,
this.dateHour,
this.dateMinute,
this.dateSecond,
Expand Down
32 changes: 31 additions & 1 deletion packages/phone_log/test/phone_log_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ void main() {
dynamic arguments;
MockPlatformChannel mockChannel;
MockPlatformChannel mockChannelForGetLogs;
MockPlatformChannel mockChannelForGranted;
MockPlatformChannel mockChannelForDenied;
MockPlatformChannel mockChannelForDeniedCannotRequest;

setUp(() {
mockChannel = new MockPlatformChannel();
mockChannelForGetLogs = new MockPlatformChannel();
mockChannelForGranted = new MockPlatformChannel();
mockChannelForDenied = new MockPlatformChannel();
mockChannelForDeniedCannotRequest = new MockPlatformChannel();

when(mockChannel.invokeMethod(typed(any), any))
.thenAnswer((Invocation invocation) {
Expand All @@ -24,7 +30,7 @@ void main() {
});

when(mockChannelForGetLogs.invokeMethod('getPhoneLogs', any))
.thenReturn(new Future(() => [
.thenAnswer((_) => new Future(() => [
{
'formattedNumber': '123 123 1234',
'number': '1231231234',
Expand All @@ -38,6 +44,15 @@ void main() {
'duration': 123
}
]));

when(mockChannelForGranted.invokeMethod('checkPermission', any))
.thenAnswer((_) => new Future(() => 'granted'));

when(mockChannelForDenied.invokeMethod('checkPermission', any))
.thenAnswer((_) => new Future(() => 'denied'));

when(mockChannelForDeniedCannotRequest.invokeMethod('checkPermission', any))
.thenAnswer((_) => new Future(() => 'deniedAndCannotRequest'));
});

group('Phone log plugin', () {
Expand Down Expand Up @@ -70,6 +85,21 @@ void main() {

expect(invokedMethod, 'checkPermission');
expect(arguments, null);

var phoneLogGranted = new PhoneLog.private(mockChannelForGranted);
var permissionGranted = await phoneLogGranted.checkPermission();

expect(permissionGranted, PermissionStatus.granted);

var phoneLogDenied = new PhoneLog.private(mockChannelForDenied);
var permissionDenied = await phoneLogDenied.checkPermission();

expect(permissionDenied, PermissionStatus.denied);

var phoneLogCannotRequest = new PhoneLog.private(mockChannelForDeniedCannotRequest);
var permissionCannotRequest = await phoneLogCannotRequest.checkPermission();

expect(permissionCannotRequest, PermissionStatus.deniedAndCannotRequest);
});

test('request permission', () async {
Expand Down