/* * Copyright 2013 i'm SpA * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.imwatch.nfclottery.data.provider; import android.content.*; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteStatement; import android.net.Uri; import android.provider.BaseColumns; import android.util.Log; import it.imwatch.nfclottery.data.provider.NFCMLContent.Geeks; import java.util.ArrayList; /** * This class was generated by the ContentProviderCodeGenerator project made by Foxykeep * <p/> * (More information available https://github.com/foxykeep/ContentProviderCodeGenerator) */ public final class NFCMLProvider extends ContentProvider { private static final String LOG_TAG = NFCMLProvider.class.getSimpleName(); private static final boolean ACTIVATE_ALL_LOGS = false; public static final String DATABASE_NAME = "NFCMLProvider.db"; public static final String AUTHORITY = "it.imwatch.nfclottery.provider.NFCMLProvider"; static { Uri.parse("content://" + AUTHORITY + "/integrityCheck"); } // Version 1 : Creation of the database // Version 2 : Added autoincrement for _id and timestamp for winner mark public static final int DATABASE_VERSION = 2; private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); private enum UriType { GEEKS(Geeks.TABLE_NAME, Geeks.TABLE_NAME, Geeks.TYPE_ELEM_TYPE), GEEKS_ID(Geeks.TABLE_NAME + "/#", Geeks.TABLE_NAME, Geeks.TYPE_DIR_TYPE); private String mTableName; private String mType; UriType(String matchPath, String tableName, String type) { mTableName = tableName; mType = type; sUriMatcher.addURI(AUTHORITY, matchPath, ordinal()); } String getTableName() { return mTableName; } String getType() { return mType; } } static { // Ensures UriType is initialized UriType.values(); } private static UriType matchUri(Uri uri) { int match = sUriMatcher.match(uri); if (match < 0) { throw new IllegalArgumentException("Unknown URI " + uri); } return UriType.class.getEnumConstants()[match]; } private SQLiteDatabase mDatabase; @SuppressWarnings("deprecation") public synchronized SQLiteDatabase getDatabase(Context context) { // Always return the cached database, if we've got one if (mDatabase == null || !mDatabase.isOpen()) { DatabaseHelper helper = new DatabaseHelper(context, DATABASE_NAME); mDatabase = helper.getWritableDatabase(); if (mDatabase != null) { mDatabase.setLockingEnabled(true); } } return mDatabase; } private class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context, String name) { super(context, name, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.d(LOG_TAG, "Creating NFCMLProvider database"); // Create all tables here; each class has its own method if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "Geeks | createTable start"); } Geeks.createTable(db); if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "Geeks | createTable end"); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Upgrade all tables here; each class has its own method if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "Geeks | upgradeTable start"); } Geeks.upgradeTable(db, oldVersion, newVersion); if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "Geeks | upgradeTable end"); } } @Override public void onOpen(SQLiteDatabase db) { } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { UriType uriType = matchUri(uri); Context context = getContext(); // Pick the correct database for this operation SQLiteDatabase db = getDatabase(context); String id; if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "delete: uri=" + uri + ", match is " + uriType.name()); } int result = -1; switch (uriType) { case GEEKS_ID: id = uri.getPathSegments().get(1); result = db.delete(uriType.getTableName(), whereWithId(id, selection), selectionArgs); break; case GEEKS: result = db.delete(uriType.getTableName(), selection, selectionArgs); break; } getContext().getContentResolver().notifyChange(uri, null); return result; } @Override public String getType(Uri uri) { return matchUri(uri).getType(); } @Override public Uri insert(Uri uri, ContentValues values) { UriType uriType = matchUri(uri); Context context = getContext(); // Pick the correct database for this operation SQLiteDatabase db = getDatabase(context); long id; if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "insert: uri=" + uri + ", match is " + uriType.name()); } Uri resultUri; switch (uriType) { case GEEKS: id = db.insert(uriType.getTableName(), "foo", values); resultUri = id == -1 ? null : ContentUris.withAppendedId(uri, id); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } // Notify with the base uri, not the new uri (nobody is watching a new // record) getContext().getContentResolver().notifyChange(uri, null); return resultUri; } @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { SQLiteDatabase db = getDatabase(getContext()); db.beginTransaction(); try { int numOperations = operations.size(); ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); db.yieldIfContendedSafely(); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } } @Override public int bulkInsert(Uri uri, ContentValues[] values) { UriType uriType = matchUri(uri); Context context = getContext(); // Pick the correct database for this operation SQLiteDatabase db = getDatabase(context); if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "bulkInsert: uri=" + uri + ", match is " + uriType.name()); } int numberInserted = 0; SQLiteStatement insertStmt; db.beginTransaction(); try { switch (uriType) { case GEEKS: insertStmt = db.compileStatement(Geeks.getBulkInsertString()); for (ContentValues value : values) { Geeks.bindValuesInBulkInsert(insertStmt, value); insertStmt.execute(); insertStmt.clearBindings(); } insertStmt.close(); db.setTransactionSuccessful(); numberInserted = values.length; if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "bulkInsert: uri=" + uri + " | nb inserts : " + numberInserted); } break; default: throw new IllegalArgumentException("Unknown URI " + uri); } } finally { db.endTransaction(); } // Notify with the base uri, not the new uri (nobody is watching a new // record) context.getContentResolver().notifyChange(uri, null); return numberInserted; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor c = null; Uri notificationUri = NFCMLContent.CONTENT_URI; UriType uriType = matchUri(uri); Context context = getContext(); // Pick the correct database for this operation SQLiteDatabase db = getDatabase(context); String id; if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "query: uri=" + uri + ", match is " + uriType.name()); } switch (uriType) { case GEEKS_ID: id = uri.getPathSegments().get(1); c = db.query(uriType.getTableName(), projection, whereWithId(id, selection), selectionArgs, null, null, sortOrder); break; case GEEKS: c = db.query(uriType.getTableName(), projection, selection, selectionArgs, null, null, sortOrder); break; } if ((c != null) && !isTemporary()) { c.setNotificationUri(getContext().getContentResolver(), notificationUri); } return c; } private String whereWithId(String id, String selection) { StringBuilder sb = new StringBuilder(256); sb.append(BaseColumns._ID); sb.append(" = "); sb.append(id); if (selection != null) { sb.append(" AND ("); sb.append(selection); sb.append(')'); } return sb.toString(); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { UriType uriType = matchUri(uri); Context context = getContext(); // Pick the correct database for this operation SQLiteDatabase db = getDatabase(context); if (ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "update: uri=" + uri + ", match is " + uriType.name()); } int result = -1; switch (uriType) { case GEEKS_ID: String id = uri.getPathSegments().get(1); result = db.update(uriType.getTableName(), values, whereWithId(id, selection), selectionArgs); break; case GEEKS: result = db.update(uriType.getTableName(), values, selection, selectionArgs); break; } getContext().getContentResolver().notifyChange(uri, null); return result; } @Override public boolean onCreate() { return true; } }