Example usage for android.webkit WebView clearCache

List of usage examples for android.webkit WebView clearCache

Introduction

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

Prototype

public void clearCache(boolean includeDiskFiles) 

Source Link

Document

Clears the resource cache.

Usage

From source file:org.zeroxlab.zeroxbenchmark.Benchmark.java

private void initViews() {
        /*/*  w w w . ja  v a  2 s.co m*/
        mRun = (Button)findViewById(R.id.btn_run);
        mRun.setOnClickListener(this);
            
        mShow = (Button)findViewById(R.id.btn_show);
        mShow.setOnClickListener(this);
        mShow.setClickable(false);
            
        mLinearLayout = (LinearLayout)findViewById(R.id.list_container);
        mMainView = (LinearLayout)findViewById(R.id.main_view);
            
        mBannerInfo = (TextView)findViewById(R.id.banner_info);
        mBannerInfo.setText("Hello!\nSelect cases to Run.\nUploaded results:\nhttp://0xbenchmark.appspot.com");
        */

        mTabHost = getTabHost();

        int length = mCases.size();
        mCheckList = new CheckBox[length];
        mDesc = new TextView[length];
        for (int i = 0; i < length; i++) {
            mCheckList[i] = new CheckBox(this);
            mCheckList[i].setText(mCases.get(i).getTitle());
            mDesc[i] = new TextView(this);
            mDesc[i].setText(mCases.get(i).getDescription());
            mDesc[i].setTextSize(mDesc[i].getTextSize() - 2);
            mDesc[i].setPadding(42, 0, 10, 10);
        }

        TabContentFactory mTCF = new TabContentFactory() {
            public View createTabContent(String tag) {
                ViewGroup.LayoutParams fillParent = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.FILL_PARENT);
                ViewGroup.LayoutParams fillWrap = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams wrapContent = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                wrapContent.gravity = Gravity.CENTER;
                LinearLayout.LayoutParams weightedFillWrap = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                weightedFillWrap.weight = 1;

                if (tag.equals(MAIN)) {
                    LinearLayout mMainView = new LinearLayout(Benchmark.this);
                    mMainView.setOrientation(1);
                    ScrollView mListScroll = new ScrollView(Benchmark.this);

                    LinearLayout mMainViewContainer = new LinearLayout(Benchmark.this);
                    mMainViewContainer.setOrientation(1);
                    ImageView mIconView = new ImageView(Benchmark.this);
                    mIconView.setImageResource(R.drawable.icon);

                    TextView mBannerInfo = new TextView(Benchmark.this);
                    mBannerInfo.setText("0xbench\nSelect benchmarks in the tabs,\nor batch select:");

                    d2CheckBox = new CheckBox(Benchmark.this);
                    d2CheckBox.setText(D2);
                    d2CheckBox.setOnClickListener(Benchmark.this);

                    d3CheckBox = new CheckBox(Benchmark.this);
                    d3CheckBox.setText(D3);
                    d3CheckBox.setOnClickListener(Benchmark.this);

                    mathCheckBox = new CheckBox(Benchmark.this);
                    mathCheckBox.setText(MATH);
                    mathCheckBox.setOnClickListener(Benchmark.this);

                    vmCheckBox = new CheckBox(Benchmark.this);
                    vmCheckBox.setText(VM);
                    vmCheckBox.setOnClickListener(Benchmark.this);

                    nativeCheckBox = new CheckBox(Benchmark.this);
                    nativeCheckBox.setText(NATIVE);
                    nativeCheckBox.setOnClickListener(Benchmark.this);

                    miscCheckBox = new CheckBox(Benchmark.this);
                    miscCheckBox.setText(MISC);
                    miscCheckBox.setOnClickListener(Benchmark.this);

                    TextView mWebInfo = new TextView(Benchmark.this);
                    mWebInfo.setText("Uploaded results:\nhttp://0xbenchmark.appspot.com");

                    LinearLayout mButtonContainer = new LinearLayout(Benchmark.this);
                    mRun = new Button(Benchmark.this);
                    mShow = new Button(Benchmark.this);
                    mRun.setText("Run");
                    mShow.setText("Show");
                    mRun.setOnClickListener(Benchmark.this);
                    mShow.setOnClickListener(Benchmark.this);
                    mButtonContainer.addView(mRun, weightedFillWrap);
                    mButtonContainer.addView(mShow, weightedFillWrap);
                    WebView mTracker = new WebView(Benchmark.this);
                    mTracker.clearCache(true);
                    mTracker.setWebViewClient(new WebViewClient() {
                        public void onPageFinished(WebView view, String url) {
                            Log.i(TAG, "Tracker: " + view.getTitle() + " -> " + url);
                        }

                        public void onReceivedError(WebView view, int errorCode, String description,
                                String failingUrl) {
                            Log.e(TAG, "Track err: " + description);
                        }
                    });
                    mTracker.loadUrl(trackerUrl);
                    mMainViewContainer.addView(mIconView, wrapContent);
                    mMainViewContainer.addView(mBannerInfo);
                    mMainViewContainer.addView(mathCheckBox);
                    mMainViewContainer.addView(d2CheckBox);
                    mMainViewContainer.addView(d3CheckBox);
                    mMainViewContainer.addView(vmCheckBox);
                    mMainViewContainer.addView(nativeCheckBox);
                    mMainViewContainer.addView(miscCheckBox);
                    mMainViewContainer.addView(mWebInfo);
                    mMainViewContainer.addView(mButtonContainer, fillWrap);
                    mMainViewContainer.addView(mTracker, 0, 0);
                    mListScroll.addView(mMainViewContainer, fillParent);
                    mMainView.addView(mListScroll, fillWrap);

                    return mMainView;

                }

                LinearLayout mMainView = new LinearLayout(Benchmark.this);
                mMainView.setOrientation(1);
                ScrollView mListScroll = new ScrollView(Benchmark.this);
                LinearLayout mListContainer = new LinearLayout(Benchmark.this);
                mListContainer.setOrientation(1);
                mListScroll.addView(mListContainer, fillParent);
                mMainView.addView(mListScroll, fillWrap);

                boolean gray = true;
                int length = mCases.size();
                Log.i(TAG, "L: " + length);
                Log.i(TAG, "TCF: " + tag);
                for (int i = 0; i < length; i++) {
                    if (!mCategory.get(tag).contains(mCases.get(i)))
                        continue;
                    Log.i(TAG, "Add: " + i);
                    mListContainer.addView(mCheckList[i], fillWrap);
                    mListContainer.addView(mDesc[i], fillWrap);
                    if (gray) {
                        int color = 0xFF333333; //ARGB
                        mCheckList[i].setBackgroundColor(color);
                        mDesc[i].setBackgroundColor(color);
                    }
                    gray = !gray;
                }
                return mMainView;
            }
        };

        mTabHost.addTab(mTabHost.newTabSpec(MAIN).setIndicator(MAIN, getResources().getDrawable(R.drawable.ic_eye))
                .setContent(mTCF));
        mTabHost.addTab(mTabHost.newTabSpec(D2).setIndicator(D2, getResources().getDrawable(R.drawable.ic_2d))
                .setContent(mTCF));
        mTabHost.addTab(mTabHost.newTabSpec(D3).setIndicator(D3, getResources().getDrawable(R.drawable.ic_3d))
                .setContent(mTCF));
        mTabHost.addTab(mTabHost.newTabSpec(MATH).setIndicator(MATH, getResources().getDrawable(R.drawable.ic_pi))
                .setContent(mTCF));
        mTabHost.addTab(mTabHost.newTabSpec(VM).setIndicator(VM, getResources().getDrawable(R.drawable.ic_vm))
                .setContent(mTCF));
        mTabHost.addTab(mTabHost.newTabSpec(NATIVE)
                .setIndicator(NATIVE, getResources().getDrawable(R.drawable.ic_c)).setContent(mTCF));
        mTabHost.addTab(mTabHost.newTabSpec(MISC).setIndicator(MISC, getResources().getDrawable(R.drawable.ic_misc))
                .setContent(mTCF));
    }

From source file:net.basov.ticketinfo.UI.java

public void displayHelpScreen(String title, final WebView wv) {

    setHelp("h_header", title);
    setHelp("h_storage", FileIO.getFilesDir(wv.getContext()).getAbsolutePath());

    wv.setWebViewClient(new MyWebViewClient() {
        @Override//from  w  w  w  . j  a v  a  2 s .  c o m
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(wv, url);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                view.evaluateJavascript("javascript:jreplace('" + help_json.toString() + "')", null);
            } else {
                view.loadUrl("javascript:jreplace('" + help_json.toString() + "')");
            }
            wv.clearCache(true);
        }
    });
    Context c = wv.getContext();
    wv.loadUrl("file:///android_asset/" + c.getString(R.string.help_ui_file));

}

From source file:org.zeroxlab.benchmark.Benchmark.java

private void initViews() {
    /*/*from   ww w.j  a  va  2  s. c  om*/
    mRun = (Button)findViewById(R.id.btn_run);
    mRun.setOnClickListener(this);
            
    mShow = (Button)findViewById(R.id.btn_show);
    mShow.setOnClickListener(this);
    mShow.setClickable(false);
            
    mLinearLayout = (LinearLayout)findViewById(R.id.list_container);
    mMainView = (LinearLayout)findViewById(R.id.main_view);
            
    mBannerInfo = (TextView)findViewById(R.id.banner_info);
    mBannerInfo.setText("Hello!\nSelect cases to Run.\nUploaded results:\nhttp://0xbenchmark.appspot.com");
    */

    mTabHost = getTabHost();

    int length = mCases.size();
    mCheckList = new CheckBox[length];
    mDesc = new TextView[length];
    for (int i = 0; i < length; i++) {
        mCheckList[i] = new CheckBox(this);
        mCheckList[i].setText(mCases.get(i).getTitle());
        mDesc[i] = new TextView(this);
        mDesc[i].setText(mCases.get(i).getDescription());
        mDesc[i].setTextSize(mDesc[i].getTextSize() - 2);
        mDesc[i].setPadding(42, 0, 10, 10);
    }

    TabContentFactory mTCF = new TabContentFactory() {
        public View createTabContent(String tag) {
            ViewGroup.LayoutParams fillParent = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.FILL_PARENT);
            ViewGroup.LayoutParams fillWrap = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams wrapContent = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            wrapContent.gravity = Gravity.CENTER;
            LinearLayout.LayoutParams weightedFillWrap = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            weightedFillWrap.weight = 1;

            if (tag.equals(MAIN)) {
                LinearLayout mMainView = new LinearLayout(Benchmark.this);
                mMainView.setOrientation(1);
                ScrollView mListScroll = new ScrollView(Benchmark.this);

                LinearLayout mMainViewContainer = new LinearLayout(Benchmark.this);
                mMainViewContainer.setOrientation(1);
                ImageView mIconView = new ImageView(Benchmark.this);
                mIconView.setImageResource(R.drawable.icon);

                TextView mBannerInfo = new TextView(Benchmark.this);
                mBannerInfo.setText("0xbench\nSelect benchmarks in the tabs,\nor batch select:");

                d2CheckBox = new CheckBox(Benchmark.this);
                d2CheckBox.setText(D2);
                d2CheckBox.setOnClickListener(Benchmark.this);

                d2HWCheckBox = new CheckBox(Benchmark.this);
                d2HWCheckBox.setText(D2HW);
                d2HWCheckBox.setOnClickListener(Benchmark.this);

                d2SW1CheckBox = new CheckBox(Benchmark.this);
                d2SW1CheckBox.setText(D2SW1);
                d2SW1CheckBox.setOnClickListener(Benchmark.this);

                d2SW2CheckBox = new CheckBox(Benchmark.this);
                d2SW2CheckBox.setText(D2SW2);
                d2SW2CheckBox.setOnClickListener(Benchmark.this);

                d3CheckBox = new CheckBox(Benchmark.this);
                d3CheckBox.setText(D3);
                d3CheckBox.setOnClickListener(Benchmark.this);

                mathCheckBox = new CheckBox(Benchmark.this);
                mathCheckBox.setText(MATH);
                mathCheckBox.setOnClickListener(Benchmark.this);

                vmCheckBox = new CheckBox(Benchmark.this);
                vmCheckBox.setText(VM);
                vmCheckBox.setOnClickListener(Benchmark.this);

                nativeCheckBox = new CheckBox(Benchmark.this);
                nativeCheckBox.setText(NATIVE);
                nativeCheckBox.setOnClickListener(Benchmark.this);

                miscCheckBox = new CheckBox(Benchmark.this);
                miscCheckBox.setText(MISC);
                miscCheckBox.setOnClickListener(Benchmark.this);

                filterBitmapCheckBox = new CheckBox(Benchmark.this);
                filterBitmapCheckBox.setText("Filter Bitmap in Draw Image");
                filterBitmapCheckBox.setOnClickListener(Benchmark.this);

                useGradientCheckBox = new CheckBox(Benchmark.this);
                useGradientCheckBox.setText("Use Gradient in Draw Canvas/Circle/Rect/Arc");
                useGradientCheckBox.setOnClickListener(Benchmark.this);

                useTextureCheckBox = new CheckBox(Benchmark.this);
                useTextureCheckBox.setText("Use Texture in Draw Canvas/Circle/Rect/Arc");
                useTextureCheckBox.setOnClickListener(Benchmark.this);

                TextView mWebInfo = new TextView(Benchmark.this);
                mWebInfo.setText("Uploaded results:\nhttp://0xbenchmark.appspot.com");

                LinearLayout mButtonContainer = new LinearLayout(Benchmark.this);
                mRun = new Button(Benchmark.this);
                mShow = new Button(Benchmark.this);
                mRun.setText("Run");
                mShow.setText("Show");
                mRun.setOnClickListener(Benchmark.this);
                mShow.setOnClickListener(Benchmark.this);
                mButtonContainer.addView(mRun, weightedFillWrap);
                mButtonContainer.addView(mShow, weightedFillWrap);
                WebView mTracker = new WebView(Benchmark.this);
                mTracker.clearCache(true);
                mTracker.setWebViewClient(new WebViewClient() {
                    public void onPageFinished(WebView view, String url) {
                        Log.i(TAG, "Tracker: " + view.getTitle() + " -> " + url);
                    }

                    public void onReceivedError(WebView view, int errorCode, String description,
                            String failingUrl) {
                        Log.e(TAG, "Track err: " + description);
                    }
                });
                mTracker.loadUrl(trackerUrl);
                mMainViewContainer.addView(mIconView, wrapContent);
                mMainViewContainer.addView(mBannerInfo);
                mMainViewContainer.addView(mathCheckBox);
                mMainViewContainer.addView(d2CheckBox);
                mMainViewContainer.addView(d2HWCheckBox);
                mMainViewContainer.addView(d2SW1CheckBox);
                mMainViewContainer.addView(d2SW2CheckBox);
                mMainViewContainer.addView(d3CheckBox);
                mMainViewContainer.addView(vmCheckBox);
                mMainViewContainer.addView(nativeCheckBox);
                mMainViewContainer.addView(miscCheckBox);
                mMainViewContainer.addView(filterBitmapCheckBox);
                mMainViewContainer.addView(useGradientCheckBox);
                mMainViewContainer.addView(useTextureCheckBox);
                mMainViewContainer.addView(mWebInfo);
                mMainViewContainer.addView(mButtonContainer, fillWrap);
                mMainViewContainer.addView(mTracker, 0, 0);
                mListScroll.addView(mMainViewContainer, fillParent);
                mMainView.addView(mListScroll, fillWrap);

                return mMainView;

            }

            LinearLayout mMainView = new LinearLayout(Benchmark.this);
            mMainView.setOrientation(1);
            ScrollView mListScroll = new ScrollView(Benchmark.this);
            LinearLayout mListContainer = new LinearLayout(Benchmark.this);
            mListContainer.setOrientation(1);
            mListScroll.addView(mListContainer, fillParent);
            mMainView.addView(mListScroll, fillWrap);

            boolean gray = true;
            int length = mCases.size();
            Log.i(TAG, "L: " + length);
            Log.i(TAG, "TCF: " + tag);
            for (int i = 0; i < length; i++) {
                if (!mCategory.get(tag).contains(mCases.get(i)))
                    continue;
                Log.i(TAG, "Add: " + i);
                mListContainer.addView(mCheckList[i], fillWrap);
                mListContainer.addView(mDesc[i], fillWrap);
                if (gray) {
                    int color = 0xFF333333; //ARGB
                    mCheckList[i].setBackgroundColor(color);
                    mDesc[i].setBackgroundColor(color);
                }
                gray = !gray;
            }
            return mMainView;
        }
    };

    mTabHost.addTab(mTabHost.newTabSpec(MAIN).setIndicator(MAIN, getResources().getDrawable(R.drawable.ic_eye))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(D2).setIndicator(D2, getResources().getDrawable(R.drawable.ic_2d))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(D2HW).setIndicator(D2HW, getResources().getDrawable(R.drawable.ic_2d))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(D2SW1).setIndicator(D2SW1, getResources().getDrawable(R.drawable.ic_2d))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(D2SW2).setIndicator(D2SW2, getResources().getDrawable(R.drawable.ic_2d))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(D3).setIndicator(D3, getResources().getDrawable(R.drawable.ic_3d))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(MATH).setIndicator(MATH, getResources().getDrawable(R.drawable.ic_pi))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(VM).setIndicator(VM, getResources().getDrawable(R.drawable.ic_vm))
            .setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(NATIVE)
            .setIndicator(NATIVE, getResources().getDrawable(R.drawable.ic_c)).setContent(mTCF));
    mTabHost.addTab(mTabHost.newTabSpec(MISC).setIndicator(MISC, getResources().getDrawable(R.drawable.ic_misc))
            .setContent(mTCF));
}

From source file:com.battlelancer.seriesguide.getglueapi.GetGlueAuthActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    setupActionBar();//from   w w  w .  j  a v a  2s. co  m

    WebView webview = (WebView) findViewById(R.id.webView);

    final FragmentActivity activity = this;
    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, getString(R.string.getglue_authfailed) + " " + description,
                    Toast.LENGTH_LONG).show();

            finish();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(GetGlueCheckin.OAUTH_CALLBACK_URL)) {
                Uri uri = Uri.parse(url);

                new RetrieveAccessTokenTask(getApplicationContext()).execute(uri);

                finish();
                return true;
            }
            return false;
        }
    });

    // mWebview.getSettings().setJavaScriptEnabled(true);

    // make sure we start fresh
    webview.clearCache(true);

    Timber.d("Initiating authorization request...");
    Resources res = getResources();
    try {
        OAuthClientRequest request = com.uwetrottmann.getglue.GetGlue
                .getAuthorizationRequest(BuildConfig.TVTAG_CLIENT_ID, GetGlueCheckin.OAUTH_CALLBACK_URL);
        webview.loadUrl(request.getLocationUri());
    } catch (OAuthSystemException e) {
        Timber.e(e, "Auth request failed");
    }
}

From source file:net.basov.ticketinfo.UI.java

public void displayMainScreen(final WebView wv) {
    wv.setWebViewClient(new MyWebViewClient() {
        @Override// w  w w.  j a va  2 s  . co  m
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(wv, url);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                view.evaluateJavascript("javascript:jreplace('" + header_json.toString() + "')", null);
                view.evaluateJavascript("javascript:jreplace('" + ticket_json.toString() + "')", null);
                view.evaluateJavascript("javascript:jreplace('" + ic_json.toString() + "')", null);
                view.evaluateJavascript("javascript:jreplace('" + dump_json.toString() + "')", null);
            } else {
                view.loadUrl("javascript:jreplace('" + header_json.toString() + "')");
                view.loadUrl("javascript:jreplace('" + ticket_json.toString() + "')");
                view.loadUrl("javascript:jreplace('" + ic_json.toString() + "')");
                view.loadUrl("javascript:jreplace('" + dump_json.toString() + "')");
            }
            wv.clearCache(true);
            wv.clearHistory();
            //TODO: remove debug
            //Log.d("hhhh", header_json.toString());
            //Log.d("tttt", ticket_json.toString());
            //Log.d("iiii", ic_json.toString());
        }
    });
    Context c = wv.getContext();
    wv.loadUrl("file:///android_asset/" + c.getString(R.string.ticket_ui_file));
}

From source file:org.golang.app.WViewActivity.java

private void initWebView(WebView webView) {

    WebSettings settings = webView.getSettings();

    settings.setJavaScriptEnabled(true);
    settings.setAllowFileAccess(true);// ww  w . ja v a 2  s  .  com
    settings.setDomStorageEnabled(true);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setSupportZoom(true);
    // settings.setPluginsEnabled(true);
    methodInvoke(settings, "setPluginsEnabled", new Class[] { boolean.class }, new Object[] { true });
    // settings.setPluginState(PluginState.ON);
    methodInvoke(settings, "setPluginState", new Class[] { PluginState.class },
            new Object[] { PluginState.ON });
    // settings.setPluginsEnabled(true);
    methodInvoke(settings, "setPluginsEnabled", new Class[] { boolean.class }, new Object[] { true });
    // settings.setAllowUniversalAccessFromFileURLs(true);
    methodInvoke(settings, "setAllowUniversalAccessFromFileURLs", new Class[] { boolean.class },
            new Object[] { true });
    // settings.setAllowFileAccessFromFileURLs(true);
    methodInvoke(settings, "setAllowFileAccessFromFileURLs", new Class[] { boolean.class },
            new Object[] { true });

    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.clearHistory();
    webView.clearFormData();
    webView.clearCache(true);

    webView.setWebChromeClient(new MyWebChromeClient());
    // webView.setDownloadListener(downloadListener);
}

From source file:net.basov.ticketinfo.UI.java

public void displayWelcomeScreen(final WebView wv) {
    SharedPreferences defSharedPref = PreferenceManager.getDefaultSharedPreferences(wv.getContext());

    setWelcome("s_lang", defSharedPref.getString(wv.getContext().getString(R.string.pk_app_lang),
            wv.getContext().getString(R.string.pref_lang_def)));
    if (defSharedPref.getBoolean(wv.getContext().getString(R.string.pk_transliterate_flag), false)) {
        setWelcome("s_translit", "<input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\">");

    } else {/*from ww w . j ava  2  s  .  c  o  m*/
        setWelcome("s_translit", "<input type=\"checkbox\" disabled=\"disabled\">");
    }
    if (defSharedPref.getBoolean(wv.getContext().getString(R.string.pk_send_platform_info), true)) {
        setWelcome("s_sendinfo", "<input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\">");
    } else {
        setWelcome("s_sendinfo", "<input type=\"checkbox\" disabled=\"disabled\">");
    }
    if (defSharedPref.getBoolean(wv.getContext().getString(R.string.pk_use_view_directory), false)) {
        setWelcome("s_use_view_directory",
                "<input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\">");
    } else {
        setWelcome("s_use_view_directory", "<input type=\"checkbox\" disabled=\"disabled\">");
    }

    wv.setWebViewClient(new MyWebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(wv, url);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                view.evaluateJavascript("javascript:jreplace('" + welcome_json.toString() + "')", null);
            } else {
                view.loadUrl("javascript:jreplace('" + welcome_json.toString() + "')", null);
            }
            wv.clearCache(true);
            wv.clearHistory();
        }
    });
    Context c = wv.getContext();
    wv.loadUrl("file:///android_asset/" + c.getString(R.string.welcome_ui_file));
}

From source file:nya.miku.wishmaster.http.cloudflare.CloudflareChecker.java

private Cookie checkAntiDDOS(final CloudflareException exception, final HttpHost proxy, CancellableTask task,
        final Activity activity) {
    synchronized (lock) {
        if (processing)
            return null;
        processing = true;/* w w w .java 2  s  . c o m*/
    }
    processing2 = true;
    currentCookie = null;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        CookieSyncManager.createInstance(activity);
        CookieManager.getInstance().removeAllCookie();
    } else {
        CompatibilityImpl.clearCookies(CookieManager.getInstance());
    }

    final ViewGroup layout = (ViewGroup) activity.getWindow().getDecorView().getRootView();
    final WebViewClient client = new WebViewClient() {
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
            super.onPageFinished(webView, url);
            Logger.d(TAG, "Got Page: " + url);
            String value = null;
            try {
                String[] cookies = CookieManager.getInstance().getCookie(url).split("[;]");
                for (String cookie : cookies) {
                    if ((cookie != null) && (!cookie.trim().equals(""))
                            && (cookie.startsWith(" " + exception.getRequiredCookieName() + "="))) {
                        value = cookie.substring(exception.getRequiredCookieName().length() + 2);
                    }
                }
            } catch (NullPointerException e) {
                Logger.e(TAG, e);
            }
            if (value != null) {
                BasicClientCookieHC4 cf_cookie = new BasicClientCookieHC4(exception.getRequiredCookieName(),
                        value);
                cf_cookie.setDomain("." + Uri.parse(url).getHost());
                cf_cookie.setPath("/");
                currentCookie = cf_cookie;
                Logger.d(TAG, "Cookie found: " + value);
                processing2 = false;
            } else {
                Logger.d(TAG, "Cookie is not found");
            }
        }
    };

    activity.runOnUiThread(new Runnable() {
        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void run() {
            webView = new WebView(activity);
            webView.setVisibility(View.GONE);
            layout.addView(webView);
            webView.setWebViewClient(client);
            webView.getSettings().setUserAgentString(HttpConstants.USER_AGENT_STRING);
            webView.getSettings().setJavaScriptEnabled(true);
            webViewContext = webView.getContext();
            if (proxy != null)
                WebViewProxy.setProxy(webViewContext, proxy.getHostName(), proxy.getPort());
            webView.loadUrl(exception.getCheckUrl());
        }
    });

    long startTime = System.currentTimeMillis();
    while (processing2) {
        long time = System.currentTimeMillis() - startTime;
        if ((task != null && task.isCancelled()) || time > TIMEOUT) {
            processing2 = false;
        }
    }

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                layout.removeView(webView);
                webView.stopLoading();
                webView.clearCache(true);
                webView.destroy();
                webView = null;
            } finally {
                if (proxy != null)
                    WebViewProxy.setProxy(webViewContext, null, 0);
                processing = false;
            }
        }
    });

    return currentCookie;
}

From source file:ar.com.tristeslostrestigres.diasporanativewebapp.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progressBar = findViewById(R.id.progressBar);

    pm = new PrefManager(MainActivity.this);

    SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE);
    podDomain = config.getString("podDomain", null);

    fab = findViewById(R.id.multiple_actions);
    fab.setVisibility(View.GONE);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);/*from   w  w w  .  j  ava  2  s.  com*/
    getSupportActionBar().setTitle(null);

    txtTitle = (TextView) findViewById(R.id.toolbar_title);
    txtTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Helpers.isOnline(MainActivity.this)) {
                txtTitle.setText(R.string.jb_stream);
                webView.loadUrl("https://" + podDomain + "/stream");
            } else {
                Snackbar.make(v, R.string.no_internet, Snackbar.LENGTH_SHORT).show();
            }
        }
    });

    webView = findViewById(R.id.webView);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.addJavascriptInterface(new JavaScriptInterface(), "NotificationCounter");

    if (savedInstanceState != null) {
        webView.restoreState(savedInstanceState);
    }

    wSettings = webView.getSettings();
    wSettings.setJavaScriptEnabled(true);
    wSettings.setUseWideViewPort(true);
    wSettings.setLoadWithOverviewMode(true);
    wSettings.setDomStorageEnabled(true);
    wSettings.setMinimumFontSize(pm.getMinimumFontSize());
    wSettings.setLoadsImagesAutomatically(pm.getLoadImages());

    if (android.os.Build.VERSION.SDK_INT >= 21)
        wSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

    /*
     * WebViewClient
     */
    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!url.contains(podDomain)) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
                return true;
            }
            return false;
        }

        public void onPageFinished(WebView view, String url) {
            if (url.contains("/new") || url.contains("/sign_in")) {
                fab.setVisibility(View.GONE);
            } else {
                fab.setVisibility(View.VISIBLE);
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            new AlertDialog.Builder(MainActivity.this).setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(description).setPositiveButton("CLOSE", null).show();
        }
    });

    /*
     * WebChromeClient
     */
    webView.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView wv, int progress) {
            progressBar.setProgress(progress);

            if (progress > 0 && progress <= 60) {
                Helpers.getNotificationCount(wv);
            }

            if (progress > 60) {
                Helpers.hideTopBar(wv);
                fab.setVisibility(View.VISIBLE);
            }

            if (progress == 100) {
                fab.collapse();
                progressBar.setVisibility(View.GONE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
                FileChooserParams fileChooserParams) {
            if (mFilePathCallback != null)
                mFilePathCallback.onReceiveValue(null);

            mFilePathCallback = filePathCallback;

            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                    takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Snackbar.make(getWindow().findViewById(R.id.drawer), "Unable to get image",
                            Snackbar.LENGTH_SHORT).show();
                }

                // Continue only if the File was successfully created
                if (photoFile != null) {
                    mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                } else {
                    takePictureIntent = null;
                }
            }

            Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
            contentSelectionIntent.setType("image/*");

            Intent[] intentArray;
            if (takePictureIntent != null) {
                intentArray = new Intent[] { takePictureIntent };
            } else {
                intentArray = new Intent[0];
            }

            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

            startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

            return true;
        }

        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            return super.onJsAlert(view, url, message, result);
        }
    });

    /*
     * NavigationView
     */
    NavigationView navigationView = findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if (menuItem.isChecked())
                menuItem.setChecked(false);
            else
                menuItem.setChecked(true);

            drawerLayout.closeDrawers();

            switch (menuItem.getItemId()) {
            default:
                Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                        Snackbar.LENGTH_SHORT).show();
                return true;

            case R.id.jb_stream:
                if (Helpers.isOnline(MainActivity.this)) {
                    txtTitle.setText(R.string.jb_stream);
                    webView.loadUrl("https://" + podDomain + "/stream");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_public:
                setTitle(R.string.jb_public);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/public");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_liked:
                txtTitle.setText(R.string.jb_liked);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/liked");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_commented:
                txtTitle.setText(R.string.jb_commented);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/commented");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_contacts:
                txtTitle.setText(R.string.jb_contacts);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/contacts");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_mentions:
                txtTitle.setText(R.string.jb_mentions);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/mentions");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_activities:
                txtTitle.setText(R.string.jb_activities);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/activity");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_followed_tags:
                txtTitle.setText(R.string.jb_followed_tags);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/followed_tags");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_manage_tags:

                txtTitle.setText(R.string.jb_manage_tags);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/tag_followings/manage");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_license:
                txtTitle.setText(R.string.jb_license);
                new AlertDialog.Builder(MainActivity.this).setTitle(getString(R.string.license_title))
                        .setMessage(getString(R.string.license_text))
                        .setPositiveButton(getString(R.string.license_yes),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        Intent i = new Intent(Intent.ACTION_VIEW,
                                                Uri.parse("https://github.com/mdev88/Diaspora-Native-WebApp"));
                                        startActivity(i);
                                        dialog.cancel();
                                    }
                                })
                        .setNegativeButton(getString(R.string.license_no),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                })
                        .show();

                return true;

            case R.id.jb_aspects:
                txtTitle.setText(R.string.jb_aspects);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/aspects");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }

            case R.id.jb_settings:
                txtTitle.setText(R.string.jb_settings);
                if (Helpers.isOnline(MainActivity.this)) {
                    webView.loadUrl("https://" + podDomain + "/user/edit");
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();

                    return false;
                }

            case R.id.jb_pod:
                txtTitle.setText(R.string.jb_pod);
                if (Helpers.isOnline(MainActivity.this)) {
                    new AlertDialog.Builder(MainActivity.this).setTitle(getString(R.string.confirmation))
                            .setMessage(getString(R.string.change_pod_warning))
                            .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                                @TargetApi(11)
                                public void onClick(DialogInterface dialog, int id) {
                                    webView.clearCache(true);
                                    dialog.cancel();
                                    Intent i = new Intent(MainActivity.this, PodsActivity.class);
                                    startActivity(i);
                                    finish();
                                }
                            }).setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                                @TargetApi(11)
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            }).show();
                    return true;
                } else {
                    Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet,
                            Snackbar.LENGTH_SHORT).show();
                    return false;
                }
            }
        }
    });

    /*
     * DrawerLayout
     */
    drawerLayout = findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.openDrawer, R.string.closeDrawer);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    //calling sync state is necessary or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();

    if (savedInstanceState == null) {
        if (Helpers.isOnline(MainActivity.this)) {
            webView.loadData("", "text/html", null);
            webView.loadUrl("https://" + podDomain);
        } else {
            Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT)
                    .show();
        }
    }

}