Android Open Source - Netball Netball Content Provider






From Project

Back to project page Netball.

License

The source code is released under:

GNU General Public License

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

package com.prisch.content;
/*from   w ww .jav  a 2s . c  o m*/
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 com.prisch.model.*;

import java.util.HashMap;
import java.util.Map;

public class NetballContentProvider extends ContentProvider {

    // ===== Internal Constants =====

    private static final String AUTHORITY = "com.prisch.NetballTracker";

    private static final int CODE_PLAYERS = 1;
    private static final int CODE_GAMES = 2;
    private static final int CODE_TEAMS = 3;
    private static final int CODE_ACTIVE_TEAM = 301;
    private static final int CODE_RECORDS = 4;
    private static final int CODE_OPPONENT_RECORDS = 5;

    private static final String PATH_PLAYERS = "players";
    private static final String PATH_GAMES = "games";
    private static final String PATH_TEAMS = "teams";
    private static final String PATH_ACTIVE_TEAM = PATH_TEAMS + "/active";
    private static final String PATH_RECORDS = "records";
    private static final String PATH_OPPONENT_RECORDS = "opponentRecords";

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(AUTHORITY, PATH_PLAYERS, CODE_PLAYERS);
        URI_MATCHER.addURI(AUTHORITY, PATH_GAMES, CODE_GAMES);
        URI_MATCHER.addURI(AUTHORITY, PATH_TEAMS, CODE_TEAMS);
        URI_MATCHER.addURI(AUTHORITY, PATH_ACTIVE_TEAM, CODE_ACTIVE_TEAM);
        URI_MATCHER.addURI(AUTHORITY, PATH_RECORDS, CODE_RECORDS);
        URI_MATCHER.addURI(AUTHORITY, PATH_OPPONENT_RECORDS, CODE_OPPONENT_RECORDS);
    }

    private static final Map<Integer, String> URI_QUERY_TABLE_MAP = new HashMap<Integer, String>();
    static {
        URI_QUERY_TABLE_MAP.put(CODE_PLAYERS, Player.TABLE);
        URI_QUERY_TABLE_MAP.put(CODE_GAMES, Game.TABLE);
        URI_QUERY_TABLE_MAP.put(CODE_TEAMS, TeamMember.TABLE_JOIN_PLAYERS);
        URI_QUERY_TABLE_MAP.put(CODE_RECORDS, Record.TABLE_JOIN_TEAM_JOIN_PLAYER);
        URI_QUERY_TABLE_MAP.put(CODE_OPPONENT_RECORDS, OpponentRecord.TABLE);
    }

    private static final Map<Integer, String> URI_WRITE_TABLE_MAP = new HashMap<Integer, String>();
    static {
        URI_WRITE_TABLE_MAP.put(CODE_PLAYERS, Player.TABLE);
        URI_WRITE_TABLE_MAP.put(CODE_GAMES, Game.TABLE);
        URI_WRITE_TABLE_MAP.put(CODE_TEAMS, TeamMember.TABLE);
        URI_WRITE_TABLE_MAP.put(CODE_RECORDS, Record.TABLE);
        URI_WRITE_TABLE_MAP.put(CODE_OPPONENT_RECORDS, OpponentRecord.TABLE);
    }

    private static final Map<Integer, String> URI_PATH_MAP = new HashMap<Integer, String>();
    static {
        URI_PATH_MAP.put(CODE_PLAYERS, PATH_PLAYERS);
        URI_PATH_MAP.put(CODE_GAMES, PATH_GAMES);
        URI_PATH_MAP.put(CODE_TEAMS, PATH_TEAMS);
        URI_PATH_MAP.put(CODE_RECORDS, PATH_RECORDS);
        URI_PATH_MAP.put(CODE_OPPONENT_RECORDS, PATH_OPPONENT_RECORDS);
    }

    // ===== Exposed Constants =====

    public static final Uri URI_PLAYERS = Uri.parse("content://" + AUTHORITY + "/" + PATH_PLAYERS);
    public static final Uri URI_GAMES = Uri.parse("content://" + AUTHORITY + "/" + PATH_GAMES);
    public static final Uri URI_TEAMS = Uri.parse("content://" + AUTHORITY + "/" + PATH_TEAMS);
    public static final Uri URI_ACTIVE_TEAM = Uri.parse("content://" + AUTHORITY + "/" + PATH_ACTIVE_TEAM);
    public static final Uri URI_RECORDS = Uri.parse("content://" + AUTHORITY + "/" + PATH_RECORDS);
    public static final Uri URI_OPPONENT_RECORDS = Uri.parse("content://" + AUTHORITY + "/" + PATH_OPPONENT_RECORDS);

    // ===== Fields =====

    private DatabaseHelper databaseHelper;

    // ===== Inherited Operations =====

    @Override
    public boolean onCreate() {
        databaseHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        Cursor cursor;
        int uriType = URI_MATCHER.match(uri);
        switch (uriType) {
            case CODE_ACTIVE_TEAM:
                cursor = getActiveTeam();
                break;
            default:
                if (!URI_QUERY_TABLE_MAP.containsKey(uriType)) {
                    throw new IllegalArgumentException("Unknown URI: " + uri);
                }
                queryBuilder.setTables(URI_QUERY_TABLE_MAP.get(uriType));

                cursor = queryBuilder.query(databaseHelper.getWritableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
                break;
        }

        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }

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

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase database = databaseHelper.getWritableDatabase();

        int uriType = URI_MATCHER.match(uri);
        if (!URI_WRITE_TABLE_MAP.containsKey(uriType)) {
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        long id = database.insert(URI_WRITE_TABLE_MAP.get(uriType), null, values);

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

        String path = URI_PATH_MAP.get(uriType);
        return Uri.parse(path + "/" + id);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase database = databaseHelper.getWritableDatabase();

        int uriType = URI_MATCHER.match(uri);
        if (!URI_WRITE_TABLE_MAP.containsKey(uriType)) {
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        int count = database.delete(URI_WRITE_TABLE_MAP.get(uriType), selection, selectionArgs);

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase database = databaseHelper.getWritableDatabase();

        int uriType = URI_MATCHER.match(uri);
        if (!URI_WRITE_TABLE_MAP.containsKey(uriType)) {
            throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        int count = database.update(URI_WRITE_TABLE_MAP.get(uriType), values, selection, selectionArgs);

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    // ===== Query Operations =====

    private Cursor getActiveTeam() {
        final String QUERY = String.format("SELECT %s, %s FROM %s", TeamMember.prefixTable("*"), Player.prefixTable(Player.NAME), TeamMember.TABLE_JOIN_PLAYERS)
                             + String.format(" WHERE %s IN", TeamMember.GAME_ID)
                             + String.format(" (SELECT %s FROM %s WHERE %s = 1)", Game.ID, Game.TABLE, Game.ACTIVE)
                             + String.format(" AND %s = 1", TeamMember.prefixTable(TeamMember.ACTIVE));

        return databaseHelper.getWritableDatabase().rawQuery(QUERY, null);
    }
}




Java Source Code List

com.prisch.activities.ActionsActivity.java
com.prisch.activities.BaseTeamActivity.java
com.prisch.activities.DashboardActivity.java
com.prisch.activities.GameStatsActivity.java
com.prisch.activities.GamesActivity.java
com.prisch.activities.PlayersActivity.java
com.prisch.activities.PositionsActivity.java
com.prisch.activities.SubstitutionActivity.java
com.prisch.activities.TeamActivity.java
com.prisch.content.DatabaseHelper.java
com.prisch.content.NetballContentProvider.java
com.prisch.controls.ActionButton.java
com.prisch.fragments.PositionStatsFragment.java
com.prisch.loaders.GameStatsLoader.java
com.prisch.model.Action.java
com.prisch.model.GameStats.java
com.prisch.model.Game.java
com.prisch.model.OpponentAction.java
com.prisch.model.OpponentRecord.java
com.prisch.model.PlayerStats.java
com.prisch.model.Player.java
com.prisch.model.Position.java
com.prisch.model.Record.java
com.prisch.model.TeamMember.java
com.prisch.repositories.GameRepository.java
com.prisch.repositories.OpponentRecordRepository.java
com.prisch.repositories.PlayerRepository.java
com.prisch.repositories.RecordRepository.java
com.prisch.repositories.StatsRepository.java
com.prisch.repositories.TeamMemberRepository.java
com.prisch.util.DateUtils.java
com.prisch.views.GameAdapter.java
com.prisch.views.GameStatsAdapter.java
com.prisch.views.PlayerStatsAdapter.java
com.prisch.views.PlayerStatsListItem.java
com.prisch.views.TeamAdapter.java