use of android.content.ContentValues in project SeriesGuide by UweTrottmann.
the class DBUtils method updateLatestEpisode.
/**
* Update next episode field and unwatched episode count for the given show. If no show id is
* passed, will update next episodes for all shows.
*
* @return If only one show was passed, the TVDb id of the new next episode. Otherwise -1.
*/
public static long updateLatestEpisode(Context context, Integer showTvdbIdToUpdate) {
// get a list of shows and their last watched episodes
Cursor shows;
try {
shows = context.getContentResolver().query(Shows.CONTENT_URI_WITH_LAST_EPISODE, LastWatchedEpisodeQuery.PROJECTION, showTvdbIdToUpdate != null ? Qualified.SHOWS_ID + "=" + showTvdbIdToUpdate : null, null, null);
} catch (SQLiteException e) {
shows = null;
Timber.e(e, "updateLatestEpisode: show query failed.");
postDatabaseError(e);
}
if (shows == null) {
// abort completely on query failure
Timber.e("Failed to update next episode values");
return -1;
}
final List<String[]> showsLastEpisodes = new ArrayList<>();
while (shows.moveToNext()) {
showsLastEpisodes.add(new String[] { // 0
shows.getString(LastWatchedEpisodeQuery.SHOW_TVDB_ID), // 1
shows.getString(LastWatchedEpisodeQuery.LAST_EPISODE_TVDB_ID), // 2
shows.getString(LastWatchedEpisodeQuery.LAST_EPISODE_SEASON), // 3
shows.getString(LastWatchedEpisodeQuery.LAST_EPISODE_NUMBER), shows.getString(LastWatchedEpisodeQuery.LAST_EPISODE_FIRST_RELEASE_MS) });
}
shows.close();
// pre-build next episode selection
final boolean isNoReleasedEpisodes = DisplaySettings.isNoReleasedEpisodes(context);
final String nextEpisodeSelection = buildNextEpisodeSelection(DisplaySettings.isHidingSpecials(context), isNoReleasedEpisodes);
// build updated next episode values for each show
int nextEpisodeTvdbId = -1;
final ContentValues newShowValues = new ContentValues();
final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
final String currentTime = String.valueOf(TimeTools.getCurrentTime(context));
final boolean displayExactDate = DisplaySettings.isDisplayExactDate(context);
DisplaySettings.preventSpoilers(context);
for (String[] show : showsLastEpisodes) {
// STEP 1: get last watched episode details
final String showTvdbId = show[0];
final String lastEpisodeTvdbId = show[1];
String season = show[2];
String number = show[3];
String releaseTime = show[4];
if (TextUtils.isEmpty(lastEpisodeTvdbId) || season == null || number == null || releaseTime == null) {
// by default: no watched episodes, include all starting with special 0
season = "-1";
number = "-1";
releaseTime = String.valueOf(Long.MIN_VALUE);
}
// STEP 2: get episode released closest afterwards; or at the same time,
// but with a higher number
final String[] selectionArgs;
if (isNoReleasedEpisodes) {
// restrict to episodes with future release date
selectionArgs = new String[] { releaseTime, number, season, releaseTime, currentTime };
} else {
// restrict to episodes with any valid air date
selectionArgs = new String[] { releaseTime, number, season, releaseTime };
}
Cursor next;
try {
next = context.getContentResolver().query(Episodes.buildEpisodesOfShowUri(showTvdbId), NextEpisodesQuery.PROJECTION, nextEpisodeSelection, selectionArgs, NextEpisodesQuery.SORTORDER);
} catch (SQLiteException e) {
next = null;
Timber.e(e, "updateLatestEpisode: next episode query failed.");
postDatabaseError(e);
}
if (next == null) {
// abort completely on query failure
Timber.e("Failed to update next episode values");
return -1;
}
// STEP 3: build updated next episode values
if (next.moveToFirst()) {
final String nextEpisodeString;
int seasonNumber = next.getInt(NextEpisodesQuery.SEASON);
int episodeNumber = next.getInt(NextEpisodesQuery.NUMBER);
if (DisplaySettings.preventSpoilers(context)) {
// just the number, like '0x12'
nextEpisodeString = TextTools.getEpisodeNumber(context, seasonNumber, episodeNumber);
} else {
// next episode text, like '0x12 Episode Name'
nextEpisodeString = TextTools.getNextEpisodeString(context, seasonNumber, episodeNumber, next.getString(NextEpisodesQuery.TITLE));
}
// next release date text, e.g. "in 15 mins (Fri)"
long releaseTimeNext = next.getLong(NextEpisodesQuery.FIRST_RELEASE_MS);
Date actualRelease = TimeTools.applyUserOffset(context, releaseTimeNext);
String dateTime = displayExactDate ? TimeTools.formatToLocalDateShort(context, actualRelease) : TimeTools.formatToLocalRelativeTime(context, actualRelease);
final String nextReleaseDateString = context.getString(R.string.release_date_and_day, dateTime, TimeTools.formatToLocalDay(actualRelease));
nextEpisodeTvdbId = next.getInt(NextEpisodesQuery.ID);
newShowValues.put(Shows.NEXTEPISODE, nextEpisodeTvdbId);
newShowValues.put(Shows.NEXTAIRDATEMS, releaseTimeNext);
newShowValues.put(Shows.NEXTTEXT, nextEpisodeString);
newShowValues.put(Shows.NEXTAIRDATETEXT, nextReleaseDateString);
} else {
// no next episode, set empty values
nextEpisodeTvdbId = 0;
newShowValues.put(Shows.NEXTEPISODE, "");
newShowValues.put(Shows.NEXTAIRDATEMS, UNKNOWN_NEXT_RELEASE_DATE);
newShowValues.put(Shows.NEXTTEXT, "");
newShowValues.put(Shows.NEXTAIRDATETEXT, "");
}
next.close();
// STEP 4: get remaining episodes count
int unwatchedEpisodesCount = getUnwatchedEpisodesOfShow(context, showTvdbId);
newShowValues.put(Shows.UNWATCHED_COUNT, unwatchedEpisodesCount);
// update the show with the new next episode values
batch.add(ContentProviderOperation.newUpdate(Shows.buildShowUri(showTvdbId)).withValues(newShowValues).build());
newShowValues.clear();
}
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "Failed to update next episode values");
return -1;
}
return nextEpisodeTvdbId;
}
use of android.content.ContentValues in project SeriesGuide by UweTrottmann.
the class ShowTools method storeIsHidden.
/**
* Saves new hidden flag to the local database and, if signed in, up into the cloud as well.
*/
public void storeIsHidden(int showTvdbId, boolean isHidden) {
if (HexagonSettings.isEnabled(app)) {
if (Utils.isNotConnected(app, true)) {
return;
}
// send to cloud
Show show = new Show();
show.setTvdbId(showTvdbId);
show.setIsHidden(isHidden);
uploadShowAsync(show);
}
// save to local database
ContentValues values = new ContentValues();
values.put(SeriesGuideContract.Shows.HIDDEN, isHidden);
app.getContentResolver().update(SeriesGuideContract.Shows.buildShowUri(showTvdbId), values, null, null);
// also notify filter URI used by search
app.getContentResolver().notifyChange(SeriesGuideContract.Shows.CONTENT_URI_FILTER, null);
Toast.makeText(app, app.getString(isHidden ? R.string.hidden : R.string.unhidden), Toast.LENGTH_SHORT).show();
}
use of android.content.ContentValues in project SeriesGuide by UweTrottmann.
the class MovieTools method buildBasicMovieContentValuesWithId.
/**
* Extracts basic properties, except in_watchlist and in_collection from trakt. Also includes
* the TMDb id and watched state as value.
*/
private static ContentValues buildBasicMovieContentValuesWithId(MovieDetails details) {
ContentValues values = buildBasicMovieContentValues(details);
values.put(SeriesGuideContract.Movies.TMDB_ID, details.tmdbMovie().id);
return values;
}
use of android.content.ContentValues in project SeriesGuide by UweTrottmann.
the class MovieTools method buildMovieContentValues.
private static ContentValues buildMovieContentValues(MovieDetails details) {
ContentValues values = buildBasicMovieContentValuesWithId(details);
values.put(SeriesGuideContract.Movies.IN_COLLECTION, DBUtils.convertBooleanToInt(details.inCollection));
values.put(SeriesGuideContract.Movies.IN_WATCHLIST, DBUtils.convertBooleanToInt(details.inWatchlist));
return values;
}
use of android.content.ContentValues in project SeriesGuide by UweTrottmann.
the class MovieTools method addMovie.
private boolean addMovie(int movieTmdbId, Lists listToAddTo) {
// get movie info
MovieDetails details = getMovieDetails(movieTmdbId);
if (details.tmdbMovie() == null) {
// abort if minimal data failed to load
return false;
}
// build values
ContentValues values = buildBasicMovieContentValuesWithId(details);
// set flags
values.put(SeriesGuideContract.Movies.IN_COLLECTION, DBUtils.convertBooleanToInt(listToAddTo == Lists.COLLECTION));
values.put(SeriesGuideContract.Movies.IN_WATCHLIST, DBUtils.convertBooleanToInt(listToAddTo == Lists.WATCHLIST));
// add to database
context.getContentResolver().insert(SeriesGuideContract.Movies.CONTENT_URI, values);
// ensure ratings and watched flags are downloaded on next sync
TraktSettings.resetMoviesLastActivity(context);
return true;
}
Aggregations