package com.weikou.beibeivideo.db;
|
|
import java.util.HashMap;
|
|
import android.content.ContentProvider;
|
import android.content.ContentUris;
|
import android.content.ContentValues;
|
import android.content.UriMatcher;
|
import android.database.Cursor;
|
import android.database.SQLException;
|
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteQueryBuilder;
|
import android.net.Uri;
|
import androidx.core.database.DatabaseUtilsCompat;
|
import android.util.Log;
|
|
public class WatchHistoryProvider extends ContentProvider {
|
|
// A projection map used to select columns from the database
|
private final HashMap<String, String> mNotesProjectionMap;
|
// Uri matcher to decode incoming URIs.
|
private final UriMatcher mUriMatcher;
|
|
// The incoming URI matches the main table URI pattern
|
private static final int WATCH_HISTORY = 1;
|
// The incoming URI matches the main table row ID URI pattern
|
private static final int WATCH_HISTORY_ID = 2;
|
|
// Handle to a new DatabaseHelper.
|
private BeibeiSQLiteOpenHelper mOpenHelper;
|
|
/**
|
* Global provider initialization.
|
*/
|
public WatchHistoryProvider() {
|
// Create and initialize URI matcher.
|
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
mUriMatcher.addURI(WatchHistoryTable.AUTHORITY, "watchhistories", WATCH_HISTORY);
|
mUriMatcher.addURI(WatchHistoryTable.AUTHORITY, "watchhistories/#", WATCH_HISTORY_ID);
|
|
// Create and initialize projection map for all columns. This is
|
// simply an identity mapping.
|
mNotesProjectionMap = new HashMap<>();
|
mNotesProjectionMap.put(WatchHistoryTable._ID, WatchHistoryTable._ID);
|
mNotesProjectionMap.put(WatchHistoryTable.VIDEO_ID, WatchHistoryTable.VIDEO_ID);
|
mNotesProjectionMap.put(WatchHistoryTable.VIDEO_DETAIL_ID, WatchHistoryTable.VIDEO_DETAIL_ID);
|
mNotesProjectionMap.put(WatchHistoryTable.VIDEO_THIRD_TYPE, WatchHistoryTable.VIDEO_THIRD_TYPE);
|
mNotesProjectionMap.put(WatchHistoryTable.VIDEO_DETAIL, WatchHistoryTable.VIDEO_DETAIL);
|
mNotesProjectionMap.put(WatchHistoryTable.WATCH_TIME, WatchHistoryTable.WATCH_TIME);
|
mNotesProjectionMap.put(WatchHistoryTable.UPDATE_TIME, WatchHistoryTable.UPDATE_TIME);
|
mNotesProjectionMap.put(WatchHistoryTable.CREATE_TIME, WatchHistoryTable.CREATE_TIME);
|
mNotesProjectionMap.put(WatchHistoryTable.VIDEO_RESOURCE, WatchHistoryTable.VIDEO_RESOURCE);
|
mNotesProjectionMap.put(WatchHistoryTable.VIDEO_RESOURCE_ID, WatchHistoryTable.VIDEO_RESOURCE_ID);
|
}
|
|
/**
|
* Perform provider creation.
|
*/
|
@Override
|
public boolean onCreate() {
|
mOpenHelper = new BeibeiSQLiteOpenHelper(getContext());
|
// Assumes that any failures will be reported by a thrown exception.
|
Log.i("mResult", "BeibeiSQLiteOpenHelper");
|
return true;
|
}
|
|
/**
|
* Return the MIME type for an known URI in the provider.
|
*/
|
@Override
|
public String getType(Uri uri) {
|
switch (mUriMatcher.match(uri)) {
|
case WATCH_HISTORY:
|
return WatchHistoryTable.CONTENT_TYPE;
|
case WATCH_HISTORY_ID:
|
return WatchHistoryTable.CONTENT_ITEM_TYPE;
|
default:
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
}
|
}
|
|
/**
|
* Handle incoming queries.
|
*/
|
@Override
|
public Cursor query(Uri uri, String[] projection, String selection,
|
String[] selectionArgs, String sortOrder) {
|
Cursor c;
|
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
|
switch (mUriMatcher.match(uri)) {
|
case WATCH_HISTORY: {
|
// Constructs a new query builder and sets its table name
|
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
qb.setTables(getTableName(uri));
|
// If the incoming URI is for main table.
|
qb.setProjectionMap(mNotesProjectionMap);
|
c = qb.query(db, projection, selection, selectionArgs,
|
null /* no group */, null /* no filter */, sortOrder);
|
}
|
break;
|
case WATCH_HISTORY_ID: {
|
// Constructs a new query builder and sets its table name
|
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
qb.setTables(getTableName(uri));
|
// The incoming URI is for a single row.
|
qb.setProjectionMap(mNotesProjectionMap);
|
qb.appendWhere(WatchHistoryTable._ID + "=?");
|
selectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs,
|
new String[] { uri.getLastPathSegment() });
|
c = qb.query(db, projection, selection, selectionArgs,
|
null /* no group */, null /* no filter */, sortOrder);
|
}
|
break;
|
default:
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
}
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
return c;
|
}
|
|
/**
|
* Handler inserting new data.
|
*/
|
@Override
|
public Uri insert(Uri uri, ContentValues initialValues) {
|
if (mUriMatcher.match(uri) != WATCH_HISTORY) {
|
// Can only insert into to main URI.
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
}
|
|
ContentValues values;
|
|
if (initialValues != null) {
|
values = new ContentValues(initialValues);
|
} else {
|
values = new ContentValues();
|
}
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
long rowId = db.insert(WatchHistoryTable.TABLE_NAME, null, values);
|
// If the insert succeeded, the row ID exists.
|
if (rowId > 0) {
|
Uri noteUri = ContentUris.withAppendedId(WatchHistoryTable.CONTENT_URI, rowId);
|
getContext().getContentResolver().notifyChange(noteUri, null);
|
return noteUri;
|
}
|
|
throw new SQLException("Failed to insert row into " + uri);
|
}
|
|
/**
|
* Handle deleting data.
|
*/
|
@Override
|
public int delete(Uri uri, String where, String[] whereArgs) {
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
String finalWhere;
|
|
int count;
|
|
switch (mUriMatcher.match(uri)) {
|
case WATCH_HISTORY:
|
// If URI is main table, delete uses incoming where clause and args.
|
count = db.delete(WatchHistoryTable.TABLE_NAME, where, whereArgs);
|
break;
|
|
// If the incoming URI matches a single note ID, does the delete based on the
|
// incoming data, but modifies the where clause to restrict it to the
|
// particular note ID.
|
case WATCH_HISTORY_ID:
|
// If URI is for a particular row ID, delete is based on incoming
|
// data but modified to restrict to the given ID.
|
finalWhere = DatabaseUtilsCompat.concatenateWhere(
|
WatchHistoryTable._ID + " = " + uri.getLastPathSegment(), where);
|
count = db.delete(WatchHistoryTable.TABLE_NAME, finalWhere, whereArgs);
|
break;
|
|
default:
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
}
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
return count;
|
}
|
|
/**
|
* Handle updating data.
|
*/
|
@Override
|
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
int count;
|
String finalWhere;
|
|
switch (mUriMatcher.match(uri)) {
|
case WATCH_HISTORY:
|
// If URI is main table, update uses incoming where clause and args.
|
count = db.update(WatchHistoryTable.TABLE_NAME, values, where, whereArgs);
|
break;
|
|
case WATCH_HISTORY_ID:
|
// If URI is for a particular row ID, update is based on incoming
|
// data but modified to restrict to the given ID.
|
finalWhere = DatabaseUtilsCompat.concatenateWhere(
|
WatchHistoryTable._ID + " = " + uri.getLastPathSegment(), where);
|
count = db.update(WatchHistoryTable.TABLE_NAME, values, finalWhere, whereArgs);
|
break;
|
|
default:
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
}
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
return count;
|
}
|
|
private String getTableName(Uri uri) {
|
switch (mUriMatcher.match(uri)) {
|
case WATCH_HISTORY:
|
case WATCH_HISTORY_ID:
|
return WatchHistoryTable.TABLE_NAME;
|
default:
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
}
|
}
|
|
}
|