Search in sources :

Example 1 with Lists

use of com.uwetrottmann.seriesguide.backend.lists.Lists in project SeriesGuide by UweTrottmann.

the class AddListTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    String listId = getListId();
    if (isSendingToHexagon()) {
        Lists listsService = getContext().getHexagonTools().getListsService();
        if (listsService == null) {
            // no longer signed in
            return ERROR_HEXAGON_API;
        }
        // send list to be added to hexagon
        SgListList wrapper = new SgListList();
        List<SgList> lists = buildList(listId, listName);
        wrapper.setLists(lists);
        try {
            listsService.save(wrapper).execute();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(getContext(), "add list", e);
            return ERROR_HEXAGON_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate(listId)) {
        return ERROR_DATABASE;
    }
    return SUCCESS;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) IOException(java.io.IOException)

Example 2 with Lists

use of com.uwetrottmann.seriesguide.backend.lists.Lists in project SeriesGuide by UweTrottmann.

the class ReorderListsTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    if (isSendingToHexagon()) {
        Lists listsService = getContext().getHexagonTools().getListsService();
        if (listsService == null) {
            // no longer signed in
            return ERROR_HEXAGON_API;
        }
        // send lists with updated order to hexagon
        SgListList wrapper = new SgListList();
        List<SgList> lists = buildListsList(listIdsInOrder);
        wrapper.setLists(lists);
        try {
            listsService.save(wrapper).execute();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(getContext(), "reorder lists", e);
            return ERROR_HEXAGON_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate()) {
        return ERROR_DATABASE;
    }
    return SUCCESS;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) IOException(java.io.IOException)

Example 3 with Lists

use of com.uwetrottmann.seriesguide.backend.lists.Lists in project SeriesGuide by UweTrottmann.

the class HexagonTools method getListsService.

/**
     * Returns the instance for this hexagon service or null if not signed in.
     */
@Nullable
public synchronized Lists getListsService() {
    GoogleAccountCredential credential = getAccountCredential(true);
    if (credential.getSelectedAccount() == null) {
        return null;
    }
    if (listsService == null) {
        Lists.Builder builder = new Lists.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential);
        listsService = CloudEndpointUtils.updateBuilder(app, builder).build();
    }
    return listsService;
}
Also used : GoogleAccountCredential(com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) Nullable(android.support.annotation.Nullable)

Example 4 with Lists

use of com.uwetrottmann.seriesguide.backend.lists.Lists in project SeriesGuide by UweTrottmann.

the class ListsTools method removeListsRemovedOnHexagon.

public static boolean removeListsRemovedOnHexagon(SgApp app) {
    Timber.d("removeListsRemovedOnHexagon");
    HashSet<String> localListIds = getListIds(app);
    if (localListIds == null) {
        // query failed
        return false;
    }
    if (localListIds.size() <= 1) {
        // one or no list, can not remove any list
        return true;
    }
    // get list of ids of lists on hexagon
    List<String> hexagonListIds = new ArrayList<>(localListIds.size());
    String cursor = null;
    do {
        try {
            Lists listsService = app.getHexagonTools().getListsService();
            if (listsService == null) {
                // no longer signed in
                return false;
            }
            Lists.GetIds request = listsService.getIds();
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            SgListIds response = request.execute();
            if (response == null) {
                Timber.d("removeListsRemovedOnHexagon: failed, response is null.");
                return false;
            }
            List<String> listIds = response.getListIds();
            if (listIds == null || listIds.size() == 0) {
                // empty response, assume we got all ids
                break;
            }
            hexagonListIds.addAll(listIds);
            cursor = response.getCursor();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(app, "get list ids", e);
            return false;
        }
    } while (// fetch next batch
    !TextUtils.isEmpty(cursor));
    if (hexagonListIds.size() <= 1) {
        // one or no list on hexagon, can not remove any list
        return true;
    }
    // exclude any lists that are on hexagon
    for (String listId : hexagonListIds) {
        localListIds.remove(listId);
    }
    // remove any list not on hexagon
    if (localListIds.size() > 0) {
        ArrayList<ContentProviderOperation> batch = new ArrayList<>();
        for (String listId : localListIds) {
            batch.add(ContentProviderOperation.newDelete(SeriesGuideContract.Lists.buildListUri(listId)).build());
        }
        try {
            DBUtils.applyInSmallBatches(app, batch);
        } catch (OperationApplicationException e) {
            Timber.e(e, "removeListsRemovedOnHexagon: deleting lists failed.");
            return false;
        }
    }
    return true;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OperationApplicationException(android.content.OperationApplicationException) SgListIds(com.uwetrottmann.seriesguide.backend.lists.model.SgListIds)

Example 5 with Lists

use of com.uwetrottmann.seriesguide.backend.lists.Lists in project SeriesGuide by UweTrottmann.

the class ListsTools method downloadFromHexagon.

@SuppressLint("ApplySharedPref")
public static boolean downloadFromHexagon(SgApp app, boolean hasMergedLists) {
    long currentTime = System.currentTimeMillis();
    DateTime lastSyncTime = new DateTime(HexagonSettings.getLastListsSyncTime(app));
    if (hasMergedLists) {
        Timber.d("downloadFromHexagon: downloading lists changed since %s.", lastSyncTime);
    } else {
        Timber.d("downloadFromHexagon: downloading all lists.");
    }
    HashSet<String> localListIds = getListIds(app);
    List<SgList> lists;
    String cursor = null;
    do {
        try {
            Lists listsService = app.getHexagonTools().getListsService();
            if (listsService == null) {
                // no longer signed in
                return false;
            }
            // use default server limit
            Lists.Get request = listsService.get();
            if (hasMergedLists) {
                request.setUpdatedSince(lastSyncTime);
            }
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            SgListList response = request.execute();
            if (response == null) {
                Timber.d("downloadFromHexagon: failed, response is null.");
                break;
            }
            cursor = response.getCursor();
            lists = response.getLists();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(app, "get lists", e);
            return false;
        }
        if (lists == null || lists.size() == 0) {
            // empty response, assume we are done
            break;
        }
        if (!doListsDatabaseUpdate(app, lists, localListIds, hasMergedLists)) {
            // database update failed, abort
            return false;
        }
    } while (// fetch next batch
    !TextUtils.isEmpty(cursor));
    // set new last sync time
    if (hasMergedLists) {
        PreferenceManager.getDefaultSharedPreferences(app).edit().putLong(HexagonSettings.KEY_LAST_SYNC_LISTS, currentTime).commit();
    }
    return true;
}
Also used : SgList(com.uwetrottmann.seriesguide.backend.lists.model.SgList) SgListList(com.uwetrottmann.seriesguide.backend.lists.model.SgListList) Lists(com.uwetrottmann.seriesguide.backend.lists.Lists) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime) SuppressLint(android.annotation.SuppressLint)

Aggregations

Lists (com.uwetrottmann.seriesguide.backend.lists.Lists)7 IOException (java.io.IOException)6 SgList (com.uwetrottmann.seriesguide.backend.lists.model.SgList)5 SgListList (com.uwetrottmann.seriesguide.backend.lists.model.SgListList)5 SuppressLint (android.annotation.SuppressLint)1 ContentProviderOperation (android.content.ContentProviderOperation)1 OperationApplicationException (android.content.OperationApplicationException)1 Nullable (android.support.annotation.Nullable)1 GoogleAccountCredential (com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential)1 DateTime (com.google.api.client.util.DateTime)1 SgListIds (com.uwetrottmann.seriesguide.backend.lists.model.SgListIds)1 ArrayList (java.util.ArrayList)1