Ejemplo n.º 1
0
  /**
   * Open the book's real file in an application which will allow the user to read it. This will
   * also put the book at the top of the recents list.
   *
   * @param realm Instance of Realm to use.
   * @param book Book to open.
   * @param posToUpdate If not {@code -1}, then an {@link ActionEvent} will be fired
   */
  public static void openBookUsingIntent(Realm realm, RBook book, int posToUpdate) {
    if (!Util.checkForStoragePermAndFireEventIfNeeded(R.id.action_execute_deferred)) {
      // Defer this action while we ask for permission.
      setDeferredAction(
          params -> openBookUsingIntent(realm, (RBook) params[0], (int) params[1]),
          book,
          posToUpdate);
      return;
    }

    File file = Util.getFileFromRelPath(book.relPath);
    // Make the user aware if the underlying file doesn't exist.
    if (file == null) {
      SnackKiosk.snack(R.string.sb_err_file_not_found, Snackbar.LENGTH_SHORT);
      return;
    }

    // Construct intent to use to open the file.
    Intent newIntent = new Intent(Intent.ACTION_VIEW);
    newIntent.setDataAndType(
        FileProvider.getUriForFile(Minerva.get(), "com.bkromhout.minerva.Minerva.files", file),
        MimeTypeMap.getSingleton().getMimeTypeFromExtension(Util.getExtFromFName(file.getName())));
    newIntent.setFlags(
        Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    try {
      // Try to open the book file in the app of the user's choice.
      // TODO Animate the clicked view to fill the screen as a transition out??
      Minerva.get().startActivity(newIntent);
      // Put book at the top of the recents list.
      realm.executeTransaction(
          tRealm -> {
            book.lastReadDate = Calendar.getInstance().getTime();
            book.isInRecents = true;
          });
      // If we have a position to update, fire an ActionEvent (NOT UpdatePosEvent!!).
      if (posToUpdate != -1)
        EventBus.getDefault().post(new ActionEvent(R.id.action_read, null, posToUpdate));
    } catch (ActivityNotFoundException e) {
      // Tell the user there aren't any apps which advertise the ability to handle the book's file
      // type.
      SnackKiosk.snack(R.string.sb_err_no_apps, R.string.dismiss, Snackbar.LENGTH_LONG);
    }
  }
Ejemplo n.º 2
0
 @Override
 public void onResume() {
   super.onResume();
   SnackKiosk.startSnacking(this);
   if (moveToTop != -1) {
     adapter.notifyItemMoved(moveToTop, 0);
     moveToTop = -1;
   }
 }
Ejemplo n.º 3
0
 @Override
 public void onPause() {
   SnackKiosk.stopSnacking();
   super.onPause();
 }