Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Added a new dart api function to add the functionality to get just a … #10

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28
buildToolsVersion '28.0.3'

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,30 @@ private ContactPickerPlugin(Activity activity) {

@Override
public void onMethodCall(MethodCall call, Result result) {
Uri intentUri;
if (call.method.equals("selectContact")) {
if (pendingResult != null) {
pendingResult.error("multiple_requests", "Cancelled by a second request.", null);
pendingResult = null;
}
pendingResult = result;

Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
String requestType = "contacts";
if (call.hasArgument("type")) {
requestType = call.argument("type");
}

switch (requestType) {
case "phone":
intentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
break;
case "contacts":
default:
intentUri = ContactsContract.Contacts.CONTENT_URI;
break;
}

Intent i = new Intent(Intent.ACTION_PICK, intentUri);
activity.startActivityForResult(i, PICK_CONTACT);
} else {
result.notImplemented();
Expand All @@ -68,17 +84,20 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
Cursor cursor = activity.getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();

int phoneType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String label = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(activity.getResources(), phoneType, customLabel);
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String fullName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

HashMap<String, Object> phoneNumber = new HashMap<>();
phoneNumber.put("number", number);
phoneNumber.put("label", label);

HashMap<String, Object> contact = new HashMap<>();

int phoneTypeIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
if (phoneTypeIndex != -1) {
int phoneType = cursor.getInt(phoneTypeIndex);
String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String label = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(activity.getResources(), phoneType, customLabel);
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber.put("number", number);
phoneNumber.put("label", label);
}
String fullName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

contact.put("fullName", fullName);
contact.put("phoneNumber", phoneNumber);

Expand Down
12 changes: 6 additions & 6 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28
buildToolsVersion '28.0.3'

lintOptions {
disable 'InvalidPackage'
Expand All @@ -26,7 +26,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.yourcompany.contactpickerexample"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -46,7 +46,7 @@ flutter {
}

dependencies {
androidTestCompile 'com.android.support:support-annotations:25.4.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support:support-annotations:28.0.0'
androidTestCompile 'com.android.support.test:runner:1.0.2'
androidTestCompile 'com.android.support.test:rules:1.0.2'
}
12 changes: 11 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[
new MaterialButton(
color: Colors.blue,
child: new Text("CLICK ME"),
child: new Text("Phone"),
onPressed: () async {
Contact contact = await _contactPicker.selectContact();
setState(() {
_contact = contact;
});
},
),
new MaterialButton(
color: Colors.blue,
child: new Text("Contact"),
onPressed: () async {
Contact contact = await _contactPicker.selectContactName();
setState(() {
_contact = contact;
});
},
),
new Text(
_contact == null ? 'No contact selected.' : _contact.toString(),
),
Expand Down
64 changes: 0 additions & 64 deletions lib/contact_picker.dart

This file was deleted.