Example usage for android.text ClipboardManager hasText

List of usage examples for android.text ClipboardManager hasText

Introduction

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

Prototype

public abstract boolean hasText();

Source Link

Document

Returns true if the clipboard contains text; false otherwise.

Usage

From source file:Main.java

public static boolean checkClipborad(Context context) {
    ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    return cmb.hasText();
}

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 {/*from  w w w  .  j ava  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: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   w  w w .j  a v  a2  s.c  o  m
        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.mozilla.gecko.GeckoAppShell.java

static String getClipboardText() {
    getHandler().post(new Runnable() {
        @SuppressWarnings("deprecation")
        public void run() {
            Context context = GeckoApp.mAppContext;
            String text = null;//  ww w .j a va2 s.co  m
            if (Build.VERSION.SDK_INT >= 11) {
                android.content.ClipboardManager cm = (android.content.ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                if (cm.hasPrimaryClip()) {
                    ClipData clip = cm.getPrimaryClip();
                    if (clip != null) {
                        ClipData.Item item = clip.getItemAt(0);
                        text = item.coerceToText(context).toString();
                    }
                }
            } else {
                android.text.ClipboardManager cm = (android.text.ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                if (cm.hasText())
                    text = cm.getText().toString();
            }
            try {
                sClipboardQueue.put(text != null ? text : EMPTY_STRING);
            } catch (InterruptedException ie) {
            }
        }
    });
    try {
        String ret = sClipboardQueue.take();
        return (EMPTY_STRING.equals(ret) ? null : ret);
    } catch (InterruptedException ie) {
    }
    return null;
}

From source file:com.edible.ocr.CaptureActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    switch (item.getItemId()) {

    case OPTIONS_COPY_RECOGNIZED_TEXT_ID:
        clipboardManager.setText(ocrResultView.getText());
        if (clipboardManager.hasText()) {
            Toast toast = Toast.makeText(this, "Text copied.", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.show();//from   w w w. j a v  a2  s  .c o m
        }
        return true;
    case OPTIONS_SHARE_RECOGNIZED_TEXT_ID:
        Intent shareRecognizedTextIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareRecognizedTextIntent.setType("text/plain");
        shareRecognizedTextIntent.putExtra(android.content.Intent.EXTRA_TEXT, ocrResultView.getText());
        startActivity(Intent.createChooser(shareRecognizedTextIntent, "Share via"));
        return true;
    case OPTIONS_COPY_TRANSLATED_TEXT_ID:
        clipboardManager.setText(translationView.getText());
        if (clipboardManager.hasText()) {
            Toast toast = Toast.makeText(this, "Text copied.", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.show();
        }
        return true;
    case OPTIONS_SHARE_TRANSLATED_TEXT_ID:
        Intent shareTranslatedTextIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareTranslatedTextIntent.setType("text/plain");
        shareTranslatedTextIntent.putExtra(android.content.Intent.EXTRA_TEXT, translationView.getText());
        startActivity(Intent.createChooser(shareTranslatedTextIntent, "Share via"));
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

From source file:ru.valle.btc.MainActivity.java

@SuppressWarnings("deprecation")
private String getTextInClipboard() {
    CharSequence textInClipboard = "";
    if (Build.VERSION.SDK_INT >= 11) {
        if (clipboardHelper.hasTextInClipboard()) {
            textInClipboard = clipboardHelper.getTextInClipboard();
        }//ww  w.  j  ava2s  . c o m
    } else {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(
                Context.CLIPBOARD_SERVICE);
        if (clipboard.hasText()) {
            textInClipboard = clipboard.getText();
        }
    }
    return textInClipboard == null ? "" : textInClipboard.toString();
}

From source file:net.kidlogger.kidlogger.KLService.java

private void doScanClipboard() {
    ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    String content = "";
    int cSize = 0;
    if (cm.hasText()) {
        CharSequence tmp = cm.getText();
        cSize = tmp.length();//from  w ww .ja  v a2 s . c  om
        if ((prevClipSize != cSize) && (cSize != 0)) {
            if (cSize > 30)
                content = tmp.subSequence(0, 30).toString();
            else
                content = tmp.toString();

            prevClipSize = cSize;

            // Log clipboard content
            final String cnt = new String(content);
            new Thread(new Runnable() {
                public void run() {
                    sync.writeLog(".htm", Templates.getClipboardLog(cnt));
                }
            }).start();
            //WriteThread wcb = new WriteThread(sync, ".htm",
            //      Templates.getClipboardLog(content));
        }
    }
}

From source file:com.thomasokken.free42.Free42Activity.java

private void doPaste() {
    android.text.ClipboardManager clip = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    if (clip.hasText())
        core_paste(clip.getText().toString());
}

From source file:com.androzic.MapActivity.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuSearch:
        onSearchRequested();//  w  w w . j  a  v  a  2  s  . c  o m
        return true;
    case R.id.menuAddWaypoint: {
        double[] loc = application.getMapCenter();
        Waypoint waypoint = new Waypoint("", "", loc[0], loc[1]);
        waypoint.date = Calendar.getInstance().getTime();
        int wpt = application.addWaypoint(waypoint);
        waypoint.name = "WPT" + wpt;
        application.saveDefaultWaypoints();
        map.update();
        return true;
    }
    case R.id.menuNewWaypoint:
        startActivityForResult(new Intent(this, WaypointProperties.class).putExtra("INDEX", -1),
                RESULT_SAVE_WAYPOINT);
        return true;
    case R.id.menuProjectWaypoint:
        startActivityForResult(new Intent(this, WaypointProject.class), RESULT_SAVE_WAYPOINT);
        return true;
    case R.id.menuManageWaypoints:
        startActivityForResult(new Intent(this, WaypointList.class), RESULT_MANAGE_WAYPOINTS);
        return true;
    case R.id.menuLoadWaypoints:
        startActivityForResult(new Intent(this, WaypointFileList.class), RESULT_LOAD_WAYPOINTS);
        return true;
    case R.id.menuManageTracks:
        startActivityForResult(new Intent(this, TrackList.class), RESULT_MANAGE_TRACKS);
        return true;
    case R.id.menuExportCurrentTrack:
        FragmentManager fm = getSupportFragmentManager();
        TrackExportDialog trackExportDialog = new TrackExportDialog(locationService);
        trackExportDialog.show(fm, "track_export");
        return true;
    case R.id.menuExpandCurrentTrack:
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.warning)
                .setMessage(R.string.msg_expandcurrenttrack)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (application.currentTrackOverlay != null) {
                            Track track = locationService.getTrack();
                            track.show = true;
                            application.currentTrackOverlay.setTrack(track);
                        }
                    }
                }).setNegativeButton(R.string.no, null).show();
        return true;
    case R.id.menuClearCurrentTrack:
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.warning)
                .setMessage(R.string.msg_clearcurrenttrack)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (application.currentTrackOverlay != null)
                            application.currentTrackOverlay.clear();
                        locationService.clearTrack();
                    }
                }).setNegativeButton(R.string.no, null).show();
        return true;
    case R.id.menuManageRoutes:
        startActivityForResult(new Intent(this, RouteList.class).putExtra("MODE", RouteList.MODE_MANAGE),
                RESULT_MANAGE_ROUTES);
        return true;
    case R.id.menuStartNavigation:
        if (application.getRoutes().size() > 1) {
            startActivity(new Intent(this, RouteList.class).putExtra("MODE", RouteList.MODE_START));
        } else {
            startActivity(new Intent(this, RouteStart.class).putExtra("INDEX", 0));
        }
        return true;
    case R.id.menuNavigationDetails:
        startActivity(new Intent(this, RouteDetails.class)
                .putExtra("index", application.getRouteIndex(navigationService.navRoute))
                .putExtra("nav", true));
        return true;
    case R.id.menuNextNavPoint:
        navigationService.nextRouteWaypoint();
        return true;
    case R.id.menuPrevNavPoint:
        navigationService.prevRouteWaypoint();
        return true;
    case R.id.menuStopNavigation: {
        navigationService.stopNavigation();
        return true;
    }
    case R.id.menuHSI:
        startActivity(new Intent(this, HSIActivity.class));
        return true;
    case R.id.menuInformation:
        startActivity(new Intent(this, Information.class));
        return true;
    case R.id.menuMapInfo:
        startActivity(new Intent(this, MapInformation.class));
        return true;
    case R.id.menuCursorMaps:
        startActivityForResult(new Intent(this, MapList.class).putExtra("pos", true),
                RESULT_LOAD_MAP_ATPOSITION);
        return true;
    case R.id.menuAllMaps:
        startActivityForResult(new Intent(this, MapList.class), RESULT_LOAD_MAP);
        return true;
    case R.id.menuShare:
        Intent i = new Intent(android.content.Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_SUBJECT, R.string.currentloc);
        double[] sloc = application.getMapCenter();
        String spos = StringFormatter.coordinates(application.coordinateFormat, " ", sloc[0], sloc[1]);
        i.putExtra(Intent.EXTRA_TEXT, spos);
        startActivity(Intent.createChooser(i, getString(R.string.menu_share)));
        return true;
    case R.id.menuCopyLocation: {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        double[] cloc = application.getMapCenter();
        String cpos = StringFormatter.coordinates(application.coordinateFormat, " ", cloc[0], cloc[1]);
        clipboard.setText(cpos);
        return true;
    }
    case R.id.menuPasteLocation: {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        if (clipboard.hasText()) {
            String q = clipboard.getText().toString();
            try {
                double c[] = CoordinateParser.parse(q);
                if (!Double.isNaN(c[0]) && !Double.isNaN(c[1])) {
                    boolean mapChanged = application.setMapCenter(c[0], c[1], true, false);
                    if (mapChanged)
                        map.updateMapInfo();
                    map.update();
                    map.setFollowing(false);
                }
            } catch (IllegalArgumentException e) {
            }
        }
        return true;
    }
    case R.id.menuSetAnchor:
        if (showDistance > 0) {
            application.distanceOverlay.setAncor(application.getMapCenter());
            application.distanceOverlay.setEnabled(true);
        }
        return true;
    case R.id.menuPreferences:
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            startActivity(new Intent(this, Preferences.class));
        } else {
            startActivity(new Intent(this, PreferencesHC.class));
        }
        return true;
    }
    return false;
}

From source file:org.petero.droidfish.DroidFish.java

private final Dialog clipBoardDialog() {
    final int COPY_GAME = 0;
    final int COPY_POSITION = 1;
    final int PASTE = 2;

    setAutoMode(AutoMode.OFF);/*  w  ww. ja va 2  s.c o m*/
    List<CharSequence> lst = new ArrayList<CharSequence>();
    List<Integer> actions = new ArrayList<Integer>();
    lst.add(getString(R.string.copy_game));
    actions.add(COPY_GAME);
    lst.add(getString(R.string.copy_position));
    actions.add(COPY_POSITION);
    lst.add(getString(R.string.paste));
    actions.add(PASTE);
    final List<Integer> finalActions = actions;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.tools_menu);
    builder.setItems(lst.toArray(new CharSequence[lst.size()]), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch (finalActions.get(item)) {
            case COPY_GAME: {
                String pgn = ctrl.getPGN();
                ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboard.setText(pgn);
                break;
            }
            case COPY_POSITION: {
                String fen = ctrl.getFEN() + "\n";
                ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboard.setText(fen);
                break;
            }
            case PASTE: {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                if (clipboard.hasText()) {
                    String fenPgn = clipboard.getText().toString();
                    try {
                        ctrl.setFENOrPGN(fenPgn);
                        setBoardFlip(true);
                    } catch (ChessParseError e) {
                        Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT)
                                .show();
                    }
                }
                break;
            }
            }
        }
    });
    AlertDialog alert = builder.create();
    return alert;
}