/**
   * Performs the operation.
   *
   * @param client Client object to communicate with the remote ownCloud server.
   */
  @Override
  protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;

    /// 1. check move validity
    if (mTargetParentPath.startsWith(mSrcPath)) {
      return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
    }
    mFile = getStorageManager().getFileByPath(mSrcPath);
    if (mFile == null) {
      return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
    }

    /// 2. remote move
    String targetPath = mTargetParentPath + mFile.getFileName();
    if (mFile.isFolder()) {
      targetPath += OCFile.PATH_SEPARATOR;
    }
    MoveRemoteFileOperation operation = new MoveRemoteFileOperation(mSrcPath, targetPath, false);
    result = operation.execute(client);

    /// 3. local move
    if (result.isSuccess()) {
      getStorageManager().moveLocalFile(mFile, targetPath, mTargetParentPath);
    }
    // TODO handle ResultCode.PARTIAL_MOVE_DONE in client Activity, for the moment

    return result;
  }
示例#2
0
  /**
   * Updates title bar and home buttons (state and icon).
   *
   * <p>Assumes that navigation drawer is NOT visible.
   */
  protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
    String title = getString(R.string.default_display_name_for_root_folder); // default
    boolean inRoot;

    /// choose the appropiate title
    if (chosenFile == null) {
      chosenFile = mFile; // if no file is passed, current file decides
    }
    inRoot =
        (chosenFile == null
            || (chosenFile.isFolder()
                && chosenFile.getParentId() == FileDataStorageManager.ROOT_PARENT_ID));
    if (!inRoot) {
      title = chosenFile.getFileName();
    }

    /// set the chosen title
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(title);
    /// also as content description
    View actionBarTitleView =
        getWindow()
            .getDecorView()
            .findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
    if (actionBarTitleView != null) { // it's null in Android 2.x
      actionBarTitleView.setContentDescription(title);
    }

    /// set home button properties
    mDrawerToggle.setDrawerIndicatorEnabled(inRoot);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(true);
  }
示例#3
0
文件: Uploader.java 项目: skn/android
  private void populateDirectoryList() {
    setContentView(R.layout.uploader_layout);

    String full_path = "";
    for (String a : mParents) full_path += a + "/";

    Log.d(TAG, "Populating view with content of : " + full_path);

    mFile = mStorageManager.getFileByPath(full_path);
    if (mFile != null) {
      Vector<OCFile> files = mStorageManager.getDirectoryContent(mFile);
      List<HashMap<String, Object>> data = new LinkedList<HashMap<String, Object>>();
      for (OCFile f : files) {
        HashMap<String, Object> h = new HashMap<String, Object>();
        if (f.isDirectory()) {
          h.put("dirname", f.getFileName());
          data.add(h);
        }
      }
      SimpleAdapter sa =
          new SimpleAdapter(
              this,
              data,
              R.layout.uploader_list_item_layout,
              new String[] {"dirname"},
              new int[] {R.id.textView1});
      setListAdapter(sa);
      Button btn = (Button) findViewById(R.id.uploader_choose_folder);
      btn.setOnClickListener(this);
      getListView().setOnItemClickListener(this);
    }
  }
示例#4
0
  public void onDismiss(EditNameFragment dialog) {
    if (dialog instanceof EditNameFragment) {
      if (((EditNameFragment) dialog).getResult()) {
        String newFilename = ((EditNameFragment) dialog).getNewFilename();
        Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);
        if (!newFilename.equals(mFile.getFileName())) {
          FileDataStorageManager fdsm =
              new FileDataStorageManager(mAccount, getActivity().getContentResolver());
          if (fdsm.getFileById(mFile.getFileId()) != null) {
            OCFile newFile =
                new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath() + newFilename);
            newFile.setCreationTimestamp(mFile.getCreationTimestamp());
            newFile.setFileId(mFile.getFileId());
            newFile.setFileLength(mFile.getFileLength());
            newFile.setKeepInSync(mFile.keepInSync());
            newFile.setLastSyncDate(mFile.getLastSyncDate());
            newFile.setMimetype(mFile.getMimetype());
            newFile.setModificationTimestamp(mFile.getModificationTimestamp());
            newFile.setParentId(mFile.getParentId());
            boolean localRenameFails = false;
            if (mFile.isDown()) {
              File f = new File(mFile.getStoragePath());
              Log.e(TAG, f.getAbsolutePath());
              localRenameFails =
                  !(f.renameTo(new File(f.getParent() + File.separator + newFilename)));
              Log.e(TAG, f.getParent() + File.separator + newFilename);
              newFile.setStoragePath(f.getParent() + File.separator + newFilename);
            }

            if (localRenameFails) {
              Toast msg =
                  Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
              msg.show();

            } else {
              new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();
              boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
              getActivity()
                  .showDialog(
                      (inDisplayActivity)
                          ? FileDisplayActivity.DIALOG_SHORT_WAIT
                          : FileDetailActivity.DIALOG_SHORT_WAIT);
            }
          }
        }
      }
    } else {
      Log.e(
          TAG,
          "Unknown dialog instance passed to onDismissDalog: "
              + dialog.getClass().getCanonicalName());
    }
  }
  /**
   * Performs the rename operation.
   *
   * @param client Client object to communicate with the remote ownCloud server.
   */
  @Override
  protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;

    // check if the new name is valid in the local file system
    try {
      if (!isValidNewName()) {
        return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
      }
      String parent = (new File(mFile.getRemotePath())).getParent();
      parent = (parent.endsWith(OCFile.PATH_SEPARATOR)) ? parent : parent + OCFile.PATH_SEPARATOR;
      mNewRemotePath = parent + mNewName;
      if (mFile.isFolder()) {
        mNewRemotePath += OCFile.PATH_SEPARATOR;
      }

      // ckeck local overwrite
      if (mStorageManager.getFileByPath(mNewRemotePath) != null) {
        return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
      }

      RenameRemoteFileOperation operation =
          new RenameRemoteFileOperation(
              mFile.getFileName(), mFile.getRemotePath(), mNewName, mFile.isFolder());
      result = operation.execute(client);

      if (result.isSuccess()) {
        if (mFile.isFolder()) {
          saveLocalDirectory();

        } else {
          saveLocalFile();
        }
      }

    } catch (IOException e) {
      Log_OC.e(
          TAG,
          "Rename "
              + mFile.getRemotePath()
              + " to "
              + ((mNewRemotePath == null) ? mNewName : mNewRemotePath)
              + ": "
              + ((result != null) ? result.getLogMessage() : ""),
          e);
    }

    return result;
  }
  /**
   * Updates the view with all relevant details about that file.
   *
   * <p>TODO Remove parameter when the transferring state of files is kept in database.
   *
   * <p>TODO REFACTORING! this method called 5 times before every time the fragment is shown!
   *
   * @param transferring Flag signaling if the file should be considered as downloading or
   *     uploading, although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link
   *     FileUploaderBinder#isUploading(Account, OCFile)} return false.
   * @param refresh If 'true', try to refresh the hold file from the database
   */
  public void updateFileDetails(boolean transferring, boolean refresh) {

    if (readyToShow()) {

      if (refresh && mStorageManager != null) {
        mFile = mStorageManager.getFileByPath(mFile.getRemotePath());
      }

      // set file details
      setFilename(mFile.getFileName());
      setFiletype(mFile.getMimetype());
      setFilesize(mFile.getFileLength());
      if (ocVersionSupportsTimeCreated()) {
        setTimeCreated(mFile.getCreationTimestamp());
      }

      setTimeModified(mFile.getModificationTimestamp());

      CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
      cb.setChecked(mFile.keepInSync());

      // configure UI for depending upon local state of the file
      // if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) ||
      // FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
      FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
      FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
      if (transferring
          || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))
          || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) {
        setButtonsForTransferring();

      } else if (mFile.isDown()) {

        setButtonsForDown();

      } else {
        // TODO load default preview image; when the local file is removed, the preview remains
        // there
        setButtonsForRemote();
      }
    }
    getView().invalidate();
  }
示例#7
0
  /** Updates the view with all relevant details about that file. */
  public void updateFileDetails() {

    if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {

      // set file details
      setFilename(mFile.getFileName());
      setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile.getMimetype()));
      setFilesize(mFile.getFileLength());
      if (ocVersionSupportsTimeCreated()) {
        setTimeCreated(mFile.getCreationTimestamp());
      }

      setTimeModified(mFile.getModificationTimestamp());

      CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
      cb.setChecked(mFile.keepInSync());

      // configure UI for depending upon local state of the file
      // if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) ||
      // FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
      FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
      FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
      if ((downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))
          || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) {
        setButtonsForTransferring();

      } else if (mFile.isDown()) {
        // Update preview
        if (mFile.getMimetype().startsWith("image/")) {
          BitmapLoader bl = new BitmapLoader();
          bl.execute(new String[] {mFile.getStoragePath()});
        }

        setButtonsForDown();

      } else {
        setButtonsForRemote();
      }
    }
  }
示例#8
0
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.fdDownloadBtn:
        {
          // if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {
          FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
          FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
          if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
            downloaderBinder.cancel(mAccount, mFile);
            if (mFile.isDown()) {
              setButtonsForDown();
            } else {
              setButtonsForRemote();
            }

          } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) {
            uploaderBinder.cancel(mAccount, mFile);
            if (!mFile.fileExists()) {
              // TODO make something better
              if (getActivity() instanceof FileDisplayActivity) {
                // double pane
                FragmentTransaction transaction =
                    getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(
                    R.id.file_details_container,
                    new FileDetailFragment(null, null),
                    FTAG); // empty FileDetailFragment
                transaction.commit();
                mContainerActivity.onFileStateChanged();
              } else {
                getActivity().finish();
              }

            } else if (mFile.isDown()) {
              setButtonsForDown();
            } else {
              setButtonsForRemote();
            }

          } else {
            mLastRemoteOperation =
                new SynchronizeFileOperation(
                    mFile, null, mStorageManager, mAccount, true, false, getActivity());
            WebdavClient wc =
                OwnCloudClientUtils.createOwnCloudClient(
                    mAccount, getSherlockActivity().getApplicationContext());
            mLastRemoteOperation.execute(wc, this, mHandler);

            // update ui
            boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
            getActivity()
                .showDialog(
                    (inDisplayActivity)
                        ? FileDisplayActivity.DIALOG_SHORT_WAIT
                        : FileDetailActivity.DIALOG_SHORT_WAIT);
            setButtonsForTransferring(); // disable button immediately, although the synchronization
                                         // does not result in a file transference
          }
          break;
        }
      case R.id.fdKeepInSync:
        {
          CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
          mFile.setKeepInSync(cb.isChecked());
          mStorageManager.saveFile(mFile);

          /// register the OCFile instance in the observer service to monitor local updates;
          /// if necessary, the file is download
          Intent intent =
              new Intent(getActivity().getApplicationContext(), FileObserverService.class);
          intent.putExtra(
              FileObserverService.KEY_FILE_CMD,
              (cb.isChecked()
                  ? FileObserverService.CMD_ADD_OBSERVED_FILE
                  : FileObserverService.CMD_DEL_OBSERVED_FILE));
          intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile);
          intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount);
          Log.e(TAG, "starting observer service");
          getActivity().startService(intent);

          if (mFile.keepInSync()) {
            onClick(
                getView().findViewById(R.id.fdDownloadBtn)); // force an immediate synchronization
          }
          break;
        }
      case R.id.fdRenameBtn:
        {
          EditNameDialog dialog = EditNameDialog.newInstance(mFile.getFileName());
          dialog.setOnDismissListener(this);
          dialog.show(getFragmentManager(), "nameeditdialog");
          break;
        }
      case R.id.fdRemoveBtn:
        {
          ConfirmationDialogFragment confDialog =
              ConfirmationDialogFragment.newInstance(
                  R.string.confirmation_remove_alert,
                  new String[] {mFile.getFileName()},
                  mFile.isDown()
                      ? R.string.confirmation_remove_remote_and_local
                      : R.string.confirmation_remove_remote,
                  mFile.isDown() ? R.string.confirmation_remove_local : -1,
                  R.string.common_cancel);
          confDialog.setOnConfirmationListener(this);
          confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
          break;
        }
      case R.id.fdOpenBtn:
        {
          String storagePath = mFile.getStoragePath();
          String encodedStoragePath = WebdavUtils.encodePath(storagePath);
          try {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype());
            i.setFlags(
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            startActivity(i);

          } catch (Throwable t) {
            Log.e(
                TAG,
                "Fail when trying to open with the mimeType provided from the ownCloud server: "
                    + mFile.getMimetype());
            boolean toastIt = true;
            String mimeType = "";
            try {
              Intent i = new Intent(Intent.ACTION_VIEW);
              mimeType =
                  MimeTypeMap.getSingleton()
                      .getMimeTypeFromExtension(
                          storagePath.substring(storagePath.lastIndexOf('.') + 1));
              if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {
                i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType);
                i.setFlags(
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                startActivity(i);
                toastIt = false;
              }

            } catch (IndexOutOfBoundsException e) {
              Log.e(
                  TAG, "Trying to find out MIME type of a file without extension: " + storagePath);

            } catch (ActivityNotFoundException e) {
              Log.e(
                  TAG,
                  "No activity found to handle: "
                      + storagePath
                      + " with MIME type "
                      + mimeType
                      + " obtained from extension");

            } catch (Throwable th) {
              Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);

            } finally {
              if (toastIt) {
                Toast.makeText(
                        getActivity(),
                        "There is no application to handle file " + mFile.getFileName(),
                        Toast.LENGTH_SHORT)
                    .show();
              }
            }
          }
          break;
        }
      default:
        Log.e(TAG, "Incorrect view clicked!");
    }

    /* else if (v.getId() == R.id.fdShareBtn) {
        Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
        t.start();
    }*/
  }
示例#9
0
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.fdDownloadBtn:
        {
          Intent i = new Intent(getActivity(), FileDownloader.class);
          i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
          i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());
          i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());
          i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());

          // update ui
          setButtonsForTransferring();

          getActivity().startService(i);
          mContainerActivity
              .onFileStateChanged(); // this is not working; it is performed before the
          // fileDownloadService registers it as 'in progress'
          break;
        }
      case R.id.fdKeepInSync:
        {
          CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
          mFile.setKeepInSync(cb.isChecked());
          FileDataStorageManager fdsm =
              new FileDataStorageManager(
                  mAccount, getActivity().getApplicationContext().getContentResolver());
          fdsm.saveFile(mFile);
          if (mFile.keepInSync()) {
            onClick(getView().findViewById(R.id.fdDownloadBtn));
          } else {
            mContainerActivity
                .onFileStateChanged(); // put inside 'else' to not call it twice (here, and in the
            // virtual click on fdDownloadBtn)
          }
          Intent intent =
              new Intent(getActivity().getApplicationContext(), FileObserverService.class);
          intent.putExtra(
              FileObserverService.KEY_FILE_CMD,
              (cb.isChecked()
                  ? FileObserverService.CMD_ADD_OBSERVED_FILE
                  : FileObserverService.CMD_DEL_OBSERVED_FILE));
          intent.putExtra(FileObserverService.KEY_CMD_ARG, mFile.getStoragePath());
          getActivity().startService(intent);
          break;
        }
      case R.id.fdRenameBtn:
        {
          EditNameFragment dialog = EditNameFragment.newInstance(mFile.getFileName());
          dialog.show(getFragmentManager(), "nameeditdialog");
          dialog.setOnDismissListener(this);
          break;
        }
      case R.id.fdRemoveBtn:
        {
          ConfirmationDialogFragment confDialog =
              ConfirmationDialogFragment.newInstance(
                  R.string.confirmation_remove_alert,
                  new String[] {mFile.getFileName()},
                  mFile.isDown()
                      ? R.string.confirmation_remove_remote_and_local
                      : R.string.confirmation_remove_remote,
                  mFile.isDown() ? R.string.confirmation_remove_local : -1,
                  R.string.common_cancel);
          confDialog.setOnConfirmationListener(this);
          confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
          break;
        }
      case R.id.fdOpenBtn:
        {
          String storagePath = mFile.getStoragePath();
          String encodedStoragePath = WebdavUtils.encodePath(storagePath);
          try {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype());
            i.setFlags(
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            startActivity(i);

          } catch (Throwable t) {
            Log.e(
                TAG,
                "Fail when trying to open with the mimeType provided from the ownCloud server: "
                    + mFile.getMimetype());
            boolean toastIt = true;
            String mimeType = "";
            try {
              Intent i = new Intent(Intent.ACTION_VIEW);
              mimeType =
                  MimeTypeMap.getSingleton()
                      .getMimeTypeFromExtension(
                          storagePath.substring(storagePath.lastIndexOf('.') + 1));
              if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {
                i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType);
                i.setFlags(
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                startActivity(i);
                toastIt = false;
              }

            } catch (IndexOutOfBoundsException e) {
              Log.e(
                  TAG, "Trying to find out MIME type of a file without extension: " + storagePath);

            } catch (ActivityNotFoundException e) {
              Log.e(
                  TAG,
                  "No activity found to handle: "
                      + storagePath
                      + " with MIME type "
                      + mimeType
                      + " obtained from extension");

            } catch (Throwable th) {
              Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);

            } finally {
              if (toastIt) {
                Toast.makeText(
                        getActivity(),
                        "There is no application to handle file " + mFile.getFileName(),
                        Toast.LENGTH_SHORT)
                    .show();
              }
            }
          }
          break;
        }
      default:
        Log.e(TAG, "Incorrect view clicked!");
    }

    /* else if (v.getId() == R.id.fdShareBtn) {
        Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
        t.start();
    }*/
  }
示例#10
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    OCFile file = null;
    LayoutInflater inflator =
        (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (mFiles != null && mFiles.size() > position) {
      file = mFiles.get(position);
    }

    // Find out which layout should be displayed
    ViewType viewType;
    if (!mGridMode) {
      viewType = ViewType.LIST_ITEM;
    } else if (file.isImage()) {
      viewType = ViewType.GRID_IMAGE;
    } else {
      viewType = ViewType.GRID_ITEM;
    }

    // Create View
    switch (viewType) {
      case GRID_IMAGE:
        view = inflator.inflate(R.layout.grid_image, null);
        break;
      case GRID_ITEM:
        view = inflator.inflate(R.layout.grid_item, null);
        break;
      case LIST_ITEM:
        view = inflator.inflate(R.layout.list_item, null);
        break;
    }

    view.invalidate();

    if (file != null) {

      ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);

      fileIcon.setTag(file.getFileId());
      TextView fileName;
      String name = file.getFileName();

      LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ListItemLayout);
      linearLayout.setContentDescription("LinearLayout-" + name);

      switch (viewType) {
        case LIST_ITEM:
          TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
          TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
          ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);

          lastModV.setVisibility(View.VISIBLE);
          lastModV.setText(showRelativeTimestamp(file));

          checkBoxV.setVisibility(View.GONE);

          fileSizeV.setVisibility(View.VISIBLE);
          fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));

          if (!file.isFolder()) {
            AbsListView parentList = (AbsListView) parent;
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
              if (parentList.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) {
                checkBoxV.setVisibility(View.GONE);
              } else {
                if (parentList.isItemChecked(position)) {
                  checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
                } else {
                  checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
                }
                checkBoxV.setVisibility(View.VISIBLE);
              }
            }

          } else { // Folder
            fileSizeV.setVisibility(View.INVISIBLE);
          }

        case GRID_ITEM:
          // filename
          fileName = (TextView) view.findViewById(R.id.Filename);
          name = file.getFileName();
          fileName.setText(name);

        case GRID_IMAGE:
          // sharedIcon
          ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
          if (file.isShareByLink()) {
            sharedIconV.setVisibility(View.VISIBLE);
            sharedIconV.bringToFront();
          } else {
            sharedIconV.setVisibility(View.GONE);
          }

          // local state
          ImageView localStateView = (ImageView) view.findViewById(R.id.localFileIndicator);
          localStateView.bringToFront();
          FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
          FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
          boolean downloading =
              (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file));
          OperationsServiceBinder opsBinder = mTransferServiceGetter.getOperationsServiceBinder();
          downloading |=
              (opsBinder != null && opsBinder.isSynchronizing(mAccount, file.getRemotePath()));
          if (downloading) {
            localStateView.setImageResource(R.drawable.downloading_file_indicator);
            localStateView.setVisibility(View.VISIBLE);
          } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
            localStateView.setImageResource(R.drawable.uploading_file_indicator);
            localStateView.setVisibility(View.VISIBLE);
          } else if (file.isDown()) {
            localStateView.setImageResource(R.drawable.local_file_indicator);
            localStateView.setVisibility(View.VISIBLE);
          } else {
            localStateView.setVisibility(View.INVISIBLE);
          }

          // share with me icon
          if (!file.isFolder()) {
            ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
            sharedWithMeIconV.bringToFront();
            if (checkIfFileIsSharedWithMe(file)) {
              sharedWithMeIconV.setVisibility(View.VISIBLE);
            } else {
              sharedWithMeIconV.setVisibility(View.GONE);
            }
          }

          break;
      }

      // For all Views

      // this if-else is needed even though favorite icon is visible by default
      // because android reuses views in listview
      if (!file.keepInSync()) {
        view.findViewById(R.id.favoriteIcon).setVisibility(View.GONE);
      } else {
        view.findViewById(R.id.favoriteIcon).setVisibility(View.VISIBLE);
      }

      // No Folder
      if (!file.isFolder()) {
        if (file.isImage() && file.getRemoteId() != null) {
          // Thumbnail in Cache?
          Bitmap thumbnail =
              ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(file.getRemoteId()));
          if (thumbnail != null && !file.needsUpdateThumbnail()) {
            fileIcon.setImageBitmap(thumbnail);
          } else {
            // generate new Thumbnail
            if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
              final ThumbnailsCacheManager.ThumbnailGenerationTask task =
                  new ThumbnailsCacheManager.ThumbnailGenerationTask(
                      fileIcon, mStorageManager, mAccount);
              if (thumbnail == null) {
                thumbnail = ThumbnailsCacheManager.mDefaultImg;
              }
              final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
                  new ThumbnailsCacheManager.AsyncDrawable(
                      mContext.getResources(), thumbnail, task);
              fileIcon.setImageDrawable(asyncDrawable);
              task.execute(file);
            }
          }
        } else {
          fileIcon.setImageResource(
              DisplayUtils.getFileTypeIconId(file.getMimetype(), file.getFileName()));
        }

      } else {
        // Folder
        if (checkIfFileIsSharedWithMe(file)) {
          fileIcon.setImageResource(R.drawable.shared_with_me_folder);
        } else if (file.isShareByLink()) {
          // If folder is sharedByLink, icon folder must be changed to
          // folder-public one
          fileIcon.setImageResource(R.drawable.folder_public);
        } else {
          fileIcon.setImageResource(
              DisplayUtils.getFileTypeIconId(file.getMimetype(), file.getFileName()));
        }
      }
    }

    return view;
  }
示例#11
0
  /** Opens mFile. */
  private void openFile() {

    String storagePath = mFile.getStoragePath();
    String encodedStoragePath = WebdavUtils.encodePath(storagePath);
    try {
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype());
      i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      startActivity(i);

    } catch (Throwable t) {
      Log.e(
          TAG,
          "Fail when trying to open with the mimeType provided from the ownCloud server: "
              + mFile.getMimetype());
      boolean toastIt = true;
      String mimeType = "";
      try {
        Intent i = new Intent(Intent.ACTION_VIEW);
        mimeType =
            MimeTypeMap.getSingleton()
                .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
        if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {
          if (mimeType != null) {
            i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType);
          } else {
            // desperate try
            i.setDataAndType(Uri.parse("file://" + encodedStoragePath), "*/*");
          }
          i.setFlags(
              Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
          startActivity(i);
          toastIt = false;
        }

      } catch (IndexOutOfBoundsException e) {
        Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);

      } catch (ActivityNotFoundException e) {
        Log.e(
            TAG,
            "No activity found to handle: "
                + storagePath
                + " with MIME type "
                + mimeType
                + " obtained from extension");

      } catch (Throwable th) {
        Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);

      } finally {
        if (toastIt) {
          Toast.makeText(
                  getActivity(),
                  "There is no application to handle file " + mFile.getFileName(),
                  Toast.LENGTH_SHORT)
              .show();
        }
      }
    }
  }
示例#12
0
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.fdDownloadBtn:
        {
          FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
          FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
          if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
            downloaderBinder.cancel(mAccount, mFile);
            if (mFile.isDown()) {
              setButtonsForDown();
            } else {
              setButtonsForRemote();
            }

          } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) {
            uploaderBinder.cancel(mAccount, mFile);
            if (!mFile.fileExists()) {
              // TODO make something better
              if (getActivity() instanceof FileDisplayActivity) {
                // double pane
                FragmentTransaction transaction =
                    getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(
                    R.id.file_details_container,
                    new FileDetailFragment(null, null),
                    FTAG); // empty FileDetailFragment
                transaction.commit();
                mContainerActivity.onFileStateChanged();
              } else {
                getActivity().finish();
              }

            } else if (mFile.isDown()) {
              setButtonsForDown();
            } else {
              setButtonsForRemote();
            }

          } else {
            mLastRemoteOperation =
                new SynchronizeFileOperation(
                    mFile, null, mStorageManager, mAccount, true, false, getActivity());
            WebdavClient wc =
                OwnCloudClientUtils.createOwnCloudClient(
                    mAccount, getSherlockActivity().getApplicationContext());
            mLastRemoteOperation.execute(wc, this, mHandler);

            // update ui
            boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
            getActivity()
                .showDialog(
                    (inDisplayActivity)
                        ? FileDisplayActivity.DIALOG_SHORT_WAIT
                        : FileDetailActivity.DIALOG_SHORT_WAIT);
          }
          break;
        }
      case R.id.fdKeepInSync:
        {
          CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
          mFile.setKeepInSync(cb.isChecked());
          mStorageManager.saveFile(mFile);

          /// register the OCFile instance in the observer service to monitor local updates;
          /// if necessary, the file is download
          Intent intent =
              new Intent(getActivity().getApplicationContext(), FileObserverService.class);
          intent.putExtra(
              FileObserverService.KEY_FILE_CMD,
              (cb.isChecked()
                  ? FileObserverService.CMD_ADD_OBSERVED_FILE
                  : FileObserverService.CMD_DEL_OBSERVED_FILE));
          intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile);
          intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount);
          getActivity().startService(intent);

          if (mFile.keepInSync()) {
            onClick(
                getView().findViewById(R.id.fdDownloadBtn)); // force an immediate synchronization
          }
          break;
        }
      case R.id.fdRenameBtn:
        {
          EditNameDialog dialog =
              EditNameDialog.newInstance(
                  getString(R.string.rename_dialog_title), mFile.getFileName(), this);
          dialog.show(getFragmentManager(), "nameeditdialog");
          break;
        }
      case R.id.fdRemoveBtn:
        {
          ConfirmationDialogFragment confDialog =
              ConfirmationDialogFragment.newInstance(
                  R.string.confirmation_remove_alert,
                  new String[] {mFile.getFileName()},
                  mFile.isDown()
                      ? R.string.confirmation_remove_remote_and_local
                      : R.string.confirmation_remove_remote,
                  mFile.isDown() ? R.string.confirmation_remove_local : -1,
                  R.string.common_cancel);
          confDialog.setOnConfirmationListener(this);
          confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
          break;
        }
      case R.id.fdOpenBtn:
        {
          openFile();
          break;
        }
      case R.id.fdShareBtn:
        {;
          break;
        }
      default:
        Log.e(TAG, "Incorrect view clicked!");
    }

    /* else if (v.getId() == R.id.fdShareBtn) {
        Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
        t.start();
    }*/
  }