12 import android.database.Cursor; |
12 import android.database.Cursor; |
13 import android.database.SQLException; |
13 import android.database.SQLException; |
14 import android.database.sqlite.SQLiteDatabase; |
14 import android.database.sqlite.SQLiteDatabase; |
15 import android.database.sqlite.SQLiteQueryBuilder; |
15 import android.database.sqlite.SQLiteQueryBuilder; |
16 import android.net.Uri; |
16 import android.net.Uri; |
|
17 import android.provider.BaseColumns; |
17 import android.text.TextUtils; |
18 import android.text.TextUtils; |
18 |
19 |
19 /** |
20 /** |
20 * @author dasilvj |
21 * @author dasilvj |
21 * |
|
22 */ |
22 */ |
23 public class ContactProvider extends ContentProvider { |
23 public class ContactProvider extends ContentProvider { |
24 |
24 |
25 private final static String TAG = "ContactProvider"; |
25 private final static String TAG = "ContactProvider"; |
26 |
26 |
27 private static HashMap<String, String> sContactsProjectionMap; |
27 private static HashMap<String, String> sContactsProjectionMap; |
28 |
28 |
29 private static final int CONTACTS = 1; |
29 private static final int CONTACTS = 1; |
30 private static final int CONTACT_ID = 2; |
30 private static final int CONTACT_ID = 2; |
31 |
31 |
32 private static final UriMatcher sUriMatcher; |
32 private static final UriMatcher sUriMatcher; |
33 private BeemDatabaseHelper mOpenHelper; |
33 static { |
34 |
34 sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
35 @Override |
35 sUriMatcher.addURI(Beem.AUTHORITY, "contacts", CONTACTS); |
36 public int delete(Uri uri, String selection, String[] selectionArgs) { |
36 sUriMatcher.addURI(Beem.AUTHORITY, "contacts/#", CONTACT_ID); |
37 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
37 |
38 int count; |
38 sContactsProjectionMap = new HashMap<String, String>(); |
39 |
39 sContactsProjectionMap.put(BaseColumns._ID, BaseColumns._ID); |
40 switch (sUriMatcher.match(uri)) { |
40 sContactsProjectionMap.put(Beem.Contacts.UID, Beem.Contacts.UID); |
41 case CONTACTS: |
41 sContactsProjectionMap.put(Beem.Contacts.JID, Beem.Contacts.JID); |
42 count = db.delete(Beem.CONTACTS_TABLE_NAME, selection, selectionArgs); |
42 sContactsProjectionMap.put(Beem.Contacts.NICKNAME, Beem.Contacts.NICKNAME); |
43 break; |
43 sContactsProjectionMap.put(Beem.Contacts.ALIAS, Beem.Contacts.ALIAS); |
44 |
44 sContactsProjectionMap.put(Beem.Contacts.DATE_CREATED, Beem.Contacts.DATE_CREATED); |
45 case CONTACT_ID: |
45 sContactsProjectionMap.put(Beem.Contacts.DATE_MODIFIED, Beem.Contacts.DATE_MODIFIED); |
46 String contactId = uri.getPathSegments().get(1); |
46 } |
47 count = db.delete(Beem.CONTACTS_TABLE_NAME, Beem.Contacts._ID + "=" + contactId |
47 |
48 + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); |
48 private BeemDatabaseHelper mOpenHelper; |
49 break; |
49 |
50 |
50 @Override |
51 default: |
51 public int delete(Uri uri, String selection, String[] selectionArgs) { |
52 throw new IllegalArgumentException("Unknown URI " + uri); |
52 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
53 } |
53 int count; |
54 |
54 |
55 getContext().getContentResolver().notifyChange(uri, null); |
55 switch (sUriMatcher.match(uri)) { |
56 return count; |
56 case CONTACTS: |
57 } |
57 count = db.delete(Beem.CONTACTS_TABLE_NAME, selection, selectionArgs); |
58 |
58 break; |
59 @Override |
59 |
60 public String getType(Uri uri) { |
60 case CONTACT_ID: |
61 switch (sUriMatcher.match(uri)) { |
61 String contactId = uri.getPathSegments().get(1); |
62 case CONTACTS: |
62 count = db.delete(Beem.CONTACTS_TABLE_NAME, BaseColumns._ID + "=" + contactId |
63 return Beem.Contacts.CONTENT_TYPE; |
63 + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); |
64 |
64 break; |
65 case CONTACT_ID: |
65 |
66 return Beem.Contacts.CONTENT_ITEM_TYPE; |
66 default: |
67 |
67 throw new IllegalArgumentException("Unknown URI " + uri); |
68 default: |
68 } |
69 throw new IllegalArgumentException("Unknown URI " + uri); |
69 |
70 } |
70 getContext().getContentResolver().notifyChange(uri, null); |
71 } |
71 return count; |
72 |
72 } |
73 @Override |
73 |
74 public Uri insert(Uri uri, ContentValues initialValues) { |
74 @Override |
75 // Validate the requested uri |
75 public String getType(Uri uri) { |
76 if (sUriMatcher.match(uri) != CONTACTS) { |
76 switch (sUriMatcher.match(uri)) { |
77 throw new IllegalArgumentException("Unknown URI " + uri); |
77 case CONTACTS: |
78 } |
78 return Beem.Contacts.CONTENT_TYPE; |
79 |
79 |
80 ContentValues values; |
80 case CONTACT_ID: |
81 if (initialValues != null) { |
81 return Beem.Contacts.CONTENT_ITEM_TYPE; |
82 values = new ContentValues(initialValues); |
82 |
83 } else { |
83 default: |
84 values = new ContentValues(); |
84 throw new IllegalArgumentException("Unknown URI " + uri); |
85 } |
85 } |
86 |
86 } |
87 Long now = Long.valueOf(System.currentTimeMillis()); |
87 |
88 |
88 @Override |
89 // Make sure that the fields are all set |
89 public Uri insert(Uri uri, ContentValues initialValues) { |
90 if (values.containsKey(Beem.Contacts.UID) == false) { |
90 // Validate the requested uri |
91 // TODO :: Must check that the UID exists using UserProvider |
91 if (sUriMatcher.match(uri) != CONTACTS) { |
92 throw new SQLException("No UID specified. Failed to insert row into " + uri); |
92 throw new IllegalArgumentException("Unknown URI " + uri); |
93 } |
93 } |
94 |
94 |
95 if (values.containsKey(Beem.Contacts.JID) == false) { |
95 ContentValues values; |
96 values.put(Beem.Contacts.JID, ""); |
96 if (initialValues != null) { |
97 } |
97 values = new ContentValues(initialValues); |
98 |
98 } else { |
99 if (values.containsKey(Beem.Contacts.NICKNAME) == false) { |
99 values = new ContentValues(); |
100 values.put(Beem.Contacts.JID, ""); |
100 } |
101 } |
101 |
102 |
102 Long now = Long.valueOf(System.currentTimeMillis()); |
103 if (values.containsKey(Beem.Contacts.ALIAS) == false) { |
103 |
104 values.put(Beem.Contacts.JID, ""); |
104 // Make sure that the fields are all set |
105 } |
105 if (values.containsKey(Beem.Contacts.UID) == false) { |
106 |
106 // TODO :: Must check that the UID exists using UserProvider |
107 if (values.containsKey(Beem.Contacts.DATE_CREATED) == false) { |
107 throw new SQLException("No UID specified. Failed to insert row into " + uri); |
108 values.put(Beem.Contacts.DATE_CREATED, now); |
108 } |
109 } |
109 |
110 |
110 if (values.containsKey(Beem.Contacts.JID) == false) { |
111 if (values.containsKey(Beem.Contacts.DATE_MODIFIED) == false) { |
111 values.put(Beem.Contacts.JID, ""); |
112 values.put(Beem.Contacts.DATE_MODIFIED, now); |
112 } |
113 } |
113 |
114 |
114 if (values.containsKey(Beem.Contacts.NICKNAME) == false) { |
115 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
115 values.put(Beem.Contacts.JID, ""); |
116 long rowId = db.insert(Beem.CONTACTS_TABLE_NAME, Beem.Contacts._ID, values); |
116 } |
117 if (rowId > 0) { |
117 |
118 Uri contactUri = ContentUris.withAppendedId(Beem.Contacts.CONTENT_URI, rowId); |
118 if (values.containsKey(Beem.Contacts.ALIAS) == false) { |
119 getContext().getContentResolver().notifyChange(contactUri, null); |
119 values.put(Beem.Contacts.JID, ""); |
120 return contactUri; |
120 } |
121 } |
121 |
122 |
122 if (values.containsKey(Beem.Contacts.DATE_CREATED) == false) { |
123 throw new SQLException("Failed to insert row into " + uri); |
123 values.put(Beem.Contacts.DATE_CREATED, now); |
124 } |
124 } |
125 |
125 |
126 @Override |
126 if (values.containsKey(Beem.Contacts.DATE_MODIFIED) == false) { |
127 public boolean onCreate() { |
127 values.put(Beem.Contacts.DATE_MODIFIED, now); |
128 mOpenHelper = new BeemDatabaseHelper(getContext(), TAG, Beem.CONTACTS_TABLE_NAME, Beem.Contacts.QUERY_CREATE); |
128 } |
129 return true; |
129 |
130 } |
130 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
131 |
131 long rowId = db.insert(Beem.CONTACTS_TABLE_NAME, BaseColumns._ID, values); |
132 @Override |
132 if (rowId > 0) { |
133 public Cursor query(Uri uri, String[] projection, String selection, |
133 Uri contactUri = ContentUris.withAppendedId(Beem.Contacts.CONTENT_URI, rowId); |
134 String[] selectionArgs, String sortOrder) { |
134 getContext().getContentResolver().notifyChange(contactUri, null); |
135 SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
135 return contactUri; |
136 |
136 } |
137 switch (sUriMatcher.match(uri)) { |
137 |
138 case CONTACTS: |
138 throw new SQLException("Failed to insert row into " + uri); |
139 qb.setTables(Beem.CONTACTS_TABLE_NAME); |
139 } |
140 qb.setProjectionMap(sContactsProjectionMap); |
140 |
141 break; |
141 @Override |
142 |
142 public boolean onCreate() { |
143 case CONTACT_ID: |
143 mOpenHelper = new BeemDatabaseHelper(getContext(), TAG, Beem.CONTACTS_TABLE_NAME, Beem.Contacts.QUERY_CREATE); |
144 qb.setTables(Beem.USERS_TABLE_NAME); |
144 return true; |
145 qb.setProjectionMap(sContactsProjectionMap); |
145 } |
146 qb.appendWhere(Beem.Contacts._ID + "=" + uri.getPathSegments().get(1)); |
146 |
147 break; |
147 @Override |
148 |
148 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { |
149 default: |
149 SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
150 throw new IllegalArgumentException("Unknown URI " + uri); |
150 |
151 } |
151 switch (sUriMatcher.match(uri)) { |
152 |
152 case CONTACTS: |
153 // If no sort order is specified use the default |
153 qb.setTables(Beem.CONTACTS_TABLE_NAME); |
154 String orderBy; |
154 qb.setProjectionMap(sContactsProjectionMap); |
155 if (TextUtils.isEmpty(sortOrder)) { |
155 break; |
156 orderBy = Beem.Contacts.DEFAULT_SORT_ORDER; |
156 |
157 } else { |
157 case CONTACT_ID: |
158 orderBy = sortOrder; |
158 qb.setTables(Beem.USERS_TABLE_NAME); |
159 } |
159 qb.setProjectionMap(sContactsProjectionMap); |
160 |
160 qb.appendWhere(BaseColumns._ID + "=" + uri.getPathSegments().get(1)); |
161 // Get the database and run the query |
161 break; |
162 SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
162 |
163 Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); |
163 default: |
164 |
164 throw new IllegalArgumentException("Unknown URI " + uri); |
165 // Tell the cursor what uri to watch, so it knows when its source data changes |
165 } |
166 c.setNotificationUri(getContext().getContentResolver(), uri); |
166 |
167 return c; |
167 // If no sort order is specified use the default |
168 } |
168 String orderBy; |
169 |
169 if (TextUtils.isEmpty(sortOrder)) { |
170 @Override |
170 orderBy = Beem.Contacts.DEFAULT_SORT_ORDER; |
171 public int update(Uri uri, ContentValues values, String selection, |
171 } else { |
172 String[] selectionArgs) { |
172 orderBy = sortOrder; |
173 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
173 } |
174 int count; |
174 |
175 |
175 // Get the database and run the query |
176 switch (sUriMatcher.match(uri)) { |
176 SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
177 case CONTACTS: |
177 Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); |
178 count = db.update(Beem.CONTACTS_TABLE_NAME, values, selection, selectionArgs); |
178 |
179 break; |
179 // Tell the cursor what uri to watch, so it knows when its source data changes |
180 |
180 c.setNotificationUri(getContext().getContentResolver(), uri); |
181 case CONTACT_ID: |
181 return c; |
182 String contactId = uri.getPathSegments().get(1); |
182 } |
183 count = db.update(Beem.CONTACTS_TABLE_NAME, values, Beem.Contacts._ID + "=" + contactId |
183 |
184 + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); |
184 @Override |
185 break; |
185 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
186 |
186 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
187 default: |
187 int count; |
188 throw new IllegalArgumentException("Unknown URI " + uri); |
188 |
189 } |
189 switch (sUriMatcher.match(uri)) { |
190 |
190 case CONTACTS: |
191 getContext().getContentResolver().notifyChange(uri, null); |
191 count = db.update(Beem.CONTACTS_TABLE_NAME, values, selection, selectionArgs); |
192 return count; |
192 break; |
193 } |
193 |
194 |
194 case CONTACT_ID: |
195 static { |
195 String contactId = uri.getPathSegments().get(1); |
196 sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
196 count = db.update(Beem.CONTACTS_TABLE_NAME, values, BaseColumns._ID + "=" + contactId |
197 sUriMatcher.addURI(Beem.AUTHORITY, "contacts", CONTACTS); |
197 + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); |
198 sUriMatcher.addURI(Beem.AUTHORITY, "contacts/#", CONTACT_ID); |
198 break; |
199 |
199 |
200 sContactsProjectionMap = new HashMap<String, String>(); |
200 default: |
201 sContactsProjectionMap.put(Beem.Contacts._ID, Beem.Contacts._ID); |
201 throw new IllegalArgumentException("Unknown URI " + uri); |
202 sContactsProjectionMap.put(Beem.Contacts.UID, Beem.Contacts.UID); |
202 } |
203 sContactsProjectionMap.put(Beem.Contacts.JID, Beem.Contacts.JID); |
203 |
204 sContactsProjectionMap.put(Beem.Contacts.NICKNAME, Beem.Contacts.NICKNAME); |
204 getContext().getContentResolver().notifyChange(uri, null); |
205 sContactsProjectionMap.put(Beem.Contacts.ALIAS, Beem.Contacts.ALIAS); |
205 return count; |
206 sContactsProjectionMap.put(Beem.Contacts.DATE_CREATED, Beem.Contacts.DATE_CREATED); |
206 } |
207 sContactsProjectionMap.put(Beem.Contacts.DATE_MODIFIED, Beem.Contacts.DATE_MODIFIED); |
|
208 } |
|
209 } |
207 } |