Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add "copy to clipboard" to custom IntentChooser?

According to this solution, I would like to add "copy to clipboard" action in custom share dialog - the same as in the default action share provider.

enter image description here

What I have tried was adding to if clausule statement, word packageName.contains("clipboard") but without success.

String packageName = ri.activityInfo.packageName;
    if(packageName.contains("android.email")) {
        emailIntent.setPackage(packageName);
    } else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        if(packageName.contains("twitter")) {
            intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
        } else if(packageName.contains("facebook")) {
            intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
        } else if(packageName.contains("mms")) {
            intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
        } else if(packageName.contains("android.gm")) {
            intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
            intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));               
            intent.setType("message/rfc822");
        }

        intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
    }

The whole code is used from https://stackoverflow.com/a/18068122/619673 .

adb shell pm list packages returned me list of packagenames but without this phrase.

Can I somehow get packagename of clipboard to add it to my custom list of shared providers?

Here is an example with that "copy to clipboard" option:

enter image description here

like image 535
deadfish Avatar asked Dec 01 '22 16:12

deadfish


1 Answers

The trick is that there is actually no built-in package for the clipboard (some apps provide the Copy to Clipboard option system-wide by creating such a package with the appropriate intent-filter).

However, since you're creating the chooser's options manually, you can add your own intent to handle the copy to clipboard operation. For example, like this:

... create the intentList, as before ...

// Add a custom intent to handle the "copy to clipboard" option.
Intent copyToClipboard = new Intent(this, ShareToClipboardActivity.class);
copyToClipboard.putExtra(Intent.EXTRA_TEXT, "text to copy to clipboard");

// Wrap it with a LabeledIntent and add it to the list of choosable ones.
LabeledIntent labeledCopyToClipboard = new LabeledIntent(copyToClipboard, getPackageName(), "Copy!", 0);
intentList.add(labeledCopyToClipboard);

... convert intentList to array and show chooser, as before ...

Where ShareToClipboardActivity is your own activity, which does (at least) this:

public class ShareToClipboardActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        CharSequence text = getIntent().getCharSequenceExtra(Intent.EXTRA_TEXT);
        ClipboardManager clipboardManager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text));
        finish();
    }
}

Note that this is a bare-bones example: you would probably want drawable and string resources for the LabeledIntent, as well as possibly showing a Toast message in ShareToClipboardActivity, use the old ClipboardManager if targeting pre-API 11, &c.

like image 129
matiash Avatar answered Dec 04 '22 04:12

matiash