use of android.content.OperationApplicationException in project SeriesGuide by UweTrottmann.
the class ListsTools method doListsDatabaseUpdate.
private static boolean doListsDatabaseUpdate(Context context, List<SgList> lists, HashSet<String> localListIds, boolean hasMergedLists) {
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (SgList list : lists) {
// add or update the list
String listId = list.getListId();
ContentProviderOperation.Builder builder = null;
if (localListIds.contains(listId)) {
// update
if (hasMergedLists) {
// only overwrite name and order if data was already merged
// use case: user disconnected for a while, changed lists, then reconnects
builder = ContentProviderOperation.newUpdate(SeriesGuideContract.Lists.buildListUri(listId));
}
} else {
// insert
builder = ContentProviderOperation.newInsert(SeriesGuideContract.Lists.CONTENT_URI).withValue(SeriesGuideContract.Lists.LIST_ID, listId);
}
if (builder != null) {
builder.withValue(SeriesGuideContract.Lists.NAME, list.getName());
if (list.getOrder() != null) {
builder.withValue(SeriesGuideContract.Lists.ORDER, list.getOrder());
}
batch.add(builder.build());
}
// keep track of items not in the list on hexagon
HashSet<String> listItemsToRemove = null;
if (hasMergedLists) {
listItemsToRemove = getListItemIds(context, listId);
if (listItemsToRemove == null) {
// list item query failed
return false;
}
}
// add or update items of the list
List<SgListItem> listItems = list.getListItems();
if (listItems != null) {
for (SgListItem listItem : listItems) {
String listItemId = listItem.getListItemId();
String[] brokenUpId = SeriesGuideContract.ListItems.splitListItemId(listItemId);
if (brokenUpId == null) {
// could not break up list item id
continue;
}
int itemTvdbId = -1;
int itemType = -1;
try {
itemTvdbId = Integer.parseInt(brokenUpId[0]);
itemType = Integer.parseInt(brokenUpId[1]);
} catch (NumberFormatException ignored) {
}
if (itemTvdbId == -1 || !SeriesGuideContract.ListItems.isValidItemType(itemType)) {
// failed to extract item TVDB id or item type not known
continue;
}
// just insert the list item, if the id already exists it will be replaced
builder = ContentProviderOperation.newInsert(SeriesGuideContract.ListItems.CONTENT_URI).withValue(SeriesGuideContract.ListItems.LIST_ITEM_ID, listItemId).withValue(SeriesGuideContract.ListItems.ITEM_REF_ID, itemTvdbId).withValue(SeriesGuideContract.ListItems.TYPE, itemType).withValue(SeriesGuideContract.Lists.LIST_ID, listId);
batch.add(builder.build());
if (hasMergedLists) {
// do not remove this list item
listItemsToRemove.remove(listItemId);
}
}
}
if (hasMergedLists) {
// remove items no longer in the list
for (String listItemId : listItemsToRemove) {
builder = ContentProviderOperation.newDelete(SeriesGuideContract.ListItems.buildListItemUri(listItemId));
batch.add(builder.build());
}
}
}
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "doListsDatabaseUpdate: failed.");
return false;
}
return true;
}
use of android.content.OperationApplicationException in project SeriesGuide by UweTrottmann.
the class MovieTools method syncMovieListsWithTrakt.
/**
* Updates the local movie database against trakt movie watchlist and collection. Adds, updates
* and removes movies in the database.
*
* <p> When syncing the first time, will upload any local movies missing from trakt collection
* or watchlist instead of removing them locally.
*
* <p> Performs <b>synchronous network access</b>, make sure to run this on a background
* thread.
*/
@SuppressLint("ApplySharedPref")
public UpdateResult syncMovieListsWithTrakt(LastActivityMore activity) {
if (activity.collected_at == null) {
Timber.e("syncMoviesWithTrakt: null collected_at");
return UpdateResult.INCOMPLETE;
}
if (activity.watchlisted_at == null) {
Timber.e("syncMoviesWithTrakt: null watchlisted_at");
return UpdateResult.INCOMPLETE;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return UpdateResult.INCOMPLETE;
}
final boolean merging = !TraktSettings.hasMergedMovies(context);
if (!merging && !TraktSettings.isMovieListsChanged(context, activity.collected_at, activity.watchlisted_at)) {
Timber.d("syncMoviesWithTrakt: no changes");
return UpdateResult.SUCCESS;
}
// download collection
Set<Integer> collection;
try {
Response<List<BaseMovie>> response = traktSync.get().collectionMovies(Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
collection = buildTmdbIdSet(response.body());
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get movie collection", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get movie collection", e);
return UpdateResult.INCOMPLETE;
}
if (collection == null) {
Timber.e("syncMoviesWithTrakt: null collection response");
return UpdateResult.INCOMPLETE;
}
// download watchlist
Set<Integer> watchlist;
try {
Response<List<BaseMovie>> response = traktSync.get().watchlistMovies(Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
watchlist = buildTmdbIdSet(response.body());
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get movie watchlist", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get movie watchlist", e);
return UpdateResult.INCOMPLETE;
}
if (watchlist == null) {
Timber.e("syncMoviesWithTrakt: null watchlist response");
return UpdateResult.INCOMPLETE;
}
// build updates
// loop through all local movies
Set<Integer> moviesNotOnTraktCollection = new HashSet<>();
Set<Integer> moviesNotOnTraktWatchlist = new HashSet<>();
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
HashSet<Integer> localMovies = getMovieTmdbIdsAsSet(context);
if (localMovies == null) {
Timber.e("syncMoviesWithTrakt: querying local movies failed");
return UpdateResult.INCOMPLETE;
}
for (Integer tmdbId : localMovies) {
// is local movie in trakt collection or watchlist?
boolean inCollection = collection.remove(tmdbId);
boolean inWatchlist = watchlist.remove(tmdbId);
if (merging) {
// upload movie if missing from trakt collection or watchlist
if (!inCollection) {
moviesNotOnTraktCollection.add(tmdbId);
}
if (!inWatchlist) {
moviesNotOnTraktWatchlist.add(tmdbId);
}
// add to local collection or watchlist, but do NOT remove
if (inCollection || inWatchlist) {
ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(SeriesGuideContract.Movies.buildMovieUri(tmdbId));
if (inCollection) {
builder.withValue(SeriesGuideContract.Movies.IN_COLLECTION, true);
}
if (inWatchlist) {
builder.withValue(SeriesGuideContract.Movies.IN_WATCHLIST, true);
}
batch.add(builder.build());
}
} else {
// mirror trakt collection and watchlist flag
// will take care of removing unneeded (not watched or in any list) movies
// in later sync step
ContentProviderOperation op = ContentProviderOperation.newUpdate(SeriesGuideContract.Movies.buildMovieUri(tmdbId)).withValue(SeriesGuideContract.Movies.IN_COLLECTION, inCollection).withValue(SeriesGuideContract.Movies.IN_WATCHLIST, inWatchlist).build();
batch.add(op);
}
}
// apply collection and watchlist updates to existing movies
try {
DBUtils.applyInSmallBatches(context, batch);
Timber.d("syncMoviesWithTrakt: updated %s", batch.size());
} catch (OperationApplicationException e) {
Timber.e(e, "syncMoviesWithTrakt: database updates failed");
return UpdateResult.INCOMPLETE;
}
batch.clear();
// merge on first run
if (merging) {
// upload movies not in trakt collection or watchlist
if (toTrakt(moviesNotOnTraktCollection, moviesNotOnTraktWatchlist) != UpdateResult.SUCCESS) {
return UpdateResult.INCOMPLETE;
} else {
// set merge successful
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(TraktSettings.KEY_HAS_MERGED_MOVIES, true).commit();
}
}
// add movies from trakt missing locally
// all local movies were removed from trakt collection and watchlist,
// so they only contain movies missing locally
UpdateResult result = addMovies(collection, watchlist);
if (result == UpdateResult.SUCCESS) {
// store last activity timestamps
TraktSettings.storeLastMoviesChangedAt(context, activity.collected_at, activity.watchlisted_at);
// ensure all movie ratings and watched flags are downloaded next
if (collection.size() > 0 || watchlist.size() > 0) {
TraktSettings.resetMoviesLastActivity(context);
}
}
return result;
}
use of android.content.OperationApplicationException in project android by owncloud.
the class FileDataStorageManager method updateSharedFiles.
public void updateSharedFiles(Collection<OCFile> sharedFiles) {
resetShareFlagsInAllFiles();
if (sharedFiles != null) {
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(sharedFiles.size());
// prepare operations to insert or update files to save in the given folder
for (OCFile file : sharedFiles) {
ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
if (!file.isFolder()) {
cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
}
cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.getAvailableOfflineStatus().getValue());
cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
cv.put(ProviderTableMeta.FILE_TREE_ETAG, file.getTreeEtag());
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail() ? 1 : 0);
cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading() ? 1 : 0);
cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
boolean existsByPath = fileExists(file.getRemotePath());
if (existsByPath || fileExists(file.getFileId())) {
// updating an existing file
operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).withValues(cv).withSelection(ProviderTableMeta._ID + "=?", new String[] { String.valueOf(file.getFileId()) }).build());
} else {
// adding a new file
setInitialAvailableOfflineStatus(file, cv);
operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
}
}
// apply operations in batch
if (operations.size() > 0) {
@SuppressWarnings("unused") ContentProviderResult[] results = null;
Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
try {
if (getContentResolver() != null) {
results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
} else {
results = getContentProviderClient().applyBatch(operations);
}
} catch (OperationApplicationException e) {
Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
} catch (RemoteException e) {
Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
}
}
}
}
use of android.content.OperationApplicationException in project android by owncloud.
the class FileDataStorageManager method saveShares.
public void saveShares(Collection<OCShare> shares) {
cleanShares();
if (shares != null) {
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(shares.size());
// prepare operations to insert or update files to save in the given folder
for (OCShare share : shares) {
ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
if (shareExistsForRemoteId(share.getRemoteId())) {
// updating an existing file
operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", new String[] { String.valueOf(share.getRemoteId()) }).build());
} else {
// adding a new file
operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).build());
}
}
// apply operations in batch
if (operations.size() > 0) {
@SuppressWarnings("unused") ContentProviderResult[] results = null;
Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
try {
if (getContentResolver() != null) {
results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
} else {
results = getContentProviderClient().applyBatch(operations);
}
} catch (OperationApplicationException e) {
Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
} catch (RemoteException e) {
Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
}
}
}
}
use of android.content.OperationApplicationException in project android by owncloud.
the class FileDataStorageManager method saveSharesDB.
public void saveSharesDB(ArrayList<OCShare> shares) {
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
// Reset flags & Remove shares for this files
String filePath = "";
for (OCShare share : shares) {
if (filePath != share.getPath()) {
filePath = share.getPath();
resetShareFlagInAFile(filePath);
operations = prepareRemoveSharesInFile(filePath, operations);
}
}
// Add operations to insert shares
operations = prepareInsertShares(shares, operations);
// apply operations in batch
if (operations.size() > 0) {
Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
try {
if (getContentResolver() != null) {
getContentResolver().applyBatch(MainApp.getAuthority(), operations);
} else {
getContentProviderClient().applyBatch(operations);
}
} catch (OperationApplicationException e) {
Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
} catch (RemoteException e) {
Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
}
}
// // TODO: review if it is needed
// // Update shared files
// ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
//
// for (OCShare share : shares) {
// // Get the path
// String path = share.getPath();
// if (share.isFolder()) {
// path = path + FileUtils.PATH_SEPARATOR;
// }
//
// // Update OCFile with data from share: ShareByLink, publicLink and
// OCFile file = getFileByPath(path);
// if (file != null) {
// if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
// file.setShareViaLink(true);
// sharedFiles.add(file);
// }
// }
// }
//
// // TODO: Review
// updateSharedFiles(sharedFiles);
}
Aggregations