diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/loaders/RecentlyWatchedLoader.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/loaders/RecentlyWatchedLoader.java index ed65ee9b0d..5e486616a9 100644 --- a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/loaders/RecentlyWatchedLoader.java +++ b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/loaders/RecentlyWatchedLoader.java @@ -43,11 +43,11 @@ public List 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; } diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideContract.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideContract.java index 3da1bf4eee..ec0d0f960c 100644 --- a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideContract.java +++ b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideContract.java @@ -492,7 +492,7 @@ interface MoviesColumns { String LAST_UPDATED = "movies_last_updated"; } - interface ActivityColumns { + interface EpisodeHistoryColumns { String TIMESTAMP = "activity_time"; @@ -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); @@ -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 { @@ -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() { } } diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideDatabase.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideDatabase.java index 2bc5d777d0..931ba425de 100644 --- a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideDatabase.java +++ b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideDatabase.java @@ -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; @@ -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"; @@ -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. @@ -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 { @@ -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) { @@ -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 @@ -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 @@ -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}. */ @@ -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); } } diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideProvider.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideProvider.java index 8ea9495df1..2ceb496392 100644 --- a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideProvider.java +++ b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/provider/SeriesGuideProvider.java @@ -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; @@ -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; @@ -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 + "/" @@ -213,7 +219,7 @@ public void shutdown() { public boolean onCreate() { Context context = getContext(); - sUriMatcher = buildUriMatcher(context); + sUriMatcher = buildUriMatcher(); mDbHelper = new SeriesGuideDatabase(context); @@ -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: @@ -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: { @@ -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); diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/ActivityTools.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/ActivityTools.java deleted file mode 100644 index bb125e4d37..0000000000 --- a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/ActivityTools.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2014 Uwe Trottmann - * - * 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. - */ - -package com.battlelancer.seriesguide.util; - -import android.content.ContentValues; -import android.content.Context; -import android.text.format.DateUtils; -import com.battlelancer.seriesguide.provider.SeriesGuideContract; -import timber.log.Timber; - -/** - * Helper methods for dealing with the {@link com.battlelancer.seriesguide.provider.SeriesGuideDatabase.Tables#ACTIVITY} - * table. - */ -public class ActivityTools { - - private static final long HISTORY_THRESHOLD = 30 * DateUtils.DAY_IN_MILLIS; - - /** - * Adds an activity entry for the given episode with the current time as timestamp. If an entry - * already exists it is replaced. - * - *

Also cleans up old entries. - */ - public static void addActivity(Context context, int episodeTvdbId, int showTvdbId) { - long timeMonthAgo = System.currentTimeMillis() - HISTORY_THRESHOLD; - - // delete all entries older than 30 days - int deleted = context.getContentResolver() - .delete(SeriesGuideContract.Activity.CONTENT_URI, - SeriesGuideContract.Activity.TIMESTAMP + "<" + timeMonthAgo, null); - Timber.d("addActivity: removed " + deleted + " outdated activities"); - - // add new entry - ContentValues values = new ContentValues(); - values.put(SeriesGuideContract.Activity.EPISODE_TVDB_ID, episodeTvdbId); - values.put(SeriesGuideContract.Activity.SHOW_TVDB_ID, showTvdbId); - long currentTime = System.currentTimeMillis(); - values.put(SeriesGuideContract.Activity.TIMESTAMP, currentTime); - - context.getContentResolver().insert(SeriesGuideContract.Activity.CONTENT_URI, values); - Timber.d("addActivity: episode: " + episodeTvdbId + " timestamp: " + currentTime); - } - - /** - * Tries to remove any activity with the given episode TheTVDB id. - */ - public static void removeActivity(Context context, int episodeTvdbId) { - int deleted = context.getContentResolver().delete(SeriesGuideContract.Activity.CONTENT_URI, - SeriesGuideContract.Activity.EPISODE_TVDB_ID + "=" + episodeTvdbId, null); - Timber.d("removeActivity: deleted " + deleted + " activity entries"); - } -} diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/EpisodeTools.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/EpisodeTools.java index fe0f475d86..68ec0ef3ce 100644 --- a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/EpisodeTools.java +++ b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/EpisodeTools.java @@ -560,11 +560,11 @@ protected int getLastWatchedEpisodeTvdbId() { protected void onPostExecute() { if (isWatched(mEpisodeFlag)) { // create activity entry for watched episode - ActivityTools.addActivity(mContext, mEpisodeTvdbId, mShowTvdbId); + HistoryTools.addEpisodeActivity(mContext, mEpisodeTvdbId, mShowTvdbId); } else if (isUnwatched(mEpisodeFlag)) { // remove any previous activity entries for this episode // use case: user accidentally toggled watched flag - ActivityTools.removeActivity(mContext, mEpisodeTvdbId); + HistoryTools.removeEpisodeActivity(mContext, mEpisodeTvdbId); } } diff --git a/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/HistoryTools.java b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/HistoryTools.java new file mode 100644 index 0000000000..0a74642231 --- /dev/null +++ b/SeriesGuide/src/main/java/com/battlelancer/seriesguide/util/HistoryTools.java @@ -0,0 +1,106 @@ +/* + * Copyright 2014 Uwe Trottmann + * + * 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. + */ + +package com.battlelancer.seriesguide.util; + +import android.content.ContentValues; +import android.content.Context; +import android.net.Uri; +import android.text.format.DateUtils; +import com.battlelancer.seriesguide.provider.SeriesGuideContract.EpisodeHistory; +import com.battlelancer.seriesguide.provider.SeriesGuideContract.MovieHistory; +import timber.log.Timber; + +/** + * Helper methods for dealing with the {@link com.battlelancer.seriesguide.provider.SeriesGuideDatabase.Tables#EPISODE_HISTORY} + * and {@link com.battlelancer.seriesguide.provider.SeriesGuideDatabase.Tables#MOVIE_HISTORY} + * tables. + */ +public class HistoryTools { + + private static final long HISTORY_THRESHOLD = 30 * DateUtils.DAY_IN_MILLIS; + + /** + * Adds an activity entry for the given episode with the current time as timestamp. If an entry + * already exists it is replaced. + * + *

Also cleans up old entries. + */ + public static void addEpisodeActivity(Context context, int episodeTvdbId, int showTvdbId) { + deleteOutdatedActivity(context, EpisodeHistory.CONTENT_URI, EpisodeHistory.TIMESTAMP); + + // add new entry + ContentValues values = new ContentValues(); + values.put(EpisodeHistory.EPISODE_TVDB_ID, episodeTvdbId); + values.put(EpisodeHistory.SHOW_TVDB_ID, showTvdbId); + long currentTime = System.currentTimeMillis(); + values.put(EpisodeHistory.TIMESTAMP, currentTime); + + context.getContentResolver() + .insert(EpisodeHistory.CONTENT_URI, values); + Timber.d("addEpisodeActivity: episode: %s timestamp: %s", episodeTvdbId, currentTime); + } + + /** + * Tries to remove any activity with the given episode TheTVDB id. + */ + public static void removeEpisodeActivity(Context context, int episodeTvdbId) { + int deleted = context.getContentResolver() + .delete(EpisodeHistory.CONTENT_URI, + EpisodeHistory.EPISODE_TVDB_ID + "=" + episodeTvdbId, + null); + Timber.d("removeEpisodeActivity: deleted %s activity entries", deleted); + } + + /** + * Adds an activity entry for the given movie with the current time as timestamp. If an entry + * already exists it is replaced. + * + *

Also cleans up old entries. + */ + public static void addMovieActivity(Context context, int movieTmdbId) { + deleteOutdatedActivity(context, MovieHistory.CONTENT_URI, MovieHistory.TIMESTAMP); + + // add new entry + ContentValues values = new ContentValues(); + values.put(MovieHistory.MOVIE_TMDB_ID, movieTmdbId); + long currentTime = System.currentTimeMillis(); + values.put(MovieHistory.TIMESTAMP, currentTime); + + context.getContentResolver().insert(MovieHistory.CONTENT_URI, values); + Timber.d("addMovieActivity: movie: %s timestamp: %s", movieTmdbId, currentTime); + } + + /** + * Tries to remove any activity with the given movie TMDb id. + */ + public static void removeMovieActivity(Context context, int movieTmdbId) { + int deleted = context.getContentResolver() + .delete(MovieHistory.CONTENT_URI, + MovieHistory.MOVIE_TMDB_ID + "=" + movieTmdbId, + null); + Timber.d("removeMovieActivity: deleted %s activity entries", deleted); + } + + private static void deleteOutdatedActivity(Context context, Uri uri, String timestampColumn) { + long timeMonthAgo = System.currentTimeMillis() - HISTORY_THRESHOLD; + + // delete all entries older than 30 days + int deleted = context.getContentResolver() + .delete(uri, timestampColumn + "<" + timeMonthAgo, null); + Timber.d("removeOldActivity: removed %s outdated activities from %s", deleted, uri); + } +}