Example usage for android.os Environment DIRECTORY_DOWNLOADS

List of usage examples for android.os Environment DIRECTORY_DOWNLOADS

Introduction

In this page you can find the example usage for android.os Environment DIRECTORY_DOWNLOADS.

Prototype

String DIRECTORY_DOWNLOADS

To view the source code for android.os Environment DIRECTORY_DOWNLOADS.

Click Source Link

Document

Standard directory in which to place files that have been downloaded by the user.

Usage

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  w  w w  .  j a va2s .c  om
                }).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:com.adamas.client.android.MainActivity.java

@Override
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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if (id == R.id.action_scan_qr_code) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            IntentIntegrator integrator = new IntentIntegrator(this);
            integrator.initiateScan();/*  w w w  .  j ava2 s  . c  om*/
        } else {
            checkCameraPermission();
        }
    } else if (id == R.id.action_delete_connector) {
        if (_connectorfragment != null) {
            _connectorfragment.deleteConnector(_selectedAdamasConnector);
        }
    } else if (id == R.id.action_add_connector_manually) {
        Intent intent = new Intent(this, AddConnectorActivity.class);
        startActivityForResult(intent, MANUALLY_ADD_CONNECTOR_REQUEST_CODE);
    } else if (id == R.id.action_edit_connector) {
        Intent intent = new Intent(this, EditConnectorActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(EditConnectorActivity.ADAMAS_CONNECTOR, _selectedAdamasConnector);
        intent.putExtras(mBundle);
        startActivityForResult(intent, MANUALLY_EDIT_CONNECTOR_REQUEST_CODE);
    } else if (id == R.id.action_import_connector_from_text) {
        Intent intent = new Intent(this, ImportTextActivity.class);
        startActivityForResult(intent, IMPORT_FROM_TEXT_REQUEST_CODE);
    } else if (id == R.id.action_import_connector_from_image) {
        if (false) {
            FileChooser fileChooser = new FileChooser(this);
            fileChooser.setFileListener(new FileChooser.FileSelectedListener() {
                @Override
                public void fileSelected(final File file) {
                    String name = file.getAbsolutePath();
                    name = name;
                }
            });
            fileChooser.showDialog();
        } else {
            // this is much better
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                    .getAbsolutePath());
            intent.setDataAndType(uri, "image/jpeg");
            startActivityForResult(Intent.createChooser(intent, getString(R.string.open)),
                    IMPORT_FROM_FILE_REQUEST_CODE);
        }
    }

    return super.onOptionsItemSelected(item);
}

From source file:com.support.android.designlibdemo.activities.CampaignDetailActivity.java

public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;/* w w  w. ja  va  2  s .c o  m*/
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }

    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;

}

From source file:com.wondertoys.pokevalue.utils.AutoUpdateApk.java

protected void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    String update_file = preferences.getString(UPDATE_FILE, "");
    if (update_file.length() > 0) {
        setChanged();//from   www . j a  v a2s .  co  m
        notifyObservers(AUTOUPDATE_HAVE_UPDATE);

        // raise the notification
        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(Uri.parse(
                "file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"
                        + update_file),
                ANDROID_PACKAGE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setDefaults(
                Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        builder.setSmallIcon(appIcon);
        builder.setTicker(appName + " update");
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        builder.setContentIntent(contentIntent);
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);
        builder.setOngoing(true);

        nm.notify(NOTIFICATION_ID, builder.build());
    } else {
        //nm.cancel( NOTIFICATION_ID );   // tried this, but it just doesn't do the trick =(
        nm.cancelAll();
    }
}

From source file:co.scandy.example.scandycoreandroidexample.MainActivity.java

@SuppressWarnings("StatementWithEmptyBody")
@Override//from   w w w.  j a va  2s.c  o m
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.initialize_button) {
        // Setup the scan_controls
        if (mScanControlsView == null) {
            // Find the controls_container
            ViewStub stub = (ViewStub) findViewById(R.id.controls_container);
            // Set the resource to be the scan_controls
            stub.setLayoutResource(R.layout.scan_controls);
            // Store the view inflated as the mScanControlsView
            mScanControlsView = (LinearLayout) stub.inflate();
            // Bind the newly created controls to their callbacks
            bindScanControls();
        } else {
            // Or just make them visible again
            mScanControlsView.setVisibility(View.VISIBLE);
        }

        // Initialize the scanner with the first file we find in the Download directory
        File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .getAbsolutePath());
        String rrf_path = "";
        // NOTE: you could present your users with a list of files to chose from if you felt like it!
        for (File file : dir.listFiles()) {
            if (file.getName().contains(".rrf")) {
                rrf_path = file.getAbsolutePath();
                break;
            }
        }

        // check to see if we have a USB sensor attached
        if (ScandyCore.hasValidSensor()) {
            ScandyCore.initializeScanner();
        } else if (rrf_path != "") {
            ScandyCore.initializeScanner(rrf_path);
        } else {
            // Show an Alert that we didn't find anything to initialize a scanner with
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("No scanner found");
            alertDialog.setMessage("Didn't find a pico flexx or a pre-recorded file.");
            alertDialog.show();
        }

    } else if (id == R.id.loadmesh_button) {
        // Make sure to uninitialize the scanner. Scandy Core gracefully handles various states.
        ScandyCore.uninitializeScanner();

        try {
            // Lets load a test Obj
            ScandyCore
                    .loadMeshFromURL(new URL("https://s3.amazonaws.com/scandycore-test-assets/scandy-obj.zip"));
        } catch (MalformedURLException e) {
            Log.e(TAG, e.getMessage());
        }
    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

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();
                    }/*  w w w .  j  a v a  2 s  .  com*/
                } 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:com.viktorrudometkin.burramys.fragment.EntryFragment.java

@Override
public void onClickEnclosure() {
    getActivity().runOnUiThread(new Runnable() {
        @Override//from ww w. j a  v  a2  s.  c  o  m
        public void run() {
            final String enclosure = mEntryPagerAdapter.getCursor(mCurrentPagerPos).getString(mEnclosurePos);

            final int position1 = enclosure.indexOf(Constants.ENCLOSURE_SEPARATOR);
            final int position2 = enclosure.indexOf(Constants.ENCLOSURE_SEPARATOR, position1 + 3);

            final Uri uri = Uri.parse(enclosure.substring(0, position1));
            final String filename = uri.getLastPathSegment();

            new AlertDialog.Builder(getActivity()).setTitle(R.string.open_enclosure)
                    .setMessage(getString(R.string.file) + ": " + filename)
                    .setPositiveButton(R.string.open_link, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            showEnclosure(uri, enclosure, position1, position2);
                        }
                    }).setNegativeButton(R.string.download_and_save, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                DownloadManager.Request r = new DownloadManager.Request(uri);
                                r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
                                r.allowScanningByMediaScanner();
                                r.setNotificationVisibility(
                                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadManager dm = (DownloadManager) MainApplication.getContext()
                                        .getSystemService(Context.DOWNLOAD_SERVICE);
                                dm.enqueue(r);
                            } catch (Exception e) {
                                UiUtils.showMessage(getActivity(), R.string.error);
                            }
                        }
                    }).show();
        }
    });
}

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();
                    }//  w w w  .j a  va  2 s  .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:com.carlrice.reader.fragment.EntryFragment.java

@Override
public void onClickEnclosure() {
    getActivity().runOnUiThread(new Runnable() {
        @Override//www  .  ja v  a  2 s. co  m
        public void run() {
            final String enclosure = mEntryPagerAdapter.getCursor(mCurrentPagerPos).getString(mEnclosurePos);

            final int position1 = enclosure.indexOf(Constants.ENCLOSURE_SEPARATOR);
            final int position2 = enclosure.indexOf(Constants.ENCLOSURE_SEPARATOR, position1 + 3);

            final Uri uri = Uri.parse(enclosure.substring(0, position1));
            final String filename = uri.getLastPathSegment();

            new AlertDialog.Builder(getActivity()).setTitle(R.string.open_enclosure)
                    .setMessage(getString(R.string.file) + ": " + filename)
                    .setPositiveButton(R.string.open_link, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            showEnclosure(uri, enclosure, position1, position2);
                        }
                    }).setNegativeButton(R.string.download_and_save, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                DownloadManager.Request r = new DownloadManager.Request(uri);
                                r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
                                r.allowScanningByMediaScanner();
                                r.setNotificationVisibility(
                                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadManager dm = (DownloadManager) Application.context()
                                        .getSystemService(Context.DOWNLOAD_SERVICE);
                                dm.enqueue(r);
                            } catch (Exception e) {
                                Toast.makeText(getActivity(), R.string.error, Toast.LENGTH_LONG).show();
                            }
                        }
                    }).show();
        }
    });
}

From source file:com.flym.dennikn.fragment.EntryFragment.java

@Override
public void onClickEnclosure() {
    getActivity().runOnUiThread(new Runnable() {
        @Override/*ww  w  .  ja va  2  s . c om*/
        public void run() {
            final String enclosure = mEntryPagerAdapter.getCursor(mCurrentPagerPos).getString(mEnclosurePos);

            final int position1 = enclosure.indexOf(Constants.ENCLOSURE_SEPARATOR);
            final int position2 = enclosure.indexOf(Constants.ENCLOSURE_SEPARATOR, position1 + 3);

            final Uri uri = Uri.parse(enclosure.substring(0, position1));
            final String filename = uri.getLastPathSegment();

            new AlertDialog.Builder(getActivity()).setTitle(R.string.open_enclosure)
                    .setMessage(getString(R.string.file) + ": " + filename)
                    .setPositiveButton(R.string.open_link, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            showEnclosure(uri, enclosure, position1, position2);
                        }
                    }).setNegativeButton(R.string.download_and_save, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                DownloadManager.Request r = new DownloadManager.Request(uri);
                                r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
                                r.allowScanningByMediaScanner();
                                r.setNotificationVisibility(
                                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadManager dm = (DownloadManager) MainApplication.getContext()
                                        .getSystemService(Context.DOWNLOAD_SERVICE);
                                dm.enqueue(r);
                            } catch (Exception e) {
                                Toast.makeText(getActivity(), R.string.error, Toast.LENGTH_LONG).show();
                            }
                        }
                    }).show();
        }
    });
}