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