Back to project page GuildViewerApp2.
The source code is released under:
Apache License
If you think the Android project GuildViewerApp2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.skywomantechnology.app.guildviewer.data; /*from w w w. j a v a 2 s . c o m*/ /* * Guild Viewer is an Android app that allows users to view news feeds and news feed details * on a mobile device and while not logged into the game servers. * * Copyright 2014 Sky Woman Technology LLC * * 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. */ 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.net.Uri; import android.support.annotation.NonNull; import com.skywomantechnology.app.guildviewer.data.GuildViewerContract.GuildEntry; import com.skywomantechnology.app.guildviewer.data.GuildViewerContract.ItemEntry; import com.skywomantechnology.app.guildviewer.data.GuildViewerContract.MemberEntry; import com.skywomantechnology.app.guildviewer.data.GuildViewerContract.NewsEntry; /** * Content Provider for Guild Viewer's data as obtained * from Blizzard's Open APi * */ public class NewsProvider extends ContentProvider { private static final UriMatcher sUriMatcher = buildUriMatcher(); private GuildViewerDbHelper mOpenHelper; private static final int NEWS = 100; private static final int NEWS_ID = 110; private static final int NEWS_REGION_REALM_GUILD = 200; private static final int ITEM = 300; private static final int ITEM_ID = 310; private static final int MEMBER = 400; private static final int MEMBER_ID = 410; private static final int MEMBER_NAME = 420; private static final int GUILD = 500; /** * On create we want to make a new database helper * * @return true */ @Override public boolean onCreate() { mOpenHelper = new GuildViewerDbHelper(getContext()); return true; } /** * Processes queries on the database depending on the URI * * @param uri to insert * @param projection columns to return * @param selection where clause * @param selectionArgs where args * @param sortOrder order by * @return Cursor with the data requested */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor retCursor; switch (sUriMatcher.match(uri)) { // returns all of the news case NEWS: { retCursor = mOpenHelper.getReadableDatabase().query( NewsEntry.TABLE_NAME, projection, selection, // selection selectionArgs, // selection args null, // group by null, // having sortOrder); break; } case NEWS_ID: { retCursor = mOpenHelper.getReadableDatabase().query( NewsEntry.TABLE_NAME, projection, NewsEntry._ID + "=" + ContentUris.parseId(uri),// selection null, // selection args null, // group by null, // having sortOrder); break; } case NEWS_REGION_REALM_GUILD: { // set up the where criteria String sRegionRealmGuild = NewsEntry.COLUMN_REGION + "=" + "\'" + NewsEntry.getRegionFromUri(uri) + "\'" + " AND " + NewsEntry.COLUMN_REALM + "=" + "\'" + NewsEntry.getRealmFromUri(uri) + "\'" + " AND " + NewsEntry.COLUMN_GUILD + "=" + "\'" + NewsEntry.getGuildFromUri(uri) + "\'"; retCursor = mOpenHelper.getReadableDatabase().query( NewsEntry.TABLE_NAME, projection, sRegionRealmGuild,// selection null, // selection args null, // group by null, // having sortOrder); break; } case ITEM: { retCursor = mOpenHelper.getReadableDatabase().query( ItemEntry.TABLE_NAME, projection, selection, // selection selectionArgs, // selection args null, // group by null, // having sortOrder); break; } case ITEM_ID: { retCursor = mOpenHelper.getReadableDatabase().query( ItemEntry.TABLE_NAME, projection, ItemEntry.COLUMN_ITEM_ID + "=" + ItemEntry.getItemIdFromUri(uri),// selection null, // selection args null, // group by null, // having sortOrder); break; } case MEMBER: { retCursor = mOpenHelper.getReadableDatabase().query( MemberEntry.TABLE_NAME, projection, selection, // selection selectionArgs, // selection args null, // group by null, // having sortOrder); break; } case MEMBER_ID: { retCursor = mOpenHelper.getReadableDatabase().query( MemberEntry.TABLE_NAME, projection, MemberEntry._ID + "=" + MemberEntry.getMemberIdFromUri(uri),// selection null, // selection args null, // group by null, // having sortOrder); break; } case MEMBER_NAME: { retCursor = mOpenHelper.getReadableDatabase().query( MemberEntry.TABLE_NAME, projection, MemberEntry.COLUMN_NAME + "=" + "\'" + MemberEntry.getMemberNameFromUri(uri) + "\'",// selection null, // selection args null, // group by null, // having sortOrder); break; } case GUILD: { retCursor = mOpenHelper.getReadableDatabase().query( GuildEntry.TABLE_NAME, projection, selection, // selection selectionArgs, // selection args null, // group by null, // having sortOrder); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } retCursor.setNotificationUri(getContext().getContentResolver(), uri); return retCursor; } /** * Returns the type of data that the query will return * * @param uri to match * @return String with the content type */ @Override public String getType(Uri uri) { final int match = sUriMatcher.match(uri); switch (match) { case NEWS: { return NewsEntry.CONTENT_TYPE; } case NEWS_ID: { return NewsEntry.CONTENT_ITEM_TYPE; } case NEWS_REGION_REALM_GUILD: { return NewsEntry.CONTENT_TYPE; } case ITEM: { return ItemEntry.CONTENT_TYPE; } case ITEM_ID: { return ItemEntry.CONTENT_ITEM_TYPE; } case MEMBER: { return MemberEntry.CONTENT_TYPE; } case MEMBER_ID: case MEMBER_NAME:{ return MemberEntry.CONTENT_ITEM_TYPE; } case GUILD:{ return GuildEntry.CONTENT_TYPE; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } } /** * inserts new data as represented in the ContentValues object int the * appropriate database * * @param uri to insert * @param contentValues to insert * @return the Uri containing information as it exists in database now */ @Override public Uri insert(Uri uri, ContentValues contentValues) { Uri returnUri; final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case NEWS: case NEWS_REGION_REALM_GUILD: { long _id = db.insert(NewsEntry.TABLE_NAME, null, contentValues); if (_id > 0) { returnUri = NewsEntry.buildNewsListUriWithId(_id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } break; } case ITEM: { long _id = db.insert(ItemEntry.TABLE_NAME, null, contentValues); if (_id > 0) { returnUri = ItemEntry.buildNewsItemUriWithItemId( contentValues.getAsString(ItemEntry.COLUMN_ITEM_ID)); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } break; } case MEMBER: { long _id = db.insert(MemberEntry.TABLE_NAME, null, contentValues); if (_id > 0) { returnUri = MemberEntry.buildGuildMemberUriWithId(_id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } break; } case GUILD: { long _id = db.insert(GuildEntry.TABLE_NAME, null, contentValues); if (_id > 0) { returnUri = GuildEntry.buildGuildUriWithId(_id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return returnUri; } /** * delete the data from the database using the given parameters * * @param uri to match * @param selection where clause * @param selectionArgs where arge * @return int number of items deleted */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int rowsDeleted; final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case NEWS: { rowsDeleted = db.delete(NewsEntry.TABLE_NAME, selection, selectionArgs); break; } case NEWS_REGION_REALM_GUILD: { // set up the where criteria String sRegionRealmGuild = NewsEntry.COLUMN_REGION + "=" + "\'" + NewsEntry.getRegionFromUri(uri) + "\'" + " AND " + NewsEntry.COLUMN_REALM + "=" + "\'" + NewsEntry.getRealmFromUri(uri) + "\'" + " AND " + NewsEntry.COLUMN_GUILD + "=" + "\'" + NewsEntry.getGuildFromUri(uri) + "\'"; rowsDeleted = db.delete(NewsEntry.TABLE_NAME, sRegionRealmGuild, selectionArgs); break; } case ITEM: { rowsDeleted = db.delete(ItemEntry.TABLE_NAME, selection, selectionArgs); break; } case MEMBER: { rowsDeleted = db.delete(MemberEntry.TABLE_NAME, selection, selectionArgs); break; } case GUILD: { rowsDeleted = db.delete(GuildEntry.TABLE_NAME, selection, selectionArgs); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (selection == null || rowsDeleted != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsDeleted; } /** * updates the database information with the given information * This app never really needs updates with how it is designed right now. * * @param uri to update * @param values to use for update * @param selection where * @param selectionArgs where args * @return int rows updated */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int rowsUpdated; final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case NEWS: case NEWS_REGION_REALM_GUILD: { rowsUpdated = db.update(NewsEntry.TABLE_NAME, values, selection, selectionArgs); break; } case GUILD: { rowsUpdated = db.update(GuildEntry.TABLE_NAME, values, selection, selectionArgs); break; } case MEMBER: case MEMBER_ID: case MEMBER_NAME: { rowsUpdated = db.update(MemberEntry.TABLE_NAME, values, selection, selectionArgs); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (rowsUpdated != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsUpdated; } /** * insert array of ContentValues for the given uri * * @param uri to insert * @param values to insert * @return int number of rows inserted */ @Override public int bulkInsert(Uri uri, @NonNull ContentValues[] values) { int returnCount = 0; final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case NEWS: db.beginTransaction(); try { for (ContentValues value : values) { long _id = db.insert(NewsEntry.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } break; case MEMBER: db.beginTransaction(); try { for (ContentValues value : values) { long _id = db.insert(MemberEntry.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } break; default: return super.bulkInsert(uri, values); } getContext().getContentResolver().notifyChange(uri, null); return returnCount; } /** * set up the valid Uri's for this content provider * * @return UriMatcher configured appropriately */ private static UriMatcher buildUriMatcher() { // All paths added to the UriMatcher have a corresponding code to return when a match is // found. The code passed into the constructor represents the code to return for the root // URI. It's common to use NO_MATCH as the code for this case. final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final String authority = GuildViewerContract.CONTENT_AUTHORITY; // For each type of URI you want to add, create a corresponding code. matcher.addURI(authority, GuildViewerContract.NEWS_PATH, NEWS); matcher.addURI(authority, GuildViewerContract.NEWS_PATH + "/#", NEWS_ID); matcher.addURI(authority, GuildViewerContract.NEWS_PATH + "/*/*/*", NEWS_REGION_REALM_GUILD); matcher.addURI(authority, GuildViewerContract.ITEM_PATH, ITEM); matcher.addURI(authority, GuildViewerContract.ITEM_PATH + "/#", ITEM_ID); matcher.addURI(authority, GuildViewerContract.GUILD_PATH, GUILD); matcher.addURI(authority, GuildViewerContract.GUILD_PATH + "/*", MEMBER); matcher.addURI(authority, GuildViewerContract.GUILD_PATH + "/*/#", MEMBER_ID); matcher.addURI(authority, GuildViewerContract.GUILD_PATH + "/*/*", MEMBER_NAME); return matcher; } }