Beispiel #1
0
  @Override
  protected Integer doInBackground(Void... params) {
    // Ensure external storage
    if (!AndroidUtils.isExtStorageAvailable()) {
      return ERROR_STORAGE_ACCESS;
    }

    // Ensure no large database ops are running
    TaskManager tm = TaskManager.getInstance(mContext);
    if (SgSyncAdapter.isSyncActive(mContext, false) || tm.isAddTaskRunning()) {
      return ERROR_LARGE_DB_OP;
    }

    // Ensure JSON file is available
    File path = JsonExportTask.getExportPath(false);
    File backup = new File(path, JsonExportTask.EXPORT_JSON_FILE_SHOWS);
    if (!backup.exists() || !backup.canRead()) {
      return ERROR_FILE_ACCESS;
    }

    // Clean out all existing tables
    mContext.getContentResolver().delete(Shows.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(Seasons.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(Episodes.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(SeriesContract.Lists.CONTENT_URI, null, null);
    mContext.getContentResolver().delete(ListItems.CONTENT_URI, null, null);

    // Access JSON from backup folder to create new database
    try {
      InputStream in = new FileInputStream(backup);

      Gson gson = new Gson();

      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      reader.beginArray();

      while (reader.hasNext()) {
        Show show = gson.fromJson(reader, Show.class);
        addShowToDatabase(show);
      }

      reader.endArray();
      reader.close();

    } catch (IOException e) {
      return ERROR;
    }

    /*
     * Lists
     */
    File backupLists = new File(path, JsonExportTask.EXPORT_JSON_FILE_LISTS);
    if (!backupLists.exists() || !backupLists.canRead()) {
      // Skip lists if the file is not accessible
      return SUCCESS;
    }

    // Access JSON from backup folder to create new database
    try {
      InputStream in = new FileInputStream(backupLists);

      Gson gson = new Gson();

      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      reader.beginArray();

      while (reader.hasNext()) {
        List list = gson.fromJson(reader, List.class);
        addListToDatabase(list);
      }

      reader.endArray();
      reader.close();

    } catch (IOException e) {
      return ERROR;
    }

    // Renew search table
    mContext
        .getContentResolver()
        .query(EpisodeSearch.CONTENT_URI_RENEWFTSTABLE, null, null, null, null);

    return SUCCESS;
  }
 /** Called if the user adds a show from a trakt stream fragment. */
 @Override
 public void onAddShow(SearchResult show) {
   TaskManager.getInstance(this).performAddTask(show);
 }