Example usage for android.text ClipboardManager getText

List of usage examples for android.text ClipboardManager getText

Introduction

In this page you can find the example usage for android.text ClipboardManager getText.

Prototype

public abstract CharSequence getText();

Source Link

Document

Returns the text on the clipboard.

Usage

From source file:com.codebutler.farebot.activities.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    try {/* ww  w.j  a  v a  2 s.  co m*/
        if (item.getItemId() == R.id.supported_cards) {
            startActivity(new Intent(this, SupportedCardsActivity.class));
            return true;

        } else if (item.getItemId() == R.id.about) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://codebutler.github.com/farebot")));
            return true;

        } else if (item.getItemId() == R.id.import_clipboard) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            onCardsImported(ExportHelper.importCardsXml(this, clipboard.getText().toString()));
            return true;

        } else if (item.getItemId() == R.id.import_file) {
            Uri uri = Uri.fromFile(Environment.getExternalStorageDirectory());
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.putExtra(Intent.EXTRA_STREAM, uri);
            i.setType("application/xml");
            startActivityForResult(Intent.createChooser(i, "Select File"), SELECT_FILE);
            return true;

        } else if (item.getItemId() == R.id.import_sd) {
            String xml = FileUtils.readFileToString(new File(SD_EXPORT_PATH));
            onCardsImported(ExportHelper.importCardsXml(this, xml));
            return true;

        } else if (item.getItemId() == R.id.copy_xml) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(ExportHelper.exportCardsXml(this));
            Toast.makeText(this, "Copied to clipboard.", 5).show();
            return true;

        } else if (item.getItemId() == R.id.share_xml) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, ExportHelper.exportCardsXml(this));
            startActivity(intent);
            return true;

        } else if (item.getItemId() == R.id.save_xml) {
            String xml = ExportHelper.exportCardsXml(this);
            File file = new File(SD_EXPORT_PATH);
            FileUtils.writeStringToFile(file, xml, "UTF-8");
            Toast.makeText(this, "Wrote FareBot-Export.xml to USB Storage.", 5).show();
            return true;

        } else if (item.getItemId() == R.id.prefs) {
            startActivity(new Intent(this, FareBotPreferenceActivity.class));
        }
    } catch (Exception ex) {
        Utils.showError(this, ex);
    }
    return false;
}

From source file:com.codebutler.farebot.fragments.CardsFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    try {/* w  w w.  j a  v a  2 s  .c o  m*/
        if (item.getItemId() == R.id.import_clipboard) {
            @SuppressWarnings("deprecation")
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                    .getSystemService(Activity.CLIPBOARD_SERVICE);
            onCardsImported(ExportHelper.importCardsXml(getActivity(), clipboard.getText().toString()));
            return true;

        } else if (item.getItemId() == R.id.import_file) {
            Uri uri = Uri.fromFile(Environment.getExternalStorageDirectory());
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.putExtra(Intent.EXTRA_STREAM, uri);
            i.setType("application/xml");
            startActivityForResult(Intent.createChooser(i, "Select File"), REQUEST_SELECT_FILE);
            return true;

        } else if (item.getItemId() == R.id.import_sd) {
            String xml = FileUtils.readFileToString(new File(SD_EXPORT_PATH));
            onCardsImported(ExportHelper.importCardsXml(getActivity(), xml));
            return true;

        } else if (item.getItemId() == R.id.copy_xml) {
            @SuppressWarnings("deprecation")
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                    .getSystemService(Activity.CLIPBOARD_SERVICE);
            clipboard.setText(ExportHelper.exportCardsXml(getActivity()));
            Toast.makeText(getActivity(), "Copied to clipboard.", 5).show();
            return true;

        } else if (item.getItemId() == R.id.share_xml) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, ExportHelper.exportCardsXml(getActivity()));
            startActivity(intent);
            return true;

        } else if (item.getItemId() == R.id.save_xml) {
            String xml = ExportHelper.exportCardsXml(getActivity());
            File file = new File(SD_EXPORT_PATH);
            FileUtils.writeStringToFile(file, xml, "UTF-8");
            Toast.makeText(getActivity(), "Wrote FareBot-Export.xml to USB Storage.", 5).show();
            return true;
        }
    } catch (Exception ex) {
        Utils.showError(getActivity(), ex);
    }
    return false;
}

From source file:piuk.blockchain.android.ui.WalletAddressesActivity.java

private void handlePasteClipboard() {
    final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

    if (clipboardManager.hasText()) {
        final String text = clipboardManager.getText().toString().trim();

        try {/*  w  ww.j av a 2  s  .c o m*/
            final Address address = new Address(Constants.NETWORK_PARAMETERS, text);
            EditAddressBookEntryFragment.edit(getSupportFragmentManager(), address.toString());
        } catch (final AddressFormatException x) {
            toast(R.string.send_coins_parse_address_error_msg);
        }
    } else {
        toast(R.string.address_book_msg_clipboard_empty);
    }
}

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * Get clipboard contents, very failsafe and compat to older android versions
 *///  ww w  .j  a  va 2 s .  c  om
public List<String> getClipboard() {
    List<String> clipper = new ArrayList<>();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager cm = ((android.text.ClipboardManager) _context
                .getSystemService(Context.CLIPBOARD_SERVICE));
        if (cm != null && !TextUtils.isEmpty(cm.getText())) {
            clipper.add(cm.getText().toString());
        }
    } else {
        android.content.ClipboardManager cm = ((android.content.ClipboardManager) _context
                .getSystemService(Context.CLIPBOARD_SERVICE));
        if (cm != null && cm.hasPrimaryClip()) {
            ClipData data = cm.getPrimaryClip();
            for (int i = 0; data != null && i < data.getItemCount() && i < data.getItemCount(); i++) {
                ClipData.Item item = data.getItemAt(i);
                if (item != null && !TextUtils.isEmpty(item.getText())) {
                    clipper.add(data.getItemAt(i).getText().toString());
                }
            }
        }
    }
    return clipper;
}

From source file:com.codegarden.nativenavigation.JuceActivity.java

public final String getClipboardContent() {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    return clipboard.getText().toString();
}

From source file:net.momodalo.app.vimtouch.VimTouch.java

private void doPaste() {
    ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    CharSequence paste = clip.getText();
    mSession.write(paste.toString());/*  w w w.  j a v  a2  s . co  m*/
}

From source file:com.juce.JuceAppActivity.java

public final String getClipboardContent()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
    return clipboard.getText().toString();
}

From source file:org.petero.droidfish.activities.EditBoard.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case EDIT_DIALOG: {
        final int SIDE_TO_MOVE = 0;
        final int CLEAR_BOARD = 1;
        final int INITIAL_POS = 2;
        final int CASTLING_FLAGS = 3;
        final int EN_PASSANT_FILE = 4;
        final int MOVE_COUNTERS = 5;
        final int COPY_POSITION = 6;
        final int PASTE_POSITION = 7;
        final int GET_FEN = 8;

        List<CharSequence> lst = new ArrayList<CharSequence>();
        List<Integer> actions = new ArrayList<Integer>();
        lst.add(getString(R.string.side_to_move));
        actions.add(SIDE_TO_MOVE);/*from   ww w  .ja v a 2s. c  om*/
        lst.add(getString(R.string.clear_board));
        actions.add(CLEAR_BOARD);
        lst.add(getString(R.string.initial_position));
        actions.add(INITIAL_POS);
        lst.add(getString(R.string.castling_flags));
        actions.add(CASTLING_FLAGS);
        lst.add(getString(R.string.en_passant_file));
        actions.add(EN_PASSANT_FILE);
        lst.add(getString(R.string.move_counters));
        actions.add(MOVE_COUNTERS);
        lst.add(getString(R.string.copy_position));
        actions.add(COPY_POSITION);
        lst.add(getString(R.string.paste_position));
        actions.add(PASTE_POSITION);
        if (DroidFish.hasFenProvider(getPackageManager())) {
            lst.add(getString(R.string.get_fen));
            actions.add(GET_FEN);
        }
        final List<Integer> finalActions = actions;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.edit_board);
        builder.setItems(lst.toArray(new CharSequence[lst.size()]), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                switch (finalActions.get(item)) {
                case SIDE_TO_MOVE:
                    showDialog(SIDE_DIALOG);
                    setSelection(-1);
                    checkValidAndUpdateMaterialDiff();
                    break;
                case CLEAR_BOARD: {
                    Position pos = new Position();
                    cb.setPosition(pos);
                    setSelection(-1);
                    checkValidAndUpdateMaterialDiff();
                    break;
                }
                case INITIAL_POS: {
                    try {
                        Position pos = TextIO.readFEN(TextIO.startPosFEN);
                        cb.setPosition(pos);
                        setSelection(-1);
                        checkValidAndUpdateMaterialDiff();
                    } catch (ChessParseError e) {
                    }
                    break;
                }
                case CASTLING_FLAGS:
                    removeDialog(CASTLE_DIALOG);
                    showDialog(CASTLE_DIALOG);
                    setSelection(-1);
                    checkValidAndUpdateMaterialDiff();
                    break;
                case EN_PASSANT_FILE:
                    removeDialog(EP_DIALOG);
                    showDialog(EP_DIALOG);
                    setSelection(-1);
                    checkValidAndUpdateMaterialDiff();
                    break;
                case MOVE_COUNTERS:
                    removeDialog(MOVCNT_DIALOG);
                    showDialog(MOVCNT_DIALOG);
                    setSelection(-1);
                    checkValidAndUpdateMaterialDiff();
                    break;
                case COPY_POSITION: {
                    setPosFields();
                    String fen = TextIO.toFEN(cb.pos) + "\n";
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    clipboard.setText(fen);
                    setSelection(-1);
                    break;
                }
                case PASTE_POSITION: {
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    if (clipboard.hasText()) {
                        String fen = clipboard.getText().toString();
                        setFEN(fen);
                    }
                    break;
                }
                case GET_FEN:
                    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                    i.setType("application/x-chess-fen");
                    try {
                        startActivityForResult(i, RESULT_GET_FEN);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    case SIDE_DIALOG: {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.select_side_to_move_first);
        final int selectedItem = (cb.pos.whiteMove) ? 0 : 1;
        builder.setSingleChoiceItems(new String[] { getString(R.string.white), getString(R.string.black) },
                selectedItem, new Dialog.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (id == 0) { // white to move
                            cb.pos.setWhiteMove(true);
                            checkValidAndUpdateMaterialDiff();
                            dialog.cancel();
                        } else {
                            cb.pos.setWhiteMove(false);
                            checkValidAndUpdateMaterialDiff();
                            dialog.cancel();
                        }
                    }
                });
        AlertDialog alert = builder.create();
        return alert;
    }
    case CASTLE_DIALOG: {
        final CharSequence[] items = { getString(R.string.white_king_castle),
                getString(R.string.white_queen_castle), getString(R.string.black_king_castle),
                getString(R.string.black_queen_castle) };
        boolean[] checkedItems = { cb.pos.h1Castle(), cb.pos.a1Castle(), cb.pos.h8Castle(), cb.pos.a8Castle() };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.castling_flags);
        builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Position pos = new Position(cb.pos);
                boolean a1Castle = pos.a1Castle();
                boolean h1Castle = pos.h1Castle();
                boolean a8Castle = pos.a8Castle();
                boolean h8Castle = pos.h8Castle();
                switch (which) {
                case 0:
                    h1Castle = isChecked;
                    break;
                case 1:
                    a1Castle = isChecked;
                    break;
                case 2:
                    h8Castle = isChecked;
                    break;
                case 3:
                    a8Castle = isChecked;
                    break;
                }
                int castleMask = 0;
                if (a1Castle)
                    castleMask |= 1 << Position.A1_CASTLE;
                if (h1Castle)
                    castleMask |= 1 << Position.H1_CASTLE;
                if (a8Castle)
                    castleMask |= 1 << Position.A8_CASTLE;
                if (h8Castle)
                    castleMask |= 1 << Position.H8_CASTLE;
                pos.setCastleMask(castleMask);
                cb.setPosition(pos);
                checkValidAndUpdateMaterialDiff();
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    case EP_DIALOG: {
        final CharSequence[] items = { "A", "B", "C", "D", "E", "F", "G", "H", getString(R.string.none) };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.select_en_passant_file);
        builder.setSingleChoiceItems(items, getEPFile(), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                setEPFile(item);
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        return alert;
    }
    case MOVCNT_DIALOG: {
        View content = View.inflate(this, R.layout.edit_move_counters, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setView(content);
        builder.setTitle(R.string.edit_move_counters);
        final EditText halfMoveClock = (EditText) content.findViewById(R.id.ed_cnt_halfmove);
        final EditText fullMoveCounter = (EditText) content.findViewById(R.id.ed_cnt_fullmove);
        halfMoveClock.setText(String.format(Locale.US, "%d", cb.pos.halfMoveClock));
        fullMoveCounter.setText(String.format(Locale.US, "%d", cb.pos.fullMoveCounter));
        final Runnable setCounters = new Runnable() {
            public void run() {
                try {
                    int halfClock = Integer.parseInt(halfMoveClock.getText().toString());
                    int fullCount = Integer.parseInt(fullMoveCounter.getText().toString());
                    cb.pos.halfMoveClock = halfClock;
                    cb.pos.fullMoveCounter = fullCount;
                } catch (NumberFormatException nfe) {
                    Toast.makeText(getApplicationContext(), R.string.invalid_number_format, Toast.LENGTH_SHORT)
                            .show();
                }
            }
        };
        builder.setPositiveButton("Ok", new Dialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                setCounters.run();
            }
        });
        builder.setNegativeButton("Cancel", null);

        final Dialog dialog = builder.create();

        fullMoveCounter.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    setCounters.run();
                    dialog.cancel();
                    return true;
                }
                return false;
            }
        });
        return dialog;
    }
    }
    return null;
}

From source file:org.cryptsecure.Utility.java

/**
 * Paste text from clipboard. This method checks for honeycomp changes to
 * the clipboard.//from  w  ww  .  j ava2  s  .c o  m
 * 
 * @param context
 *            the context
 * @return the string
 */
@SuppressWarnings("deprecation")
public static String pasteFromClipboard(final Context context) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        return clipboard.getText().toString();
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        return clipboard.getText().toString();
    }
}