예제 #1
0
  /**
   * The action will take after another activity reply to this one
   *
   * @param requestCode -- the key to get result
   * @param resultCode -- result state
   * @param data -- intent to get data
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(thisClass, "in FileChooser result");
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case PICK_FILE_REQUEST_CODE:
          Uri fileUri = data.getData();

          //                    if (BuildConfig.DEBUG) {
          //                FileUtility.deleteAllFiles(currentSelectedDir());
          //                    }

          // create a file save the content under the related folder
          String path = UriUtility.getPath(this, fileUri);
          String name = FileUtility.getNameFromPath(path);
          FileEncryption file;
          File saveFile;
          try {
            saveFile = fileChooserBL.getEncryptedFilePath(currentSelectedDir(), name);
            file = new FileEncryption(saveFile, path, fileUri.toString(), password);
          } catch (IOException | NoSuchAlgorithmException e) {
            Log.e(thisClass, "File write failed: " + e.toString());
            return;
          }
          try {
            boolean succ = file.encrypt();
            if (!succ) {
              Toast.makeText(
                      getApplicationContext(), getString(R.string.encryptFail), Toast.LENGTH_SHORT)
                  .show();
              return;
            } else {
              Toast.makeText(
                      getApplicationContext(), getString(R.string.encryptSucc), Toast.LENGTH_SHORT)
                  .show();
            }
            if (!file.deleteOriginal()) {
              throw new EncryptionException("fail to delete original file");
            }
          } catch (IOException e) {
            Log.e(thisClass, "File encrypt failed: " + e.toString());
          } catch (NoSuchAlgorithmException | UnrecoverableEntryException | InvalidKeyException e) {
            Log.e(thisClass, " failed: " + e.toString());
          }
          // update the view as if it is clicked
          folderFragmentClick(fatherIndex);
          break;
      }
    } else {
      Log.e(thisClass, " fail to start activity " + data);
    }
  }