Example usage for android.content ClipboardManager setPrimaryClip

List of usage examples for android.content ClipboardManager setPrimaryClip

Introduction

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

Prototype

public void setPrimaryClip(@NonNull ClipData clip) 

Source Link

Document

Sets the current primary clip on the clipboard.

Usage

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    ClipboardManager clipboardManager = (ClipboardManager) getActivity()
            .getSystemService(Context.CLIPBOARD_SERVICE);
    try {//from www.ja  v  a 2 s  .co m
        int itemId = item.getItemId();
        switch (itemId) {
        case R.id.import_file:
            Intent target = new Intent(Intent.ACTION_GET_CONTENT);
            target.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(Environment.getExternalStorageDirectory()));
            target.setType("*/*");
            startActivityForResult(Intent.createChooser(target, getString(R.string.select_file)),
                    REQUEST_SELECT_FILE);
            return true;
        case R.id.import_clipboard:
            ClipData clip = clipboardManager.getPrimaryClip();
            if (clip != null && clip.getItemCount() > 0) {
                String text = clip.getItemAt(0).coerceToText(getActivity()).toString();
                onCardsImported(mExportHelper.importCards(text));
            }
            return true;
        case R.id.copy:
            clipboardManager.setPrimaryClip(ClipData.newPlainText(null, mExportHelper.exportCards()));
            Toast.makeText(getActivity(), R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
            return true;
        case R.id.share:
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, mExportHelper.exportCards());
            startActivity(intent);
            return true;
        case R.id.save:
            exportToFile();
            return true;
        }
    } catch (Exception ex) {
        Utils.showError(getActivity(), ex);
    }
    return false;
}

From source file:de.baumann.browser.Browser_left.java

@Override
protected void onResume() {
    super.onResume(); //To change body of overridden methods use File | Settings | File Templates.
    mWebView.onResume();/*  ww w. j  ava  2  s .  c om*/
    final String URL = sharedPref.getString("openURL", "https://github.com/scoute-dich/browser/");
    new Handler().postDelayed(new Runnable() {
        public void run() {
            if (URL.equals(mWebView.getUrl()) || URL.equals("")) {
                Log.i(TAG, "Tab switched");
            } else if (URL.equals("copyLogin")) {
                Snackbar snackbar = Snackbar
                        .make(mWebView, R.string.pass_copy_userName, Snackbar.LENGTH_INDEFINITE)
                        .setAction(getString(R.string.toast_yes), new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ClipboardManager clipboard = (ClipboardManager) Browser_left.this
                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                clipboard.setPrimaryClip(
                                        ClipData.newPlainText("userName", sharedPref.getString("copyUN", "")));

                                Snackbar snackbar = Snackbar
                                        .make(mWebView, R.string.pass_copy_userPW, Snackbar.LENGTH_INDEFINITE)
                                        .setAction(getString(R.string.toast_yes), new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                ClipboardManager clipboard = (ClipboardManager) Browser_left.this
                                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                                clipboard.setPrimaryClip(ClipData.newPlainText("userName",
                                                        sharedPref.getString("copyPW", "")));
                                            }
                                        });
                                snackbar.show();
                            }
                        });
                snackbar.show();
            } else if (URL.contains("openLogin")) {
                Snackbar snackbar = Snackbar
                        .make(mWebView, R.string.pass_copy_userName, Snackbar.LENGTH_INDEFINITE)
                        .setAction(getString(R.string.toast_yes), new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ClipboardManager clipboard = (ClipboardManager) Browser_left.this
                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                clipboard.setPrimaryClip(
                                        ClipData.newPlainText("userName", sharedPref.getString("copyUN", "")));

                                Snackbar snackbar = Snackbar
                                        .make(mWebView, R.string.pass_copy_userPW, Snackbar.LENGTH_INDEFINITE)
                                        .setAction(getString(R.string.toast_yes), new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                ClipboardManager clipboard = (ClipboardManager) Browser_left.this
                                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                                clipboard.setPrimaryClip(ClipData.newPlainText("userName",
                                                        sharedPref.getString("copyPW", "")));
                                            }
                                        });
                                snackbar.show();
                            }
                        });
                snackbar.show();
                mWebView.loadUrl(URL.replace("openLogin", ""));
            } else {
                mWebView.loadUrl(URL);
            }
        }
    }, 100);
}

From source file:de.baumann.browser.Browser_right.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    final WebView.HitTestResult result = mWebView.getHitTestResult();
    final String url = result.getExtra();

    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) {

        final CharSequence[] options = { getString(R.string.context_saveImage),
                getString(R.string.context_shareImage), getString(R.string.context_readLater),
                getString(R.string.context_left) };
        new AlertDialog.Builder(Browser_right.this)
                .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }/*from www.  j  a  va  2  s. co m*/
                }).setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals(getString(R.string.context_saveImage))) {
                            if (url != null) {
                                try {
                                    Uri source = Uri.parse(url);
                                    DownloadManager.Request request = new DownloadManager.Request(source);
                                    request.addRequestHeader("Cookie",
                                            CookieManager.getInstance().getCookie(url));
                                    request.allowScanningByMediaScanner();
                                    request.setNotificationVisibility(
                                            DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                            helper_main.newFileName());
                                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                                    dm.enqueue(request);
                                    Snackbar.make(mWebView, getString(R.string.context_saveImage_toast) + " "
                                            + helper_main.newFileName(), Snackbar.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show();
                                }
                            }
                        }
                        if (options[item].equals(getString(R.string.context_shareImage))) {
                            if (url != null) {

                                shareString = helper_main.newFileName();
                                shareFile = helper_main.newFile(mWebView);

                                try {
                                    Uri source = Uri.parse(url);
                                    DownloadManager.Request request = new DownloadManager.Request(source);
                                    request.addRequestHeader("Cookie",
                                            CookieManager.getInstance().getCookie(url));
                                    request.allowScanningByMediaScanner();
                                    request.setNotificationVisibility(
                                            DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                            shareString);
                                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                                    dm.enqueue(request);

                                    Snackbar.make(mWebView, getString(R.string.context_saveImage_toast) + " "
                                            + helper_main.newFileName(), Snackbar.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show();
                                }
                                registerReceiver(onComplete2,
                                        new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                            }
                        }
                        if (options[item].equals(getString(R.string.context_readLater))) {
                            if (url != null) {

                                if (Uri.parse(url).getHost().length() == 0) {
                                    domain = getString(R.string.app_domain);
                                } else {
                                    domain = Uri.parse(url).getHost();
                                }

                                String domain2 = domain.substring(0, 1).toUpperCase() + domain.substring(1);

                                DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_right.this);
                                db.open();
                                if (db.isExist(mWebView.getUrl())) {
                                    Snackbar.make(editText, getString(R.string.toast_newTitle),
                                            Snackbar.LENGTH_LONG).show();
                                } else {
                                    db.insert(domain2, url, "", "", helper_main.createDate());
                                    Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG)
                                            .show();
                                }
                            }
                        }
                        if (options[item].equals(getString(R.string.context_left))) {
                            if (url != null) {
                                helper_main.switchToActivity(Browser_right.this, Browser_left.class, url,
                                        false);
                            }
                        }
                    }
                }).show();

    } else if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {

        final CharSequence[] options = { getString(R.string.menu_share_link_copy),
                getString(R.string.menu_share_link), getString(R.string.context_readLater),
                getString(R.string.context_left) };
        new AlertDialog.Builder(Browser_right.this)
                .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                }).setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals(getString(R.string.menu_share_link_copy))) {
                            if (url != null) {
                                ClipboardManager clipboard = (ClipboardManager) Browser_right.this
                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
                                Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT)
                                        .show();
                            }
                        }
                        if (options[item].equals(getString(R.string.menu_share_link))) {
                            if (url != null) {
                                Intent sendIntent = new Intent();
                                sendIntent.setAction(Intent.ACTION_SEND);
                                sendIntent.putExtra(Intent.EXTRA_TEXT, url);
                                sendIntent.setType("text/plain");
                                Browser_right.this.startActivity(Intent.createChooser(sendIntent,
                                        getResources().getString(R.string.app_share_link)));
                            }
                        }
                        if (options[item].equals(getString(R.string.context_readLater))) {
                            if (url != null) {

                                if (Uri.parse(url).getHost().length() == 0) {
                                    domain = getString(R.string.app_domain);
                                } else {
                                    domain = Uri.parse(url).getHost();
                                }

                                String domain2 = domain.substring(0, 1).toUpperCase() + domain.substring(1);

                                DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_right.this);
                                db.open();
                                if (db.isExist(mWebView.getUrl())) {
                                    Snackbar.make(editText, getString(R.string.toast_newTitle),
                                            Snackbar.LENGTH_LONG).show();
                                } else {
                                    db.insert(domain2, url, "", "", helper_main.createDate());
                                    Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG)
                                            .show();
                                }
                            }
                        }
                        if (options[item].equals(getString(R.string.context_left))) {
                            if (url != null) {
                                helper_main.switchToActivity(Browser_right.this, Browser_left.class, url,
                                        false);
                            }
                        }
                    }
                }).show();
    }
}

From source file:de.baumann.browser.Browser.java

protected void onNewIntent(final Intent intent) {

    String action = intent.getAction();

    if (Intent.ACTION_SEND.equals(action)) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        String searchEngine = sharedPref.getString("searchEngine", "https://startpage.com/do/search?query=");
        mWebView.loadUrl(searchEngine + sharedText);
    } else if ("pass".equals(action)) {
        mWebView.loadUrl(intent.getStringExtra("url"));
        setTitle(intent.getStringExtra("title"));
        Snackbar snackbar = Snackbar.make(mWebView, R.string.pass_copy_userName, Snackbar.LENGTH_INDEFINITE)
                .setAction(getString(R.string.toast_yes), new View.OnClickListener() {
                    @Override//from  www .  j  a  v  a  2 s .  c  o  m
                    public void onClick(View view) {
                        ClipboardManager clipboard = (ClipboardManager) Browser.this
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setPrimaryClip(
                                ClipData.newPlainText("userName", intent.getStringExtra("userName")));

                        Snackbar snackbar = Snackbar
                                .make(mWebView, R.string.pass_copy_userPW, Snackbar.LENGTH_INDEFINITE)
                                .setAction(getString(R.string.toast_yes), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        ClipboardManager clipboard = (ClipboardManager) Browser.this
                                                .getSystemService(Context.CLIPBOARD_SERVICE);
                                        clipboard.setPrimaryClip(ClipData.newPlainText("userName",
                                                intent.getStringExtra("userPW")));
                                    }
                                });
                        snackbar.show();
                    }
                });
        snackbar.show();
    } else if (Intent.ACTION_VIEW.equals(action)) {
        Uri data = intent.getData();
        String link = data.toString();
        mWebView.loadUrl(link);
    } else {
        mWebView.loadUrl(intent.getStringExtra("url"));
    }
}

From source file:obdii.starter.automotive.iot.ibm.com.iot4a_obdii.Home.java

private void onDeviceRegistration(int statusCode, final JSONObject deviceDefinition, final boolean simulation)
        throws JSONException {
    // run in UI thread
    switch (statusCode) {
    case 201://from w ww. j  a  va 2s . c  o m
    case 202:

        final String authToken = deviceDefinition.getString("authToken");
        final String deviceId = deviceDefinition.getString("deviceId");
        iotpDevice.setDeviceToken(deviceId, authToken);

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this,
                R.style.AppCompatAlertDialogStyle);
        final View authTokenAlert = getLayoutInflater().inflate(R.layout.activity_home_authtokenalert, null,
                false);
        final EditText authTokenField = (EditText) authTokenAlert.findViewById(R.id.authTokenField);
        authTokenField.setText(authToken);

        final Button copyToClipboard = (Button) authTokenAlert.findViewById(R.id.copyToClipboard);
        copyToClipboard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(
                        CLIPBOARD_SERVICE);
                final ClipData clipData = ClipData.newPlainText("authToken", authToken);
                clipboardManager.setPrimaryClip(clipData);

                Toast.makeText(Home.this, "Successfully copied to your Clipboard!", Toast.LENGTH_SHORT).show();
            }
        });
        storeSetting(SettingsFragment.DEVICE_TOKEN, authToken);

        alertDialog.setView(authTokenAlert);
        alertDialog.setCancelable(false).setTitle("Your Device is Now Registered!")
                .setMessage("Please take note of this Autentication Token as you will need it in the future")
                .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        deviceRegistered(deviceDefinition, simulation);
                    }
                }).show();
        break;
    default:
        break;
    }
    progressBar.setVisibility(View.GONE);
}

From source file:org.sufficientlysecure.keychain.ui.EncryptFilesFragment.java

@Override
public void onQueuedOperationSuccess(final SignEncryptResult result) {
    super.onQueuedOperationSuccess(result);

    hideKeyboard();/*www  . j av a  2 s .  c  o  m*/

    // protected by Queueing*Fragment
    FragmentActivity activity = getActivity();

    if (mDeleteAfterEncrypt) {
        // TODO make behavior coherent here
        DeleteFileDialogFragment deleteFileDialog = DeleteFileDialogFragment
                .newInstance(mFilesAdapter.getAsArrayList());
        deleteFileDialog.setOnDeletedListener(new DeleteFileDialogFragment.OnDeletedListener() {

            @Override
            public void onDeleted() {
                if (mAfterEncryptAction == AfterEncryptAction.SHARE) {
                    // Share encrypted message/file
                    startActivity(
                            Intent.createChooser(createSendIntent(), getString(R.string.title_share_file)));
                } else {
                    Activity activity = getActivity();
                    if (activity == null) {
                        // it's gone, there's nothing we can do here
                        return;
                    }
                    // Save encrypted file
                    result.createNotify(activity).show();
                }
            }

        });
        deleteFileDialog.show(activity.getSupportFragmentManager(), "deleteDialog");
    } else {

        switch (mAfterEncryptAction) {

        case SHARE:
            // Share encrypted message/file
            startActivity(Intent.createChooser(createSendIntent(), getString(R.string.title_share_file)));
            break;

        case COPY:

            ClipboardManager clipMan = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipMan == null) {
                Notify.create(activity, R.string.error_clipboard_copy, Style.ERROR).show();
                break;
            }
            ClipData clip = new ClipData(getString(R.string.label_clip_title),
                    // make available as application/pgp-encrypted
                    new String[] { "text/plain" }, new ClipData.Item(mOutputUris.get(0)));
            clipMan.setPrimaryClip(clip);
            result.createNotify(activity).show();
            break;

        case SAVE:
            // Encrypted file was saved already, just show notification
            result.createNotify(activity).show();
            break;
        }
    }

}

From source file:de.baumann.browser.Browser_left.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    final WebView.HitTestResult result = mWebView.getHitTestResult();
    final String url = result.getExtra();

    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) {

        final CharSequence[] options = { getString(R.string.context_saveImage),
                getString(R.string.context_shareImage), getString(R.string.context_readLater),
                getString(R.string.context_right) };
        new AlertDialog.Builder(Browser_left.this)
                .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }//from   ww  w.  j a v  a2s.  c  o m
                }).setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals(getString(R.string.context_saveImage))) {
                            if (url != null) {
                                try {
                                    Uri source = Uri.parse(url);
                                    DownloadManager.Request request = new DownloadManager.Request(source);
                                    request.addRequestHeader("Cookie",
                                            CookieManager.getInstance().getCookie(url));
                                    request.allowScanningByMediaScanner();
                                    request.setNotificationVisibility(
                                            DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                            helper_main.newFileName());
                                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                                    dm.enqueue(request);
                                    Snackbar.make(mWebView, getString(R.string.context_saveImage_toast) + " "
                                            + helper_main.newFileName(), Snackbar.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show();
                                }
                            }
                        }
                        if (options[item].equals(getString(R.string.context_shareImage))) {
                            if (url != null) {

                                shareString = helper_main.newFileName();
                                shareFile = helper_main.newFile(mWebView);

                                try {
                                    Uri source = Uri.parse(url);
                                    DownloadManager.Request request = new DownloadManager.Request(source);
                                    request.addRequestHeader("Cookie",
                                            CookieManager.getInstance().getCookie(url));
                                    request.allowScanningByMediaScanner();
                                    request.setNotificationVisibility(
                                            DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                            shareString);
                                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                                    dm.enqueue(request);

                                    Snackbar.make(mWebView, getString(R.string.context_saveImage_toast) + " "
                                            + helper_main.newFileName(), Snackbar.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show();
                                }
                                registerReceiver(onComplete2,
                                        new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                            }
                        }
                        if (options[item].equals(getString(R.string.context_readLater))) {
                            if (url != null) {

                                if (Uri.parse(url).getHost().length() == 0) {
                                    domain = getString(R.string.app_domain);
                                } else {
                                    domain = Uri.parse(url).getHost();
                                }

                                String domain2 = domain.substring(0, 1).toUpperCase() + domain.substring(1);

                                DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_left.this);
                                db.open();
                                if (db.isExist(mWebView.getUrl())) {
                                    Snackbar.make(editText, getString(R.string.toast_newTitle),
                                            Snackbar.LENGTH_LONG).show();
                                } else {
                                    db.insert(domain2, url, "", "", helper_main.createDate());
                                    Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG)
                                            .show();
                                }
                            }
                        }
                        if (options[item].equals(getString(R.string.context_right))) {
                            if (url != null) {
                                helper_main.switchToActivity(Browser_left.this, Browser_right.class, url,
                                        false);
                            }
                        }
                    }
                }).show();

    } else if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {

        final CharSequence[] options = { getString(R.string.menu_share_link_copy),
                getString(R.string.menu_share_link), getString(R.string.context_readLater),
                getString(R.string.context_right) };
        new AlertDialog.Builder(Browser_left.this)
                .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                }).setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals(getString(R.string.menu_share_link_copy))) {
                            if (url != null) {
                                ClipboardManager clipboard = (ClipboardManager) Browser_left.this
                                        .getSystemService(Context.CLIPBOARD_SERVICE);
                                clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
                                Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT)
                                        .show();
                            }
                        }
                        if (options[item].equals(getString(R.string.menu_share_link))) {
                            if (url != null) {
                                Intent sendIntent = new Intent();
                                sendIntent.setAction(Intent.ACTION_SEND);
                                sendIntent.putExtra(Intent.EXTRA_TEXT, url);
                                sendIntent.setType("text/plain");
                                Browser_left.this.startActivity(Intent.createChooser(sendIntent,
                                        getResources().getString(R.string.app_share_link)));
                            }
                        }
                        if (options[item].equals(getString(R.string.context_readLater))) {
                            if (url != null) {

                                if (Uri.parse(url).getHost().length() == 0) {
                                    domain = getString(R.string.app_domain);
                                } else {
                                    domain = Uri.parse(url).getHost();
                                }

                                String domain2 = domain.substring(0, 1).toUpperCase() + domain.substring(1);

                                DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_left.this);
                                db.open();
                                if (db.isExist(mWebView.getUrl())) {
                                    Snackbar.make(editText, getString(R.string.toast_newTitle),
                                            Snackbar.LENGTH_LONG).show();
                                } else {
                                    db.insert(domain2, url, "", "", helper_main.createDate());
                                    Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG)
                                            .show();
                                }
                            }
                        }
                        if (options[item].equals(getString(R.string.context_right))) {
                            if (url != null) {
                                helper_main.switchToActivity(Browser_left.this, Browser_right.class, url,
                                        false);
                            }
                        }
                    }
                }).show();
    }
}

From source file:de.baumann.browser.Browser.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    final WebView.HitTestResult result = mWebView.getHitTestResult();

    final MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            final String url = result.getExtra();

            switch (item.getItemId()) {
            //Save image to external memory
            case ID_SAVE_IMAGE: {
                registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

                try {
                    if (url != null) {

                        Uri source = Uri.parse(url);
                        DownloadManager.Request request = new DownloadManager.Request(source);
                        request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(
                                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                helper_main.newFileName());
                        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        dm.enqueue(request);

                        Snackbar.make(mWebView,
                                getString(R.string.context_saveImage_toast) + " " + helper_main.newFileName(),
                                Snackbar.LENGTH_SHORT).show();
                    }/*from   w  w  w .  j a  v a 2 s . c om*/
                } catch (Exception e) {
                    e.printStackTrace();
                    Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show();
                }
            }
                break;

            case ID_SHARE_IMAGE:
                if (url != null) {

                    shareString = helper_main.newFileName();
                    shareFile = helper_main.newFile();

                    try {
                        Uri source = Uri.parse(url);
                        DownloadManager.Request request = new DownloadManager.Request(source);
                        request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(
                                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, shareString);
                        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        dm.enqueue(request);

                        Snackbar.make(mWebView,
                                getString(R.string.context_saveImage_toast) + " " + helper_main.newFileName(),
                                Snackbar.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show();
                    }
                    registerReceiver(onComplete2, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                }
                break;

            case ID_READ_LATER:
                if (url != null) {
                    int domainInt = url.indexOf("//") + 2;
                    final String domain = url.substring(domainInt, url.indexOf('/', domainInt));

                    try {
                        final Database_ReadLater db = new Database_ReadLater(Browser.this);
                        db.addBookmark(domain, url);
                        db.close();
                        Snackbar.make(mWebView, R.string.readLater_added, Snackbar.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;

            case ID_COPY_LINK:
                if (url != null) {
                    ClipboardManager clipboard = (ClipboardManager) Browser.this
                            .getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
                    Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT).show();
                }
                break;

            case ID_SHARE_LINK:
                if (url != null) {
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, url);
                    sendIntent.setType("text/plain");
                    Browser.this.startActivity(Intent.createChooser(sendIntent,
                            getResources().getString(R.string.app_share_link)));
                }
                break;
            }
            return true;
        }
    };

    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) {
        menu.add(0, ID_SAVE_IMAGE, 0, getString(R.string.context_saveImage))
                .setOnMenuItemClickListener(handler);
        menu.add(0, ID_SHARE_IMAGE, 0, getString(R.string.context_shareImage))
                .setOnMenuItemClickListener(handler);
        menu.add(0, ID_READ_LATER, 0, getString(R.string.context_readLater))
                .setOnMenuItemClickListener(handler);
    } else if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
        menu.add(0, ID_COPY_LINK, 0, getString(R.string.menu_share_link_copy))
                .setOnMenuItemClickListener(handler);
        menu.add(0, ID_SHARE_LINK, 0, getString(R.string.menu_share_link)).setOnMenuItemClickListener(handler);
        menu.add(0, ID_READ_LATER, 0, getString(R.string.context_readLater))
                .setOnMenuItemClickListener(handler);
    }
}

From source file:de.baumann.browser.Browser_right.java

@SuppressLint("SetJavaScriptEnabled")
@Override// w w w  .  j  a  v  a  2  s  . c o m
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_search) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.showKeyboard(Browser_right.this, editText, 3, "", getString(R.string.app_search_hint));
    }

    if (id == R.id.action_history) {
        helper_main.switchToActivity(Browser_right.this, Popup_history.class, "", false);
    }

    if (id == R.id.action_search_chooseWebsite) {
        helper_editText.editText_searchWeb(editText, Browser_right.this);
    }

    if (id == R.id.action_pass) {
        helper_main.switchToActivity(Browser_right.this, Popup_pass.class, "", false);
        sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
        sharedPref.edit().putString("pass_copy_title", mWebView.getTitle()).apply();
    }

    if (id == R.id.action_toggle) {

        sharedPref.edit().putString("started", "yes").apply();

        if (Uri.parse(mWebView.getUrl()).getHost().length() == 0) {
            domain = getString(R.string.app_domain);
        } else {
            domain = Uri.parse(mWebView.getUrl()).getHost();
        }

        final String whiteList = sharedPref.getString("whiteList", "");

        AlertDialog.Builder builder = new AlertDialog.Builder(Browser_right.this);
        View dialogView = View.inflate(Browser_right.this, R.layout.dialog_toggle, null);

        Switch sw_java = (Switch) dialogView.findViewById(R.id.switch1);
        Switch sw_pictures = (Switch) dialogView.findViewById(R.id.switch2);
        Switch sw_location = (Switch) dialogView.findViewById(R.id.switch3);
        Switch sw_cookies = (Switch) dialogView.findViewById(R.id.switch4);
        final ImageButton whiteList_js = (ImageButton) dialogView.findViewById(R.id.imageButton_js);

        if (whiteList.contains(domain)) {
            whiteList_js.setImageResource(R.drawable.check_green);
        } else {
            whiteList_js.setImageResource(R.drawable.close_red);
        }
        if (sharedPref.getString("java_string", "True").equals(getString(R.string.app_yes))) {
            sw_java.setChecked(true);
        } else {
            sw_java.setChecked(false);
        }
        if (sharedPref.getString("pictures_string", "True").equals(getString(R.string.app_yes))) {
            sw_pictures.setChecked(true);
        } else {
            sw_pictures.setChecked(false);
        }
        if (sharedPref.getString("loc_string", "True").equals(getString(R.string.app_yes))) {
            sw_location.setChecked(true);
        } else {
            sw_location.setChecked(false);
        }
        if (sharedPref.getString("cookie_string", "True").equals(getString(R.string.app_yes))) {
            sw_cookies.setChecked(true);
        } else {
            sw_cookies.setChecked(false);
        }

        whiteList_js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (whiteList.contains(domain)) {
                    whiteList_js.setImageResource(R.drawable.close_red);
                    String removed = whiteList.replaceAll(domain, "");
                    sharedPref.edit().putString("whiteList", removed).apply();
                } else {
                    whiteList_js.setImageResource(R.drawable.check_green);
                    sharedPref.edit().putString("whiteList", whiteList + " " + domain).apply();
                }
            }
        });
        sw_java.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("java_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(true);
                } else {
                    sharedPref.edit().putString("java_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(false);
                }

            }
        });
        sw_pictures.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(true);
                } else {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(false);
                }

            }
        });
        sw_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setGeolocationEnabled(true);
                    helper_main.grantPermissionsLoc(Browser_right.this);
                } else {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setGeolocationEnabled(false);
                }

            }
        });
        sw_cookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_yes)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(true);
                } else {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_no)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(false);
                }

            }
        });

        builder.setView(dialogView);
        builder.setTitle(R.string.menu_toggle_title);
        builder.setPositiveButton(R.string.toast_yes, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                mWebView.reload();
            }
        });
        builder.setNegativeButton(R.string.menu_settings, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
                sharedPref.edit().putString("lastActivity", "browser_right").apply();
                helper_main.switchToActivity(Browser_right.this, Activity_settings.class, "", true);
            }
        });

        final AlertDialog dialog = builder.create();
        // Display the custom alert dialog on interface
        dialog.show();

    }

    if (id == R.id.menu_save_screenshot) {
        screenshot();
    }

    if (id == R.id.menu_save_bookmark) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.editText_saveBookmark(editText, Browser_right.this, mWebView);
    }

    if (id == R.id.menu_save_readLater) {
        DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_right.this);
        db.open();
        if (db.isExist(mWebView.getUrl())) {
            Snackbar.make(editText, getString(R.string.toast_newTitle), Snackbar.LENGTH_LONG).show();
        } else {
            db.insert(helper_webView.getTitle(mWebView), mWebView.getUrl(), "", "", helper_main.createDate());
            Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG).show();
        }
    }

    if (id == R.id.menu_save_pass) {
        helper_editText.editText_savePass(Browser_right.this, mWebView, mWebView.getTitle(), mWebView.getUrl());
    }

    if (id == R.id.menu_createShortcut) {
        Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.setClassName(Browser_right.this, "de.baumann.browser.Browser_left");
        i.setData(Uri.parse(mWebView.getUrl()));

        Intent shortcut = new Intent();
        shortcut.putExtra("android.intent.extra.shortcut.INTENT", i);
        shortcut.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mWebView.getTitle());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                .fromContext(Browser_right.this.getApplicationContext(), R.mipmap.ic_launcher));
        shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        Browser_right.this.sendBroadcast(shortcut);
        Snackbar.make(mWebView, R.string.menu_createShortcut_success, Snackbar.LENGTH_SHORT).show();
    }

    if (id == R.id.menu_share_screenshot) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/png");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
        Uri bmpUri = Uri.fromFile(shareFile);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        startActivity(Intent.createChooser(sharingIntent, (getString(R.string.app_share_screenshot))));
    }

    if (id == R.id.menu_share_link) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
        startActivity(Intent.createChooser(sharingIntent, (getString(R.string.app_share_link))));
    }

    if (id == R.id.menu_share_link_copy) {
        String url = mWebView.getUrl();
        ClipboardManager clipboard = (ClipboardManager) Browser_right.this
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
        Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT).show();
    }

    if (id == R.id.action_downloads) {
        helper_main.switchToActivity(Browser_right.this, Popup_files.class, "", false);
    }

    if (id == R.id.action_search_go) {

        String text = editText.getText().toString();
        helper_webView.openURL(Browser_right.this, mWebView, editText);
        helper_editText.hideKeyboard(Browser_right.this, editText, 0, text,
                getString(R.string.app_search_hint));
        helper_editText.editText_EditorAction(editText, Browser_right.this, mWebView, urlBar);
        urlBar.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
    }

    if (id == R.id.action_search_onSite) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.showKeyboard(Browser_right.this, editText, 1, "", getString(R.string.app_search_hint));
        helper_editText.editText_FocusChange_searchSite(editText, Browser_right.this);
        helper_editText.editText_searchSite(editText, Browser_right.this, mWebView, urlBar);
    }

    if (id == R.id.action_search_onSite_go) {

        String text = editText.getText().toString();

        if (text.startsWith(getString(R.string.app_search))) {
            helper_editText.editText_searchSite(editText, Browser_right.this, mWebView, urlBar);
        } else {
            mWebView.findAllAsync(text);
            helper_editText.hideKeyboard(Browser_right.this, editText, 1,
                    getString(R.string.app_search) + " " + text, getString(R.string.app_search_hint_site));
        }
    }

    if (id == R.id.action_prev) {
        mWebView.findNext(false);
    }

    if (id == R.id.action_next) {
        mWebView.findNext(true);
    }

    if (id == R.id.action_cancel) {
        urlBar.setVisibility(View.VISIBLE);
        urlBar.setText(mWebView.getTitle());
        editText.setVisibility(View.GONE);
        helper_editText.editText_FocusChange(editText, Browser_right.this);
        helper_editText.editText_EditorAction(editText, Browser_right.this, mWebView, urlBar);
        helper_editText.hideKeyboard(Browser_right.this, editText, 0, mWebView.getTitle(),
                getString(R.string.app_search_hint));
    }

    if (id == R.id.action_save_bookmark) {
        helper_editText.editText_saveBookmark_save(editText, Browser_right.this, mWebView);
        urlBar.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
    }

    return super.onOptionsItemSelected(item);
}

From source file:sjizl.com.ChatActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    String textTocopy = ArrChatLines.get(index).getLaatstBericht();

    switch (item.getItemId()) {
    case R.id.delete:
        //   quoteResult.remove(info.position);
        //   ((StockQuoteAdapter)getListAdapter()).notifyDataSetChanged();

        Toast.makeText(getApplicationContext(), "delete almost implementated", Toast.LENGTH_SHORT).show();
        return false;

    case R.id.copy:
        //   quoteResult.remove(info.position);
        //   ((StockQuoteAdapter)getListAdapter()).notifyDataSetChanged();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(
                    Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("simple text", textTocopy);
            clipboard.setPrimaryClip(clip);
        } else {/*from w w w  .j  a v  a2  s  . c  o m*/
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(
                    Context.CLIPBOARD_SERVICE);
            clipboard.setText(textTocopy);

        }

        //place your TextView's text in clipboard

        Toast.makeText(getApplicationContext(), "Item copied", Toast.LENGTH_SHORT).show();
        return false;
    }
    return false;
}