1c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinpackage com.android.ex.chips;
2c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
3c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.app.AlertDialog;
4c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.app.Dialog;
5c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.app.DialogFragment;
6c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.content.ClipData;
7c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.content.ClipboardManager;
8c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.content.Context;
9c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.content.DialogInterface;
10c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinimport android.os.Bundle;
11c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
12c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein/**
13c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein * Simple dialog fragment for copying the contents of a chip.
14c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein */
15c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sappersteinpublic class CopyDialog extends DialogFragment implements DialogInterface.OnClickListener {
16c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
17c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    public static final String TAG = "chips-copy-dialog";
18c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
19c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    private static final String ARG_TEXT = "text";
20c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
21c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    private String mText;
22c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
23c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    public static CopyDialog newInstance(String text) {
24c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        final CopyDialog fragment = new CopyDialog();
25c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        final Bundle args = new Bundle(1);
26c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        args.putString(ARG_TEXT, text);
27c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        fragment.setArguments(args);
28c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        return fragment;
29c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    }
30c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
31c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    @Override
32c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    public Dialog onCreateDialog(Bundle savedInstanceState) {
33c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        final Bundle args = getArguments();
34c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        mText = args.getString(ARG_TEXT);
35c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
36c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        return new AlertDialog.Builder(getActivity())
37c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein                .setMessage(mText)
38c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein                .setPositiveButton(R.string.chips_action_copy, this)
39c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein                .setNegativeButton(R.string.chips_action_cancel, null)
40c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein                .create();
41c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    }
42c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein
43c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    @Override
44c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    public void onClick(DialogInterface dialog, int which) {
45c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        if (which == DialogInterface.BUTTON_POSITIVE) {
46c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein            final ClipboardManager clipboard = (ClipboardManager)
47c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein                    getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
48c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein            clipboard.setPrimaryClip(ClipData.newPlainText(null, mText));
49c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein        }
50c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein    }
51c6e314b5a20a234a8207ef4cd67e2f042579a087Andrew Sapperstein}
52