Android Open Source - cannonball-android Poem Provider






From Project

Back to project page cannonball-android.

License

The source code is released under:

Apache License

If you think the Android project cannonball-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright (C) 2014 Twitter Inc and other contributors.
 *//www .j  av a  2  s  .  com
 * 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 io.fabric.samples.cannonball.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.crashlytics.android.Crashlytics;

public class PoemProvider extends ContentProvider {
    private DbHelper dbHelper;

    static final int POEMS = 1;
    static final int POEMS_ID = 2;

    private static final UriMatcher sUriMatcher;
    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(PoemContract.AUTHORITY, "poems", POEMS);
        sUriMatcher.addURI(PoemContract.AUTHORITY, "poems/#", POEMS_ID);
    }

    @Override
    public boolean onCreate() {
        dbHelper = new DbHelper(getContext());
        return (dbHelper != null);
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        final SQLiteDatabase db = dbHelper.getWritableDatabase();
        final long id = db.insertWithOnConflict(PoemContract.TABLE, null, values,
                SQLiteDatabase.CONFLICT_IGNORE);

        if (id > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
            return ContentUris.withAppendedId(uri, id);
        } else {
            Crashlytics.log("PoemProvider: not able to insert a poem in Database");
            return null;
        }
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        final SQLiteDatabase db = dbHelper.getWritableDatabase();
        final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(PoemContract.TABLE);

        switch (sUriMatcher.match(uri)) {
            case POEMS_ID:
                queryBuilder.appendWhere(PoemContract.Columns.ID + "=" + uri.getLastPathSegment());
                break;
            default:
                break;
        }

        final Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null,
                null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        final SQLiteDatabase db = dbHelper.getWritableDatabase();
        final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(PoemContract.TABLE);

        switch (sUriMatcher.match(uri)) {
            case POEMS:
                return 0;
            case POEMS_ID:
                final String[] args = { uri.getLastPathSegment() };
                final int count
                        = db.delete(PoemContract.TABLE, PoemContract.Columns.ID + " = ?", args);
                if (count != 0)
                    getContext().getContentResolver().notifyChange(uri, null);
                return count;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}




Java Source Code List

io.fabric.samples.cannonball.AppService.java
io.fabric.samples.cannonball.App.java
io.fabric.samples.cannonball.ApplicationTest.java
io.fabric.samples.cannonball.SessionRecorder.java
io.fabric.samples.cannonball.activity.AboutActivity.java
io.fabric.samples.cannonball.activity.InitialActivity.java
io.fabric.samples.cannonball.activity.LoginActivity.java
io.fabric.samples.cannonball.activity.PoemBuilderActivity.java
io.fabric.samples.cannonball.activity.PoemHistoryActivity.java
io.fabric.samples.cannonball.activity.PoemPopularActivity.java
io.fabric.samples.cannonball.activity.ThemeChooserActivity.java
io.fabric.samples.cannonball.db.DbHelper.java
io.fabric.samples.cannonball.db.PoemContract.java
io.fabric.samples.cannonball.db.PoemProvider.java
io.fabric.samples.cannonball.model.ThemeDrawables.java
io.fabric.samples.cannonball.model.Theme.java
io.fabric.samples.cannonball.model.WordBank.java
io.fabric.samples.cannonball.view.AvenirTextView.java
io.fabric.samples.cannonball.view.CannonballDigitsAuthButton.java
io.fabric.samples.cannonball.view.CannonballTwitterLoginButton.java
io.fabric.samples.cannonball.view.CountdownView.java
io.fabric.samples.cannonball.view.FlowLayout.java
io.fabric.samples.cannonball.view.ImageAdapter.java
io.fabric.samples.cannonball.view.ImageLoader.java
io.fabric.samples.cannonball.view.ThemeAdapter.java