Java tutorial
/* 50AH-code ========= 50 Android Hacks (http://manning.com/sessa/) book source code Copyright (c) 2012 Manning Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /******************************************************************************* * Copyright (c) 2012 Manning * See the file license.txt for copying permission. ******************************************************************************/ package com.manning.androidhacks.hack043.provider; import java.util.HashMap; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; public class NoBatchNumbersContentProvider extends ContentProvider { public static final String TABLE_NAME = "no_batch_numbers"; public static final String AUTHORITY = NoBatchNumbersContentProvider.class.getCanonicalName(); public static final String COLUMN_ID = "_id"; public static final String COLUMN_TEXT = "text"; private static final int ITEM = 1; private static final int ITEM_ID = 2; public static final String DEFAULT_SORT_ORDER = "_id ASC"; private static HashMap<String, String> projectionMap; private static final UriMatcher sUriMatcher; public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.androidhacks.nobatchnumber"; public static final String CONTENT_TYPE_ID = "vnd.android.cursor.item/vnd.androidhacks.nobatchnumber"; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME); private DatabaseHelper dbHelper; static { sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(AUTHORITY, TABLE_NAME, ITEM); sUriMatcher.addURI(AUTHORITY, TABLE_NAME + "/#", ITEM_ID); projectionMap = new HashMap<String, String>(); projectionMap.put(COLUMN_ID, COLUMN_ID); projectionMap.put(COLUMN_TEXT, COLUMN_TEXT); } @Override public boolean onCreate() { dbHelper = new DatabaseHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); switch (sUriMatcher.match(uri)) { case ITEM: qb.setTables(TABLE_NAME); qb.setProjectionMap(projectionMap); break; case ITEM_ID: qb.setTables(TABLE_NAME); qb.setProjectionMap(projectionMap); qb.appendWhere(COLUMN_ID + "=" + uri.getPathSegments().get(1)); break; default: throw new RuntimeException("Unknown URI"); } SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @Override public String getType(Uri uri) { switch (sUriMatcher.match(uri)) { case ITEM: return CONTENT_TYPE; case ITEM_ID: return CONTENT_TYPE_ID; default: throw new IllegalArgumentException("Unknown URI " + uri); } } @Override public Uri insert(Uri uri, ContentValues initialValues) { ContentValues values; if (initialValues != null) { values = new ContentValues(initialValues); } else { values = new ContentValues(); } String table = null; String nullableCol = null; switch (sUriMatcher.match(uri)) { case ITEM: table = TABLE_NAME; break; default: new RuntimeException("Invalid URI for inserting: " + uri); } SQLiteDatabase db = dbHelper.getWritableDatabase(); long rowId = db.insert(table, nullableCol, values); if (rowId > 0) { Uri noteUri = ContentUris.withAppendedId(uri, rowId); getContext().getContentResolver().notifyChange(noteUri, null); return noteUri; } throw new SQLException("Failed to insert row into " + uri); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count; switch (sUriMatcher.match(uri)) { case ITEM: count = db.delete(TABLE_NAME, selection, selectionArgs); break; case ITEM_ID: String id = uri.getPathSegments().get(1); count = db.delete(TABLE_NAME, COLUMN_ID + "=" + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : ""), selectionArgs); break; default: throw new RuntimeException("Unkown URI: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count = 0; switch (sUriMatcher.match(uri)) { case ITEM: count = db.update(TABLE_NAME, values, selection, selectionArgs); break; case ITEM_ID: count = db.update(TABLE_NAME, values, COLUMN_ID + "=" + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : ""), selectionArgs); break; default: throw new RuntimeException("Unknown URI " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } }