Example usage for android.content ClipboardManager hasPrimaryClip

List of usage examples for android.content ClipboardManager hasPrimaryClip

Introduction

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

Prototype

public boolean hasPrimaryClip() 

Source Link

Document

Returns true if there is currently a primary clip on the clipboard.

Usage

From source file:com.softminds.matrixcalculator.OperationFragments.NormOne.java

@Override
public void onListItemClick(ListView L, View V, int position, long id) {
    final int pos = position;
    final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(
            getContext());//from w  w w .j av  a2s. c  o  m
    builder.setPositiveButton(R.string.copy, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            ClipboardManager clipboardManager = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("NORM_ONE_RES", GetText(
                    ((GlobalValues) getActivity().getApplication()).GetCompleteList().get(pos).getNorm1()));
            clipboardManager.setPrimaryClip(clipData);
            if (clipboardManager.hasPrimaryClip()) {
                Toast.makeText(getContext(), R.string.CopyToClip, Toast.LENGTH_SHORT).show();
            } else
                Log.d("ClipData", "Failed to set to Clip board");
            dialogInterface.dismiss();
        }
    });

    builder.setNeutralButton(R.string.Done, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    builder.setMessage("First Norm is : " + GetText(
            ((GlobalValues) getActivity().getApplication()).GetCompleteList().get(position).getNorm1()));
    builder.setTitle(R.string.normOne);
    builder.setCancelable(false);
    builder.show();
}

From source file:com.softminds.matrixcalculator.OperationFragments.NormTwo.java

@Override
public void onListItemClick(ListView L, View V, int position, long id) {
    final int pos = position;
    final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(
            getContext());//w  w  w  . jav  a  2s. c om
    builder.setPositiveButton(R.string.copy, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            ClipboardManager clipboardManager = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("NORM_TWO_RES", GetText(
                    ((GlobalValues) getActivity().getApplication()).GetCompleteList().get(pos).getNorm2()));
            clipboardManager.setPrimaryClip(clipData);
            if (clipboardManager.hasPrimaryClip()) {
                Toast.makeText(getContext(), R.string.CopyToClip, Toast.LENGTH_SHORT).show();
            } else
                Log.d("ClipData", "Failed to set to Clip board");
            dialogInterface.dismiss();
        }
    });

    builder.setNeutralButton(R.string.Done, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    builder.setMessage("Second Norm is : " + GetText(
            ((GlobalValues) getActivity().getApplication()).GetCompleteList().get(position).getNorm2()));
    builder.setTitle(R.string.normTwo);
    builder.setCancelable(false);
    builder.show();
}

From source file:com.softminds.matrixcalculator.OperationFragments.NormFreb.java

@Override
public void onListItemClick(ListView L, View V, int position, long id) {
    final int pos = position;
    final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(
            getContext());// w ww. ja va  2s  . co  m
    builder.setPositiveButton(R.string.copy, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            ClipboardManager clipboardManager = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("NORM_FREB_RES",
                    GetText(((GlobalValues) getActivity().getApplication()).GetCompleteList().get(pos)
                            .getNormFrobenious()));
            clipboardManager.setPrimaryClip(clipData);
            if (clipboardManager.hasPrimaryClip()) {
                Toast.makeText(getContext(), R.string.CopyToClip, Toast.LENGTH_SHORT).show();
            } else
                Log.d("ClipData", "Failed to set to Clip board");
            dialogInterface.dismiss();
        }
    });

    builder.setNeutralButton(R.string.Done, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    builder.setMessage("Frebenious Norm is : " + GetText(((GlobalValues) getActivity().getApplication())
            .GetCompleteList().get(position).getNormFrobenious()));
    builder.setTitle(R.string.normFreb);
    builder.setCancelable(false);
    builder.show();
}

From source file:com.softminds.matrixcalculator.OperationFragments.NormInfinity.java

@Override
public void onListItemClick(ListView L, View V, int position, long id) {
    final int pos = position;
    final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(
            getContext());// w ww . j  a v  a2  s.co  m
    builder.setPositiveButton(R.string.copy, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            ClipboardManager clipboardManager = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("NORM_INFINITY_RES",
                    GetText(((GlobalValues) getActivity().getApplication()).GetCompleteList().get(pos)
                            .getNormInfinity()));
            clipboardManager.setPrimaryClip(clipData);
            if (clipboardManager.hasPrimaryClip()) {
                Toast.makeText(getContext(), R.string.CopyToClip, Toast.LENGTH_SHORT).show();
            } else
                Log.d("ClipData", "Failed to set to Clip board");
            dialogInterface.dismiss();
        }
    });

    builder.setNeutralButton(R.string.Done, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    builder.setMessage("Infinity Norm is : " + GetText(
            ((GlobalValues) getActivity().getApplication()).GetCompleteList().get(position).getNormInfinity()));
    builder.setTitle(R.string.normInfinity);
    builder.setCancelable(false);
    builder.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);//from  ww w.j a  v  a  2  s. co m

    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: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());/* www  . j a  v  a  2 s  .c  o m*/
        }
    }

    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.mikhaellopez.saveinsta.activity.MainActivity.java

private String pastClipboard() {
    String textToPaste = null;/*from   ww  w  . j  a va  2 s.  co  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:com.manzdagratiano.gobbledygook.Gobbledygook.java

/**
 * @brief   Called after onCreate() (and onStart(),
 * when the activity begins interacting with the user)
 * @return  Does not return a value/*from w  ww .  j  av  a2s .com*/
 */
@Override
public void onResume() {
    Log.i(Env.LOG_CATEGORY, "onResume(): Configuring...");
    super.onResume();

    // ----------------------------------------------------------------
    // Retrieve saved preferences
    // The SharedPreferences handle
    Log.i(Env.LOG_CATEGORY, "Reading saved preferences...");
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    // The salt key 
    String saltKey = "";
    Integer defaultIterations = Attributes.DEFAULT_ITERATIONS;
    String encodedAttributesList = "";
    // Catch all exceptions when reading from SharedPreferences
    try {
        saltKey = sharedPrefs.getString(getString(R.string.pref_saltKey_key), "");
        // The default number of PBKDF2 iterations.
        // Since the preference is an EditTextPreference,
        // even though it has the numeric attribute,
        // and is entered as an integer,
        // it is saved as a string.
        // Therefore, it must be retrieved as a string and then cast.
        String defaultIterationsStr = sharedPrefs.getString(getString(R.string.pref_defaultIterations_key), "");
        // If non-empty, parse as an Integer.
        // The empty case will leave defaultIterations at
        // Attributes.DEFAULT_ITERATIONS.
        if (!defaultIterationsStr.isEmpty()) {
            defaultIterations = Integer.parseInt(defaultIterationsStr);
        }
        // The encoded siteAttributesList
        encodedAttributesList = sharedPrefs.getString(getString(R.string.pref_siteAttributesList_key), "");
    } catch (Exception e) {
        Log.e(Env.LOG_CATEGORY, "ERROR: Caught " + e);
        e.printStackTrace();
    }

    Log.d(Env.LOG_CATEGORY, "savedPreferences=[ saltKey='" + saltKey + "', " + "defaultIterations="
            + defaultIterations.toString() + ", " + "siteAttributesList='" + encodedAttributesList + "' ]");

    // ----------------------------------------------------------------
    // Create the "actors"

    AttributesCodec codec = new AttributesCodec();
    Configurator config = new Configurator();

    // ----------------------------------------------------------------
    // Read the url from the clipboard
    Log.i(Env.LOG_CATEGORY, "Reading url from clipboard...");
    String url = "";
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()) {
        ClipData.Item clipItem = clipboard.getPrimaryClip().getItemAt(0);
        CharSequence clipText = clipItem.getText();
        if (null != clipText) {
            url = clipText.toString();
        }
    }
    Log.i(Env.LOG_CATEGORY, "url='" + url + "'");

    // ----------------------------------------------------------------
    // Extract the domain from the url

    String domain = config.extractDomain(url);
    Log.i(Env.LOG_CATEGORY, "domain='" + domain + "'");

    // ----------------------------------------------------------------
    // Saved and Proposed Attributes

    // Obtain the saved attributes for this domain, if any
    Attributes savedAttributes = codec.getSavedAttributes(domain, encodedAttributesList);
    Log.i(Env.LOG_CATEGORY, "savedAttributes='" + codec.encode(savedAttributes) + "'");

    // The "proposed" attributes,
    // which would be used to generate the proxy password,
    // unless overridden
    Attributes proposedAttributes = new Attributes(config.configureDomain(domain, savedAttributes.domain()),
            config.configureIterations(defaultIterations, savedAttributes.iterations()),
            config.configureTruncation(savedAttributes.truncation()));

    config.configureHash();
}

From source file:org.mdc.chess.MDChess.java

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

    setAutoMode(AutoMode.OFF);//from   ww  w  .  j a  v a  2 s .  c o m
    List<CharSequence> lst = new ArrayList<>();
    List<Integer> actions = new ArrayList<>();
    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;
    new MaterialDialog.Builder(this).title(R.string.tools_menu).items(lst)
            .itemsCallback(new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                    switch (finalActions.get(which)) {
                    case COPY_GAME: {
                        String pgn = ctrl.getPGN();
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(new ClipData("MD Chess game",
                                new String[] { "application/x-chess-pgn", ClipDescription.MIMETYPE_TEXT_PLAIN },
                                new ClipData.Item(pgn)));
                        break;
                    }
                    case COPY_POSITION: {
                        String fen = ctrl.getFEN() + "\n";
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(new ClipData(fen,
                                new String[] { "application/x-chess-fen", ClipDescription.MIMETYPE_TEXT_PLAIN },
                                new ClipData.Item(fen)));
                        break;
                    }
                    case PASTE: {
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        if (clipboard.hasPrimaryClip()) {
                            ClipData clip = clipboard.getPrimaryClip();
                            StringBuilder fenPgn = new StringBuilder();
                            for (int i = 0; i < clip.getItemCount(); i++) {
                                fenPgn.append(clip.getItemAt(i).coerceToText(getApplicationContext()));
                            }
                            try {
                                ctrl.setFENOrPGN(fenPgn.toString());
                                setBoardFlip(true);
                            } catch (ChessParseError e) {
                                Toast.makeText(getApplicationContext(), getParseErrString(e),
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                        break;
                    }
                    }
                }
            }).show();
}