2017년 7월 8일 토요일

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

--
Update: My solution is dependent on another application package and I realized it might not be the correct approach. Hence would suggest to define your own Activity which will handle copy and paste functionality using ClipboardManager, as suggested in another answer here.
Original Answer
Clipboard activity details:
⦁ Package name : com.google.android.apps.docs
⦁ Activity name 

: com.google.android.apps.docs.app.SendTextToClipboardActivity

Following Intent code will start and execute the clipboard activity.


Intent i = new Intent();
i.setComponent(new ComponentName("com.google.android.apps.docs", "com.google.android.apps.docs.app.SendTextToClipboardActivity"));
i.setAction(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "text to copy to clipboard");
startActivity(i);

You can add appropriate if clause while adding this intent to the intentList.

--
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.

--

댓글 없음:

댓글 쓰기