Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tools for recording movie activity history #1074

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public List<NowAdapter.NowItem> loadInBackground() {

// get activity of the last 24 hours with the latest one first
Cursor query = getContext().getContentResolver()
.query(SeriesGuideContract.Activity.CONTENT_URI,
new String[] { SeriesGuideContract.Activity.TIMESTAMP,
SeriesGuideContract.Activity.EPISODE_TVDB_ID },
SeriesGuideContract.Activity.TIMESTAMP + ">" + timeDayAgo, null,
SeriesGuideContract.Activity.TIMESTAMP + " DESC");
.query(SeriesGuideContract.EpisodeHistory.CONTENT_URI,
new String[] { SeriesGuideContract.EpisodeHistory.TIMESTAMP,
SeriesGuideContract.EpisodeHistory.EPISODE_TVDB_ID },
SeriesGuideContract.EpisodeHistory.TIMESTAMP + ">" + timeDayAgo, null,
SeriesGuideContract.EpisodeHistory.TIMESTAMP + " DESC");
if (query == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ interface MoviesColumns {
String LAST_UPDATED = "movies_last_updated";
}

interface ActivityColumns {
interface EpisodeHistoryColumns {

String TIMESTAMP = "activity_time";

Expand All @@ -501,6 +501,13 @@ interface ActivityColumns {
String SHOW_TVDB_ID = "activity_show";
}

interface MovieHistoryColumns {

String TIMESTAMP = "activity_time";

String MOVIE_TMDB_ID = "activity_movie";
}

private static final Uri BASE_CONTENT_URI = Uri.parse("content://"
+ SeriesGuideApplication.CONTENT_AUTHORITY);

Expand Down Expand Up @@ -538,7 +545,9 @@ interface ActivityColumns {

public static final String PATH_MOVIES = "movies";

public static final String PATH_ACTIVITY = "activity";
public static final String PATH_EPISODE_HISTORY = "episode-history";

public static final String PATH_MOVIE_HISTORY = "movie-history";

public static class Shows implements ShowsColumns, BaseColumns {

Expand Down Expand Up @@ -891,22 +900,40 @@ public static String getId(Uri uri) {
}
}

public static class Activity implements ActivityColumns, BaseColumns {
public static class EpisodeHistory implements EpisodeHistoryColumns, BaseColumns {

public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_ACTIVITY)
.appendPath(PATH_EPISODE_HISTORY)
.build();

/**
* Use if multiple items get returned
*/
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.seriesguide.activity";
public static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.seriesguide.history.episodes";

public static Uri buildActivityUri(String episodeTvdbId) {
return CONTENT_URI.buildUpon().appendPath(episodeTvdbId).build();
}
}

public static class MovieHistory implements MovieHistoryColumns, BaseColumns {

public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_MOVIE_HISTORY)
.build();

/**
* Use if multiple items get returned
*/
public static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.seriesguide.history.movies";

public static Uri buildActivityUri(String movieTmdbId) {
return CONTENT_URI.buildUpon().appendPath(movieTmdbId).build();
}
}

private SeriesGuideContract() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodeHistoryColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodeSearch;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodeSearchColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.Episodes;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodesColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.ListItems;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.ListItemsColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.Lists;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.ListsColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.MovieHistoryColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.Movies;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.MoviesColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.Seasons;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.SeasonsColumns;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.Shows;
import com.battlelancer.seriesguide.provider.SeriesGuideContract.ShowsColumns;
Expand All @@ -42,11 +47,6 @@
import org.joda.time.LocalTime;
import timber.log.Timber;

import static com.battlelancer.seriesguide.provider.SeriesGuideContract.ActivityColumns;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.ListItems;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Movies;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Seasons;

public class SeriesGuideDatabase extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "seriesdatabase";
Expand Down Expand Up @@ -136,7 +136,12 @@ public class SeriesGuideDatabase extends SQLiteOpenHelper {
*/
public static final int DBVER_36_ORDERABLE_LISTS = 36;

public static final int DATABASE_VERSION = DBVER_36_ORDERABLE_LISTS;
/**
* Add activity table to store recently watched movies.
*/
public static final int DBVER_37_MOVIE_HISTORY = 37;

public static final int DATABASE_VERSION = DBVER_37_MOVIE_HISTORY;

/**
* Qualifies column names by prefixing their {@link Tables} name.
Expand Down Expand Up @@ -206,7 +211,9 @@ public interface Tables {

String MOVIES = "movies";

String ACTIVITY = "activity";
String EPISODE_HISTORY = "activity";

String MOVIE_HISTORY = "activity_movies";
}

private interface Selections {
Expand Down Expand Up @@ -510,13 +517,23 @@ interface References {

+ ");";

private static final String CREATE_ACTIVITY_TABLE = "CREATE TABLE " + Tables.ACTIVITY
private static final String CREATE_EPISODE_HISTORY_TABLE = "CREATE TABLE "
+ Tables.EPISODE_HISTORY
+ " ("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ActivityColumns.EPISODE_TVDB_ID + " TEXT NOT NULL,"
+ ActivityColumns.SHOW_TVDB_ID + " TEXT NOT NULL,"
+ ActivityColumns.TIMESTAMP + " INTEGER NOT NULL,"
+ "UNIQUE (" + ActivityColumns.EPISODE_TVDB_ID + ") ON CONFLICT REPLACE"
+ EpisodeHistoryColumns.EPISODE_TVDB_ID + " TEXT NOT NULL,"
+ EpisodeHistoryColumns.SHOW_TVDB_ID + " TEXT NOT NULL,"
+ EpisodeHistoryColumns.TIMESTAMP + " INTEGER NOT NULL,"
+ "UNIQUE (" + EpisodeHistoryColumns.EPISODE_TVDB_ID + ") ON CONFLICT REPLACE"
+ ");";

private static final String CREATE_MOVIE_HISTORY_TABLE = "CREATE TABLE "
+ Tables.MOVIE_HISTORY
+ " ("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ MovieHistoryColumns.MOVIE_TMDB_ID + " TEXT NOT NULL,"
+ MovieHistoryColumns.TIMESTAMP + " INTEGER NOT NULL,"
+ "UNIQUE (" + MovieHistoryColumns.MOVIE_TMDB_ID + ") ON CONFLICT REPLACE"
+ ");";

public SeriesGuideDatabase(Context context) {
Expand All @@ -539,7 +556,9 @@ public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_MOVIES_TABLE);

db.execSQL(CREATE_ACTIVITY_TABLE);
db.execSQL(CREATE_EPISODE_HISTORY_TABLE);

db.execSQL(CREATE_MOVIE_HISTORY_TABLE);
}

@Override
Expand Down Expand Up @@ -595,7 +614,9 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
upgradeToThirtyFive(db);
case DBVER_35_ACTIVITY_TABLE:
upgradeToThirtySix(db);
version = DBVER_36_ORDERABLE_LISTS;
case DBVER_36_ORDERABLE_LISTS:
upgradeToThirtySeven(db);
version = DBVER_37_MOVIE_HISTORY;
}

// drop all tables if version is not right
Expand All @@ -616,13 +637,23 @@ private void onResetDatabase(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + Tables.LISTS);
db.execSQL("DROP TABLE IF EXISTS " + Tables.LIST_ITEMS);
db.execSQL("DROP TABLE IF EXISTS " + Tables.MOVIES);
db.execSQL("DROP TABLE IF EXISTS " + Tables.ACTIVITY);
db.execSQL("DROP TABLE IF EXISTS " + Tables.EPISODE_HISTORY);
db.execSQL("DROP TABLE IF EXISTS " + Tables.MOVIE_HISTORY);

db.execSQL("DROP TABLE IF EXISTS " + Tables.EPISODES_SEARCH);

onCreate(db);
}

/**
* See {@link #DBVER_37_MOVIE_HISTORY}.
*/
private static void upgradeToThirtySeven(SQLiteDatabase db) {
if (!isTableExisting(db, Tables.MOVIE_HISTORY)) {
db.execSQL(CREATE_MOVIE_HISTORY_TABLE);
}
}

/**
* See {@link #DBVER_36_ORDERABLE_LISTS}.
*/
Expand All @@ -637,8 +668,8 @@ private static void upgradeToThirtySix(SQLiteDatabase db) {
* See {@link #DBVER_35_ACTIVITY_TABLE}.
*/
private static void upgradeToThirtyFive(SQLiteDatabase db) {
if (!isTableExisting(db, Tables.ACTIVITY)) {
db.execSQL(CREATE_ACTIVITY_TABLE);
if (!isTableExisting(db, Tables.EPISODE_HISTORY)) {
db.execSQL(CREATE_EPISODE_HISTORY_TABLE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
import java.util.Arrays;
import timber.log.Timber;

import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Activity;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodeHistory;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodeSearch;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Episodes;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.ListItems;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Lists;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.MovieHistory;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Movies;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Seasons;
import static com.battlelancer.seriesguide.provider.SeriesGuideContract.Shows;
Expand Down Expand Up @@ -105,17 +106,19 @@ public class SeriesGuideProvider extends ContentProvider {

private static final int MOVIES_ID = 701;

private static final int ACTIVITY = 800;
private static final int EPISODE_HISTORY = 800;

private static final int SEARCH_SUGGEST = 900;
private static final int MOVIE_HISTORY = 900;

private static final int RENEW_FTSTABLE = 1000;
private static final int SEARCH_SUGGEST = 1000;

private static final int RENEW_FTSTABLE = 1100;

/**
* Build and return a {@link UriMatcher} that catches all {@link Uri} variations supported by
* this {@link ContentProvider}.
*/
private static UriMatcher buildUriMatcher(Context context) {
private static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = SeriesGuideApplication.CONTENT_AUTHORITY;

Expand Down Expand Up @@ -173,8 +176,11 @@ private static UriMatcher buildUriMatcher(Context context) {
matcher.addURI(authority, SeriesGuideContract.PATH_MOVIES, MOVIES);
matcher.addURI(authority, SeriesGuideContract.PATH_MOVIES + "/*", MOVIES_ID);

// Activity
matcher.addURI(authority, SeriesGuideContract.PATH_ACTIVITY, ACTIVITY);
// Episode history
matcher.addURI(authority, SeriesGuideContract.PATH_EPISODE_HISTORY, EPISODE_HISTORY);

// Movie history
matcher.addURI(authority, SeriesGuideContract.PATH_MOVIE_HISTORY, MOVIE_HISTORY);

// Search
matcher.addURI(authority, SeriesGuideContract.PATH_EPISODESEARCH + "/"
Expand Down Expand Up @@ -213,7 +219,7 @@ public void shutdown() {
public boolean onCreate() {
Context context = getContext();

sUriMatcher = buildUriMatcher(context);
sUriMatcher = buildUriMatcher();

mDbHelper = new SeriesGuideDatabase(context);

Expand Down Expand Up @@ -321,8 +327,10 @@ public String getType(Uri uri) {
return Movies.CONTENT_TYPE;
case MOVIES_ID:
return Movies.CONTENT_ITEM_TYPE;
case ACTIVITY:
return Activity.CONTENT_TYPE;
case EPISODE_HISTORY:
return EpisodeHistory.CONTENT_TYPE;
case MOVIE_HISTORY:
return MovieHistory.CONTENT_TYPE;
case SEARCH_SUGGEST:
return SearchManager.SUGGEST_MIME_TYPE;
case RENEW_FTSTABLE:
Expand Down Expand Up @@ -440,12 +448,22 @@ private Uri insertInTransaction(SQLiteDatabase db, Uri uri, ContentValues values
notifyUri = Movies.buildMovieUri(values.getAsInteger(Movies.TMDB_ID));
break;
}
case ACTIVITY: {
long id = db.insert(Tables.ACTIVITY, null, values);
case EPISODE_HISTORY: {
long id = db.insert(Tables.EPISODE_HISTORY, null, values);
if (id < 0) {
break;
}
notifyUri = EpisodeHistory.buildActivityUri(
values.getAsString(EpisodeHistory.EPISODE_TVDB_ID));
break;
}
case MOVIE_HISTORY: {
long id = db.insert(Tables.MOVIE_HISTORY, null, values);
if (id < 0) {
break;
}
notifyUri = Activity.buildActivityUri(values.getAsString(Activity.EPISODE_TVDB_ID));
notifyUri = MovieHistory.buildActivityUri(
values.getAsString(MovieHistory.MOVIE_TMDB_ID));
break;
}
default: {
Expand Down Expand Up @@ -676,8 +694,11 @@ private static SelectionBuilder buildSelection(Uri uri, int match) {
final String movieId = Movies.getId(uri);
return builder.table(Tables.MOVIES).where(Movies.TMDB_ID + "=?", movieId);
}
case ACTIVITY: {
return builder.table(Tables.ACTIVITY);
case EPISODE_HISTORY: {
return builder.table(Tables.EPISODE_HISTORY);
}
case MOVIE_HISTORY: {
return builder.table(Tables.MOVIE_HISTORY);
}
default: {
throw new UnsupportedOperationException("Unknown uri: " + uri);
Expand Down
Loading