Exemple #1
0
  /** Method that reads the requested file. */
  private void writeFile() {
    try {
      // Configure the writer
      AsyncWriter writer = new AsyncWriter();

      // Create the writable command
      WriteExecutable cmd = CommandHelper.write(this, this.mFso.getFullPath(), writer, null);

      // Obtain access to the buffer (IMP! don't close the buffer here, it's manage
      // by the command)
      OutputStream os = cmd.createOutputStream();
      try {
        // Retrieve the text from the editor
        String text = this.mEditor.getText().toString();
        ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes());
        text = null;
        try {
          // Buffered write
          byte[] data = new byte[this.mBufferSize];
          int read = 0;
          while ((read = bais.read(data, 0, this.mBufferSize)) != -1) {
            os.write(data, 0, read);
          }
        } finally {
          try {
            bais.close();
          } catch (Exception e) {
            /** NON BLOCK* */
          }
        }

      } finally {
        // Ok. Data is written or ensure buffer close
        cmd.end();
      }

      // Sleep a bit
      Thread.sleep(150L);

      // Is error?
      if (writer.mCause != null) {
        // Something was wrong. The file probably is corrupted
        DialogHelper.showToast(this, R.string.msgs_operation_failure, Toast.LENGTH_SHORT);
      } else {
        // Success. The file was saved
        DialogHelper.showToast(this, R.string.editor_successfully_saved, Toast.LENGTH_SHORT);
        setDirty(false);

        // Send a message that allow other activities to update his data
        Intent intent = new Intent(FileManagerSettings.INTENT_FILE_CHANGED);
        intent.putExtra(FileManagerSettings.EXTRA_FILE_CHANGED_KEY, this.mFso.getFullPath());
        sendBroadcast(intent);
      }

    } catch (Exception e) {
      // Something was wrong, but the file was NOT written
      DialogHelper.showToast(this, R.string.msgs_operation_failure, Toast.LENGTH_SHORT);
      return;
    }
  }
Exemple #2
0
  /** Method that reads the requested file */
  private void readFile() {
    // For now editor is not dirty and editable.
    setDirty(false);
    this.mBinary = false;

    // Check for a valid action
    String action = getIntent().getAction();
    if (action == null
        || (action.compareTo(Intent.ACTION_VIEW) != 0)
            && (action.compareTo(Intent.ACTION_EDIT) != 0)) {
      DialogHelper.showToast(this, R.string.editor_invalid_file_msg, Toast.LENGTH_SHORT);
      return;
    }
    // This var should be set depending on ACTION_VIEW or ACTION_EDIT action, but for
    // better compatibility, IntentsActionPolicy use always ACTION_VIEW, so we have
    // to ignore this check here
    this.mReadOnly = false;

    // Read the intent and check that is has a valid request
    String path = getIntent().getData().getPath();
    if (path == null || path.length() == 0) {
      DialogHelper.showToast(this, R.string.editor_invalid_file_msg, Toast.LENGTH_SHORT);
      return;
    }

    // Set the title of the dialog
    File f = new File(path);
    getActionBar().setTitle(f.getName());

    // Check that we have access to the file (the real file, not the symlink)
    try {
      this.mFso = CommandHelper.getFileInfo(this, path, true, null);
      if (this.mFso == null) {
        DialogHelper.showToast(this, R.string.editor_file_not_found_msg, Toast.LENGTH_SHORT);
        return;
      }
    } catch (Exception e) {
      Log.e(TAG, "Failed to get file reference", e); // $NON-NLS-1$
      DialogHelper.showToast(this, R.string.editor_file_not_found_msg, Toast.LENGTH_SHORT);
      return;
    }

    // Check that we can handle the length of the file (by device)
    if (this.mMaxFileSize < this.mFso.getSize()) {
      DialogHelper.showToast(this, R.string.editor_file_exceed_size_msg, Toast.LENGTH_SHORT);
      return;
    }

    // Read the file in background
    asyncRead();
  }