1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core.internal.extension.api.contacts;
7 import android.content.ContentProviderOperation;
8 import android.content.ContentResolver;
9 import android.content.OperationApplicationException;
10 import android.database.Cursor;
11 import android.net.Uri;
12 import android.os.Handler;
13 import android.os.RemoteException;
14 import android.provider.ContactsContract;
15 import android.provider.ContactsContract.RawContacts;
16 import android.util.Log;
18 import java.util.ArrayList;
20 import org.json.JSONArray;
21 import org.json.JSONException;
22 import org.json.JSONObject;
24 import org.xwalk.core.internal.extension.XWalkExtension;
25 import org.xwalk.core.internal.extension.XWalkExtensionContext;
27 public class Contacts extends XWalkExtension {
28 public static final String NAME = "xwalk.experimental.contacts";
29 public static final String JS_API_PATH = "jsapi/contacts_api.js";
31 private static final String TAG = "Contacts";
33 private final ContactEventListener mObserver;
34 private final ContentResolver mResolver;
36 public Contacts(String jsApiContent, XWalkExtensionContext context) {
37 super(NAME, jsApiContent, context);
38 mResolver = context.getContext().getContentResolver();
39 mObserver = new ContactEventListener(new Handler(), this, mResolver);
40 mResolver.registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, mObserver);
44 public void onMessage(int instanceID, String message) {
45 if (message.isEmpty()) return;
47 JSONObject jsonInput = new JSONObject(message);
48 String cmd = jsonInput.getString("cmd");
49 if (cmd.equals("addEventListener")) {
50 mObserver.startListening();
53 JSONObject jsonOutput = new JSONObject();
54 jsonOutput.put("_promise_id", jsonInput.getString("_promise_id"));
55 if (cmd.equals("save")) {
56 ContactSaver saver = new ContactSaver(mResolver);
57 jsonOutput.put("data", saver.save(jsonInput.getString("contact")));
58 } else if (cmd.equals("find")) {
59 ContactFinder finder = new ContactFinder(mResolver);
60 String options = jsonInput.has("options") ? jsonInput.getString("options") : null;
61 JSONArray results = finder.find(options);
62 jsonOutput.put("data", results);
63 } else if (cmd.equals("remove")) {
64 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
65 String[] args = new String[] { jsonInput.getString("contactId") };
66 ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
67 .withSelection(RawContacts.CONTACT_ID + "=?", args).build());
69 mResolver.applyBatch(ContactsContract.AUTHORITY, ops);
70 } catch (Exception e) {
71 if (e instanceof RemoteException ||
72 e instanceof OperationApplicationException ||
73 e instanceof SecurityException) {
74 Log.e(TAG, "onMessage - Failed to apply batch: " + e.toString());
77 throw new RuntimeException(e);
80 } else if (cmd.equals("clear")) {
83 Log.e(TAG, "Unexpected message received: " + message);
86 this.postMessage(instanceID, jsonOutput.toString());
87 } catch (JSONException e) {
88 Log.e(TAG, e.toString());
93 public void onResume() {
95 mResolver.registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, mObserver);
99 public void onPause() {
100 mResolver.unregisterContentObserver(mObserver);
104 public void onDestroy() {
105 mResolver.unregisterContentObserver(mObserver);
108 // Remove all contacts.
109 private void handleClear() {
112 c = mResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
113 while (c.moveToNext()) {
114 String key = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
115 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, key);
116 mResolver.delete(uri, null, null);
118 } catch (SecurityException e) {
119 Log.e(TAG, "handleClear - failed to query: " + e.toString());
121 if (c != null) c.close();