diff --git a/.gitignore b/.gitignore index d82c06bd..fd45cbc3 100644 --- a/.gitignore +++ b/.gitignore @@ -76,3 +76,5 @@ unlinked_spec.ds # code coverage coverage +example/ios/Flutter/ephemeral/flutter_lldbinit +example/ios/Flutter/ephemeral/flutter_lldb_helper.py diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..8501eac7 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,3 @@ +analyzer: + exclude: + - "**" 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..d7170a64 100644 --- a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java +++ b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java @@ -41,7 +41,6 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry; -import io.flutter.plugin.common.PluginRegistry.Registrar; import static android.app.Activity.RESULT_CANCELED; import static android.provider.ContactsContract.CommonDataKinds; @@ -66,16 +65,6 @@ public class ContactsServicePlugin implements MethodCallHandler, FlutterPlugin, private final ExecutorService executor = new ThreadPoolExecutor(0, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(1000)); - private void initDelegateWithRegister(Registrar registrar) { - this.delegate = new ContactServiceDelegateOld(registrar); - } - - public static void registerWith(Registrar registrar) { - ContactsServicePlugin instance = new ContactsServicePlugin(); - instance.initInstance(registrar.messenger(), registrar.context()); - instance.initDelegateWithRegister(registrar); - } - private void initInstance(BinaryMessenger messenger, Context context) { methodChannel = new MethodChannel(messenger, "github.com/clovisnicolas/flutter_contacts"); methodChannel.setMethodCallHandler(this); @@ -282,21 +271,24 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent intent) } if (requestCode == REQUEST_OPEN_CONTACT_PICKER) { - if (resultCode == RESULT_CANCELED) { + if (resultCode == RESULT_CANCELED || intent == null) { 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); + if (cursor != 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); + } + cursor.close(); } else { - Log.e(LOG_TAG, "onActivityResult - cursor.moveToFirst() returns false"); finishWithResult(FORM_OPERATION_CANCELED); - }}else{return true;} - cursor.close(); + } return true; } @@ -374,25 +366,6 @@ private void openDeviceContactPicker(Result result, boolean localizedLabels) { result.success(FORM_COULD_NOT_BE_OPEN); } } - - private class ContactServiceDelegateOld extends BaseContactsServiceDelegate { - private final PluginRegistry.Registrar registrar; - - ContactServiceDelegateOld(PluginRegistry.Registrar registrar) { - this.registrar = registrar; - registrar.addActivityResultListener(this); - } - - @Override - void startIntent(Intent intent, int request) { - if (registrar.activity() != null) { - registrar.activity().startActivityForResult(intent, request); - } else { - registrar.context().startActivity(intent); - } - } - } - private class ContactServiceDelegate extends BaseContactsServiceDelegate { private final Context context; private ActivityPluginBinding activityPluginBinding; diff --git a/example/pubspec.yaml b/example/pubspec.yaml index f2c15eb8..daa331c1 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,7 +1,7 @@ name: contacts_service_example description: Demonstrates how to use the contacts_service plugin. environment: - sdk: '>=2.10.0 <3.0.0' + sdk: '>=2.12.0 <4.0.0' dependencies: flutter: sdk: flutter diff --git a/ios/Classes/SwiftContactsServicePlugin.swift b/ios/Classes/SwiftContactsServicePlugin.swift index 8babff12..bd8ea20b 100644 --- a/ios/Classes/SwiftContactsServicePlugin.swift +++ b/ios/Classes/SwiftContactsServicePlugin.swift @@ -13,7 +13,19 @@ public class SwiftContactsServicePlugin: NSObject, FlutterPlugin, CNContactViewC public static func register(with registrar: FlutterPluginRegistrar) { let channel = FlutterMethodChannel(name: "github.com/clovisnicolas/flutter_contacts", binaryMessenger: registrar.messenger()) - let rootViewController = UIApplication.shared.delegate!.window!!.rootViewController!; + var rootViewController: UIViewController + if #available(iOS 13.0, *), + let sceneWindow = UIApplication.shared.connectedScenes + .compactMap({ $0 as? UIWindowScene }) + .flatMap({ $0.windows }) + .first(where: { $0.isKeyWindow }), + let root = sceneWindow.rootViewController { + rootViewController = root + } else if let delegate = UIApplication.shared.delegate, let win = delegate.window, let root = win?.rootViewController { + rootViewController = root + } else { + rootViewController = UIViewController() + } let instance = SwiftContactsServicePlugin(rootViewController) registrar.addMethodCallDelegate(instance, channel: channel) instance.preLoadContactView()