| 1 | /** |
| 2 | <Trolly is a simple shopping list application for android phones.> |
| 3 | Copyright (C) 2009 Ben Caldwell |
| 4 | |
| 5 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | package caldwell.ben.trolly; |
| 20 | |
| 21 | import caldwell.ben.provider.Trolly; |
| 22 | import caldwell.ben.provider.Trolly.ShoppingList; |
| 23 | |
| 24 | import android.content.ContentProvider; |
| 25 | import android.content.ContentUris; |
| 26 | import android.content.ContentValues; |
| 27 | import android.content.Context; |
| 28 | import android.content.UriMatcher; |
| 29 | import android.content.res.Resources; |
| 30 | import android.database.Cursor; |
| 31 | import android.database.SQLException; |
| 32 | import android.database.sqlite.SQLiteDatabase; |
| 33 | import android.database.sqlite.SQLiteOpenHelper; |
| 34 | import android.database.sqlite.SQLiteQueryBuilder; |
| 35 | import android.net.Uri; |
| 36 | import android.text.TextUtils; |
| 37 | import android.util.Log; |
| 38 | |
| 39 | import java.util.HashMap; |
| 40 | |
| 41 | /** |
| 42 | * Provides access to a database of notes. Each note has a title, the note |
| 43 | * itself, a creation date and a modified data. |
| 44 | */ |
| 45 | public class TrollyProvider extends ContentProvider { |
| 46 | |
| 47 | private static final String TAG = "TrollyProvider"; |
| 48 | |
| 49 | private static final String DATABASE_NAME = "trolly.db"; |
| 50 | private static final int DATABASE_VERSION = 2; |
| 51 | private static final String TABLE_NAME = "shopping_list"; |
| 52 | |
| 53 | private static HashMap<String, String> sProjectionMap; |
| 54 | |
| 55 | private static final int ITEMS = 1; |
| 56 | private static final int ITEM_ID = 2; |
| 57 | |
| 58 | private static final UriMatcher sUriMatcher; |
| 59 | |
| 60 | /** |
| 61 | * This class helps open, create, and upgrade the database file. |
| 62 | */ |
| 63 | private static class DatabaseHelper extends SQLiteOpenHelper { |
| 64 | |
| 65 | DatabaseHelper(Context context) { |
| 66 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onCreate(SQLiteDatabase db) { |
| 71 | db.execSQL("CREATE TABLE " + TABLE_NAME + " (" |
| 72 | + ShoppingList._ID + " INTEGER PRIMARY KEY," |
| 73 | + ShoppingList.ITEM + " TEXT," |
| 74 | + ShoppingList.STATUS + " INTEGER," |
| 75 | + ShoppingList.CREATED_DATE + " INTEGER," |
| 76 | + ShoppingList.MODIFIED_DATE + " INTEGER" |
| 77 | + ");"); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 82 | Log.w(TAG, "Upgrading database from version " + oldVersion + " to " |
| 83 | + newVersion + ", which will destroy all old data"); |
| 84 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); |
| 85 | onCreate(db); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private DatabaseHelper mOpenHelper; |
| 90 | |
| 91 | @Override |
| 92 | public boolean onCreate() { |
| 93 | mOpenHelper = new DatabaseHelper(getContext()); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 99 | String sortOrder) { |
| 100 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 101 | |
| 102 | switch (sUriMatcher.match(uri)) { |
| 103 | case ITEMS: |
| 104 | qb.setTables(TABLE_NAME); |
| 105 | qb.setProjectionMap(sProjectionMap); |
| 106 | break; |
| 107 | |
| 108 | case ITEM_ID: |
| 109 | qb.setTables(TABLE_NAME); |
| 110 | qb.setProjectionMap(sProjectionMap); |
| 111 | qb.appendWhere(ShoppingList._ID + "=" + uri.getPathSegments().get(1)); |
| 112 | break; |
| 113 | |
| 114 | default: |
| 115 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 116 | } |
| 117 | |
| 118 | // If no sort order is specified use the default |
| 119 | String orderBy; |
| 120 | if (TextUtils.isEmpty(sortOrder)) { |
| 121 | orderBy = ShoppingList.DEFAULT_SORT_ORDER; |
| 122 | } else { |
| 123 | orderBy = sortOrder; |
| 124 | } |
| 125 | |
| 126 | // Get the database and run the query |
| 127 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 128 | Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); |
| 129 | |
| 130 | // Tell the cursor what uri to watch, so it knows when its source data changes |
| 131 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 132 | return c; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String getType(Uri uri) { |
| 137 | switch (sUriMatcher.match(uri)) { |
| 138 | case ITEMS: |
| 139 | return ShoppingList.CONTENT_TYPE; |
| 140 | |
| 141 | case ITEM_ID: |
| 142 | return ShoppingList.CONTENT_ITEM_TYPE; |
| 143 | |
| 144 | default: |
| 145 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Uri insert(Uri uri, ContentValues initialValues) { |
| 151 | // Validate the requested uri |
| 152 | if (sUriMatcher.match(uri) != ITEMS) { |
| 153 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 154 | } |
| 155 | |
| 156 | ContentValues values; |
| 157 | if (initialValues != null) { |
| 158 | values = new ContentValues(initialValues); |
| 159 | } else { |
| 160 | values = new ContentValues(); |
| 161 | } |
| 162 | |
| 163 | Long now = Long.valueOf(System.currentTimeMillis()); |
| 164 | |
| 165 | // Make sure that the fields are all set |
| 166 | if (values.containsKey(ShoppingList.CREATED_DATE) == false) { |
| 167 | values.put(ShoppingList.CREATED_DATE, now); |
| 168 | } |
| 169 | |
| 170 | if (values.containsKey(ShoppingList.MODIFIED_DATE) == false) { |
| 171 | values.put(ShoppingList.MODIFIED_DATE, now); |
| 172 | } |
| 173 | |
| 174 | if (values.containsKey(ShoppingList.ITEM) == false) { |
| 175 | Resources r = Resources.getSystem(); |
| 176 | values.put(ShoppingList.ITEM, r.getString(android.R.string.untitled)); |
| 177 | } |
| 178 | |
| 179 | if (values.containsKey(ShoppingList.STATUS) == false) { |
| 180 | values.put(ShoppingList.STATUS, Trolly.ShoppingList.ON_LIST); |
| 181 | } |
| 182 | |
| 183 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 184 | long rowId = db.insert(TABLE_NAME, ShoppingList.ITEM, values); |
| 185 | if (rowId > 0) { |
| 186 | Uri itemUri = ContentUris.withAppendedId(ShoppingList.CONTENT_URI, rowId); |
| 187 | getContext().getContentResolver().notifyChange(itemUri, null); |
| 188 | return itemUri; |
| 189 | } |
| 190 | |
| 191 | throw new SQLException("Failed to insert row into " + uri); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int delete(Uri uri, String where, String[] whereArgs) { |
| 196 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 197 | int count; |
| 198 | switch (sUriMatcher.match(uri)) { |
| 199 | case ITEMS: |
| 200 | count = db.delete(TABLE_NAME, where, whereArgs); |
| 201 | break; |
| 202 | |
| 203 | case ITEM_ID: |
| 204 | String noteId = uri.getPathSegments().get(1); |
| 205 | count = db.delete(TABLE_NAME, ShoppingList._ID + "=" + noteId |
| 206 | + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs); |
| 207 | break; |
| 208 | |
| 209 | default: |
| 210 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 211 | } |
| 212 | |
| 213 | getContext().getContentResolver().notifyChange(uri, null); |
| 214 | return count; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public int update(Uri uri, ContentValues initialValues, String where, String[] whereArgs) { |
| 219 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 220 | int count; |
| 221 | |
| 222 | ContentValues values; |
| 223 | if (initialValues != null) { |
| 224 | values = new ContentValues(initialValues); |
| 225 | } else { |
| 226 | values = new ContentValues(); |
| 227 | } |
| 228 | |
| 229 | Long now = Long.valueOf(System.currentTimeMillis()); |
| 230 | |
| 231 | // Update the modified field |
| 232 | if (values.containsKey(ShoppingList.MODIFIED_DATE) == false) { |
| 233 | values.put(ShoppingList.MODIFIED_DATE, now); |
| 234 | } |
| 235 | |
| 236 | switch (sUriMatcher.match(uri)) { |
| 237 | case ITEMS: |
| 238 | count = db.update(TABLE_NAME, values, where, whereArgs); |
| 239 | break; |
| 240 | |
| 241 | case ITEM_ID: |
| 242 | String noteId = uri.getPathSegments().get(1); |
| 243 | count = db.update(TABLE_NAME, values, ShoppingList._ID + "=" + noteId |
| 244 | + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs); |
| 245 | break; |
| 246 | |
| 247 | default: |
| 248 | throw new IllegalArgumentException("Unknown URI " + uri); |
| 249 | } |
| 250 | |
| 251 | getContext().getContentResolver().notifyChange(uri, null); |
| 252 | return count; |
| 253 | } |
| 254 | |
| 255 | static { |
| 256 | sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 257 | sUriMatcher.addURI(Trolly.AUTHORITY, "shoppinglist", ITEMS); |
| 258 | sUriMatcher.addURI(Trolly.AUTHORITY, "shoppinglist/#", ITEM_ID); |
| 259 | |
| 260 | sProjectionMap = new HashMap<String, String>(); |
| 261 | sProjectionMap.put(ShoppingList._ID, ShoppingList._ID); |
| 262 | sProjectionMap.put(ShoppingList.ITEM, ShoppingList.ITEM); |
| 263 | sProjectionMap.put(ShoppingList.STATUS, ShoppingList.STATUS); |
| 264 | sProjectionMap.put(ShoppingList.CREATED_DATE, ShoppingList.CREATED_DATE); |
| 265 | sProjectionMap.put(ShoppingList.MODIFIED_DATE, ShoppingList.MODIFIED_DATE); |
| 266 | } |
| 267 | } |