Example usage for android.webkit WebView getHandler

List of usage examples for android.webkit WebView getHandler

Introduction

In this page you can find the example usage for android.webkit WebView getHandler.

Prototype

@Override
    public Handler getHandler() 

Source Link

Usage

From source file:com.just.agentweb.AgentWebUtils.java

static final void clearWebView(WebView m) {

    if (m == null) {
        return;/*from   w  w  w  .j a  v  a 2s  . c  o  m*/
    }
    if (Looper.myLooper() != Looper.getMainLooper()) {
        return;
    }
    m.loadUrl("about:blank");
    m.stopLoading();
    if (m.getHandler() != null) {
        m.getHandler().removeCallbacksAndMessages(null);
    }
    m.removeAllViews();
    ViewGroup mViewGroup = null;
    if ((mViewGroup = ((ViewGroup) m.getParent())) != null) {
        mViewGroup.removeView(m);
    }
    m.setWebChromeClient(null);
    m.setWebViewClient(null);
    m.setTag(null);
    m.clearHistory();
    m.destroy();
    m = null;

}

From source file:com.free.searcher.MainFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewContainer, Bundle savedInstanceState) {

    super.onCreateView(inflater, viewContainer, savedInstanceState);
    this.activity = getActivity();
    actionBar = activity.getActionBar();

    View v = inflater.inflate(R.layout.main, viewContainer, false);
    v.setOnSystemUiVisibilityChangeListener(this);

    webView = (WebView) v.findViewById(R.id.webView1);
    statusView = (TextView) v.findViewById(R.id.statusView);

    if (webViewBundle != null) {
        webView.restoreState(webViewBundle);
        Log.d("onCreateView.webView.restoreState", webViewBundle + "");
    } else if (currentUrl.length() > 0) {
        Log.d("onCreateView.locX, locY", locX + ", " + locY + ", " + currentUrl);
        webView.loadUrl(currentUrl);/*from  w  ww .j  a v a2  s  .c o m*/
        webView.setScrollX(locX);
        webView.setScrollY(locY);
        Log.d("currentUrl 8", currentUrl);
    }
    statusView.setText(status);

    Log.d("onCreateView.savedInstanceState", savedInstanceState + "vvv");
    mNotificationManager = (NotificationManager) activity.getSystemService(activity.NOTIFICATION_SERVICE);

    webView.setFocusable(true);
    webView.setFocusableInTouchMode(true);
    webView.requestFocus();
    webView.requestFocusFromTouch();
    webView.getSettings().setAllowContentAccess(false);
    webView.getSettings().setPluginState(WebSettings.PluginState.OFF);
    // webView.setBackgroundColor(LIGHT_BLUE);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.setBackgroundColor(getResources().getColor(R.color.lightyellow));

    webView.setOnLongClickListener(this);
    statusView.setOnLongClickListener(this);

    webView.setWebViewClient(new WebViewClient() {

        private void jumpTo(final int xLocation, final int yLocation) {
            webView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Log.d("jumpTo1.locX, locY", xLocation + ", " + yLocation + ", " + currentUrl);
                    try {
                        webView.scrollTo(xLocation, yLocation);
                        webView.setScrollX(xLocation);
                        webView.setScrollY(yLocation);
                        //                           locX = 0;
                        //                           locY = 0;
                    } catch (RuntimeException e) {
                        Log.e("error jumpTo2.locX, locY", locX + ", " + locY + ", " + currentUrl);
                    }
                }
            }, 100);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, final String url) {
            if (currentZipFileName.length() > 0 && (extractFile == null || extractFile.isClosed())) {
                try {
                    extractFile = new ExtractFile(currentZipFileName,
                            MainFragment.PRIVATE_PATH + currentZipFileName);
                } catch (RarException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int ind = url.indexOf("?deleteFile=");
            if (ind >= 0) {
                if (dupTask == null) {
                    showToast("Please do duplicate finding again.");
                    return true;
                }

                String urlStatus = Util.getUrlStatus(url);
                final String selectedFile = urlStatus.substring(urlStatus.indexOf("?deleteFile=") + 12,
                        urlStatus.length());
                Log.d("deleteFile", "url=" + url + ", urlStatus=" + urlStatus);

                AlertDialog.Builder alert = new AlertDialog.Builder(activity);
                alert.setTitle("Delete File?");
                alert.setMessage("Do you really want to delete file \"" + selectedFile + "\"?");
                alert.setCancelable(true);
                alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            locX = webView.getScrollX();
                            locY = webView.getScrollY();
                            webView.loadUrl(
                                    new File(dupTask.deleteFile(selectedFile)).toURI().toURL().toString());
                        } catch (IOException e) {
                            statusView.setText(e.getMessage());
                            Log.d("deleteFile", e.getMessage(), e);
                        }
                    }
                });
                alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
                return true;
            }

            ind = url.indexOf("?deleteGroup=");
            if (ind >= 0) {
                if (dupTask == null) {
                    showToast("Please do duplicate finding again.");
                    return true;
                }

                String urlStatus = Util.getUrlStatus(url);
                final String groupFile = urlStatus.substring(urlStatus.indexOf("?deleteGroup=") + 13,
                        urlStatus.length());
                int indexOf = groupFile.indexOf(",");
                final int group = Integer.parseInt(groupFile.substring(0, indexOf));
                final String selectedFile = groupFile.substring(indexOf + 1);
                Log.d("groupFile", ",groupFile=" + groupFile + ", url=" + url + ", urlStatus=" + urlStatus);

                AlertDialog.Builder alert = new AlertDialog.Builder(activity);
                alert.setTitle("Delete Group of Files?");
                alert.setMessage(
                        "Do you really want to delete this group, except file \"" + selectedFile + "\"?");
                alert.setCancelable(true);
                alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        locX = webView.getScrollX();
                        locY = webView.getScrollY();
                        webView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    webView.loadUrl(new File(dupTask.deleteGroup(group, selectedFile)).toURI()
                                            .toURL().toString());
                                } catch (Throwable e) {
                                    statusView.setText(e.getMessage());
                                    Log.e("Delete Group", e.getMessage(), e);
                                    Log.e("Delete Group", locX + ", " + locY + ", " + currentUrl);
                                }
                            }
                        }, 0);
                    }
                });
                alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
                return true;
            }

            ind = url.indexOf("?deleteFolder=");
            if (ind >= 0) {
                if (dupTask == null) {
                    showToast("Please do duplicate finding again.");
                    return true;
                }

                String urlStatus = Util.getUrlStatus(url);
                final String selectedFile = urlStatus.substring(urlStatus.indexOf("?deleteFolder=") + 14,
                        urlStatus.length());
                Log.d("deleteFolder",
                        ",deleteFolder=" + selectedFile + ", url=" + url + ", urlStatus=" + urlStatus);
                AlertDialog.Builder alert = new AlertDialog.Builder(activity);
                alert.setTitle("Delete folder?");
                alert.setMessage("Do you really want to delete duplicate in folder \""
                        + selectedFile.substring(0, selectedFile.lastIndexOf("/")) + "\"?");
                alert.setCancelable(true);
                alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        locX = webView.getScrollX();
                        locY = webView.getScrollY();
                        webView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    webView.loadUrl(new File(dupTask.deleteFolder(selectedFile)).toURI().toURL()
                                            .toString());
                                } catch (Throwable e) {
                                    statusView.setText(e.getMessage());
                                    Log.e("Delete folder", e.getMessage(), e);
                                    Log.e("Delete folder", locX + ", " + locY + ", " + currentUrl);
                                }
                            }
                        }, 0);
                    }
                });
                alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
                return true;
            }

            ind = url.indexOf("?deleteSub=");
            if (ind >= 0) {
                if (dupTask == null) {
                    showToast("Please do duplicate finding again.");
                    return true;
                }

                String urlStatus = Util.getUrlStatus(url);

                final String selectedFile = urlStatus.substring(urlStatus.indexOf("?deleteSub=") + 11,
                        urlStatus.length());
                Log.d("deleteSub", ",deleteSub=" + selectedFile + ", url=" + url + ", urlStatus=" + urlStatus);
                AlertDialog.Builder alert = new AlertDialog.Builder(activity);
                alert.setTitle("Delete sub folder?");
                alert.setMessage("Do you really want to delete duplicate files in sub folder of \""
                        + selectedFile.substring(0, selectedFile.lastIndexOf("/")) + "\"?");
                alert.setCancelable(true);
                alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        locX = webView.getScrollX();
                        locY = webView.getScrollY();
                        webView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    webView.loadUrl(new File(dupTask.deleteSubFolder(selectedFile)).toURI()
                                            .toURL().toString());
                                } catch (Throwable e) {
                                    statusView.setText(e.getMessage());
                                    Log.e("Delete sub folder", e.getMessage(), e);
                                    Log.e("Delete sub folder", locX + ", " + locY + ", " + currentUrl);
                                }
                            }
                        }, 0);
                    }
                });
                alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
                return true;
            }

            ind = url.indexOf("?viewName");
            if (ind >= 0) {
                if (dupTask == null) {
                    showToast("Please do duplicate finding again.");
                    return true;
                }
                nameOrder = !nameOrder;
                locX = 0;
                locY = 0;
                Log.d("url=", url + ", viewName");
                try {
                    webView.loadUrl(new File(dupTask.genFile(dupTask.groupList, dupTask.NAME_VIEW)).toURI()
                            .toURL().toString());
                } catch (IOException e) {
                    Log.e("viewName", e.getMessage(), e);
                }
                return true;
            }

            ind = url.indexOf("?viewGroup");
            if (ind >= 0) {
                if (dupTask == null) {
                    showToast("Please do duplicate finding again.");
                    return true;
                }
                groupViewChanged = true;
                locX = 0;
                locY = 0;
                Log.d("url=", url + ", viewGroup");
                try {
                    webView.loadUrl(new File(dupTask.genFile(dupTask.groupList, dupTask.GROUP_VIEW)).toURI()
                            .toURL().toString());
                } catch (IOException e) {
                    Log.e("viewGroup", e.getMessage(), e);
                }
                return true;
            }
            if (zextr == null) {
                locX = 0;
                locY = 0;
            } else {
                zextr = null;
            }

            if (MainActivity.popup) {
                final MainFragment frag = ((MainActivity) activity).addFragmentToStack();
                frag.status = Util.getUrlStatus(url);
                frag.load = load;
                frag.currentSearching = currentSearching;
                frag.selectedFiles = selectedFiles;
                frag.files = files;
                frag.currentZipFileName = currentZipFileName;
                if (extractFile != null) {
                    try {
                        frag.extractFile = new ExtractFile();
                        extractFile.copyTo(frag.extractFile);
                    } catch (RarException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                frag.home = home;
                //                  if (mSearchView != null && mSearchView.getQuery().length() > 0) {
                //                     frag.mSearchView.setQuery(mSearchView.getQuery(), false);
                //                  }
                view.getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        frag.webTask = new WebTask(MainFragment.this);
                        frag.webTask.init(frag.webView, url);
                        frag.webTask.execute();
                        frag.statusView.setText(frag.status);
                    }
                }, 100);
            } else {
                currentUrl = url;
                Log.d("currentUrl 19", currentUrl);
                status = Util.getUrlStatus(url);
                statusView.setText("Opening " + url + "...");
                webTask = new WebTask(MainFragment.this, webView, url, status.toString());
                webTask.execute();
            }
            //               setNavVisibility(false);
            return true;
        }

        //         @Override
        //         public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        //         }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (container != null) {
                if (showFind) {
                    container.setVisibility(View.VISIBLE);
                    webView.findAllAsync(findBox.getText().toString());
                } else {
                    container.setVisibility(View.INVISIBLE);
                }
            }
            Log.d("onPageFinished", locX + ", " + locY + ", currentUrl=" + currentUrl + ", url=" + url);
            setNavVisibility(false);
            if (!backForward) { //if (zextr != null) {
                // zextr = null;
                jumpTo(locX, locY);
            } else {
                backForward = false;
            }
            //               locX = 0;
            //               locY = 0;
            Log.d("onPageFinished", url);
            /* This call inject JavaScript into the page which just finished loading. */
            //              webView.loadUrl("javascript:window.HTMLOUT.processHTML(" +
            //                    "'<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
        }
    });

    WebSettings settings = webView.getSettings();
    //      webView.setWebViewClient(new WebViewClient());
    //      webView.setWebChromeClient(new WebChromeClient());
    //      webView.addJavascriptInterface(new HtmlSourceViewJavaScriptInterface(), "HTMLOUT");
    settings.setMinimumFontSize(13);
    settings.setJavaScriptEnabled(true);
    settings.setDefaultTextEncodingName("UTF-8");
    settings.setBuiltInZoomControls(true);
    // settings.setSansSerifFontFamily("Tahoma");
    settings.setEnableSmoothTransition(true);

    return v;
}