diff --git a/android/build.gradle b/android/build.gradle index 2feed78f..5cdcf125 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -22,6 +22,7 @@ rootProject.allprojects { apply plugin: 'com.android.library' android { + namespace 'flutter.plugins.contactsservice.contactsservice' compileSdkVersion 30 defaultConfig { diff --git a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java index 8c69de78..d58ea3e5 100644 --- a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java +++ b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java @@ -271,7 +271,7 @@ void finishWithResult(Object result) { @Override public boolean onActivityResult(int requestCode, int resultCode, Intent intent) { - if(requestCode == REQUEST_OPEN_EXISTING_CONTACT || requestCode == REQUEST_OPEN_CONTACT_FORM) { + if (requestCode == REQUEST_OPEN_EXISTING_CONTACT || requestCode == REQUEST_OPEN_CONTACT_FORM) { try { Uri ur = intent.getData(); finishWithResult(getContactByIdentifier(ur.getLastPathSegment())); @@ -286,17 +286,27 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent intent) finishWithResult(FORM_OPERATION_CANCELED); return true; } - Uri contactUri = intent.getData(); - if (intent != null){ - Cursor cursor = contentResolver.query(contactUri, null, null, null, null); - if (cursor.moveToFirst()) { - String id = contactUri.getLastPathSegment(); - getContacts("openDeviceContactPicker", id, false, false, false, localizedLabels, this.result); - } else { - Log.e(LOG_TAG, "onActivityResult - cursor.moveToFirst() returns false"); - finishWithResult(FORM_OPERATION_CANCELED); - }}else{return true;} - cursor.close(); + + Cursor cursor = null; + + try { + if (intent != null) { + Uri contactUri = intent.getData(); + cursor = contentResolver.query(contactUri, null, null, null, null); + if (cursor != null && cursor.moveToFirst()) { + String id = contactUri.getLastPathSegment(); + getContacts("openDeviceContactPicker", id, false, false, false, localizedLabels, this.result); + } else { + Log.e(LOG_TAG, "onActivityResult - cursor.moveToFirst() returns false"); + finishWithResult(FORM_OPERATION_CANCELED); + } + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + return true; } @@ -304,6 +314,7 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent intent) return false; } + void openExistingContact(Contact contact) { String identifier = contact.identifier; try {