Example usage for android.content ClipboardManager getPrimaryClip

List of usage examples for android.content ClipboardManager getPrimaryClip

Introduction

In this page you can find the example usage for android.content ClipboardManager getPrimaryClip.

Prototype

public @Nullable ClipData getPrimaryClip() 

Source Link

Document

Returns the current primary clip on the clipboard.

Usage

From source file:com.ruesga.rview.fragments.SnippetFragment.java

public void onActionPressed() {
    if (mNeedPermissions) {
        return;/*w  w  w .  j  ava  2s  . c  o m*/
    }
    if (!mReadOnly) {
        // Paste
        //noinspection ConstantConditions
        ClipboardManager clipboard = (ClipboardManager) getContext()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.getPrimaryClip() != null && clipboard.getPrimaryClip().getItemCount() > 0) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
            CharSequence data = item.getText();
            if (data != null) {
                byte[] content = data.toString().getBytes();
                loadContent(content);
                mUiHandler.post(() -> saveContent(content));
            }
        }

    } else {
        // Open in external
        String action = getString(R.string.action_share);
        //noinspection ConstantConditions
        Uri uri = FileProvider.getUriForFile(getContext(), "com.ruesga.rview.content",
                new File(mUri.getPath()));
        ActivityHelper.open(getContext(), action, uri, "text/plain");
        dismiss();
    }
}

From source file:de.syss.MifareClassicTool.Common.java

/**
 * Get the content of the Android clipboard (if it is plain text).
 * @param context Context of the SystemService
 * @return The content of the Android clipboard. On error
 * (clipboard empty, clipboard content not plain text, etc.) null will
 * be returned./*from  w ww .java2  s . c  o m*/
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static String getFromClipboard(Context context) {
    if (Build.VERSION.SDK_INT >= 11) {
        // Android API level 11+.
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.getPrimaryClip() != null && clipboard.getPrimaryClip().getItemCount() > 0 && clipboard
                .getPrimaryClipDescription().hasMimeType(android.content.ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            return clipboard.getPrimaryClip().getItemAt(0).getText().toString();
        }
    } else {
        // Android API level 10.
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.hasText()) {
            return clipboard.getText().toString();
        }
    }

    // Error.
    return null;
}

From source file:com.coloreight.plugin.clipboard.Clipboard.java

public void paste(final CallbackContext callbackContext) {
    cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            ClipboardManager clipboard = (ClipboardManager) cordova.getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);

            if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
            }/*  www .j a v a  2  s .c  o  m*/

            try {
                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
                String text = item.getText().toString();

                callbackContext.success(text);
            } catch (Exception e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
            }
        }
    });
}

From source file:org.protocoderrunner.apprunner.api.PDevice.java

@ProtoMethod(description = "Get the content from the clipboard", example = "")
@ProtoMethodParam(params = { "label", "text" })
public String getFromClipboard(String label, String text) {
    ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    return clipboard.getPrimaryClip().getItemAt(clipboard.getPrimaryClip().getItemCount()).getText().toString();
}

From source file:com.eng.arab.translator.androidtranslator.translate.TranslateViewActivity.java

public String readFromClipboard() {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()) {
        android.content.ClipDescription description = clipboard.getPrimaryClipDescription();
        android.content.ClipData data = clipboard.getPrimaryClip();
        if (data != null && description != null && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
            return String.valueOf(data.getItemAt(0).getText());
    }/* w  ww.j  a v a  2  s. c  o m*/
    return null;
}

From source file:com.timrae.rikaidroid.MainActivity.java

private void loadContentFromClipboard() {
    if (mWebView == null) {
        Log.e(TAG, "Webview is null");
        return;/*from   w w w .  j  a va2  s .  c om*/
    }
    // Get clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    if (!(clipboard.hasPrimaryClip())) {
        Log.w(TAG, "Clipboard is empty");
        return;
    }
    // Get string from clipboard
    ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
    String pasteData = item.getText().toString();
    //pasteData = "??????????";
    if (pasteData == null) {
        Log.w(TAG, "Clipboard doesn't contain text");
        return;
    }
    mEditText.setText(pasteData);
}

From source file:com.mikhaellopez.saveinsta.activity.MainActivity.java

private String pastClipboard() {
    String textToPaste = null;/*from w  w w .ja  v a 2s .  c  o m*/
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            textToPaste = clip.getItemAt(0).getText().toString();
        }
    }
    return textToPaste;
}

From source file:github.popeen.dsub.fragments.SelectPodcastsFragment.java

private void addNewPodcast() {
    View dialogView = context.getLayoutInflater().inflate(R.layout.create_podcast, null);
    final TextView urlBox = (TextView) dialogView.findViewById(R.id.create_podcast_url);

    //If a string is in clipboard and it contains http or https, add it to the textbox
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) {

        String url = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
        if (url.contains("http")) {
            urlBox.setText(url.trim());//from w w w.jav  a 2  s  . com
        }
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.menu_add_podcast).setView(dialogView)
            .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String url = urlBox.getText().toString();
                    addNewPodcast(url);
                }
            }).setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            }).setCancelable(true);

    AlertDialog dialog = builder.create();
    dialog.show();
}

From source file:com.google.reviewit.ServerSettingsFragment.java

private void init() {
    final AutoCompleteTextView urlInput = (AutoCompleteTextView) v(R.id.urlInput);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.urls));
    urlInput.setAdapter(adapter);/*  w ww. ja  v a2 s.c  om*/

    v(R.id.pasteCredentialsButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipboard.hasPrimaryClip()
                    && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
                String pasteData = item.getText().toString();
                if (!pasteData.contains("/.gitcookies")) {
                    return;
                }

                pasteData = pasteData.substring(pasteData.indexOf("/.gitcookies"));
                pasteData = pasteData.substring(pasteData.lastIndexOf(",") + 1);
                int pos = pasteData.indexOf("=");
                String user = pasteData.substring(0, pos);
                pasteData = pasteData.substring(pos + 1);
                String password = pasteData.substring(0, pasteData.indexOf("\n"));
                WidgetUtil.setText(v(R.id.userInput), user);
                WidgetUtil.setText(v(R.id.passwordInput), password);

                // hide keyboard if it is open
                View view = getActivity().getCurrentFocus();
                if (view != null) {
                    ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        }
    });

    ((EditText) v(R.id.urlInput)).addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (Strings.isNullOrEmpty(textOf(R.id.nameInput))) {
                try {
                    String host = new URL(s.toString()).getHost();
                    int pos = host.indexOf(".");
                    WidgetUtil.setText(v(R.id.nameInput), pos > 0 ? host.substring(0, pos) : host);
                } catch (MalformedURLException e) {
                    // ignore
                }
            }
            displayCredentialsInfo(s.toString());
        }
    });

    v(R.id.saveServerSettings).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enabledForm(false);

            if (!isServerInputComplete()) {
                widgetUtil.showError(R.string.incompleteInput);
                enabledForm(true);
                return;
            }

            if (!isUrlValid()) {
                widgetUtil.showError(R.string.invalidUrl);
                enabledForm(true);
                return;
            }

            if (!hasUniqueName()) {
                widgetUtil.showError(getString(R.string.duplicate_server_name, textOf(R.id.nameInput)));
                enabledForm(true);
                return;
            }

            setVisible(v(R.id.statusTestConnection, R.id.statusTestConnectionProgress));
            WidgetUtil.setText(v(R.id.statusTestConnectionText), null);
            new AsyncTask<Void, Void, String>() {
                private TextView status;
                private View statusTestConnectionProgress;
                private View statusTestConnection;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    status = tv(R.id.statusTestConnectionText);
                    statusTestConnectionProgress = v(R.id.statusTestConnectionProgress);
                    statusTestConnection = v(R.id.statusTestConnection);
                }

                @Override
                protected String doInBackground(Void... v) {
                    return testConnection();
                }

                protected void onPostExecute(String errorMsg) {
                    if (errorMsg != null) {
                        enabledForm(true);
                        status.setTextColor(widgetUtil.color(R.color.statusFailed));
                        status.setText(getString(R.string.test_server_connection_failed));
                        setInvisible(statusTestConnectionProgress);
                        new AlertDialog.Builder(getContext()).setTitle(getString(R.string.error_title))
                                .setMessage(getString(R.string.connection_failed, errorMsg))
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // do nothing
                                    }
                                }).setNegativeButton(getString(R.string.save_anyway),
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                setGone(statusTestConnection);
                                                onServerSave(saveServerSettings());
                                            }
                                        })
                                .setIcon(android.R.drawable.ic_dialog_alert).show();
                    } else {
                        status.setTextColor(widgetUtil.color(R.color.statusOk));
                        status.setText(getString(R.string.test_server_connection_ok));
                        setGone(statusTestConnection);
                        onServerSave(saveServerSettings());
                    }
                }
            }.execute();
        }
    });

    enabledForm(true);
    setGone(v(R.id.statusTestConnection));
}

From source file:net.sourceforge.fidocadj.FidoMain.java

/** Get the current data (as String) in the clipboard.
*//*w  ww .ja  v a  2  s.c  o m*/
public String pasteText() {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

    String pasteData = "";
    ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

    // Gets the clipboard as text.
    pasteData = item.getText().toString();

    // If the string contains data, then the paste operation is done
    return pasteData;
}