use of android.content.OperationApplicationException in project SeriesGuide by UweTrottmann.
the class TraktTools method downloadEpisodeRatings.
/**
* Downloads trakt episode ratings and applies the latest ones to the database.
*
* <p> To apply all ratings, set {@link TraktSettings#KEY_LAST_EPISODES_RATED_AT} to 0.
*/
public UpdateResult downloadEpisodeRatings(@Nullable DateTime ratedAt) {
if (ratedAt == null) {
Timber.e("downloadEpisodeRatings: null rated_at");
return UpdateResult.INCOMPLETE;
}
long lastRatedAt = TraktSettings.getLastEpisodesRatedAt(context);
if (!ratedAt.isAfter(lastRatedAt)) {
// not initial sync, no ratings have changed
Timber.d("downloadEpisodeRatings: no changes since %tF %tT", lastRatedAt, lastRatedAt);
return UpdateResult.SUCCESS;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return UpdateResult.INCOMPLETE;
}
// download rated episodes
List<RatedEpisode> ratedEpisodes;
try {
Response<List<RatedEpisode>> response = traktSync.get().ratingsEpisodes(RatingsFilter.ALL, Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
ratedEpisodes = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get episode ratings", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get episode ratings", e);
return UpdateResult.INCOMPLETE;
}
if (ratedEpisodes == null) {
Timber.e("downloadEpisodeRatings: null response");
return UpdateResult.INCOMPLETE;
}
if (ratedEpisodes.isEmpty()) {
Timber.d("downloadEpisodeRatings: no ratings on trakt");
return UpdateResult.SUCCESS;
}
// trakt last activity rated_at timestamp is set after the rating timestamp
// so include ratings that are a little older
long ratedAtThreshold = lastRatedAt - 5 * DateUtils.MINUTE_IN_MILLIS;
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (RatedEpisode episode : ratedEpisodes) {
if (episode.rating == null || episode.episode == null || episode.episode.ids == null || episode.episode.ids.tvdb == null) {
// skip, can't handle
continue;
}
if (episode.rated_at != null && episode.rated_at.isBefore(ratedAtThreshold)) {
// no need to apply older ratings again
break;
}
// if an episode does not exist, this update will do nothing
ContentProviderOperation op = ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodeUri(episode.episode.ids.tvdb)).withValue(SeriesGuideContract.Episodes.RATING_USER, episode.rating.value).build();
batch.add(op);
}
// apply database updates
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "downloadEpisodeRatings: database update failed");
return UpdateResult.INCOMPLETE;
}
// save last rated instant
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_EPISODES_RATED_AT, ratedAt.getMillis()).commit();
Timber.d("downloadEpisodeRatings: success, last rated_at %tF %tT", ratedAt.getMillis(), ratedAt.getMillis());
return UpdateResult.SUCCESS;
}
use of android.content.OperationApplicationException in project SeriesGuide by UweTrottmann.
the class TraktTools method processTraktShows.
private int processTraktShows(@NonNull List<BaseShow> remoteShows, @NonNull HashSet<Integer> localShows, boolean isInitialSync, Flag flag) {
HashMap<Integer, BaseShow> traktShows = buildTraktShowsMap(remoteShows);
int uploadedShowsCount = 0;
final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (Integer localShow : localShows) {
if (traktShows.containsKey(localShow)) {
// show watched/collected on trakt
BaseShow traktShow = traktShows.get(localShow);
int result = processTraktSeasons(isInitialSync, localShow, traktShow, flag);
if (result < SUCCESS) {
// processing seasons failed, give up.
return result;
}
if (flag == Flag.WATCHED) {
updateLastWatchedTime(localShow, traktShow, batch);
}
} else {
// show not watched/collected on trakt
// check if this is because the show can not be tracked with trakt (yet)
// some shows only exist on TheTVDB, keep state local and maybe upload in the future
Integer showTraktId = ShowTools.getShowTraktId(context, localShow);
if (showTraktId != null) {
if (isInitialSync) {
// upload all watched/collected episodes of the show
// do in between processing to stretch uploads over longer time periods
uploadEpisodes(localShow, showTraktId, flag);
uploadedShowsCount++;
} else {
// set all watched/collected episodes of show not watched/collected
batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfShowUri(localShow)).withSelection(flag.clearFlagSelection, null).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
}
}
}
}
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "processTraktShows: failed to remove flag for %s.", flag.name);
}
if (uploadedShowsCount > 0) {
Timber.d("processTraktShows: uploaded %s flags for %s complete shows.", flag.name, localShows.size());
}
return SUCCESS;
}
use of android.content.OperationApplicationException in project SeriesGuide by UweTrottmann.
the class TraktTools method downloadMovieRatings.
/**
* Downloads trakt movie ratings and applies the latest ones to the database.
*
* <p> To apply all ratings, set {@link TraktSettings#KEY_LAST_MOVIES_RATED_AT} to 0.
*/
public UpdateResult downloadMovieRatings(DateTime ratedAt) {
if (ratedAt == null) {
Timber.e("downloadMovieRatings: null rated_at");
return UpdateResult.INCOMPLETE;
}
long lastRatedAt = TraktSettings.getLastMoviesRatedAt(context);
if (!ratedAt.isAfter(lastRatedAt)) {
// not initial sync, no ratings have changed
Timber.d("downloadMovieRatings: no changes since %tF %tT", lastRatedAt, lastRatedAt);
return UpdateResult.SUCCESS;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return UpdateResult.INCOMPLETE;
}
// download rated shows
List<RatedMovie> ratedMovies;
try {
Response<List<RatedMovie>> response = traktSync.get().ratingsMovies(RatingsFilter.ALL, Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
ratedMovies = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get movie ratings", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get movie ratings", e);
return UpdateResult.INCOMPLETE;
}
if (ratedMovies == null) {
Timber.e("downloadMovieRatings: null response");
return UpdateResult.INCOMPLETE;
}
if (ratedMovies.isEmpty()) {
Timber.d("downloadMovieRatings: no ratings on trakt");
return UpdateResult.SUCCESS;
}
// trakt last activity rated_at timestamp is set after the rating timestamp
// so include ratings that are a little older
long ratedAtThreshold = lastRatedAt - 5 * DateUtils.MINUTE_IN_MILLIS;
// go through ratings, latest first (trakt sends in that order)
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (RatedMovie movie : ratedMovies) {
if (movie.rating == null || movie.movie == null || movie.movie.ids == null || movie.movie.ids.tmdb == null) {
// skip, can't handle
continue;
}
if (movie.rated_at != null && movie.rated_at.isBefore(ratedAtThreshold)) {
// no need to apply older ratings again
break;
}
// if a movie does not exist, this update will do nothing
ContentProviderOperation op = ContentProviderOperation.newUpdate(SeriesGuideContract.Movies.buildMovieUri(movie.movie.ids.tmdb)).withValue(SeriesGuideContract.Movies.RATING_USER, movie.rating.value).build();
batch.add(op);
}
// apply database updates
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "downloadMovieRatings: database update failed");
return UpdateResult.INCOMPLETE;
}
// save last rated instant
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_MOVIES_RATED_AT, ratedAt.getMillis()).commit();
Timber.d("downloadMovieRatings: success, last rated_at %tF %tT", ratedAt.getMillis(), ratedAt.getMillis());
return UpdateResult.SUCCESS;
}
use of android.content.OperationApplicationException in project Meltdown by phubbard.
the class MeltdownApp method doCPtest.
public void doCPtest() {
ContentResolver cr = getContentResolver();
List<Integer> items = fetchUnreadItemsIDs();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// Get data in max-fever-size chunks
final int PER_FETCH = 50;
final int num_fetches = (int) Math.ceil(items.size() / PER_FETCH) + 1;
for (int idx = 0; idx < num_fetches; idx++) {
int left_index = idx * PER_FETCH;
int right_index = Math.min((idx + 1) * PER_FETCH, items.size());
Log.d(TAG, "On run " + idx + " pulling from " + left_index + " to " + (right_index - 1) + " of " + items.size());
List<Integer> ids = items.subList(left_index, right_index);
String payload = xcvr.fetchListOfItems(ids);
for (int j = 0; j < ids.size(); j++) {
JSONArray jitems;
RssItem this_item;
try {
JSONObject jdata = new JSONObject(payload);
jitems = jdata.getJSONArray("items");
this_item = new RssItem(jitems.getJSONObject(j));
ops.add(ContentProviderOperation.newInsert(ItemProvider.URI).withYieldAllowed(true).withValues(this_item.getCV()).build());
} catch (JSONException je) {
Log.e(TAG, "JSON error in CP import logic: ", je);
} finally {
}
}
}
try {
cr.applyBatch(ItemProvider.AUTHORITY, ops);
} catch (RemoteException e) {
Log.e(TAG, "Remote exception in bulk save", e);
e.printStackTrace();
} catch (OperationApplicationException e) {
Log.e(TAG, "OAE exception in bulk save", e);
e.printStackTrace();
}
cr.notifyChange(ItemProvider.URI, null, false);
}
use of android.content.OperationApplicationException in project android_frameworks_base by ResurrectionRemix.
the class TvInputManagerService method registerBroadcastReceivers.
private void registerBroadcastReceivers() {
PackageMonitor monitor = new PackageMonitor() {
private void buildTvInputList(String[] packages) {
synchronized (mLock) {
if (mCurrentUserId == getChangingUserId()) {
buildTvInputListLocked(mCurrentUserId, packages);
buildTvContentRatingSystemListLocked(mCurrentUserId);
}
}
}
@Override
public void onPackageUpdateFinished(String packageName, int uid) {
if (DEBUG)
Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")");
// This callback is invoked when the TV input is reinstalled.
// In this case, isReplacing() always returns true.
buildTvInputList(new String[] { packageName });
}
@Override
public void onPackagesAvailable(String[] packages) {
if (DEBUG) {
Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")");
}
// available.
if (isReplacing()) {
buildTvInputList(packages);
}
}
@Override
public void onPackagesUnavailable(String[] packages) {
// unavailable.
if (DEBUG) {
Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) + ")");
}
if (isReplacing()) {
buildTvInputList(packages);
}
}
@Override
public void onSomePackagesChanged() {
// the TV inputs.
if (DEBUG)
Slog.d(TAG, "onSomePackagesChanged()");
if (isReplacing()) {
if (DEBUG)
Slog.d(TAG, "Skipped building TV input list due to replacing");
// methods instead.
return;
}
buildTvInputList(null);
}
@Override
public boolean onPackageChanged(String packageName, int uid, String[] components) {
// the update can be handled in {@link #onSomePackagesChanged}.
return true;
}
@Override
public void onPackageRemoved(String packageName, int uid) {
synchronized (mLock) {
UserState userState = getOrCreateUserStateLocked(getChangingUserId());
if (!userState.packageSet.contains(packageName)) {
// Not a TV input package.
return;
}
}
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
String selection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
String[] selectionArgs = { packageName };
operations.add(ContentProviderOperation.newDelete(TvContract.Channels.CONTENT_URI).withSelection(selection, selectionArgs).build());
operations.add(ContentProviderOperation.newDelete(TvContract.Programs.CONTENT_URI).withSelection(selection, selectionArgs).build());
operations.add(ContentProviderOperation.newDelete(TvContract.WatchedPrograms.CONTENT_URI).withSelection(selection, selectionArgs).build());
ContentProviderResult[] results = null;
try {
ContentResolver cr = getContentResolverForUser(getChangingUserId());
results = cr.applyBatch(TvContract.AUTHORITY, operations);
} catch (RemoteException | OperationApplicationException e) {
Slog.e(TAG, "error in applyBatch", e);
}
if (DEBUG) {
Slog.d(TAG, "onPackageRemoved(packageName=" + packageName + ", uid=" + uid + ")");
Slog.d(TAG, "results=" + results);
}
}
};
monitor.register(mContext, null, UserHandle.ALL, true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
intentFilter.addAction(Intent.ACTION_USER_REMOVED);
mContext.registerReceiverAsUser(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_USER_SWITCHED.equals(action)) {
switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
} else if (Intent.ACTION_USER_REMOVED.equals(action)) {
removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
}
}
}, UserHandle.ALL, intentFilter, null, null);
}
Aggregations