Example usage for android.text.style URLSpan getURL

List of usage examples for android.text.style URLSpan getURL

Introduction

In this page you can find the example usage for android.text.style URLSpan getURL.

Prototype

public String getURL() 

Source Link

Document

Get the url string for this span.

Usage

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            if (!mLoadingPlugin) {
                mLoadingPlugin = true;/*from  www . ja  v  a  2  s. c  o  m*/
                String url = span.getURL();

                if (url.startsWith("http://play.google.com/store/apps/details?id=")) {
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse(url.replace("http://play.google.com/store/apps", "market:/"))));
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    }

                    mLoadingPlugin = false;
                } else if (url.startsWith("plugin://") || url.startsWith("plugins://")) {
                    final File path = IOUtils.getDownloadDirectory(getApplicationContext());

                    if (!path.isDirectory()) {
                        path.mkdirs();
                    }

                    if (url.startsWith("plugin://")) {
                        url = url.replace("plugin://", "http://");
                    } else if (url.startsWith("plugins://")) {
                        url = url.replace("plugins://", "https://");
                    }

                    String name = url.substring(url.lastIndexOf("/") + 1);

                    mCurrentDownloadPlugin = new File(path, name);

                    if (mCurrentDownloadPlugin.isFile()) {
                        mCurrentDownloadPlugin.delete();
                    }

                    final String downloadUrl = url;

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            AsyncTask<String, Void, Boolean> async = new AsyncTask<String, Void, Boolean>() {
                                private ProgressDialog mProgress;
                                private File mPluginFile;

                                protected void onPreExecute() {
                                    mProgress = new ProgressDialog(TvBrowser.this);
                                    mProgress.setMessage(getString(R.string.plugin_info_donwload).replace("{0}",
                                            mCurrentDownloadPlugin.getName()));
                                    mProgress.show();
                                };

                                @Override
                                protected Boolean doInBackground(String... params) {
                                    mPluginFile = new File(params[0]);
                                    return IOUtils.saveUrl(params[0], params[1], 15000);
                                }

                                protected void onPostExecute(Boolean result) {
                                    mProgress.dismiss();

                                    if (result) {
                                        Intent intent = new Intent(Intent.ACTION_VIEW);
                                        intent.setDataAndType(Uri.fromFile(mPluginFile),
                                                "application/vnd.android.package-archive");
                                        TvBrowser.this.startActivityForResult(intent, INSTALL_PLUGIN);
                                    }

                                    mLoadingPlugin = false;
                                };
                            };

                            async.execute(mCurrentDownloadPlugin.toString(), downloadUrl);
                        }
                    });
                } else {
                    mLoadingPlugin = false;
                }
            }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}