rhodes 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. data/CHANGELOG +6 -0
  2. data/Manifest.txt +3 -1
  3. data/bin/rhodes-setup +1 -32
  4. data/lib/framework/erb.rb +3 -3
  5. data/lib/framework/rhodes.rb +2 -2
  6. data/lib/framework/version.rb +2 -2
  7. data/lib/rhodes.rb +2 -2
  8. data/platform/android/Rhodes/AndroidManifest.xml +3 -2
  9. data/platform/android/Rhodes/jni/include/rhodes/jni/com_rhomobile_rhodes_Rhodes.h +2 -2
  10. data/platform/android/Rhodes/jni/src/rhodes.cpp +7 -5
  11. data/platform/android/Rhodes/src/com/rhomobile/rhodes/Capabilities.java +2 -0
  12. data/platform/android/Rhodes/src/com/rhomobile/rhodes/Rhodes.java +15 -6
  13. data/platform/android/Rhodes/src/com/rhomobile/rhodes/camera/ImageCapture.java +5 -2
  14. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/Contact.java +8 -2
  15. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/ContactAccessor.java +11 -0
  16. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/ContactAccessorNew.java +310 -0
  17. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/ContactAccessorOld.java +275 -0
  18. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/Phonebook.java +125 -322
  19. data/platform/android/build/RhodesSRC_build.files +23 -21
  20. data/platform/android/build/android.rake +40 -17
  21. data/platform/iphone/Info.plist +1 -1
  22. data/platform/iphone/rbuild/iphone.rake +58 -33
  23. data/platform/iphone/rhorunner.xcodeproj/project.pbxproj +1 -1
  24. data/platform/shared/common/RhoPort.h +18 -3
  25. data/platform/shared/common/RhodesApp.cpp +76 -54
  26. data/platform/shared/common/StringConverter.h +3 -2
  27. data/platform/shared/common/stat.h +29 -4
  28. data/platform/shared/db/DBAdapter.cpp +2 -1
  29. data/platform/shared/db/DBAdapter.h +1 -1
  30. data/platform/shared/json/RJSONTokener.c +1 -1
  31. data/platform/shared/json/json_object.c +2 -2
  32. data/platform/shared/json/json_tokener.c +1 -1
  33. data/rhodes.gemspec +1 -1
  34. data/spec/phone_spec/app/Spec/contacts_spec.rb +10 -3
  35. data/spec/phone_spec/app/Spec/controller.rb +3 -3
  36. data/spec/phone_spec/app/Spec/mapview_spec.rb +3 -1
  37. data/spec/phone_spec/build.yml +2 -1
  38. metadata +6 -4
  39. data/rhobuild.yml +0 -39
@@ -0,0 +1,275 @@
1
+ package com.rhomobile.rhodes.phonebook;
2
+
3
+ import java.util.HashMap;
4
+ import java.util.Map;
5
+
6
+ import android.app.Activity;
7
+ import android.content.ContentResolver;
8
+ import android.content.ContentUris;
9
+ import android.content.ContentValues;
10
+ import android.database.Cursor;
11
+ import android.net.Uri;
12
+ import android.provider.Contacts;
13
+ import android.provider.Contacts.Organizations;
14
+ import android.provider.Contacts.People;
15
+ import android.provider.Contacts.Phones;
16
+
17
+ import com.rhomobile.rhodes.RhodesInstance;
18
+
19
+ @SuppressWarnings("deprecation")
20
+ public class ContactAccessorOld implements ContactAccessor {
21
+
22
+ //private static final String TAG = "ContactsAccessorOld";
23
+
24
+ private static final String PB_ID = Phonebook.PB_ID;
25
+ private static final String PB_FIRST_NAME = Phonebook.PB_FIRST_NAME;
26
+ private static final String PB_LAST_NAME = Phonebook.PB_LAST_NAME;
27
+ private static final String PB_MOBILE_NUMBER = Phonebook.PB_MOBILE_NUMBER;
28
+ private static final String PB_HOME_NUMBER = Phonebook.PB_HOME_NUMBER;
29
+ private static final String PB_BUSINESS_NUMBER = Phonebook.PB_BUSINESS_NUMBER;
30
+ private static final String PB_EMAIL_ADDRESS = Phonebook.PB_EMAIL_ADDRESS;
31
+ private static final String PB_COMPANY_NAME = Phonebook.PB_COMPANY_NAME;
32
+
33
+ private Activity activity;
34
+ private ContentResolver cr;
35
+
36
+ public ContactAccessorOld() {
37
+ activity = RhodesInstance.getInstance();
38
+ cr = activity.getContentResolver();
39
+ }
40
+
41
+ private void fillPhones(String id, Contact contact) {
42
+ Cursor cursor = cr.query(Contacts.Phones.CONTENT_URI,
43
+ new String[] {Phones.NUMBER, Phones.TYPE},
44
+ "person=?", new String[] {id},
45
+ null);
46
+
47
+ try {
48
+ int numColumn = cursor.getColumnIndex(Phones.NUMBER);
49
+ int typeColumn = cursor.getColumnIndex(Phones.TYPE);
50
+ cursor.moveToFirst();
51
+ for (int i = 0, lim = cursor.getCount(); i < lim; ++i) {
52
+ switch (cursor.getInt(typeColumn)) {
53
+ case Phones.TYPE_WORK:
54
+ contact.setField(PB_BUSINESS_NUMBER, cursor.getString(numColumn));
55
+ break;
56
+ case Phones.TYPE_HOME:
57
+ contact.setField(PB_HOME_NUMBER, cursor.getString(numColumn));
58
+ break;
59
+ case Phones.TYPE_MOBILE:
60
+ contact.setField(PB_MOBILE_NUMBER, cursor.getString(numColumn));
61
+ break;
62
+ }
63
+ cursor.moveToNext();
64
+ }
65
+ }
66
+ finally {
67
+ cursor.close();
68
+ }
69
+ }
70
+
71
+ public Map<String, Contact> getAll() throws Exception {
72
+ Map<String, Contact> contacts = new HashMap<String, Contact>();
73
+
74
+ Cursor cursor = cr.query(People.CONTENT_URI, null, null, null, null);
75
+ try {
76
+ if (!cursor.moveToFirst())
77
+ return contacts;
78
+
79
+ do {
80
+ Contact contact = new Contact();
81
+
82
+ String id = cursor.getString(cursor.getColumnIndex(People._ID));
83
+ contact.setId(id);
84
+
85
+ //contact.setField(PB_COMPANY_NAME, cursor.getString(cursor.getColumnIndex(People.COMPANY)));
86
+
87
+ contact.setField(PB_FIRST_NAME, "");
88
+ contact.setField(PB_LAST_NAME, "");
89
+ String name = cursor.getString(cursor.getColumnIndex(People.NAME));
90
+ if (name != null) {
91
+ String[] names = name.split(" ");
92
+
93
+ if (names.length == 1) {
94
+ contact.setField(PB_FIRST_NAME, names[0]);
95
+ }
96
+ else if (names.length > 1) {
97
+ contact.setField(PB_FIRST_NAME, names[0]);
98
+ contact.setField(PB_LAST_NAME, name.replaceFirst(names[0] + " ", ""));
99
+ }
100
+ }
101
+
102
+ fillPhones(id, contact);
103
+
104
+ Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
105
+ Long.parseLong(contact.id()));
106
+
107
+ Uri orgUri = Uri.withAppendedPath(uri,
108
+ Contacts.Organizations.CONTENT_DIRECTORY);
109
+
110
+ String[] organizationProjection = new String[] { Organizations.COMPANY };
111
+ Cursor organizationCursor = activity.managedQuery(orgUri,
112
+ organizationProjection, "person=?", new String[] {id}, null);
113
+ try {
114
+ int organizationCursorCount = organizationCursor.getCount();
115
+ if (organizationCursorCount > 0) {
116
+ organizationCursor.moveToFirst();
117
+ int numberColumn = organizationCursor
118
+ .getColumnIndex(Organizations.COMPANY);
119
+
120
+ if (numberColumn != -1)
121
+ contact.setField(PB_COMPANY_NAME,
122
+ organizationCursor.getString(numberColumn));
123
+ }
124
+ }
125
+ finally {
126
+ organizationCursor.close();
127
+ }
128
+
129
+ String[] contactProjection = new String[] {
130
+ Contacts.ContactMethods.KIND,
131
+ Contacts.ContactMethods.DATA };
132
+
133
+ Cursor contactCursor = activity.managedQuery(
134
+ Contacts.ContactMethods.CONTENT_URI,
135
+ contactProjection, "person=?", new String[] {id}, null);
136
+ try {
137
+ int contactCursorCount = contactCursor.getCount();
138
+ if (contactCursorCount > 0) {
139
+ contactCursor.moveToFirst();
140
+ int numberColumn = contactCursor
141
+ .getColumnIndex(Contacts.ContactMethods.DATA);
142
+ int typeColumn = contactCursor
143
+ .getColumnIndex(Contacts.ContactMethods.KIND);
144
+ for (int j = 0; j < contactCursorCount; j++) {
145
+ switch (contactCursor.getInt(typeColumn)) {
146
+ case Contacts.ContactMethods.TYPE_HOME:
147
+ contact.setField(PB_EMAIL_ADDRESS, contactCursor.getString(numberColumn));
148
+ break;
149
+ }
150
+ contactCursor.moveToNext();
151
+ }
152
+ }
153
+ }
154
+ finally {
155
+ contactCursor.close();
156
+ }
157
+
158
+ contacts.put(contact.getField(PB_ID), contact);
159
+ } while (cursor.moveToNext());
160
+ }
161
+ finally {
162
+ cursor.close();
163
+ }
164
+
165
+ return contacts;
166
+ }
167
+
168
+ public void save(Contact contact) throws Exception {
169
+ String rbID = contact.id();
170
+ Uri uri = null;
171
+
172
+ String firstName = contact.getField(PB_FIRST_NAME);
173
+ String lastName = contact.getField(PB_LAST_NAME);
174
+ String name = firstName + " " + lastName;
175
+
176
+ boolean isNew = false;
177
+ if (rbID != null && !"".equals(rbID)) {
178
+ // update
179
+ long id = Long.parseLong(rbID);
180
+ uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
181
+ }
182
+ else {
183
+ // create
184
+ isNew = true;
185
+ ContentValues person = new ContentValues();
186
+ person.put(People.NAME, name);
187
+
188
+ uri = cr.insert(People.CONTENT_URI, person);
189
+ }
190
+
191
+ if (uri == null)
192
+ throw new Exception("Can not save contact");
193
+
194
+ if (!isNew) {
195
+ ContentValues values = new ContentValues();
196
+ values.put(People.NAME, name);
197
+
198
+ cr.update(uri, values, null, null);
199
+ }
200
+
201
+ String pathLeaf = (String) uri.getPathSegments().get(
202
+ uri.getPathSegments().size() - 1);
203
+
204
+ contact.setId(pathLeaf);
205
+
206
+ String[] phones = {PB_MOBILE_NUMBER, PB_HOME_NUMBER, PB_BUSINESS_NUMBER};
207
+ int[] types = {Phones.TYPE_MOBILE, Phones.TYPE_HOME, Phones.TYPE_WORK};
208
+ for (int i = 0; i < phones.length; ++i) {
209
+ String phName = phones[i];
210
+ String value = contact.getField(phName);
211
+ if (value == null)
212
+ continue;
213
+ ContentValues number = new ContentValues();
214
+ number.put(Phones.PERSON_ID, pathLeaf);
215
+ number.put(Phones.NUMBER, value);
216
+ number.put(Phones.TYPE, types[i]);
217
+
218
+ Uri phoneUpdate = cr.insert(Phones.CONTENT_URI, number);
219
+
220
+ if (phoneUpdate == null) {
221
+ int retval = cr.update(People.CONTENT_URI, number, null, null);
222
+
223
+ if (retval == 0)
224
+ throw new Exception("Failed to insert phone number");
225
+ }
226
+ }
227
+
228
+ // add email
229
+ if (contact.getField(PB_EMAIL_ADDRESS) != null) {
230
+ ContentValues email = new ContentValues();
231
+ email.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
232
+ email.put(Contacts.ContactMethods.KIND,
233
+ Contacts.ContactMethods.TYPE_HOME);
234
+
235
+ email.put(Contacts.ContactMethods.DATA, contact.getField(PB_EMAIL_ADDRESS));
236
+
237
+ email.put(Contacts.ContactMethods.TYPE,
238
+ Contacts.ContactMethods.CONTENT_EMAIL_ITEM_TYPE);
239
+
240
+ Uri emailUpdate = cr.insert(Uri.withAppendedPath(uri,
241
+ Contacts.ContactMethods.CONTENT_URI.getPath()
242
+ .substring(1)), email);
243
+ if (emailUpdate == null) {
244
+ throw new Exception("Failed to insert primary email");
245
+ }
246
+ }
247
+ // add organization
248
+ if (contact.getField(PB_COMPANY_NAME) != null) {
249
+ Uri orgUri = Uri.withAppendedPath(uri,
250
+ Contacts.Organizations.CONTENT_DIRECTORY);
251
+
252
+ ContentValues company = new ContentValues();
253
+ company.put(Contacts.Organizations.PERSON_ID, pathLeaf);
254
+ company.put(Contacts.Organizations.TYPE,
255
+ Contacts.Organizations.TYPE_WORK);
256
+
257
+ company.put(Contacts.Organizations.COMPANY, contact
258
+ .getField(PB_COMPANY_NAME));
259
+
260
+ Uri companyUpdate = cr.insert(orgUri, company);
261
+
262
+ if (companyUpdate == null) {
263
+ throw new Exception("Failed to insert company");
264
+ }
265
+ }
266
+ }
267
+
268
+ public void remove(Contact contact) {
269
+ Uri uri = People.CONTENT_URI;
270
+
271
+ String id = contact.id();
272
+ cr.delete(uri, People._ID + "=" + id, null);
273
+ }
274
+
275
+ }
@@ -20,383 +20,186 @@
20
20
  */
21
21
  package com.rhomobile.rhodes.phonebook;
22
22
 
23
- import java.util.HashMap;
24
23
  import java.util.Iterator;
25
24
  import java.util.Map;
26
- import java.util.regex.Matcher;
27
- import java.util.regex.Pattern;
28
25
 
29
- import android.app.Activity;
30
- import android.content.ContentResolver;
31
- import android.content.ContentUris;
32
- import android.content.ContentValues;
33
- import android.database.Cursor;
34
- import android.net.Uri;
35
- import android.provider.Contacts;
36
- import android.provider.Contacts.Organizations;
37
- import android.provider.Contacts.People;
38
- import android.provider.Contacts.Phones;
26
+ import android.os.Build;
39
27
 
40
28
  import com.rhomobile.rhodes.Capabilities;
41
29
  import com.rhomobile.rhodes.Logger;
42
- import com.rhomobile.rhodes.RhodesInstance;
43
30
 
44
31
  //@RubyLevelClass(name="Phonebook")
45
32
  public class Phonebook {
46
33
 
47
34
  private static final String TAG = "Phonebook";
48
35
 
49
- private Map<String, Contact> contactList = new HashMap<String, Contact>();
50
- private Activity activity;
51
- private ContentResolver cr;
52
- private Pattern idPattern;
36
+ public static final String PB_ID = "id";
37
+ public static final String PB_FIRST_NAME = "first_name";
38
+ public static final String PB_LAST_NAME = "last_name";
39
+ public static final String PB_MOBILE_NUMBER = "mobile_number";
40
+ public static final String PB_HOME_NUMBER = "home_number";
41
+ public static final String PB_BUSINESS_NUMBER = "business_number";
42
+ public static final String PB_EMAIL_ADDRESS = "email_address";
43
+ public static final String PB_COMPANY_NAME = "company_name";
53
44
 
45
+ private Map<String, Contact> contactList;
46
+ private ContactAccessor accessor;
54
47
  private Iterator<Contact> iter = null;
55
48
 
56
- static final String PB_ID = "id";
57
- static final String PB_FIRST_NAME = "first_name";
58
- static final String PB_LAST_NAME = "last_name";
59
- static final String PB_MOBILE_NUMBER = "mobile_number";
60
- static final String PB_HOME_NUMBER = "home_number";
61
- static final String PB_BUSINESS_NUMBER = "business_number";
62
- static final String PB_EMAIL_ADDRESS = "email_address";
63
- static final String PB_COMPANY_NAME = "company_name";
64
-
65
- private String getId(Contact contact) {
66
- Matcher m = idPattern.matcher(contact.getField(PB_ID));
67
- return m.find() ? m.group(1) : "";
68
- }
69
-
70
49
  private boolean checkState() {
71
50
  if (!Capabilities.PIM_ENABLED)
72
51
  Logger.E(TAG, "Can not execute: PIM disabled");
73
52
  return Capabilities.PIM_ENABLED;
74
53
  }
75
54
 
76
- public Phonebook() {
77
- if (!checkState())
78
- return;
55
+ private ContactAccessor createAccessor() {
56
+ String className;
57
+ int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
58
+ if (sdkVersion < Build.VERSION_CODES.ECLAIR)
59
+ className = "ContactAccessorOld";
60
+ else
61
+ className = "ContactAccessorNew";
79
62
 
80
- fill();
63
+ try {
64
+ String fullName = "com.rhomobile.rhodes.phonebook." + className;
65
+ Class<? extends ContactAccessor> klass =
66
+ Class.forName(fullName).asSubclass(ContactAccessor.class);
67
+ return klass.newInstance();
68
+ }
69
+ catch (Exception e) {
70
+ throw new IllegalStateException(e);
71
+ }
81
72
  }
82
73
 
83
- private void fill() {
84
-
85
- idPattern = Pattern.compile("\\{([0-9]+)\\}");
86
- activity = RhodesInstance.getInstance();
87
- cr = activity.getContentResolver();
88
- Cursor cursor = cr.query(People.CONTENT_URI, null, null, null, null);
89
-
90
- // load contacts
91
-
92
- if (cursor.moveToFirst()) {
93
- do {
94
- Contact contact = new Contact();
95
-
96
- contact.setField(PB_ID, "{" + cursor.getString(cursor.getColumnIndex(People._ID)) + "}");
97
-
98
- //contact.setField(PB_COMPANY_NAME, cursor.getString(cursor.getColumnIndex(People.COMPANY)));
99
-
100
- String name = cursor.getString(cursor
101
- .getColumnIndex(People.NAME));
102
-
103
- contact.setField(PB_FIRST_NAME, "");
104
- contact.setField(PB_LAST_NAME, "");
105
-
106
- if (name != null) {
107
- String[] names = name.split(" ");
108
-
109
- if (names.length == 1) {
110
- contact.setField(PB_FIRST_NAME, names[0]);
111
- }
112
- else if (names.length > 1) {
113
- contact.setField(PB_FIRST_NAME, names[0]);
114
- contact.setField(PB_LAST_NAME, name.replaceFirst(names[0] + " ", ""));
115
- }
116
- }
117
-
118
- long personId = cursor.getLong(cursor
119
- .getColumnIndex(People._ID));
120
-
121
- String whereClause = "person=" + personId;
122
-
123
- String[] phonesProjection = new String[] { Phones.NUMBER,
124
- Phones.TYPE };
125
- Cursor phonesCursor = activity.managedQuery(
126
- Contacts.Phones.CONTENT_URI, phonesProjection,
127
- whereClause, null, null);
128
-
129
- int phonesCursorCount = phonesCursor.getCount();
130
- if (phonesCursorCount > 0) {
131
- phonesCursor.moveToFirst();
132
- int numberColumn = phonesCursor
133
- .getColumnIndex(Phones.NUMBER);
134
- int typeColumn = phonesCursor
135
- .getColumnIndex(Phones.TYPE);
136
- for (int j = 0; j < phonesCursorCount; j++) {
137
- switch (phonesCursor.getInt(typeColumn)) {
138
- case Phones.TYPE_WORK:
139
- contact.setField(PB_BUSINESS_NUMBER, phonesCursor.getString(numberColumn));
140
- break;
141
- case Phones.TYPE_HOME:
142
- contact.setField(PB_HOME_NUMBER, phonesCursor.getString(numberColumn));
143
- break;
144
- case Phones.TYPE_MOBILE:
145
- contact.setField(PB_MOBILE_NUMBER, phonesCursor.getString(numberColumn));
146
- break;
147
- }
148
- phonesCursor.moveToNext();
149
- }
150
- }
151
-
152
- phonesCursor.close();
153
-
154
- Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
155
- Long.parseLong(getId(contact)));
156
-
157
- Uri orgUri = Uri.withAppendedPath(uri,
158
- Contacts.Organizations.CONTENT_DIRECTORY);
159
-
160
- String[] organizationProjection = new String[] { Organizations.COMPANY };
161
- Cursor organizationCursor = activity.managedQuery(orgUri,
162
- organizationProjection, whereClause, null, null);
163
-
164
- int organizationCursorCount = organizationCursor.getCount();
165
- if (organizationCursorCount > 0) {
166
- organizationCursor.moveToFirst();
167
- int numberColumn = organizationCursor
168
- .getColumnIndex(Organizations.COMPANY);
169
-
170
- if (numberColumn != -1)
171
- contact.setField(PB_COMPANY_NAME,
172
- organizationCursor.getString(numberColumn));
173
- }
174
-
175
- organizationCursor.close();
176
-
177
- String[] contactProjection = new String[] {
178
- Contacts.ContactMethods.KIND,
179
- Contacts.ContactMethods.DATA };
180
-
181
- Cursor contactCursor = activity.managedQuery(
182
- Contacts.ContactMethods.CONTENT_URI,
183
- contactProjection, whereClause, null, null);
184
-
185
- int contactCursorCount = contactCursor.getCount();
186
- if (contactCursorCount > 0) {
187
- contactCursor.moveToFirst();
188
- int numberColumn = contactCursor
189
- .getColumnIndex(Contacts.ContactMethods.DATA);
190
- int typeColumn = contactCursor
191
- .getColumnIndex(Contacts.ContactMethods.KIND);
192
- for (int j = 0; j < contactCursorCount; j++) {
193
- switch (contactCursor.getInt(typeColumn)) {
194
- case Contacts.ContactMethods.TYPE_HOME:
195
- contact.setField(PB_EMAIL_ADDRESS, contactCursor.getString(numberColumn));
196
- break;
197
- }
198
- contactCursor.moveToNext();
199
- }
200
- }
201
-
202
- contactCursor.close();
203
-
204
- this.contactList.put(contact.getField(PB_ID), contact);
205
- } while (cursor.moveToNext());
74
+ public Phonebook() {
75
+ try {
76
+ if (!checkState())
77
+ return;
78
+
79
+ accessor = createAccessor();
80
+ contactList = accessor.getAll();
81
+ moveToBegin();
82
+ }
83
+ catch (Exception e) {
84
+ Logger.E(TAG, e);
206
85
  }
207
-
208
- cursor.close();
209
-
210
- moveToBegin();
211
86
  }
212
-
87
+
213
88
  public void close() {
214
- if (!checkState())
215
- return;
216
-
217
- this.contactList.clear();
89
+ try {
90
+ if (!checkState())
91
+ return;
92
+
93
+ this.contactList.clear();
94
+ }
95
+ catch (Exception e) {
96
+ Logger.E(TAG, e);
97
+ }
218
98
  }
219
99
 
220
100
  public void moveToBegin() {
221
- if (!checkState())
222
- return;
223
-
224
- iter = contactList.values().iterator();
101
+ try {
102
+ if (!checkState())
103
+ return;
104
+
105
+ iter = contactList.values().iterator();
106
+ }
107
+ catch (Exception e) {
108
+ Logger.E(TAG, e);
109
+ }
225
110
  }
226
111
 
227
112
  public boolean hasNext() {
228
- if (!checkState())
113
+ try {
114
+ if (!checkState())
115
+ return false;
116
+
117
+ return iter.hasNext();
118
+ }
119
+ catch (Exception e) {
120
+ Logger.E(TAG, e);
229
121
  return false;
230
-
231
- return iter.hasNext();
122
+ }
232
123
  }
233
124
 
234
125
  public Object next() {
235
- if (!checkState())
126
+ try {
127
+ if (!checkState())
128
+ return null;
129
+
130
+ return iter.next();
131
+ }
132
+ catch (Exception e) {
133
+ Logger.E(TAG, e);
236
134
  return null;
237
-
238
- return iter.next();
135
+ }
239
136
  }
240
137
 
241
138
  public Contact getFirstRecord() {
242
- if (!checkState())
243
- return null;
244
-
245
- moveToBegin();
246
- if (!iter.hasNext())
139
+ try {
140
+ if (!checkState())
141
+ return null;
142
+
143
+ moveToBegin();
144
+ if (!iter.hasNext())
145
+ return null;
146
+ return iter.next();
147
+ }
148
+ catch (Exception e) {
149
+ Logger.E(TAG, e);
247
150
  return null;
248
- return iter.next();
151
+ }
249
152
  }
250
153
 
251
154
  public Contact getNextRecord() {
252
- if (!checkState())
155
+ try {
156
+ if (!checkState())
157
+ return null;
158
+
159
+ return iter.next();
160
+ }
161
+ catch (Exception e) {
162
+ Logger.E(TAG, e);
253
163
  return null;
254
-
255
- return iter.next();
164
+ }
256
165
  }
257
166
 
258
167
  public Contact getRecord(String id) {
259
- if (!checkState())
168
+ try {
169
+ if (!checkState())
170
+ return null;
171
+
172
+ return contactList.get(id);
173
+ }
174
+ catch (Exception e) {
175
+ Logger.E(TAG, e);
260
176
  return null;
261
-
262
- return contactList.get(id);
177
+ }
263
178
  }
264
179
 
265
- public void removeContact(Contact contact) throws Exception {
266
- if (!checkState())
267
- return;
268
-
269
- Uri uri = People.CONTENT_URI;
270
-
271
- String id = getId(contact);
272
- cr.delete(uri, People._ID + "=" + id, null);
180
+ public void removeContact(Contact contact) {
181
+ try {
182
+ if (!checkState())
183
+ return;
184
+
185
+ accessor.remove(contact);
186
+ contactList.remove(contact.getField(PB_ID));
187
+ }
188
+ catch (Exception e) {
189
+ Logger.E(TAG, e);
190
+ }
273
191
  }
274
192
 
275
- public void saveContact(Contact contact) throws Exception {
276
- if (!checkState())
277
- return;
278
-
279
- String rbID = getId(contact);
280
- Uri uri = null;
281
-
282
- String name = contact.getField(PB_FIRST_NAME) + " "
283
- + contact.getField(PB_LAST_NAME);
284
-
285
- boolean isNew = false;
286
- if (rbID != null && !"".equals(rbID))// update
287
- {
288
- uri = ContentUris.withAppendedId(People.CONTENT_URI, Long.parseLong(rbID));
289
- } else // create
290
- {
291
- isNew = true;
292
- ContentValues person = new ContentValues();
293
- person.put(Contacts.People.NAME, name);
294
-
295
- uri = cr.insert(Contacts.People.CONTENT_URI, person);
193
+ public void saveContact(Contact contact) {
194
+ try {
195
+ if (!checkState())
196
+ return;
197
+
198
+ accessor.save(contact);
199
+ contactList.put(contact.getField(PB_ID), contact);
296
200
  }
297
-
298
- if (uri != null) {
299
-
300
- if (!isNew) {
301
- ContentValues values = new ContentValues();
302
- values.put(People.NAME, name);
303
-
304
- activity.getContentResolver().update(uri, values, null, null);
305
- }
306
-
307
- String pathLeaf = (String) uri.getPathSegments().get(
308
- uri.getPathSegments().size() - 1);
309
-
310
- contact.setField(PB_ID, "{" + pathLeaf + "}");
311
-
312
- // add mobile phone number
313
-
314
- if (!contact.getField(PB_MOBILE_NUMBER).equals("")) {
315
- ContentValues number = new ContentValues();
316
- number.put(Contacts.Phones.PERSON_ID, pathLeaf);
317
- number.put(Contacts.Phones.NUMBER, contact.getField(PB_MOBILE_NUMBER));
318
- number.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_MOBILE);
319
-
320
- Uri phoneUpdate = cr.insert(Contacts.Phones.CONTENT_URI, number);
321
-
322
- if (phoneUpdate == null) {
323
- int retval = cr.update(People.CONTENT_URI, number, null, null);
324
-
325
- if (retval == 0)
326
- throw new Exception("Failed to insert mobile number");
327
- }
328
- }
329
-
330
- // add home phone number
331
- if (!contact.getField(PB_HOME_NUMBER).equals("")) {
332
- ContentValues home = new ContentValues();
333
- home.put(Contacts.Phones.PERSON_ID, pathLeaf);
334
- home.put(Contacts.Phones.NUMBER, contact.getField(PB_HOME_NUMBER));
335
- home.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_HOME);
336
-
337
- Uri homeUpdate = cr.insert(Contacts.Phones.CONTENT_URI, home);
338
-
339
- if (homeUpdate == null) {
340
- int retval = cr.update(People.CONTENT_URI, home, null, null);
341
-
342
- if (retval == 0)
343
- throw new Exception("Failed to insert home number");
344
- }
345
- }
346
- // add work phone number
347
- if (!contact.getField(PB_BUSINESS_NUMBER).equals("")) {
348
- ContentValues work = new ContentValues();
349
- work.put(Contacts.Phones.PERSON_ID, pathLeaf);
350
- work.put(Contacts.Phones.NUMBER, contact.getField(PB_BUSINESS_NUMBER));
351
- work.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_WORK);
352
-
353
- Uri workUpdate = cr.insert(Contacts.Phones.CONTENT_URI, work);
354
-
355
- if (workUpdate == null) {
356
- int retval = cr.update(People.CONTENT_URI, work, null, null);
357
-
358
- if (retval == 0)
359
- throw new Exception("Failed to insert work number");
360
- }
361
- }
362
- // add email
363
- if (!contact.getField(PB_EMAIL_ADDRESS).equals("")) {
364
- ContentValues email = new ContentValues();
365
- email.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
366
- email.put(Contacts.ContactMethods.KIND,
367
- Contacts.ContactMethods.TYPE_HOME);
368
-
369
- email.put(Contacts.ContactMethods.DATA, contact.getField(PB_EMAIL_ADDRESS));
370
-
371
- email.put(Contacts.ContactMethods.TYPE,
372
- Contacts.ContactMethods.CONTENT_EMAIL_ITEM_TYPE);
373
-
374
- Uri emailUpdate = cr.insert(Uri.withAppendedPath(uri,
375
- Contacts.ContactMethods.CONTENT_URI.getPath()
376
- .substring(1)), email);
377
- if (emailUpdate == null) {
378
- throw new Exception("Failed to insert primary email");
379
- }
380
- }
381
- // add organization
382
- if (!contact.getField(PB_COMPANY_NAME).equals("")) {
383
- Uri orgUri = Uri.withAppendedPath(uri,
384
- Contacts.Organizations.CONTENT_DIRECTORY);
385
-
386
- ContentValues company = new ContentValues();
387
- company.put(Contacts.Organizations.PERSON_ID, pathLeaf);
388
- company.put(Contacts.Organizations.TYPE,
389
- Contacts.Organizations.TYPE_WORK);
390
-
391
- company.put(Contacts.Organizations.COMPANY, contact
392
- .getField(PB_COMPANY_NAME));
393
-
394
- Uri companyUpdate = cr.insert(orgUri, company);
395
-
396
- if (companyUpdate == null) {
397
- throw new Exception("Failed to insert company");
398
- }
399
- }
201
+ catch (Exception e) {
202
+ Logger.E(TAG, e);
400
203
  }
401
204
  }
402
205