private void deleteItem(final int position) {
    final ContentListItem listItem = contentListItems.getItem(position);
    final ContentItem content = listItem.getContentItem();
    final ProgressDialog dialog =
        getProgressDialog(
            R.string.user_files_browser_progress_dialog_message_delete_item, content.getFilePath());

    userFileManager.deleteRemoteContent(
        content.getFilePath(),
        new UserFileManager.ResponseHandler() {
          @Override
          public void onSuccess() {
            dialog.dismiss();
            // Remove from the list if not cached locally.
            if (!ContentState.isCached(content.getContentState())) {
              contentListItems.remove(listItem);
              contentListItems.notifyDataSetChanged();
            } else {
              content.setContentState(ContentState.CACHED);
            }
          }

          @Override
          public void onError(final AmazonClientException ex) {
            dialog.dismiss();
            showError(R.string.user_files_browser_error_message_delete_item, ex.getMessage());
          }
        });
  }
 @Override
 public void onItemClick(
     final AdapterView<?> parent, final View view, final int position, final long id) {
   final ContentItem content = contentListItems.getItem(position).getContentItem();
   if (content.getContentState().equals(ContentState.REMOTE_DIRECTORY)) {
     changeDirectory(content.getFilePath());
   } else {
     listView.showContextMenuForChild(view);
   }
 }
  @Override
  public void onCreateContextMenu(
      final ContextMenu menu, final View view, final ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
    if (view == cacheLimitTextView) {
      for (final SpannableString string : ContentUtils.cacheSizeStrings) {
        menu.add(string).setActionView(view);
      }
      menu.setHeaderTitle(ContentUtils.getCenteredString(setCacheSizeText));
    } else if (view.getId() == listView.getId()) {
      final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
      final ContentItem content = contentListItems.getItem(info.position).getContentItem();

      if (ContentState.REMOTE_DIRECTORY.equals(content.getContentState())) {
        menu.add(R.string.user_files_browser_context_menu_delete).setActionView(view);
      } else {
        final ContentState contentState = content.getContentState();
        final boolean isNewerVersionAvailable =
            ContentState.isCachedWithNewerVersionAvailableOrTransferring(contentState);
        final boolean isCached = contentState == ContentState.CACHED || isNewerVersionAvailable;
        final boolean isPinned = userFileManager.isContentPinned(content.getFilePath());

        // if item is downloaded
        if (isCached) {
          menu.add(getString(R.string.content_context_menu_open)).setActionView(view);
        } else {
          menu.add(R.string.content_context_menu_open_remote).setActionView(view);
          menu.add(getString(R.string.content_context_menu_download)).setActionView(view);
        }
        if (isNewerVersionAvailable) {
          menu.add(getString(R.string.content_context_menu_download_latest)).setActionView(view);
        }
        if (isCached && !isPinned) {
          menu.add(getString(R.string.content_context_menu_pin)).setActionView(view);
        }
        if (!isCached) {
          menu.add(getString(R.string.content_context_menu_download_pin)).setActionView(view);
        }
        if (isPinned) {
          menu.add(getString(R.string.content_context_menu_unpin)).setActionView(view);
        }
        if (isCached) {
          menu.add(getString(R.string.content_context_menu_delete_local)).setActionView(view);
        }
        menu.add(R.string.user_files_browser_context_menu_delete).setActionView(view);
      }
      menu.setHeaderTitle(content.getFilePath());
    }
  }
  private void refreshContent(final String newCurrentPath) {
    if (!listingContentInProgress) {
      listingContentInProgress = true;

      refreshCacheSummary();

      // Remove all progress listeners.
      userFileManager.clearProgressListeners();

      // Clear old content.
      contentListItems.clear();
      contentListItems.notifyDataSetChanged();

      currentPath = newCurrentPath;
      updatePath();

      final ProgressDialog dialog =
          getProgressDialog(R.string.content_progress_dialog_message_load_content);

      userFileManager.listAvailableContent(
          newCurrentPath,
          new ContentListHandler() {
            @Override
            public boolean onContentReceived(
                final int startIndex,
                final List<ContentItem> partialResults,
                final boolean hasMoreResults) {
              // if the activity is no longer alive, we can stop immediately.
              if (getActivity() == null) {
                listingContentInProgress = false;
                return false;
              }
              if (startIndex == 0) {
                dialog.dismiss();
              }

              for (final ContentItem contentItem : partialResults) {
                // Add the item to the list.
                contentListItems.add(new ContentListItem(contentItem));

                // If the content is transferring, ensure the progress listener is set.
                final ContentState contentState = contentItem.getContentState();
                if (ContentState.isTransferringOrWaitingToTransfer(contentState)) {
                  userFileManager.setProgressListener(contentItem.getFilePath(), contentListItems);
                }
              }
              contentListItems.sort(ContentListItem.contentAlphebeticalComparator);

              if (!hasMoreResults) {
                listingContentInProgress = false;
              }
              // Return true to continue listing.
              return true;
            }

            @Override
            public void onError(final Exception ex) {
              dialog.dismiss();
              listingContentInProgress = false;
              final Activity activity = getActivity();
              if (activity != null) {
                final AlertDialog.Builder errorDialogBuilder = new AlertDialog.Builder(activity);
                errorDialogBuilder.setTitle(activity.getString(R.string.content_list_failure_text));
                errorDialogBuilder.setMessage(ex.getMessage());
                errorDialogBuilder.setNegativeButton(
                    activity.getString(R.string.content_dialog_ok), null);
                errorDialogBuilder.show();
              }
            }
          });
    }
  }
  @Override
  public boolean onContextItemSelected(final MenuItem item) {
    if (item.getActionView() == cacheLimitTextView) {
      final SpannableString itemTitle = (SpannableString) item.getTitle();
      final int cacheSizeIndex = ContentUtils.cacheSizeStrings.indexOf(itemTitle);
      if (cacheSizeIndex > -1) {
        userFileManager.setContentCacheSize(ContentUtils.cacheSizeValues[cacheSizeIndex]);
        refreshCacheSummary();
      }
      return true;
    } else if (item.getActionView() == listView) {
      final CharSequence itemTitle = item.getTitle();
      final AdapterView.AdapterContextMenuInfo info =
          (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
      final ContentItem content = contentListItems.getItem(info.position).getContentItem();

      if (itemTitle.equals(getString(R.string.content_context_menu_open))) {
        ContentHelper.openFileViewer(getActivity(), content.getFile());
      } else if (itemTitle.equals(getString(R.string.content_context_menu_open_remote))) {
        // Open the object in a browser via a presigned URL.
        final AmazonS3 s3 =
            new AmazonS3Client(
                AWSMobileClient.defaultMobileClient()
                    .getIdentityManager()
                    .getCredentialsProvider());

        final URL presignedUrl =
            s3.generatePresignedUrl(
                bucket,
                prefix + content.getFilePath(),
                new Date(new Date().getTime() + 60 * 60 * 1000));
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(presignedUrl.toString()));
        startActivity(intent);
      } else if (itemTitle.equals(getString(R.string.content_context_menu_download))) {
        // Download the content.
        userFileManager.getContent(
            content.getFilePath(),
            content.getSize(),
            ContentDownloadPolicy.DOWNLOAD_ALWAYS,
            false,
            contentListItems);
      } else if (itemTitle.equals(getString(R.string.content_context_menu_download_latest))) {
        // Download the content.
        userFileManager.getContent(
            content.getFilePath(),
            content.getSize(),
            ContentDownloadPolicy.DOWNLOAD_IF_NEWER_EXIST,
            false,
            contentListItems);
      } else if (itemTitle.equals(getString(R.string.content_context_menu_pin))) {
        userFileManager.pinContent(content.getFilePath(), contentListItems);
      } else if (itemTitle.equals(getString(R.string.content_context_menu_download_pin))) {
        userFileManager.getContent(
            content.getFilePath(),
            content.getSize(),
            ContentDownloadPolicy.DOWNLOAD_ALWAYS,
            true,
            contentListItems);
      } else if (itemTitle.equals(getString(R.string.content_context_menu_unpin))) {
        userFileManager.unPinContent(
            content.getFilePath(),
            new Runnable() {
              @Override
              public void run() {
                refreshCacheSummary();
                contentListItems.notifyDataSetChanged();
              }
            });
      } else if (itemTitle.equals(getString(R.string.content_context_menu_delete_local))) {
        userFileManager.removeLocal(content.getFilePath());
      } else if (itemTitle.equals(getString(R.string.user_files_browser_context_menu_delete))) {
        deleteItem(info.position);
      }
      return true;
    }

    return super.onContextItemSelected(item);
  }