Merge branch 'master' of github.com:phonegap/phonegap-android

This commit is contained in:
brianleroux 2010-10-08 11:40:09 -07:00
commit a31ce5ef2a
7 changed files with 634 additions and 428 deletions

View File

@ -109,15 +109,6 @@ Contacts.prototype.create = function(properties) {
return contact; return contact;
}; };
Contacts.prototype.droidDone = function(contacts) {
this.win(eval('(' + contacts + ')'));
};
Contacts.prototype.m_foundContacts = function(win, contacts) {
this.inProgress = false;
win(contacts);
};
var ContactFindOptions = function(filter, multiple, limit, updatedSince) { var ContactFindOptions = function(filter, multiple, limit, updatedSince) {
this.filter = filter || ''; this.filter = filter || '';
this.multiple = multiple || true; this.multiple = multiple || true;

View File

@ -6,9 +6,10 @@ function Notification() {
/** /**
* Open a native alert dialog, with a customizable title and button text. * Open a native alert dialog, with a customizable title and button text.
*
* @param {String} message Message to print in the body of the alert * @param {String} message Message to print in the body of the alert
* @param {String} [title="Alert"] Title of the alert dialog (default: Alert) * @param {String} title Title of the alert dialog (default: Alert)
* @param {String} [buttonLabel="OK"] Label of the close button (default: OK) * @param {String} buttonLabel Label of the close button (default: OK)
*/ */
Notification.prototype.alert = function(message, title, buttonLabel) { Notification.prototype.alert = function(message, title, buttonLabel) {
var _title = (title || "Alert"); var _title = (title || "Alert");
@ -16,29 +17,73 @@ Notification.prototype.alert = function(message, title, buttonLabel) {
PhoneGap.execAsync(null, null, "Notification", "alert", [message,_title,_buttonLabel]); PhoneGap.execAsync(null, null, "Notification", "alert", [message,_title,_buttonLabel]);
}; };
/**
* Open a native confirm dialog, with a customizable title and button text.
*
* @param {String} message Message to print in the body of the alert
* @param {String} title Title of the alert dialog (default: Confirm)
* @param {String} buttonLabels Comma separated list of the labels of the buttons (default: 'OK,Cancel')
* @return {Number} The index of the button clicked
*/
Notification.prototype.confirm = function(message, title, buttonLabels) {
var _title = (title || "Confirm");
var _buttonLabels = (buttonLabels || "OK,Cancel");
return PhoneGap.execAsync(null, null, "Notification", "confirm", [message,_title,_buttonLabels]);
};
/** /**
* Start spinning the activity indicator on the statusbar * Start spinning the activity indicator on the statusbar
*/ */
Notification.prototype.activityStart = function() { Notification.prototype.activityStart = function() {
PhoneGap.execAsync(null, null, "Notification", "activityStart", ["Busy","Please wait..."]);
}; };
/** /**
* Stop spinning the activity indicator on the statusbar, if it's currently spinning * Stop spinning the activity indicator on the statusbar, if it's currently spinning
*/ */
Notification.prototype.activityStop = function() { Notification.prototype.activityStop = function() {
PhoneGap.execAsync(null, null, "Notification", "activityStop", []);
};
/**
* Display a progress dialog with progress bar that goes from 0 to 100.
*
* @param {String} title Title of the progress dialog.
* @param {String} message Message to display in the dialog.
*/
Notification.prototype.progressStart = function(title, message) {
PhoneGap.execAsync(null, null, "Notification", "progressStart", [title, message]);
};
/**
* Set the progress dialog value.
*
* @param {Number} value 0-100
*/
Notification.prototype.progressValue = function(value) {
PhoneGap.execAsync(null, null, "Notification", "progressValue", [value]);
};
/**
* Close the progress dialog.
*/
Notification.prototype.progressStop = function() {
PhoneGap.execAsync(null, null, "Notification", "progressStop", []);
}; };
/** /**
* Causes the device to blink a status LED. * Causes the device to blink a status LED.
*
* @param {Integer} count The number of blinks. * @param {Integer} count The number of blinks.
* @param {String} colour The colour of the light. * @param {String} colour The colour of the light.
*/ */
Notification.prototype.blink = function(count, colour) { Notification.prototype.blink = function(count, colour) {
// NOT IMPLEMENTED
}; };
/** /**
* Causes the device to vibrate. * Causes the device to vibrate.
*
* @param {Integer} mills The number of milliseconds to vibrate for. * @param {Integer} mills The number of milliseconds to vibrate for.
*/ */
Notification.prototype.vibrate = function(mills) { Notification.prototype.vibrate = function(mills) {
@ -47,7 +92,7 @@ Notification.prototype.vibrate = function(mills) {
/** /**
* Causes the device to beep. * Causes the device to beep.
* On Android, the default notification ringtone is played. * On Android, the default notification ringtone is played "count" times.
* *
* @param {Integer} count The number of beeps. * @param {Integer} count The number of beeps.
*/ */
@ -55,8 +100,6 @@ Notification.prototype.beep = function(count) {
PhoneGap.execAsync(null, null, "Notification", "beep", [count]); PhoneGap.execAsync(null, null, "Notification", "beep", [count]);
}; };
// TODO: of course on Blackberry and Android there notifications in the UI as well
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
if (typeof navigator.notification == "undefined") navigator.notification = new Notification(); if (typeof navigator.notification == "undefined") navigator.notification = new Notification();
}); });

View File

@ -245,6 +245,9 @@ PhoneGap.Channel.join(function() {
* received from native side. * received from native side.
*/ */
PhoneGap.Channel.join(function() { PhoneGap.Channel.join(function() {
// Turn off app loading dialog
navigator.notification.activityStop();
PhoneGap.onDeviceReady.fire(); PhoneGap.onDeviceReady.fire();
// Fire the onresume event, since first one happens before JavaScript is loaded // Fire the onresume event, since first one happens before JavaScript is loaded

View File

@ -159,4 +159,21 @@ public abstract class ContactAccessor {
* Handles removing a contact from the database. * Handles removing a contact from the database.
*/ */
public abstract boolean remove(String id); public abstract boolean remove(String id);
class WhereOptions {
private String where;
private String[] whereArgs;
public void setWhere(String where) {
this.where = where;
}
public String getWhere() {
return where;
}
public void setWhereArgs(String[] whereArgs) {
this.whereArgs = whereArgs;
}
public String[] getWhereArgs() {
return whereArgs;
}
}
} }

View File

@ -17,20 +17,16 @@
package com.phonegap; package com.phonegap;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
import android.webkit.WebView; import android.webkit.WebView;
@ -55,7 +51,6 @@ import android.webkit.WebView;
*/ */
public class ContactAccessorSdk5 extends ContactAccessor { public class ContactAccessorSdk5 extends ContactAccessor {
private static final String WHERE_STRING = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
private static final Map<String, String> dbMap = new HashMap<String, String>(); private static final Map<String, String> dbMap = new HashMap<String, String>();
static { static {
dbMap.put("id", ContactsContract.Contacts._ID); dbMap.put("id", ContactsContract.Contacts._ID);
@ -113,7 +108,11 @@ public class ContactAccessorSdk5 extends ContactAccessor {
} }
@Override @Override
public JSONArray search(JSONArray filter, JSONObject options) { public JSONArray search(JSONArray fields, JSONObject options) {
long totalEnd;
long totalStart = System.currentTimeMillis();
// Get the find options
String searchTerm = ""; String searchTerm = "";
int limit = Integer.MAX_VALUE; int limit = Integer.MAX_VALUE;
boolean multiple = true; boolean multiple = true;
@ -133,175 +132,124 @@ public class ContactAccessorSdk5 extends ContactAccessor {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
// Get a cursor by creating the query. // Loop through the fields the user provided to see what data should be returned.
ContentResolver cr = mApp.getContentResolver(); HashMap<String,Boolean> populate = buildPopulationSet(fields);
Set<String> contactIds = buildSetOfContactIds(filter, searchTerm); // Build the ugly where clause and where arguments for one big query.
HashMap<String,Boolean> populate = buildPopulationSet(filter); WhereOptions whereOptions = buildWhereClause(fields, searchTerm);
Iterator<String> it = contactIds.iterator(); // Get all the rows where the search term matches the fields passed in.
Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null,
whereOptions.getWhere(),
whereOptions.getWhereArgs(),
ContactsContract.Data.CONTACT_ID + " ASC");
String contactId = "";
String oldContactId = "";
boolean newContact = true;
String mimetype = "";
JSONArray contacts = new JSONArray(); JSONArray contacts = new JSONArray();
JSONObject contact; JSONObject contact = new JSONObject();
String contactId; JSONArray organizations = new JSONArray();
int pos = 0; JSONArray addresses = new JSONArray();
String[] events = null; JSONArray phones = new JSONArray();
while (it.hasNext() && (pos < limit)) { JSONArray emails = new JSONArray();
JSONArray ims = new JSONArray();
JSONArray websites = new JSONArray();
JSONArray relationships = new JSONArray();
while (c.moveToNext() && (contacts.length() < (limit-1))) {
try {
contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
// If we are in the first row set the oldContactId
if (c.getPosition() == 0) {
oldContactId = contactId;
}
// When the contact ID changes we need to push the Contact object
// to the array of contacts and create new objects.
if (!oldContactId.equals(contactId)) {
// Populate the Contact object with it's arrays
// and push the contact into the contacts array
contacts.put(populateContact(contact, organizations, addresses, phones,
emails, ims, websites, relationships));
// Clean up the objects
contact = new JSONObject(); contact = new JSONObject();
contactId = it.next(); organizations = new JSONArray();
addresses = new JSONArray();
phones = new JSONArray();
emails = new JSONArray();
ims = new JSONArray();
websites = new JSONArray();
relationships = new JSONArray();
try { // Set newContact to true as we are starting to populate a new contact
newContact = true;
}
// When we detect a new contact set the ID and display name.
// These fields are available in every row in the result set returned.
if (newContact) {
newContact = false;
contact.put("id", contactId); contact.put("id", contactId);
if (isRequired("displayName",populate)) { contact.put("displayName", c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contact.put("displayName", displayNameQuery(cr, contactId));
}
if (isRequired("name",populate)) {
contact.put("name", nameQuery(cr, contactId));
}
if (isRequired("phoneNumbers",populate)) {
contact.put("phoneNumbers", phoneQuery(cr, contactId));
}
if (isRequired("emails",populate)) {
contact.put("emails", emailQuery(cr, contactId));
}
if (isRequired("addresses",populate)) {
contact.put("addresses", addressQuery(cr, contactId));
}
if (isRequired("organizations",populate)) {
contact.put("organizations", organizationQuery(cr, contactId));
}
if (isRequired("ims",populate)) {
contact.put("ims",imQuery(cr, contactId));
}
if (isRequired("note",populate)) {
contact.put("note",noteQuery(cr, contactId));
}
if (isRequired("nickname",populate)) {
contact.put("nickname",nicknameQuery(cr, contactId));
}
if (isRequired("urls",populate)) {
contact.put("urls",websiteQuery(cr, contactId));
}
if (isRequired("relationships",populate)) {
contact.put("relationships",relationshipQuery(cr, contactId));
}
if (isRequired("birthday",populate) || isRequired("anniversary",populate)) {
events = eventQuery(cr, contactId);
contact.put("birthday",events[0]);
contact.put("anniversary",events[1]);
}
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
Log.d(LOG_TAG, "putting in contact ID = " + contactId);
contacts.put(contact);
pos++;
}
return contacts;
} }
private Set<String> buildSetOfContactIds(JSONArray filter, String searchTerm) { // Grab the mimetype of the current row as it will be used in a lot of comparisons
Set<String> contactIds = new HashSet<String>(); mimetype = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
/* if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
* Special case for when the user wants all the contacts && isRequired("name",populate)) {
*/ contact.put("name", nameQuery(c));
if ("%".equals(searchTerm)) {
doQuery(searchTerm, contactIds,
ContactsContract.Contacts.CONTENT_URI,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
new String[] {searchTerm});
return contactIds;
} }
else if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
String key; && isRequired("phoneNumbers",populate)) {
try { phones.put(phoneQuery(c));
for (int i=0; i<filter.length(); i++) {
key = filter.getString(i);
if (key.startsWith("displayName")) {
doQuery(searchTerm, contactIds,
ContactsContract.Contacts.CONTENT_URI,
ContactsContract.Contacts._ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
} }
else if (key.startsWith("name")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("emails",populate)) {
ContactsContract.Data.CONTENT_URI, emails.put(emailQuery(c));
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
} }
else if (key.startsWith("nickname")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("addresses",populate)) {
ContactsContract.Data.CONTENT_URI, addresses.put(addressQuery(c));
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE});
} }
else if (key.startsWith("phoneNumbers")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("organizations",populate)) {
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, organizations.put(organizationQuery(c));
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
} }
else if (key.startsWith("emails")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("ims",populate)) {
ContactsContract.CommonDataKinds.Email.CONTENT_URI, ims.put(imQuery(c));
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
dbMap.get(key) + " LIKE ?",
new String[] {searchTerm});
} }
else if (key.startsWith("addresses")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("note",populate)) {
ContactsContract.Data.CONTENT_URI, contact.put("note",c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)));
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE});
} }
else if (key.startsWith("ims")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("nickname",populate)) {
ContactsContract.Data.CONTENT_URI, contact.put("nickname",c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE});
} }
else if (key.startsWith("organizations")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
doQuery(searchTerm, contactIds, && isRequired("urls",populate)) {
ContactsContract.Data.CONTENT_URI, websites.put(websiteQuery(c));
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE});
} }
else if (key.startsWith("birthday")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE)
&& isRequired("relationships",populate)) {
relationships.put(relationshipQuery(c));
} }
else if (key.startsWith("anniversary")) { else if (mimetype.equals(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)) {
if (ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
&& isRequired("anniversary",populate)) {
contact.put("anniversary", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
} }
else if (key.startsWith("note")) { else if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))
doQuery(searchTerm, contactIds, && isRequired("birthday",populate)) {
ContactsContract.Data.CONTENT_URI, contact.put("birthday", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)));
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("relationships")) {
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE});
}
else if (key.startsWith("urls")) {
doQuery(searchTerm, contactIds,
ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID,
dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[] {searchTerm, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE});
} }
} }
} }
@ -309,53 +257,161 @@ public class ContactAccessorSdk5 extends ContactAccessor {
Log.e(LOG_TAG, e.getMessage(),e); Log.e(LOG_TAG, e.getMessage(),e);
} }
return contactIds; // Set the old contact ID
oldContactId = contactId;
}
c.close();
// Push the last contact into the contacts array
contacts.put(populateContact(contact, organizations, addresses, phones,
emails, ims, websites, relationships));
totalEnd = System.currentTimeMillis();
Log.d(LOG_TAG,"Total time = " + (totalEnd-totalStart));
return contacts;
} }
private void doQuery(String searchTerm, Set<String> contactIds, private JSONObject populateContact(JSONObject contact, JSONArray organizations,
Uri uri, String projection, String selection, String[] selectionArgs) { JSONArray addresses, JSONArray phones, JSONArray emails,
// Get a cursor by creating the query. JSONArray ims, JSONArray websites, JSONArray relationships) {
ContentResolver cr = mApp.getContentResolver(); try {
contact.put("organizations", organizations);
Cursor cursor = cr.query( contact.put("addresses", addresses);
uri, contact.put("phoneNumbers", phones);
new String[] {projection}, contact.put("emails", emails);
selection, contact.put("ims", ims);
selectionArgs, contact.put("websites", websites);
null); contact.put("relationships", relationships);
while (cursor.moveToNext()) {
contactIds.add(cursor.getString(cursor.getColumnIndex(projection)));
} }
cursor.close(); catch (JSONException e) {
Log.e(LOG_TAG,e.getMessage(),e);
}
return contact;
} }
private String displayNameQuery(ContentResolver cr, String contactId) { private WhereOptions buildWhereClause(JSONArray filter, String searchTerm) {
Cursor cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI, ArrayList<String> where = new ArrayList<String>();
new String[] {ContactsContract.Contacts.DISPLAY_NAME}, ArrayList<String> whereArgs = new ArrayList<String>();
ContactsContract.Contacts._ID + " = ?",
new String[] {contactId}, WhereOptions options = new WhereOptions();
null);
cursor.moveToFirst(); /*
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); * Special case for when the user wants all the contacts
cursor.close(); */
return displayName; if ("%".equals(searchTerm)) {
options.setWhere("(" + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? )");
options.setWhereArgs(new String[] {searchTerm});
return options;
} }
private JSONArray organizationQuery(ContentResolver cr, String contactId) { String key;
String[] orgWhereParams = new String[]{contactId, try {
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; for (int i=0; i<filter.length(); i++) {
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, key = filter.getString(i);
new String[] {ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, if (key.startsWith("displayName")) {
ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, where.add("(" + dbMap.get(key) + " LIKE ? )");
ContactsContract.CommonDataKinds.Organization.COMPANY, whereArgs.add(searchTerm);
ContactsContract.CommonDataKinds.Organization.TITLE}, }
WHERE_STRING, orgWhereParams, null); else if (key.startsWith("name")) {
JSONArray organizations = new JSONArray(); where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("nickname")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("phoneNumbers")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("emails")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("addresses")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("ims")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("organizations")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
}
// else if (key.startsWith("birthday")) {
// where.add("(" + dbMap.get(key) + " LIKE ? AND "
// + ContactsContract.Data.MIMETYPE + " = ? )");
// }
// else if (key.startsWith("anniversary")) {
// where.add("(" + dbMap.get(key) + " LIKE ? AND "
// + ContactsContract.Data.MIMETYPE + " = ? )");
// whereArgs.add(searchTerm);
// whereArgs.add(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
// }
else if (key.startsWith("note")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("relationships")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE);
}
else if (key.startsWith("urls")) {
where.add("(" + dbMap.get(key) + " LIKE ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? )");
whereArgs.add(searchTerm);
whereArgs.add(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
}
}
}
catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
// Creating the where string
StringBuffer selection = new StringBuffer();
for (int i=0; i<where.size(); i++) {
selection.append(where.get(i));
if (i != (where.size()-1)) {
selection.append(" OR ");
}
}
options.setWhere(selection.toString());
// Creating the where args array
String[] selectionArgs = new String[whereArgs.size()];
for (int i=0; i<whereArgs.size(); i++) {
selectionArgs[i] = whereArgs.get(i);
}
options.setWhereArgs(selectionArgs);
return options;
}
private JSONObject organizationQuery(Cursor cursor) {
JSONObject organization = new JSONObject(); JSONObject organization = new JSONObject();
while (cursor.moveToNext()) {
try { try {
organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT))); organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT)));
organization.put("description", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION))); organization.put("description", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION)));
@ -366,29 +422,14 @@ public class ContactAccessorSdk5 extends ContactAccessor {
// TODO no startDate // TODO no startDate
// organization.put("startDate", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization))); // organization.put("startDate", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization)));
organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE))); organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
organizations.put(organization);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
} return organization;
cursor.close();
return organizations;
} }
private JSONArray addressQuery(ContentResolver cr, String contactId) { private JSONObject addressQuery(Cursor cursor) {
String[] addrWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
ContactsContract.CommonDataKinds.StructuredPostal.REGION,
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY},
WHERE_STRING, addrWhereParams, null);
JSONArray addresses = new JSONArray();
JSONObject address = new JSONObject(); JSONObject address = new JSONObject();
while (cursor.moveToNext()) {
try { try {
address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS))); address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)));
address.put("streetAddress", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET))); address.put("streetAddress", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
@ -396,33 +437,20 @@ public class ContactAccessorSdk5 extends ContactAccessor {
address.put("region", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION))); address.put("region", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
address.put("postalCode", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE))); address.put("postalCode", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
address.put("country", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY))); address.put("country", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)));
addresses.put(address);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
} return address;
cursor.close();
return addresses;
} }
private JSONObject nameQuery(ContentResolver cr, String contactId) { private JSONObject nameQuery(Cursor cursor) {
String[] addrWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
Cursor name = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
ContactsContract.CommonDataKinds.StructuredName.PREFIX,
ContactsContract.CommonDataKinds.StructuredName.SUFFIX},
WHERE_STRING, addrWhereParams, null);
JSONObject contactName = new JSONObject(); JSONObject contactName = new JSONObject();
if (name.moveToFirst()) {
try { try {
String familyName = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String givenName = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String middleName = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)); String middleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
String honorificPrefix = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX)); String honorificPrefix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
String honorificSuffix = name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)); String honorificSuffix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
// Create the formatted name // Create the formatted name
StringBuffer formatted = new StringBuffer(""); StringBuffer formatted = new StringBuffer("");
@ -441,163 +469,70 @@ public class ContactAccessorSdk5 extends ContactAccessor {
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
}
name.close();
return contactName; return contactName;
} }
private JSONArray phoneQuery(ContentResolver cr, String contactId) { private JSONObject phoneQuery(Cursor cursor) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONArray phoneNumbers = new JSONArray();
JSONObject phoneNumber = new JSONObject(); JSONObject phoneNumber = new JSONObject();
while (phones.moveToNext()) {
try { try {
phoneNumber.put("primary", false); // Android does not store primary attribute phoneNumber.put("primary", false); // Android does not store primary attribute
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); phoneNumber.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
phoneNumber.put("type", phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))); phoneNumber.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));
phoneNumbers.put(phoneNumber);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
catch (Exception excp) {
Log.e(LOG_TAG, excp.getMessage(), excp);
} }
phones.close(); return phoneNumber;
return phoneNumbers;
} }
private JSONArray emailQuery(ContentResolver cr, String contactId) { private JSONObject emailQuery(Cursor cursor) {
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Email.DATA,ContactsContract.CommonDataKinds.Email.TYPE},
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
JSONArray emailAddresses = new JSONArray();
JSONObject email = new JSONObject(); JSONObject email = new JSONObject();
while (emails.moveToNext()) {
try { try {
email.put("primary", false); // Android does not store primary attribute email.put("primary", false); // Android does not store primary attribute
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); email.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
email.put("type", emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))); email.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)));
emailAddresses.put(email);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
} return email;
emails.close();
return emailAddresses;
} }
private JSONArray imQuery(ContentResolver cr, String contactId) { private JSONObject imQuery(Cursor cursor) {
String[] addrWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Im.DATA,ContactsContract.CommonDataKinds.Im.TYPE},
WHERE_STRING, addrWhereParams, null);
JSONArray ims = new JSONArray();
JSONObject im = new JSONObject(); JSONObject im = new JSONObject();
while (cursor.moveToNext()) {
try { try {
im.put("primary", false); // Android does not store primary attribute im.put("primary", false); // Android does not store primary attribute
im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA))); im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
im.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE))); im.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)));
ims.put(im);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
} return im;
cursor.close();
return ims;
} }
private String noteQuery(ContentResolver cr, String contactId) { private JSONObject websiteQuery(Cursor cursor) {
String[] noteWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Note.NOTE}, WHERE_STRING, noteWhereParams, null);
String note = new String("");
if (cursor.moveToFirst()) {
note = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
}
cursor.close();
return note;
}
private String nicknameQuery(ContentResolver cr, String contactId) {
String[] nicknameWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Nickname.NAME}, WHERE_STRING, nicknameWhereParams, null);
String nickname = new String("");
if (cursor.moveToFirst()) {
nickname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
}
cursor.close();
return nickname;
}
private JSONArray websiteQuery(ContentResolver cr, String contactId) {
String[] websiteWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Website.URL,ContactsContract.CommonDataKinds.Website.TYPE},
WHERE_STRING, websiteWhereParams, null);
JSONArray websites = new JSONArray();
JSONObject website = new JSONObject(); JSONObject website = new JSONObject();
while (cursor.moveToNext()) {
try { try {
website.put("primary", false); // Android does not store primary attribute website.put("primary", false); // Android does not store primary attribute
website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL))); website.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)));
website.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE))); website.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE)));
websites.put(website);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
} return website;
cursor.close();
return websites;
} }
private JSONArray relationshipQuery(ContentResolver cr, String contactId) { private JSONObject relationshipQuery(Cursor cursor) {
String[] relationshipWhereParams = new String[]{contactId,
ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.CommonDataKinds.Relation.NAME,ContactsContract.CommonDataKinds.Relation.TYPE},
WHERE_STRING, relationshipWhereParams, null);
JSONArray relationships = new JSONArray();
JSONObject relationship = new JSONObject(); JSONObject relationship = new JSONObject();
while (cursor.moveToNext()) {
try { try {
relationship.put("primary", false); // Android does not store primary attribute relationship.put("primary", false); // Android does not store primary attribute
relationship.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.NAME))); relationship.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.NAME)));
relationship.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE))); relationship.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE)));
relationships.put(relationship);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
} return relationship;
cursor.close();
return relationships;
}
private String[] eventQuery(ContentResolver cr, String contactId) {
String[] whereParams = new String[]{contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, WHERE_STRING, whereParams, null);
String anniversary = null;
String birthday = null;
while (cursor.moveToNext()) {
if (ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))) {
anniversary = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
}
else if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))) {
birthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
}
}
cursor.close();
return new String[] {anniversary, birthday};
} }
@Override @Override

View File

@ -84,7 +84,7 @@ public class DroidGap extends Activity {
private BrowserKey mKey; private BrowserKey mKey;
public CallbackServer callbackServer; public CallbackServer callbackServer;
private PluginManager pluginManager; protected PluginManager pluginManager;
private String url; // The initial URL for our app private String url; // The initial URL for our app
private String baseUrl; // The base of the initial URL for our app private String baseUrl; // The base of the initial URL for our app

View File

@ -5,6 +5,7 @@ import org.json.JSONException;
import com.phonegap.api.Plugin; import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult; import com.phonegap.api.PluginResult;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.media.Ringtone; import android.media.Ringtone;
@ -17,6 +18,10 @@ import android.os.Vibrator;
*/ */
public class Notification extends Plugin { public class Notification extends Plugin {
public int confirmResult = -1;
public ProgressDialog spinnerDialog = null;
public ProgressDialog progressDialog = null;
/** /**
* Constructor. * Constructor.
*/ */
@ -45,6 +50,25 @@ public class Notification extends Plugin {
else if (action.equals("alert")) { else if (action.equals("alert")) {
this.alert(args.getString(0),args.getString(1),args.getString(2)); this.alert(args.getString(0),args.getString(1),args.getString(2));
} }
else if (action.equals("confirm")) {
int i = this.confirm(args.getString(0),args.getString(1),args.getString(2));
return new PluginResult(status, i);
}
else if (action.equals("activityStart")) {
this.activityStart(args.getString(0),args.getString(1));
}
else if (action.equals("activityStop")) {
this.activityStop();
}
else if (action.equals("progressStart")) {
this.progressStart(args.getString(0),args.getString(1));
}
else if (action.equals("progressValue")) {
this.progressValue(args.getInt(0));
}
else if (action.equals("progressStop")) {
this.progressStop();
}
return new PluginResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION); return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
@ -58,11 +82,31 @@ public class Notification extends Plugin {
* @return T=returns value * @return T=returns value
*/ */
public boolean isSynch(String action) { public boolean isSynch(String action) {
if(action.equals("alert")) if (action.equals("alert")) {
return true; return true;
else }
else if (action.equals("confirm")) {
return true;
}
else if (action.equals("activityStart")) {
return true;
}
else if (action.equals("activityStop")) {
return true;
}
else if (action.equals("progressStart")) {
return true;
}
else if (action.equals("progressValue")) {
return true;
}
else if (action.equals("progressStop")) {
return true;
}
else {
return false; return false;
} }
}
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// LOCAL METHODS // LOCAL METHODS
@ -128,4 +172,177 @@ public class Notification extends Plugin {
dlg.show(); dlg.show();
} }
/**
* Builds and shows a native Android confirm dialog with given title, message, buttons.
* This dialog only shows up to 3 buttons. Any labels after that will be ignored.
*
* @param message The message the dialog should display
* @param title The title of the dialog
* @param buttonLabels A comma separated list of button labels (Up to 3 buttons)
* @return The index of the button clicked (1,2 or 3)
*/
public synchronized int confirm(final String message, final String title, String buttonLabels) {
// Create dialog on UI thread
final DroidGap ctx = this.ctx;
final Notification notification = this;
final String[] fButtons = buttonLabels.split(",");
Runnable runnable = new Runnable() {
public void run() {
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(false);
// First button
if (fButtons.length > 0) {
dlg.setPositiveButton(fButtons[0],
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
synchronized(notification) {
notification.confirmResult = 1;
notification.notifyAll();
}
}
});
}
// Second button
if (fButtons.length > 1) {
dlg.setNeutralButton(fButtons[1],
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
synchronized(notification) {
notification.confirmResult = 2;
notification.notifyAll();
}
}
});
}
// Third button
if (fButtons.length > 2) {
dlg.setNegativeButton(fButtons[2],
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
synchronized(notification) {
notification.confirmResult = 3;
notification.notifyAll();
}
}
}
);
}
dlg.create();
dlg.show();
}
};
this.ctx.runOnUiThread(runnable);
// Wait for dialog to close
synchronized(runnable) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return this.confirmResult;
}
/**
* Show the spinner.
*
* @param title Title of the dialog
* @param message The message of the dialog
*/
public synchronized void activityStart(final String title, final String message) {
if (this.spinnerDialog != null) {
this.spinnerDialog.dismiss();
this.spinnerDialog = null;
}
final Notification notification = this;
final DroidGap ctx = this.ctx;
Runnable runnable = new Runnable() {
public void run() {
notification.spinnerDialog = ProgressDialog.show(ctx, title , message, true, true,
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
notification.spinnerDialog = null;
}
});
}
};
this.ctx.runOnUiThread(runnable);
}
/**
* Stop spinner.
*/
public synchronized void activityStop() {
if (this.spinnerDialog != null) {
this.spinnerDialog.dismiss();
this.spinnerDialog = null;
}
}
/**
* Show the progress dialog.
*
* @param title Title of the dialog
* @param message The message of the dialog
*/
public synchronized void progressStart(final String title, final String message) {
if (this.progressDialog != null) {
this.progressDialog.dismiss();
this.progressDialog = null;
}
final Notification notification = this;
final DroidGap ctx = this.ctx;
Runnable runnable = new Runnable() {
public void run() {
notification.progressDialog = new ProgressDialog(ctx);
notification.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
notification.progressDialog.setTitle(title);
notification.progressDialog.setMessage(message);
notification.progressDialog.setCancelable(true);
notification.progressDialog.setMax(100);
notification.progressDialog.setProgress(0);
notification.progressDialog.setOnCancelListener(
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
notification.progressDialog = null;
}
});
notification.progressDialog.show();
}
};
this.ctx.runOnUiThread(runnable);
}
/**
* Set value of progress bar.
*
* @param value 0-100
*/
public synchronized void progressValue(int value) {
if (this.progressDialog != null) {
this.progressDialog.setProgress(value);
}
}
/**
* Stop progress dialog.
*/
public synchronized void progressStop() {
if (this.progressDialog != null) {
this.progressDialog.dismiss();
this.progressDialog = null;
}
}
} }