Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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
46 changes: 32 additions & 14 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 @@ -15,18 +23,18 @@ class PhoneLog {
factory PhoneLog() => _instance;

@visibleForTesting
PhoneLog.private(MethodChannel platformChannel):_channel = platformChannel;
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 {
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);
await _channel.invokeMethod("requestPermission", null);
return isGranted;
}

Expand All @@ -36,14 +44,23 @@ class PhoneLog {
///The unit of [duration] is second.
Future<Iterable<CallRecord>> getPhoneLogs(
{Int64 startDate, Int64 duration}) async {
var _startDate = startDate?.toString();
var _duration = duration?.toString();
Iterable records = await _channel.invokeMethod(
'getPhoneLogs', {"startDate": _startDate, "duration": _duration});
return records?.map((m) => new CallRecord.fromMap(m));
final String _startDate = startDate?.toString();
final String _duration = duration?.toString();

final Iterable<Map> records = (await _channel.invokeMethod('getPhoneLogs',
<String, String>{"startDate": _startDate, "duration": _duration}))
.cast<Map>();
return records
?.map((m) => new CallRecord.fromMap(m.cast<String, Object>()));
}
}

Map<String, PermissionStatus> 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 +69,7 @@ class CallRecord {
this.callType,
this.dateYear,
this.dateMonth,
this.dateDay,
this.dateHour,
this.dateMinute,
this.dateSecond,
Expand All @@ -61,7 +79,7 @@ class CallRecord {
String formattedNumber, number, callType;
int dateYear, dateMonth, dateDay, dateHour, dateMinute, dateSecond, duration;

CallRecord.fromMap(Map m) {
CallRecord.fromMap(Map<String, Object> m) {
formattedNumber = m['formattedNumber'];
number = m['number'];
callType = m['callType'];
Expand All @@ -73,4 +91,4 @@ class CallRecord {
dateSecond = m['dateSecond'];
duration = m['duration'];
}
}
}
2 changes: 1 addition & 1 deletion packages/phone_log/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: phone_log
description: A new flutter plugin project.
version: 0.0.2
version: 0.0.3
author: Jiaming Cheng <jiamingc@google.com>
homepage: https://github.com/jiajiabingcheng/phone_log

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