mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
Change to use Commands and CommandManager.
This commit is contained in:
Regular → Executable
+109
-25
@@ -1,17 +1,22 @@
|
||||
package com.phonegap;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import com.phonegap.api.Command;
|
||||
import com.phonegap.api.CommandResult;
|
||||
|
||||
import android.provider.Contacts.ContactMethods;
|
||||
import android.provider.Contacts.People;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebView;
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContactManager extends Module {
|
||||
public class ContactManager implements Command {
|
||||
|
||||
public class ContactTriplet
|
||||
{
|
||||
@@ -19,21 +24,100 @@ public class ContactManager extends Module {
|
||||
public String email = "";
|
||||
public String phone = "";
|
||||
}
|
||||
|
||||
|
||||
WebView webView; // WebView object
|
||||
DroidGap ctx; // DroidGap object
|
||||
|
||||
private static final String LOG_TAG = "Contact Query";
|
||||
DroidGap mApp;
|
||||
WebView mView;
|
||||
Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
||||
Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
||||
Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
|
||||
|
||||
public ContactManager(WebView view, DroidGap app)
|
||||
{
|
||||
super(view, app);
|
||||
mApp = app;
|
||||
mView = view;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public ContactManager() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the context of the Command. This can then be used to do things like
|
||||
* get file paths associated with the Activity.
|
||||
*
|
||||
* @param ctx The context of the main Activity.
|
||||
*/
|
||||
public void setContext(DroidGap ctx) {
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the main View of the application, this is the WebView within which
|
||||
* a PhoneGap app runs.
|
||||
*
|
||||
* @param webView The PhoneGap WebView
|
||||
*/
|
||||
public void setView(WebView webView) {
|
||||
this.webView = webView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the request and returns CommandResult.
|
||||
*
|
||||
* @param action The command to execute.
|
||||
* @param args JSONArry of arguments for the command.
|
||||
* @return A CommandResult object with a status and message.
|
||||
*/
|
||||
public CommandResult execute(String action, JSONArray args) {
|
||||
CommandResult.Status status = CommandResult.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));
|
||||
}
|
||||
return new CommandResult(status, result);
|
||||
} catch (JSONException e) {
|
||||
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the system is about to start resuming a previous activity.
|
||||
*/
|
||||
public void onPause() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity will start interacting with the user.
|
||||
*/
|
||||
public void onResume() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by AccelBroker when listener is to be shut down.
|
||||
* Stop listener.
|
||||
*/
|
||||
public void onDestroy() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when an activity you launched exits, giving you the requestCode you started it with,
|
||||
* the resultCode it returned, and any additional data from it.
|
||||
*
|
||||
* @param requestCode The request code originally supplied to startActivityForResult(),
|
||||
* allowing you to identify who this result came from.
|
||||
* @param resultCode The integer result code returned by the child activity through its setResult().
|
||||
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
||||
*/
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// LOCAL METHODS
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
// This is to add backwards compatibility to the OLD Contacts API\
|
||||
public void getContactsAndSendBack()
|
||||
{
|
||||
@@ -45,7 +129,7 @@ public class ContactManager extends Module {
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
||||
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
null, null , People.NAME + " ASC");
|
||||
processResults(myCursor, true);
|
||||
}
|
||||
@@ -77,7 +161,7 @@ public class ContactManager extends Module {
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
||||
Cursor myCursor = this.ctx.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
getMethodData(myCursor);
|
||||
|
||||
@@ -126,7 +210,7 @@ public class ContactManager extends Module {
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
||||
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
conditions, variables , People.NAME + " ASC");
|
||||
processResults(myCursor, false);
|
||||
}
|
||||
@@ -162,26 +246,26 @@ public class ContactManager extends Module {
|
||||
|
||||
// Code for backwards compatibility with the OLD Contacts API
|
||||
if (all) {
|
||||
mApp.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
this.ctx.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
}
|
||||
else {
|
||||
mApp.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
this.ctx.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
if (all) {
|
||||
mApp.sendJavascript("navigator.ContactManager.droidDone();");
|
||||
this.ctx.sendJavascript("navigator.ContactManager.droidDone();");
|
||||
}
|
||||
else {
|
||||
mApp.sendJavascript("navigator.contacts.droidDone();");
|
||||
this.ctx.sendJavascript("navigator.contacts.droidDone();");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (all) {
|
||||
mApp.sendJavascript("navigator.ContactManager.fail();");
|
||||
this.ctx.sendJavascript("navigator.ContactManager.fail();");
|
||||
}
|
||||
else {
|
||||
mApp.sendJavascript("navigator.contacts.fail('None found!');");
|
||||
this.ctx.sendJavascript("navigator.contacts.fail('None found!');");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,10 +289,10 @@ public class ContactManager extends Module {
|
||||
if(data != null)
|
||||
{
|
||||
data.email = email;
|
||||
mApp.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
|
||||
this.ctx.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
mApp.sendJavascript("navigator.contacts.droidDoneContacts();");
|
||||
this.ctx.sendJavascript("navigator.contacts.droidDoneContacts();");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +310,7 @@ public class ContactManager extends Module {
|
||||
};
|
||||
|
||||
try{
|
||||
Cursor myCursor = mApp.managedQuery(mPeople, projection,
|
||||
Cursor myCursor = this.ctx.managedQuery(mPeople, projection,
|
||||
People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC");
|
||||
data = getTriplet(myCursor);
|
||||
}
|
||||
@@ -281,7 +365,7 @@ public class ContactManager extends Module {
|
||||
|
||||
try
|
||||
{
|
||||
Cursor myCursor = mApp.managedQuery(mEmail, projection,
|
||||
Cursor myCursor = this.ctx.managedQuery(mEmail, projection,
|
||||
"contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC");
|
||||
email = getEmailColumnData(myCursor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user