d299969ab4aaeabb04c5f149f52bc797f644c000
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / extension / api / contacts / Contacts.java
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.
4
5 package org.xwalk.core.internal.extension.api.contacts;
6
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;
17
18 import java.util.ArrayList;
19
20 import org.json.JSONArray;
21 import org.json.JSONException;
22 import org.json.JSONObject;
23
24 import org.xwalk.core.internal.extension.XWalkExtension;
25 import org.xwalk.core.internal.extension.XWalkExtensionContext;
26
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";
30
31     private static final String TAG = "Contacts";
32
33     private final ContactEventListener mObserver;
34     private final ContentResolver mResolver;
35
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);
41     }
42
43     @Override
44     public void onMessage(int instanceID, String message) {
45         if (message.isEmpty()) return;
46         try {
47             JSONObject jsonInput = new JSONObject(message);
48             String cmd = jsonInput.getString("cmd");
49             if (cmd.equals("addEventListener")) {
50                 mObserver.startListening();
51                 return;
52             }
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());
68                 try {
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());
75                         return;
76                     } else {
77                         throw new RuntimeException(e);
78                     }
79                 }
80             } else if (cmd.equals("clear")) {
81                 handleClear();
82             } else {
83                 Log.e(TAG, "Unexpected message received: " + message);
84                 return;
85             }
86             this.postMessage(instanceID, jsonOutput.toString());
87         } catch (JSONException e) {
88             Log.e(TAG, e.toString());
89         }
90     }
91
92     @Override
93     public void onResume() {
94         mObserver.onResume();
95         mResolver.registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, mObserver);
96     }
97
98     @Override
99     public void onPause() {
100         mResolver.unregisterContentObserver(mObserver);
101     }
102
103     @Override
104     public void onDestroy() {
105         mResolver.unregisterContentObserver(mObserver);
106     }
107
108     // Remove all contacts.
109     private void handleClear() {
110         Cursor c = null;
111         try {
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);
117             }
118         } catch (SecurityException e) {
119             Log.e(TAG, "handleClear - failed to query: " + e.toString());
120         } finally {
121             if (c != null) c.close();
122         }
123     }
124 }