/** * Delete the users that are present in the local content providers, but are not returned anymore * by Dailymotion. It means that these users are outdated and should not be proposed to the user. * * @return A boolean indicating if the deletion successfully occured. */ private boolean deleteOutdatedUsers() { if (mLocalUsers.size() > 0) { int count = mLocalUsers.size(); KidsLogger.i(LOG_TAG, String.format("Removing %d users from db", count)); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(count); for (String key : mLocalUsers.keySet()) { ContentProviderOperation operation = ContentProviderOperation.newDelete(UsersProvider.CONTENT_URI) .withSelection(UserContract.DAILYMOTION_ID + "=?", new String[] {key}) .build(); ops.add(operation); } try { return mContentResolver.applyBatch(UsersProvider.AUTHORITY, ops).length == count; } catch (RemoteException e) { // Should not happen, the Content Provider is local. KidsLogger.w(LOG_TAG, "An exception occured while communicating with provider."); return false; } catch (OperationApplicationException e) { KidsLogger.w(LOG_TAG, "An operation has failed when deleting local users."); return false; } } return true; }
@Override public void onSuccess(JSONObject response) { JSONArray array = response.optJSONArray("list"); List<User> usersToInsert = parseUsers(array); for (int i = 0; i < usersToInsert.size(); i++) { KidsLogger.d( LOG_TAG, String.format("Getting user : %s ", usersToInsert.get(i).getScreenName())); if (usersToInsert.get(i).getScreenName().equals("null")) { KidsLogger.d( LOG_TAG, String.format("Removing user : %s ", usersToInsert.get(i).getScreenName())); usersToInsert.remove(i); } } if (!insertNewUsers(usersToInsert)) { mCallback.onFailure(new IOException("Impossible to insert users in db.")); return; } if (response.optBoolean("has_more") && response.optInt("page") != 0) { fetchPage(response.optInt("page") + 1); } else { deleteOutdatedUsers(); mCallback.onSuccess(mFetchedUsers); } }
/** * Fetch the provided page of users for the Dailymotion end point generated based on the object we * need to fetch the users for. * * @param page Page to fetch as an int. */ private void fetchPage(int page) { KidsLogger.d(LOG_TAG, String.format("Fetching users from %s, page : %d", mEndPoint, page)); Bundle bundle = new Bundle(); bundle.putString("fields", FIELDS); bundle.putString("tileset", "kidsplus"); bundle.putString("localization", "fr"); bundle.putString("page", String.valueOf(page)); bundle.putString("limit", String.valueOf(Constants.PAGE_LIMIT)); DailymotionRequestor requestor = new DailymotionRequestor(mDailymotion); requestor.anonymousRequest(mEndPoint, bundle, new UserFetchingRequestListener()); }
/** * Insert the provided playlists in the local database. A boolean is returned to indicate if the * insertion went correctly. */ private boolean insertNewUsers(List<User> users) { if (users.size() > 0) { KidsLogger.d(LOG_TAG, String.format("Inserting %d users", users.size())); ContentValues[] values = new ContentValues[users.size()]; for (int i = 0; i < users.size(); ++i) { values[i] = users.get(i).toContentValues(); } return (mContentResolver.bulkInsert(UsersProvider.CONTENT_URI, values) == users.size()); } return true; }