コード例 #1
0
        @Override
        public void onClick(DialogInterface dialog, int which) {

          switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
              // Delete
              AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              builder.setIcon(android.R.drawable.ic_dialog_alert);
              builder.setTitle("Delete File");
              builder.setMessage("Are you sure?");
              builder.setPositiveButton("Yes", dialogClickListener_YesOrNo);
              builder.setNegativeButton("No", dialogClickListener_YesOrNo).show();
              break;
            case DialogInterface.BUTTON_NEGATIVE:
              // Copy
              builder = null;
              ClipboardManager clipboard =
                  (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
              File file =
                  new File(clickedFile.getFolder() + File.separator + clickedFile.getName());
              Uri uri = Uri.fromFile(file);
              ClipData clip = ClipData.newUri(context.getContentResolver(), "URI", uri);
              clipboard.setPrimaryClip(clip);
              Toast.makeText(
                      context.getApplicationContext(),
                      "File copied to clipboard",
                      Toast.LENGTH_SHORT)
                  .show();
              break;
            case DialogInterface.BUTTON_NEUTRAL:
              // Exclude File
              Log.i(TAG, clickedFile.getFolder() + " added to excludedHogFiles");
              switch (settings.getSelectedSearchDirectory()) {
                case Settings.EXTERNAL_DIRECTORY:
                  if (isBiggestFiles) {
                    settings.getBiggestExternalExcludedHogFiles().add(clickedFile);
                    List<FileInformation> updatedHogFiles =
                        removeExcludedFromHogFiles(settings.getBiggestExternalExcludedHogFiles());
                    fileInformationAdapter.setFileInformations(updatedHogFiles);
                  } else {
                    settings.getSmallestExternalExcludedHogFiles().add(clickedFile);
                    List<FileInformation> updatedHogFiles =
                        removeExcludedFromHogFiles(settings.getSmallestExternalExcludedHogFiles());
                    fileInformationAdapter.setFileInformations(updatedHogFiles);
                  }
                  break;
                case Settings.ROOT_DIRECTORY:
                  if (isBiggestFiles) {
                    settings.getBiggestRootExcludedHogFiles().add(clickedFile);
                    List<FileInformation> updatedHogFiles =
                        removeExcludedFromHogFiles(settings.getBiggestRootExcludedHogFiles());
                    fileInformationAdapter.setFileInformations(updatedHogFiles);
                  } else {
                    settings.getSmallestRootExcludedHogFiles().add(clickedFile);
                    List<FileInformation> updatedHogFiles =
                        removeExcludedFromHogFiles(settings.getSmallestRootExcludedHogFiles());
                    fileInformationAdapter.setFileInformations(updatedHogFiles);
                  }
                  break;
              }
              break;
          }
        }
コード例 #2
0
  /**
   * This method is called when the user selects an item from the context menu (see
   * onCreateContextMenu()). The only menu items that are actually handled are DELETE and COPY.
   * Anything else is an alternative option, for which default handling should be done.
   *
   * @param item The selected menu item
   * @return True if the menu item was DELETE, and no default processing is need, otherwise false,
   *     which triggers the default handling of the item.
   * @throws ClassCastException
   */
  @Override
  public boolean OnContextItemSelected(MenuItem item) throws Throwable {
    // The data from the menu item.
    AdapterView.AdapterContextMenuInfo info;

    /*
     * Gets the extra info from the menu item. When an note in the Notes list is long-pressed, a
     * context menu appears. The menu items for the menu automatically get the data
     * associated with the note that was long-pressed. The data comes from the provider that
     * backs the list.
     *
     * The note's data is passed to the context menu creation routine in a ContextMenuInfo
     * object.
     *
     * When one of the context menu items is clicked, the same data is passed, along with the
     * note ID, to onContextItemSelected() via the item parameter.
     */
    try {
      // Casts the data object in the item into the type for AdapterView objects.
      info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {

      // If the object can't be cast, logs an error
      Log.e(TAG, "bad menuInfo", e);

      // Triggers default processing of the menu item.
      return false;
    }
    // Appends the selected note's ID to the URI sent with the incoming Intent.
    Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);

    /*
     * Gets the menu item's ID and compares it to known actions.
     */
    switch (item.getItemId()) {
      case R.id.context_open:
        // Launch activity to view/edit the currently selected item
        StartActivity(new Intent(Intent.ACTION_EDIT, noteUri));
        return true;

      case R.id.context_copy:
        // Gets a handle to the clipboard service.
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

        // Copies the notes URI to the clipboard. In effect, this copies the note itself
        clipboard.setPrimaryClip(
            ClipData.newUri( // new clipboard item holding a URI
                getContentResolver(), // resolver to retrieve URI info
                "Note", // label for the clip
                noteUri) // the URI
            );

        // Returns to the caller and skips further processing.
        return true;

      case R.id.context_delete:

        // Deletes the note from the provider by passing in a URI in note ID format.
        // Please see the introductory note about performing provider operations on the
        // UI thread.
        getContentResolver()
            .delete(
                noteUri, // The URI of the provider
                null, // No where clause is needed, since only a single note ID is being
                // passed in.
                null // No where clause is used, so no where arguments are needed.
                );

        // Returns to the caller and skips further processing.
        return true;
      default:
        return super.OnContextItemSelected(item); // Issue: handling return values.
    }
  }
コード例 #3
0
 /**
  * 复制uri到剪贴板
  *
  * @param context 上下文
  * @param uri uri
  */
 public static void copyUri(Context context, Uri uri) {
   ClipboardManager clipboard =
       (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
   clipboard.setPrimaryClip(ClipData.newUri(context.getContentResolver(), "uri", uri));
 }