fluttercommunity / flutter_contacts

Contacts Service - A Flutter plugin to retrieve and manage contacts on Android and iOS devices. Maintainer: @lukasgit
https://pub.dev/packages/contacts_service
MIT License
153 stars 69 forks source link

java.lang.SecurityException: Permission Denial #7

Closed zoechi closed 6 years ago

zoechi commented 6 years ago

I added <uses-permission android:name="android.permission.READ_CONTACTS" /> to AndroidManifest.xml, but I still get

+2189 ms] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983): Failed to handle method call
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983): java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{cee4cc6 20983:com.lookatyou.lookatmybaby/u0a377} (pid=20983, uid=10377) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.os.Parcel.readException(Parcel.java:2004)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.os.Parcel.readException(Parcel.java:1950)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4758)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.app.ActivityThread.acquireProvider(ActivityThread.java:5836)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2526)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1780)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.content.ContentResolver.query(ContentResolver.java:738)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.content.ContentResolver.query(ContentResolver.java:704)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.content.ContentResolver.query(ContentResolver.java:662)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.getCursor(ContactsServicePlugin.java:123)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.getContacts(ContactsServicePlugin.java:107)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.onMethodCall(ContactsServicePlugin.java:49)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:191)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:136)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.os.MessageQueue.nativePollOnce(Native Method)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.os.MessageQueue.next(MessageQueue.java:325)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.os.Looper.loop(Looper.java:142)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at android.app.ActivityThread.main(ActivityThread.java:6494)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at java.lang.reflect.Method.invoke(Native Method)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
[        ] E/MethodChannel#github.com/clovisnicolas/flutter_contacts(20983):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
[  +25 ms] I/flutter (20983): 598 2018-04-05 15:28:39.239197 SEVERE lookatmybaby_flutter.sentry_error_reporter: 
[   +1 ms] I/flutter (20983):     error: PlatformException(error, Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{cee4cc6 20983:com.lookatyou.lookatmybaby/u0a377} (pid=20983, uid=10377) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS, null)
[   +4 ms] I/flutter (20983): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:544:7)
[        ] I/flutter (20983): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:154:18)
[        ] I/flutter (20983): <asynchronous suspension>
[        ] I/flutter (20983): #2      ContactsService.getContacts (package:contacts_service/contacts_service.dart:13:45)
[        ] I/flutter (20983): <asynchronous suspension>
[        ] I/flutter (20983): #3      InvitationDaoFirebase.loadContacts (package:lookatmybaby_flutter/src/server_api_firebase/invitation_dao_firebase.dart:140:50)
[        ] I/flutter (20983): <asynchronous suspension>
[        ] I/flutter (20983): #4      new InvitationController (package:lookatmybaby_shared/src/client/controller/invitation_controller.dart:27:26)
[        ] I/flutter (20983): #5      AppController.invitationController (package:lookatmybaby_shared/src/client/controller/app_controller.dart:241:37)
[        ] I/flutter (20983): #6      _InvitePeopleScreenState.controller (package:lookatmybaby_flutter/widgets/screens/invite_people_screen/invite_people_screen.dart:27:56)
[        ] I/flutter (20983): #7      ScreenState._buildAppBarCallback (package:lookatmybaby_flutter/widgets/generic_widgets/screen_state.dart:134:16)
[        ] I/flutter (20983): #8      SearchBar.bu
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel dev, v0.2.2-pre.7, on Mac OS X 10.13.3 17D102, locale en-AT)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 9.3)
[✓] Android Studio (version 3.1)
[✓] IntelliJ IDEA Ultimate Edition (version 2018.1)
[✓] Connected devices (2 available)

Do you have any idea how to fix that?

zoechi commented 6 years ago

The image_picker plugin had a similar problem recently.

When I go to Settings > Apps & notifications > My App > Permissions I get Contacts shown as disabled. When I enable it, reading the contacts works.

I'd expect to get asked in a popup if permissions are not set (like the image_picker plugin does for camera and storage access)

clovisnicolas commented 6 years ago

I wish we would have a permission plugin such as https://github.com/yonahforst/react-native-permissions

zoechi commented 6 years ago

I haven't done any mobile development before Flutter and didn't have time yet to dive into it. I hope someone else knows how to get this fixed ;-)

zoechi commented 6 years ago

https://github.com/Lyokone/flutterlocation/pull/25 seems to fix a similar issue

freewheelnat commented 6 years ago

There seems to be a new plugin https://github.com/Ethras/flutter_simple_permissions . It looks incomplete though - no "shouldShowRationale" for Android, and only a subset of permissions supported for now. I wrote a tutorial about Android permissions in Flutter ( http://cogitas.net/android-runtime-permission-flow-flutter/ ) so I'm going to see if I can improve that plugin for the Android side, as it would be great to have a plugin handling this.

knight-shade commented 6 years ago

I am confused whether we need to have a separate plugin for permission handling or the plugin itself should take care of permissions ?

reeteshkumar39 commented 6 years ago

how can get all contacts automatic without user interaction for enabling permission

zoechi commented 6 years ago

@reeteshkumar39 I don't think you should be able to access a users contacts without his consent.

knight-shade commented 6 years ago

@zoechi @reeteshkumar39 nope you can't access without the user giving the permission, thsts why its crashing

rodydavis commented 6 years ago

There needs to be a way to request contacts permission on initPlatformState and provide a fallback if permission is denied. The app will crash otherwise.

freewheelnat commented 6 years ago

Following from my previous comment, I have done the PR to add Android contact permissions (read/write) to the simple_permissions plugin; the maintainer has now done the iOS code and has published an updated version of the plugin. Therefore, I would recommend using that plugin.

I will work on the PR for "shouldShowRationale" flow soon (see my comment above), but I prioritised the PR for adding contacts.

rodydavis commented 6 years ago

@freewheelnat thanks for that I had actually today just finished getting the permissions working on iOS 9.0 and later for Reading and Writing Contacts.

https://github.com/AppleEducate/flutter_simple_permissions

clovisnicolas commented 6 years ago

Thanks for handling contact permissions in your plugin, i'll add a link to this in the Readme.