mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Merging Fil's contact changes with Bryce's Plugins
This commit is contained in:
parent
1c0de5ad8d
commit
633100a3ce
@ -101,7 +101,11 @@
|
||||
|
||||
function get_contacts()
|
||||
{
|
||||
navigator.ContactManager.getAllContacts(count_contacts, fail, null);
|
||||
var obj = new Contact();
|
||||
var name = new ContactName();
|
||||
name.givenName = '';
|
||||
obj.name = name;
|
||||
navigator.contacts.find(obj, count_contacts, fail);
|
||||
}
|
||||
|
||||
function count_contacts(contacts)
|
||||
|
98
framework/src/com/phonegap/ContactAccessor.java
Normal file
98
framework/src/com/phonegap/ContactAccessor.java
Normal file
@ -0,0 +1,98 @@
|
||||
// Taken from Android Tutorials
|
||||
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.phonegap;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* This abstract class defines SDK-independent API for communication with
|
||||
* Contacts Provider. The actual implementation used by the application depends
|
||||
* on the level of API available on the device. If the API level is Cupcake or
|
||||
* Donut, we want to use the {@link ContactAccessorSdk3_4} class. If it is
|
||||
* Eclair or higher, we want to use {@link ContactAccessorSdk5}.
|
||||
*/
|
||||
public abstract class ContactAccessor {
|
||||
|
||||
public class ContactTriplet
|
||||
{
|
||||
public String name = "";
|
||||
public String email = "";
|
||||
public String phone = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Static singleton instance of {@link ContactAccessor} holding the
|
||||
* SDK-specific implementation of the class.
|
||||
*/
|
||||
private static ContactAccessor sInstance;
|
||||
protected final String LOG_TAG = "ContactsAccessor";
|
||||
protected Activity mApp;
|
||||
protected WebView mView;
|
||||
|
||||
public static ContactAccessor getInstance(WebView view, Activity app) {
|
||||
if (sInstance == null) {
|
||||
String className;
|
||||
|
||||
/*
|
||||
* Check the version of the SDK we are running on. Choose an
|
||||
* implementation class designed for that version of the SDK.
|
||||
*
|
||||
* Unfortunately we have to use strings to represent the class
|
||||
* names. If we used the conventional ContactAccessorSdk5.class.getName()
|
||||
* syntax, we would get a ClassNotFoundException at runtime on pre-Eclair SDKs.
|
||||
* Using the above syntax would force Dalvik to load the class and try to
|
||||
* resolve references to all other classes it uses. Since the pre-Eclair
|
||||
* does not have those classes, the loading of ContactAccessorSdk5 would fail.
|
||||
*/
|
||||
|
||||
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
|
||||
className = "com.phonegap.ContactAccessorSdk3_4";
|
||||
} else {
|
||||
className = "com.phonegap.ContactAccessorSdk5";
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the required class by name and instantiate it.
|
||||
*/
|
||||
try {
|
||||
Class<? extends ContactAccessor> clazz =
|
||||
Class.forName(className).asSubclass(ContactAccessor.class);
|
||||
// Grab constructor for contactsmanager class dynamically.
|
||||
Constructor<? extends ContactAccessor> classConstructor = clazz.getConstructor(Class.forName("android.webkit.WebView"), Class.forName("android.app.Activity"));
|
||||
sInstance = classConstructor.newInstance(view, app);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles searching through SDK-specific contacts API.
|
||||
*/
|
||||
public abstract void search(String name, String npa, String email);
|
||||
}
|
287
framework/src/com/phonegap/ContactAccessorSdk3_4.java
Normal file
287
framework/src/com/phonegap/ContactAccessorSdk3_4.java
Normal file
@ -0,0 +1,287 @@
|
||||
// Taken from Android tutorials
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.phonegap;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.AsyncQueryHandler;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.net.Uri;
|
||||
import android.provider.Contacts.ContactMethods;
|
||||
import android.provider.Contacts.People;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ContactAccessor} that uses legacy Contacts API.
|
||||
* These APIs are deprecated and should not be used unless we are running on a
|
||||
* pre-Eclair SDK.
|
||||
* <p>
|
||||
* There are several reasons why we wouldn't want to use this class on an Eclair device:
|
||||
* <ul>
|
||||
* <li>It would see at most one account, namely the first Google account created on the device.
|
||||
* <li>It would work through a compatibility layer, which would make it inherently less efficient.
|
||||
* <li>Not relevant to this particular example, but it would not have access to new kinds
|
||||
* of data available through current APIs.
|
||||
* </ul>
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContactAccessorSdk3_4 extends ContactAccessor {
|
||||
|
||||
private Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
||||
private Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
||||
private Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
|
||||
|
||||
public ContactAccessorSdk3_4(WebView view, Activity app)
|
||||
{
|
||||
mApp = app;
|
||||
mView = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search(String name, String npa, String email) {
|
||||
if (email.length() > 0)
|
||||
searchByEmail(email);
|
||||
else
|
||||
searchPeople(name, npa);
|
||||
}
|
||||
|
||||
private void searchByEmail(String email)
|
||||
{
|
||||
String[] projection = new String[] {
|
||||
ContactMethods._ID,
|
||||
ContactMethods.DATA,
|
||||
ContactMethods.KIND,
|
||||
ContactMethods.PERSON_ID
|
||||
};
|
||||
String[] variables = new String[] {
|
||||
email
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
getMethodData(myCursor);
|
||||
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(this.LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void searchPeople(String name, String number)
|
||||
{
|
||||
String conditions = "";
|
||||
|
||||
if (name.length() == 0)
|
||||
{
|
||||
name = "%";
|
||||
conditions += People.NAME + " LIKE ? AND ";
|
||||
}
|
||||
else
|
||||
{
|
||||
conditions += People.NAME + " = ? AND ";
|
||||
}
|
||||
|
||||
if (number.length() == 0)
|
||||
number = "%";
|
||||
else
|
||||
{
|
||||
number = number.replace('+', '%');
|
||||
number = number.replace('.', '%');
|
||||
number = number.replace('-', '%');
|
||||
}
|
||||
|
||||
conditions += People.NUMBER + " LIKE ? ";
|
||||
|
||||
String[] projection = new String[] {
|
||||
People._ID,
|
||||
People.NAME,
|
||||
People.NUMBER,
|
||||
People.PRIMARY_EMAIL_ID
|
||||
};
|
||||
|
||||
String[] variables = new String[] {
|
||||
name, number
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
||||
conditions, variables , People.NAME + " ASC");
|
||||
processResults(myCursor, false);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(this.LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void processResults(Cursor cur, boolean all){
|
||||
|
||||
if (cur.moveToFirst()) {
|
||||
|
||||
String name;
|
||||
String phoneNumber;
|
||||
String email_id;
|
||||
String email;
|
||||
|
||||
int nameColumn = cur.getColumnIndex(People.NAME);
|
||||
int phoneColumn = cur.getColumnIndex(People.NUMBER);
|
||||
int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID);
|
||||
|
||||
do {
|
||||
// Get the field values
|
||||
name = cur.getString(nameColumn);
|
||||
phoneNumber = cur.getString(phoneColumn);
|
||||
email_id = cur.getString(emailIdColumn);
|
||||
if (email_id != null && email_id.length() > 0)
|
||||
email = getEmail(email_id);
|
||||
else
|
||||
email = "";
|
||||
|
||||
// Code for backwards compatibility with the OLD Contacts API
|
||||
if (all)
|
||||
mView.loadUrl("javascript:navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
||||
|
||||
} while (cur.moveToNext());
|
||||
if (all)
|
||||
mView.loadUrl("javascript:navigator.ContactManager.droidDone()");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.contacts.droidDone();");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(all)
|
||||
mView.loadUrl("javascript:navigator.ContactManager.fail()");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.contacts.fail('None found!')");
|
||||
}
|
||||
}
|
||||
|
||||
private void getMethodData(Cursor cur)
|
||||
{
|
||||
ContactTriplet data = new ContactTriplet();
|
||||
String id;
|
||||
String email;
|
||||
|
||||
if (cur.moveToFirst()) {
|
||||
|
||||
int idColumn = cur.getColumnIndex(ContactMethods._ID);
|
||||
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||
do {
|
||||
// Get the field values
|
||||
id = cur.getString(idColumn);
|
||||
email = cur.getString(emailColumn);
|
||||
|
||||
data = getContactData(id);
|
||||
if(data != null)
|
||||
{
|
||||
data.email = email;
|
||||
mView.loadUrl("javascript:navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"')");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
mView.loadUrl("javascript:navigator.contacts.droidDoneContacts();");
|
||||
}
|
||||
}
|
||||
|
||||
private ContactTriplet getContactData(String id) {
|
||||
ContactTriplet data = null;
|
||||
String[] projection = new String[] {
|
||||
People._ID,
|
||||
People.NAME,
|
||||
People.NUMBER,
|
||||
People.PRIMARY_EMAIL_ID
|
||||
};
|
||||
|
||||
String[] variables = new String[] {
|
||||
id
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
||||
People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
|
||||
data = getTriplet(myCursor);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private ContactTriplet getTriplet(Cursor cur) {
|
||||
ContactTriplet data = new ContactTriplet();
|
||||
if (cur.moveToFirst()) {
|
||||
|
||||
int nameColumn = cur.getColumnIndex(People.NAME);
|
||||
int numberColumn = cur.getColumnIndex(People.NUMBER);
|
||||
do {
|
||||
|
||||
data.name = cur.getString(nameColumn);
|
||||
data.phone = cur.getString(numberColumn);
|
||||
|
||||
} while (cur.moveToNext());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private String getEmailColumnData(Cursor cur)
|
||||
{
|
||||
String email = "";
|
||||
if (cur != null && cur.moveToFirst()) {
|
||||
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||
do {
|
||||
// Get the field values
|
||||
email = cur.getString(emailColumn);
|
||||
} while (cur.moveToNext());
|
||||
}
|
||||
return email;
|
||||
}
|
||||
|
||||
private String getEmail(String id)
|
||||
{
|
||||
String email = "";
|
||||
String[] projection = new String[] {
|
||||
ContactMethods._ID,
|
||||
ContactMethods.DATA,
|
||||
ContactMethods.KIND
|
||||
};
|
||||
String[] variables = new String[] {
|
||||
id
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
email = getEmailColumnData(myCursor);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
return email;
|
||||
}
|
||||
}
|
108
framework/src/com/phonegap/ContactAccessorSdk5.java
Normal file
108
framework/src/com/phonegap/ContactAccessorSdk5.java
Normal file
@ -0,0 +1,108 @@
|
||||
// Taken from Android tutorials
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.phonegap;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.Contacts.People;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ContactAccessor} that uses current Contacts API.
|
||||
* This class should be used on Eclair or beyond, but would not work on any earlier
|
||||
* release of Android. As a matter of fact, it could not even be loaded.
|
||||
* <p>
|
||||
* This implementation has several advantages:
|
||||
* <ul>
|
||||
* <li>It sees contacts from multiple accounts.
|
||||
* <li>It works with aggregated contacts. So for example, if the contact is the result
|
||||
* of aggregation of two raw contacts from different accounts, it may return the name from
|
||||
* one and the phone number from the other.
|
||||
* <li>It is efficient because it uses the more efficient current API.
|
||||
* <li>Not obvious in this particular example, but it has access to new kinds
|
||||
* of data available exclusively through the new APIs. Exercise for the reader: add support
|
||||
* for nickname (see {@link android.provider.ContactsContract.CommonDataKinds.Nickname}) or
|
||||
* social status updates (see {@link android.provider.ContactsContract.StatusUpdates}).
|
||||
* </ul>
|
||||
*/
|
||||
public class ContactAccessorSdk5 extends ContactAccessor {
|
||||
|
||||
public ContactAccessorSdk5(WebView view, Activity app)
|
||||
{
|
||||
mApp = app;
|
||||
mView = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search(String name, String npa, String email) {
|
||||
if (name.length()==0) name = "%";
|
||||
// Get a cursor by creating the query.
|
||||
// TODO: parse name/number/email and dispatch to different query types.
|
||||
// Right now assumption is only name search. Lame but I'm on time constraints.
|
||||
ContentResolver cr = mApp.getContentResolver();
|
||||
Cursor cursor = cr.query(
|
||||
ContactsContract.Contacts.CONTENT_URI,
|
||||
new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.DISPLAY_NAME},
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
|
||||
new String[] {name},
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
while (cursor.moveToNext()) {
|
||||
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||
if (contactName.trim().length() == 0) continue;
|
||||
String phoneNumber = "null";
|
||||
String emailAddress = "null";
|
||||
|
||||
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
||||
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
|
||||
if (Boolean.parseBoolean(hasPhone)) {
|
||||
Cursor phones = cr.query(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
|
||||
null, null);
|
||||
if (phones.moveToFirst()) {
|
||||
phoneNumber = "'" + phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`') + "'";
|
||||
}
|
||||
phones.close();
|
||||
}
|
||||
Cursor emails = cr.query(
|
||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
|
||||
null, null);
|
||||
if (emails.moveToFirst()) {
|
||||
// This would allow you get several email addresses
|
||||
emailAddress = "'" + emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`') + "'";
|
||||
}
|
||||
emails.close();
|
||||
String contactAddJS = "javascript:navigator.contacts.droidFoundContact('" + contactName.replace('\'', '`') + "'," + phoneNumber + "," + emailAddress +")";
|
||||
mView.loadUrl(contactAddJS);
|
||||
}
|
||||
cursor.close();
|
||||
mView.loadUrl("javascript:navigator.contacts.droidDone();");
|
||||
}
|
||||
|
||||
}
|
@ -18,13 +18,14 @@ import android.database.sqlite.SQLiteException;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContactManager implements Plugin {
|
||||
|
||||
public class ContactTriplet
|
||||
{
|
||||
public String name = "";
|
||||
public String email = "";
|
||||
public String phone = "";
|
||||
}
|
||||
// public class ContactTriplet
|
||||
// {
|
||||
// public String name = "";
|
||||
// public String email = "";
|
||||
// public String phone = "";
|
||||
// }
|
||||
|
||||
private static ContactAccessor contactAccessor;
|
||||
WebView webView; // WebView object
|
||||
DroidGap ctx; // DroidGap object
|
||||
|
||||
@ -67,15 +68,20 @@ public class ContactManager implements Plugin {
|
||||
* @return A CommandResult object with a status and message.
|
||||
*/
|
||||
public PluginResult execute(String action, JSONArray args) {
|
||||
if (contactAccessor == null) {
|
||||
contactAccessor = ContactAccessor.getInstance(webView, ctx);
|
||||
}
|
||||
PluginResult.Status status = PluginResult.Status.OK;
|
||||
String result = "";
|
||||
|
||||
try {
|
||||
if (action.equals("getContactsAndSendBack")) {
|
||||
this.getContactsAndSendBack();
|
||||
}
|
||||
else if (action.equals("search")) {
|
||||
this.search(args.getString(0), args.getString(1), args.getString(2));
|
||||
//if (action.equals("getContactsAndSendBack")) {
|
||||
// contactAccessor.getContactsAndSendBack();
|
||||
//}
|
||||
//else if (action.equals("search")) {
|
||||
if (action.equals("search")) {
|
||||
Log.d(LOG_TAG, "Executing search using accessor");
|
||||
contactAccessor.search(args.getString(0), args.getString(1), args.getString(2));
|
||||
}
|
||||
return new PluginResult(status, result);
|
||||
} catch (JSONException e) {
|
||||
@ -129,263 +135,263 @@ public class ContactManager implements Plugin {
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
// This is to add backwards compatibility to the OLD Contacts API\
|
||||
public void getContactsAndSendBack()
|
||||
{
|
||||
String[] projection = new String[] {
|
||||
People._ID,
|
||||
People.NAME,
|
||||
People.NUMBER,
|
||||
People.PRIMARY_EMAIL_ID
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
null, null , People.NAME + " ASC");
|
||||
processResults(myCursor, true);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void search(String name, String npa, String email)
|
||||
{
|
||||
|
||||
if (email.length() > 0)
|
||||
searchByEmail(email);
|
||||
else
|
||||
searchPeople(name, npa);
|
||||
}
|
||||
|
||||
private void searchByEmail(String email)
|
||||
{
|
||||
String[] projection = new String[] {
|
||||
ContactMethods._ID,
|
||||
ContactMethods.DATA,
|
||||
ContactMethods.KIND,
|
||||
ContactMethods.PERSON_ID
|
||||
};
|
||||
String[] variables = new String[] {
|
||||
email
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = this.ctx.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
getMethodData(myCursor);
|
||||
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void searchPeople(String name, String number)
|
||||
{
|
||||
String conditions = "";
|
||||
|
||||
if (name.length() == 0)
|
||||
{
|
||||
name = "%";
|
||||
conditions += People.NAME + " LIKE ? AND ";
|
||||
}
|
||||
else
|
||||
{
|
||||
conditions += People.NAME + " = ? AND ";
|
||||
}
|
||||
|
||||
if (number.length() == 0)
|
||||
number = "%";
|
||||
else
|
||||
{
|
||||
number = number.replace('+', '%');
|
||||
number = number.replace('.', '%');
|
||||
number = number.replace('-', '%');
|
||||
}
|
||||
|
||||
conditions += People.NUMBER + " LIKE ? ";
|
||||
|
||||
String[] projection = new String[] {
|
||||
People._ID,
|
||||
People.NAME,
|
||||
People.NUMBER,
|
||||
People.PRIMARY_EMAIL_ID
|
||||
};
|
||||
|
||||
String[] variables = new String[] {
|
||||
name, number
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
conditions, variables , People.NAME + " ASC");
|
||||
processResults(myCursor, false);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void processResults(Cursor cur, boolean all){
|
||||
|
||||
if (cur.moveToFirst()) {
|
||||
|
||||
String name;
|
||||
String phoneNumber;
|
||||
String email_id;
|
||||
String email;
|
||||
|
||||
int nameColumn = cur.getColumnIndex(People.NAME);
|
||||
int phoneColumn = cur.getColumnIndex(People.NUMBER);
|
||||
int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID);
|
||||
|
||||
do {
|
||||
// Get the field values
|
||||
name = cur.getString(nameColumn);
|
||||
phoneNumber = cur.getString(phoneColumn);
|
||||
email_id = cur.getString(emailIdColumn);
|
||||
if (email_id != null && email_id.length() > 0)
|
||||
email = getEmail(email_id);
|
||||
else
|
||||
email = "";
|
||||
|
||||
// Code for backwards compatibility with the OLD Contacts API
|
||||
if (all) {
|
||||
this.ctx.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
}
|
||||
else {
|
||||
this.ctx.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
if (all) {
|
||||
this.ctx.sendJavascript("navigator.ContactManager.droidDone();");
|
||||
}
|
||||
else {
|
||||
this.ctx.sendJavascript("navigator.contacts.droidDone();");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (all) {
|
||||
this.ctx.sendJavascript("navigator.ContactManager.fail();");
|
||||
}
|
||||
else {
|
||||
this.ctx.sendJavascript("navigator.contacts.fail('None found!');");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void getMethodData(Cursor cur)
|
||||
{
|
||||
ContactTriplet data = new ContactTriplet();
|
||||
String id;
|
||||
String email;
|
||||
|
||||
if (cur.moveToFirst()) {
|
||||
|
||||
int idColumn = cur.getColumnIndex(ContactMethods._ID);
|
||||
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||
do {
|
||||
// Get the field values
|
||||
id = cur.getString(idColumn);
|
||||
email = cur.getString(emailColumn);
|
||||
|
||||
data = getContactData(id);
|
||||
if(data != null)
|
||||
{
|
||||
data.email = email;
|
||||
this.ctx.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
this.ctx.sendJavascript("navigator.contacts.droidDoneContacts();");
|
||||
}
|
||||
}
|
||||
|
||||
private ContactTriplet getContactData(String id) {
|
||||
ContactTriplet data = null;
|
||||
String[] projection = new String[] {
|
||||
People._ID,
|
||||
People.NAME,
|
||||
People.NUMBER,
|
||||
People.PRIMARY_EMAIL_ID
|
||||
};
|
||||
|
||||
String[] variables = new String[] {
|
||||
id
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
|
||||
data = getTriplet(myCursor);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private ContactTriplet getTriplet(Cursor cur) {
|
||||
ContactTriplet data = new ContactTriplet();
|
||||
if (cur.moveToFirst()) {
|
||||
|
||||
int nameColumn = cur.getColumnIndex(People.NAME);
|
||||
int numberColumn = cur.getColumnIndex(People.NUMBER);
|
||||
do {
|
||||
|
||||
data.name = cur.getString(nameColumn);
|
||||
data.phone = cur.getString(numberColumn);
|
||||
|
||||
} while (cur.moveToNext());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private String getEmailColumnData(Cursor cur)
|
||||
{
|
||||
String email = "";
|
||||
if (cur != null && cur.moveToFirst()) {
|
||||
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||
do {
|
||||
// Get the field values
|
||||
email = cur.getString(emailColumn);
|
||||
} while (cur.moveToNext());
|
||||
}
|
||||
return email;
|
||||
}
|
||||
|
||||
private String getEmail(String id)
|
||||
{
|
||||
String email = "";
|
||||
String[] projection = new String[] {
|
||||
ContactMethods._ID,
|
||||
ContactMethods.DATA,
|
||||
ContactMethods.KIND
|
||||
};
|
||||
String[] variables = new String[] {
|
||||
id
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Cursor myCursor = this.ctx.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
email = getEmailColumnData(myCursor);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
}
|
||||
|
||||
return email;
|
||||
}
|
||||
// public void getContactsAndSendBack()
|
||||
// {
|
||||
// String[] projection = new String[] {
|
||||
// People._ID,
|
||||
// People.NAME,
|
||||
// People.NUMBER,
|
||||
// People.PRIMARY_EMAIL_ID
|
||||
// };
|
||||
//
|
||||
// try{
|
||||
// Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
// null, null , People.NAME + " ASC");
|
||||
// processResults(myCursor, true);
|
||||
// }
|
||||
// catch (SQLiteException ex)
|
||||
// {
|
||||
// Log.d(LOG_TAG, ex.getMessage());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void search(String name, String npa, String email)
|
||||
// {
|
||||
//
|
||||
// if (email.length() > 0)
|
||||
// searchByEmail(email);
|
||||
// else
|
||||
// searchPeople(name, npa);
|
||||
// }
|
||||
//
|
||||
// private void searchByEmail(String email)
|
||||
// {
|
||||
// String[] projection = new String[] {
|
||||
// ContactMethods._ID,
|
||||
// ContactMethods.DATA,
|
||||
// ContactMethods.KIND,
|
||||
// ContactMethods.PERSON_ID
|
||||
// };
|
||||
// String[] variables = new String[] {
|
||||
// email
|
||||
// };
|
||||
//
|
||||
// try{
|
||||
// Cursor myCursor = this.ctx.managedQuery(mEmail, projection,
|
||||
// "contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
// getMethodData(myCursor);
|
||||
//
|
||||
// }
|
||||
// catch (SQLiteException ex)
|
||||
// {
|
||||
// Log.d(LOG_TAG, ex.getMessage());
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private void searchPeople(String name, String number)
|
||||
// {
|
||||
// String conditions = "";
|
||||
//
|
||||
// if (name.length() == 0)
|
||||
// {
|
||||
// name = "%";
|
||||
// conditions += People.NAME + " LIKE ? AND ";
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// conditions += People.NAME + " = ? AND ";
|
||||
// }
|
||||
//
|
||||
// if (number.length() == 0)
|
||||
// number = "%";
|
||||
// else
|
||||
// {
|
||||
// number = number.replace('+', '%');
|
||||
// number = number.replace('.', '%');
|
||||
// number = number.replace('-', '%');
|
||||
// }
|
||||
//
|
||||
// conditions += People.NUMBER + " LIKE ? ";
|
||||
//
|
||||
// String[] projection = new String[] {
|
||||
// People._ID,
|
||||
// People.NAME,
|
||||
// People.NUMBER,
|
||||
// People.PRIMARY_EMAIL_ID
|
||||
// };
|
||||
//
|
||||
// String[] variables = new String[] {
|
||||
// name, number
|
||||
// };
|
||||
//
|
||||
// try{
|
||||
// Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
// conditions, variables , People.NAME + " ASC");
|
||||
// processResults(myCursor, false);
|
||||
// }
|
||||
// catch (SQLiteException ex)
|
||||
// {
|
||||
// Log.d(LOG_TAG, ex.getMessage());
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private void processResults(Cursor cur, boolean all){
|
||||
//
|
||||
// if (cur.moveToFirst()) {
|
||||
//
|
||||
// String name;
|
||||
// String phoneNumber;
|
||||
// String email_id;
|
||||
// String email;
|
||||
//
|
||||
// int nameColumn = cur.getColumnIndex(People.NAME);
|
||||
// int phoneColumn = cur.getColumnIndex(People.NUMBER);
|
||||
// int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID);
|
||||
//
|
||||
// do {
|
||||
// // Get the field values
|
||||
// name = cur.getString(nameColumn);
|
||||
// phoneNumber = cur.getString(phoneColumn);
|
||||
// email_id = cur.getString(emailIdColumn);
|
||||
// if (email_id != null && email_id.length() > 0)
|
||||
// email = getEmail(email_id);
|
||||
// else
|
||||
// email = "";
|
||||
//
|
||||
// // Code for backwards compatibility with the OLD Contacts API
|
||||
// if (all) {
|
||||
// this.ctx.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
// }
|
||||
// else {
|
||||
// this.ctx.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
// }
|
||||
// } while (cur.moveToNext());
|
||||
// if (all) {
|
||||
// this.ctx.sendJavascript("navigator.ContactManager.droidDone();");
|
||||
// }
|
||||
// else {
|
||||
// this.ctx.sendJavascript("navigator.contacts.droidDone();");
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (all) {
|
||||
// this.ctx.sendJavascript("navigator.ContactManager.fail();");
|
||||
// }
|
||||
// else {
|
||||
// this.ctx.sendJavascript("navigator.contacts.fail('None found!');");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void getMethodData(Cursor cur)
|
||||
// {
|
||||
// ContactTriplet data = new ContactTriplet();
|
||||
// String id;
|
||||
// String email;
|
||||
//
|
||||
// if (cur.moveToFirst()) {
|
||||
//
|
||||
// int idColumn = cur.getColumnIndex(ContactMethods._ID);
|
||||
// int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||
// do {
|
||||
// // Get the field values
|
||||
// id = cur.getString(idColumn);
|
||||
// email = cur.getString(emailColumn);
|
||||
//
|
||||
// data = getContactData(id);
|
||||
// if(data != null)
|
||||
// {
|
||||
// data.email = email;
|
||||
// this.ctx.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
|
||||
// }
|
||||
// } while (cur.moveToNext());
|
||||
// this.ctx.sendJavascript("navigator.contacts.droidDoneContacts();");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private ContactTriplet getContactData(String id) {
|
||||
// ContactTriplet data = null;
|
||||
// String[] projection = new String[] {
|
||||
// People._ID,
|
||||
// People.NAME,
|
||||
// People.NUMBER,
|
||||
// People.PRIMARY_EMAIL_ID
|
||||
// };
|
||||
//
|
||||
// String[] variables = new String[] {
|
||||
// id
|
||||
// };
|
||||
//
|
||||
// try{
|
||||
// Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
// People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
|
||||
// data = getTriplet(myCursor);
|
||||
// }
|
||||
// catch (SQLiteException ex)
|
||||
// {
|
||||
// Log.d(LOG_TAG, ex.getMessage());
|
||||
// }
|
||||
//
|
||||
// return data;
|
||||
// }
|
||||
//
|
||||
// private ContactTriplet getTriplet(Cursor cur) {
|
||||
// ContactTriplet data = new ContactTriplet();
|
||||
// if (cur.moveToFirst()) {
|
||||
//
|
||||
// int nameColumn = cur.getColumnIndex(People.NAME);
|
||||
// int numberColumn = cur.getColumnIndex(People.NUMBER);
|
||||
// do {
|
||||
//
|
||||
// data.name = cur.getString(nameColumn);
|
||||
// data.phone = cur.getString(numberColumn);
|
||||
//
|
||||
// } while (cur.moveToNext());
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
//
|
||||
// private String getEmailColumnData(Cursor cur)
|
||||
// {
|
||||
// String email = "";
|
||||
// if (cur != null && cur.moveToFirst()) {
|
||||
// int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
|
||||
// do {
|
||||
// // Get the field values
|
||||
// email = cur.getString(emailColumn);
|
||||
// } while (cur.moveToNext());
|
||||
// }
|
||||
// return email;
|
||||
// }
|
||||
//
|
||||
// private String getEmail(String id)
|
||||
// {
|
||||
// String email = "";
|
||||
// String[] projection = new String[] {
|
||||
// ContactMethods._ID,
|
||||
// ContactMethods.DATA,
|
||||
// ContactMethods.KIND
|
||||
// };
|
||||
// String[] variables = new String[] {
|
||||
// id
|
||||
// };
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// Cursor myCursor = this.ctx.managedQuery(mEmail, projection,
|
||||
// "contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
// email = getEmailColumnData(myCursor);
|
||||
// }
|
||||
// catch (SQLiteException ex)
|
||||
// {
|
||||
// Log.d(LOG_TAG, ex.getMessage());
|
||||
// }
|
||||
//
|
||||
// return email;
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user