Back to project page HelloMundo.
The source code is released under:
GNU General Public License
If you think the Android project HelloMundo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * This source is part of the/*from w w w .j a va 2 s . co m*/ * _____ ___ ____ * __ / / _ \/ _ | / __/___ _______ _ * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / * /___/ * repository. * * Copyright (C) 2009-2014 Benoit 'BoD' Lubek (BoD@JRAF.org) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.jraf.android.hellomundo.provider; import java.util.ArrayList; import java.util.Arrays; import android.content.ContentProvider; import android.content.ContentProviderOperation; import android.content.ContentProviderResult; import android.content.ContentValues; import android.content.OperationApplicationException; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.provider.BaseColumns; import android.util.Log; import org.jraf.android.hellomundo.provider.appwidget.AppwidgetColumns; import org.jraf.android.hellomundo.provider.webcam.WebcamColumns; import org.jraf.android.latoureiffel.BuildConfig; public class HelloMundoProvider extends ContentProvider { private static final String TAG = HelloMundoProvider.class.getSimpleName(); private static final String TYPE_CURSOR_ITEM = "vnd.android.cursor.item/"; private static final String TYPE_CURSOR_DIR = "vnd.android.cursor.dir/"; public static final String AUTHORITY = "org.jraf.android.hellomundo.provider"; public static final String CONTENT_URI_BASE = "content://" + AUTHORITY; public static final String QUERY_NOTIFY = "QUERY_NOTIFY"; public static final String QUERY_GROUP_BY = "QUERY_GROUP_BY"; private static final int URI_TYPE_APPWIDGET = 0; private static final int URI_TYPE_APPWIDGET_ID = 1; private static final int URI_TYPE_WEBCAM = 2; private static final int URI_TYPE_WEBCAM_ID = 3; private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); static { URI_MATCHER.addURI(AUTHORITY, AppwidgetColumns.TABLE_NAME, URI_TYPE_APPWIDGET); URI_MATCHER.addURI(AUTHORITY, AppwidgetColumns.TABLE_NAME + "/#", URI_TYPE_APPWIDGET_ID); URI_MATCHER.addURI(AUTHORITY, WebcamColumns.TABLE_NAME, URI_TYPE_WEBCAM); URI_MATCHER.addURI(AUTHORITY, WebcamColumns.TABLE_NAME + "/#", URI_TYPE_WEBCAM_ID); } private HelloMundoSQLiteOpenHelper mHelloMundoSQLiteOpenHelper; @Override public boolean onCreate() { mHelloMundoSQLiteOpenHelper = HelloMundoSQLiteOpenHelper.newInstance(getContext()); return true; } @Override public String getType(Uri uri) { final int match = URI_MATCHER.match(uri); switch (match) { case URI_TYPE_APPWIDGET: return TYPE_CURSOR_DIR + AppwidgetColumns.TABLE_NAME; case URI_TYPE_APPWIDGET_ID: return TYPE_CURSOR_ITEM + AppwidgetColumns.TABLE_NAME; case URI_TYPE_WEBCAM: return TYPE_CURSOR_DIR + WebcamColumns.TABLE_NAME; case URI_TYPE_WEBCAM_ID: return TYPE_CURSOR_ITEM + WebcamColumns.TABLE_NAME; } return null; } @Override public Uri insert(Uri uri, ContentValues values) { if (BuildConfig.DEBUG) Log.d(TAG, "insert uri=" + uri + " values=" + values); final String table = uri.getLastPathSegment(); final long rowId = mHelloMundoSQLiteOpenHelper.getWritableDatabase().insert(table, null, values); String notify; if (rowId != -1 && ((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) { getContext().getContentResolver().notifyChange(uri, null); } return uri.buildUpon().appendEncodedPath(String.valueOf(rowId)).build(); } @Override public int bulkInsert(Uri uri, ContentValues[] values) { if (BuildConfig.DEBUG) Log.d(TAG, "bulkInsert uri=" + uri + " values.length=" + values.length); final String table = uri.getLastPathSegment(); final SQLiteDatabase db = mHelloMundoSQLiteOpenHelper.getWritableDatabase(); int res = 0; db.beginTransaction(); try { for (final ContentValues v : values) { final long id = db.insert(table, null, v); db.yieldIfContendedSafely(); if (id != -1) { res++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } String notify; if (res != 0 && ((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) { getContext().getContentResolver().notifyChange(uri, null); } return res; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { if (BuildConfig.DEBUG) Log.d(TAG, "update uri=" + uri + " values=" + values + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs)); final QueryParams queryParams = getQueryParams(uri, selection); final int res = mHelloMundoSQLiteOpenHelper.getWritableDatabase().update(queryParams.table, values, queryParams.selection, selectionArgs); String notify; if (res != 0 && ((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) { getContext().getContentResolver().notifyChange(uri, null); } return res; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { if (BuildConfig.DEBUG) Log.d(TAG, "delete uri=" + uri + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs)); final QueryParams queryParams = getQueryParams(uri, selection); final int res = mHelloMundoSQLiteOpenHelper.getWritableDatabase().delete(queryParams.table, queryParams.selection, selectionArgs); String notify; if (res != 0 && ((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) { getContext().getContentResolver().notifyChange(uri, null); } return res; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { final String groupBy = uri.getQueryParameter(QUERY_GROUP_BY); if (BuildConfig.DEBUG) Log.d(TAG, "query uri=" + uri + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs) + " sortOrder=" + sortOrder + " groupBy=" + groupBy); final QueryParams queryParams = getQueryParams(uri, selection); final Cursor res = mHelloMundoSQLiteOpenHelper.getReadableDatabase().query(queryParams.table, projection, queryParams.selection, selectionArgs, groupBy, null, sortOrder == null ? queryParams.orderBy : sortOrder); res.setNotificationUri(getContext().getContentResolver(), uri); return res; } @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { SQLiteDatabase db = mHelloMundoSQLiteOpenHelper.getWritableDatabase(); db.beginTransaction(); try { int numOperations = operations.size(); ContentProviderResult[] results = new ContentProviderResult[numOperations]; int i = 0; for (ContentProviderOperation operation : operations) { results[i] = operation.apply(this, results, i); if (operation.isYieldAllowed()) { db.yieldIfContendedSafely(); } i++; } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } } private static class QueryParams { public String table; public String selection; public String orderBy; } private QueryParams getQueryParams(Uri uri, String selection) { QueryParams res = new QueryParams(); String id = null; int matchedId = URI_MATCHER.match(uri); switch (matchedId) { case URI_TYPE_APPWIDGET: case URI_TYPE_APPWIDGET_ID: res.table = AppwidgetColumns.TABLE_NAME; res.orderBy = AppwidgetColumns.DEFAULT_ORDER; break; case URI_TYPE_WEBCAM: case URI_TYPE_WEBCAM_ID: res.table = WebcamColumns.TABLE_NAME; res.orderBy = WebcamColumns.DEFAULT_ORDER; break; default: throw new IllegalArgumentException("The uri '" + uri + "' is not supported by this ContentProvider"); } switch (matchedId) { case URI_TYPE_APPWIDGET_ID: case URI_TYPE_WEBCAM_ID: id = uri.getLastPathSegment(); } if (id != null) { if (selection != null) { res.selection = BaseColumns._ID + "=" + id + " and (" + selection + ")"; } else { res.selection = BaseColumns._ID + "=" + id; } } else { res.selection = selection; } return res; } public static Uri notify(Uri uri, boolean notify) { return uri.buildUpon().appendQueryParameter(QUERY_NOTIFY, String.valueOf(notify)).build(); } public static Uri groupBy(Uri uri, String groupBy) { return uri.buildUpon().appendQueryParameter(QUERY_GROUP_BY, groupBy).build(); } }