Android Open Source - AquaBase Aquabase Content Provider






From Project

Back to project page AquaBase.

License

The source code is released under:

GNU General Public License

If you think the Android project AquaBase 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

/*
 *  This file is part of AquaBase.//from  www . ja  v  a2 s . co m
 *
 *  AquaBase 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.
 *
 *  AquaBase 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 AquaBase.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (c) 2014 Cdric Bosdonnat <cedric@bosdonnat.fr>
 */
package org.aquabase.data;

import java.text.MessageFormat;
import java.util.Vector;

import android.content.ContentProvider;
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 android.util.Pair;

public class AquabaseContentProvider extends ContentProvider {

    public static final String AUTHORITY = "org.aquabase.contentprovider"; //$NON-NLS-1$
    public static final String BASE_URI = "content://" + AUTHORITY + "/";  //$NON-NLS-1$//$NON-NLS-2$

    private static final int FISHES = 0;
    private static final int FISH = 1;
    private static final int CRUSTACEANS = 2;
    private static final int CRUSTACEAN = 3;

    private static final String FISH_PATH = FishTableHelper.TABLE_NAME;
    private static final String CRUSTACEAN_PATH = CrustaceanTableHelper.TABLE_NAME;

    private static UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sUriMatcher.addURI(AUTHORITY, FISH_PATH, FISHES);
        sUriMatcher.addURI(AUTHORITY, FISH_PATH + "/#", FISH); //$NON-NLS-1$
        sUriMatcher.addURI(AUTHORITY, CRUSTACEAN_PATH, CRUSTACEANS);
        sUriMatcher.addURI(AUTHORITY, CRUSTACEAN_PATH + "/#", CRUSTACEAN); //$NON-NLS-1$
    }

    private DatabaseHelper mDatabase;


    @Override
    public boolean onCreate() {
        mDatabase = new DatabaseHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {

        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        String tableName = null;

        switch (sUriMatcher.match(uri)) {
            case FISH:
                queryBuilder.appendWhere(DatabaseHelper.COLUMN_ID +
                        "=" + uri.getLastPathSegment()); //$NON-NLS-1$
                // fall through
            case FISHES:
                tableName = FishTableHelper.TABLE_NAME;
                break;
            case CRUSTACEAN:
                queryBuilder.appendWhere(DatabaseHelper.COLUMN_ID +
                        "=" + uri.getLastPathSegment()); //$NON-NLS-1$
                // fall through
            case CRUSTACEANS:
                tableName = CrustaceanTableHelper.TABLE_NAME;
                break;
            default:
                throw new IllegalArgumentException(MessageFormat.format(
                        "Unknown URI {0}", uri.toString())); //$NON-NLS-1$
        }

        queryBuilder.setTables(tableName);

        SQLiteDatabase db = mDatabase.getWritableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, selection,
                                           selectionArgs, null, null,
                                           sortOrder);

        // make sure that potential listeners are getting notified
        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = mDatabase.getWritableDatabase();
        long id = 0;
        String tableName = null;

        switch (sUriMatcher.match(uri)) {
            case FISHES:
                tableName = FishTableHelper.TABLE_NAME;
                break;
            case CRUSTACEANS:
                tableName = CrustaceanTableHelper.TABLE_NAME;
                break;
            default:
                throw new IllegalArgumentException(MessageFormat.format(
                        "Unknown URI {0}", uri.toString())); //$NON-NLS-1$
        }

        id = db.insert(tableName, null, values);

        getContext().getContentResolver().notifyChange(uri, null);

        return Uri.parse(BASE_URI + tableName + "/" + id); //$NON-NLS-1$
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        String tableName = null;

        switch (sUriMatcher.match(uri)) {
            case FISHES:
                tableName = FishTableHelper.TABLE_NAME;
                break;
            case CRUSTACEANS:
                tableName = CrustaceanTableHelper.TABLE_NAME;
                break;
            default:
                throw new IllegalArgumentException(MessageFormat.format(
                        "Unknown URI {0}", uri.toString())); //$NON-NLS-1$
        }

        SQLiteDatabase db = mDatabase.getWritableDatabase();
        return db.delete(tableName, selection, selectionArgs);
    }

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

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

    public static Vector<Pair<String, Integer>> getColumnsNames(Uri uri) {
        Vector<Pair<String, Integer>> columns = new Vector<Pair<String,Integer>>();

        switch (sUriMatcher.match(uri)) {
            case FISHES:
            case FISH:
                columns = FishTableHelper.getColumnsNames();
                break;
            case CRUSTACEANS:
            case CRUSTACEAN:
                columns = CrustaceanTableHelper.getColumnsNames();
                break;
            default:
                throw new IllegalArgumentException(MessageFormat.format(
                        "Unknown URI {0}", uri.toString())); //$NON-NLS-1$
        }
        return columns;
    }

}




Java Source Code List

org.aquabase.DetailsActivity.java
org.aquabase.DetailsFragment.java
org.aquabase.MainActivity.java
org.aquabase.NavigationDrawerFragment.java
org.aquabase.SpeciesListFragment.java
org.aquabase.data.AquabaseContentProvider.java
org.aquabase.data.CSVParser.java
org.aquabase.data.CrustaceanTableHelper.java
org.aquabase.data.CsvMapping.java
org.aquabase.data.DatabaseHelper.java
org.aquabase.data.FishTableHelper.java
org.aquabase.data.TableHelper.java