Example usage for android.webkit WebSettings LOAD_CACHE_ELSE_NETWORK

List of usage examples for android.webkit WebSettings LOAD_CACHE_ELSE_NETWORK

Introduction

In this page you can find the example usage for android.webkit WebSettings LOAD_CACHE_ELSE_NETWORK.

Prototype

int LOAD_CACHE_ELSE_NETWORK

To view the source code for android.webkit WebSettings LOAD_CACHE_ELSE_NETWORK.

Click Source Link

Document

Use cached resources when they are available, even if they have expired.

Usage

From source file:com.barcamppenang2014.tabfragment.AgendaFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.agenda, container, false);
    WebView agenda_wv = (WebView) v.findViewById(R.id.webview);
    NetworkAvailability network = new NetworkAvailability();
    if (network.isNetworkAvalaible(getActivity().getApplicationContext())) {
        agenda_wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        agenda_wv.loadUrl("http://barcamppenang.org/agenda.html");
    } else {//from   w  w  w . j  ava 2  s  .c  o  m
        agenda_wv.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        agenda_wv.loadUrl("http://barcamppenang.org/agenda.html");
    }

    return v;
}

From source file:com.mummyding.app.leisure.ui.support.BaseWebViewActivity.java

private void initData() {
    data = getData();//from w  ww .ja v  a  2s  .c  om

    webView = (WebView) findViewById(R.id.webview);
    progressBar = (ProgressBar) findViewById(R.id.progressbar);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSupportMultipleWindows(false);
    /*
     cache web page
     */

    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);

    /*  if(Settings.isNightMode) {
    webView.setBackgroundColor(ContextCompat.getColor(this, R.color.night_primary));
     }*/
    if (HttpUtil.isWIFI == false) {
        webView.getSettings()
                .setBlockNetworkImage(Settings.getInstance().getBoolean(Settings.NO_PIC_MODE, false));
    } else {
        // fix issue #13
        webView.getSettings().setBlockNetworkImage(false);
    }

    loadData();
    setProgressBarIndeterminateVisibility(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }

        /* @Override
         public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        view.loadUrl("file:///android_asset/error.html");
         }
                
         @Override
         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        view.loadUrl("file:///android_asset/error.html");
         }*/

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (isLoading) {
                progressBar.incrementProgressBy(newProgress - progressBar.getProgress());
                if (newProgress > 25) {
                    isLoading = false;
                    progressBar.setVisibility(View.GONE);
                }
            }
        }
    });
}

From source file:com.nxt.yn.app.ui.activity.WebViewUrlActivity.java

private void initData() {
    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override//from w w  w  . j ava2  s.  co m
        public void onRefresh() {
            handler.sendEmptyMessageDelayed(0, 1000);
        }
    });
    refreshLayout.setColorSchemeResources(R.color.title_color, R.color.title_color, R.color.title_color,
            R.color.title_color);

    newsid = getIntent().getIntExtra("newsid", 0);
    LogUtils.i("ssss", "newsid---------->" + newsid);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSupportMultipleWindows(false);
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);

    /*  if(Settings.isNightMode) {
    webView.setBackgroundColor(ContextCompat.getColor(this, R.color.night_primary));
     }*/
    if (EaseCommonUtils.isWiFi(this) == false) {
        webView.getSettings()
                .setBlockNetworkImage(ZPreferenceUtils.getPrefBoolean(Settings.NO_PIC_MODE, false));
    } else {
        // fix issue #13
        webView.getSettings().setBlockNetworkImage(false);
    }
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            refreshLayout.setRefreshing(false);
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            view.loadUrl("file:///android_asset/error.html");
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            view.loadUrl("file:///android_asset/error.html");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (isfirsarload) {
                isfirsarload = false;
                return false;
            } else {
                startActivity(new Intent(WebViewUrlActivity.this, WebViewUrlActivity.class)
                        .putExtra("title", getString(R.string.app_name)).putExtra("url", url));
            }
            return true;
        }
    });

}

From source file:org.jsharkey.oilcan.BrowserActivity.java

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    requestWindowFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.act_browse);

    scriptdb = new ScriptDatabase(this);
    scriptdb.onUpgrade(scriptdb.getWritableDatabase(), -10, 10);

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

    WebSettings settings = webview.getSettings();
    settings.setSavePassword(false);/* w  ww .j  a  v  a  2s  .c o  m*/
    settings.setSaveFormData(false);
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(true);
    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    FrameLayout zoomholder = (FrameLayout) this.findViewById(R.id.browse_zoom);
    zoomholder.addView(webview.getZoomControls());
    webview.getZoomControls().setVisibility(View.GONE);

    webview.setWebViewClient(new OilCanClient());
    webview.setWebChromeClient(new OilCanChrome());

    webview.addJavascriptInterface(new IntentHelper(), "intentHelper");

    // load the last-viewed page into browser
    String url = "http://m.half.com/";
    if (icicle != null && icicle.containsKey(LAST_VIEWED))
        url = icicle.getString(LAST_VIEWED);

    // or watch for incoming requested urls
    if (getIntent().getExtras() != null && getIntent().getExtras().containsKey(SearchManager.QUERY))
        url = getIntent().getStringExtra(SearchManager.QUERY);

    webview.loadUrl(url);

}

From source file:org.safegees.safegees.gui.fragment.InfoFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_info, container, false);
    final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

    webView = (WebView) view.findViewById(R.id.webview_info);
    //webView.getSettings().setAppCachePath( this.getActivity().getApplicationContext().getCacheDir().getAbsolutePath() );
    //webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default

    if (!Connectivity.isNetworkAvaiable(getContext())) { // loading offline
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }/*  w w  w . ja  v a 2s  .  c o  m*/
    webView.getSettings().setAllowFileAccess(true);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(WebViewInfoWebDownloadController.CRISIS_HUB_DEFAULT_URL)) {
                view.loadUrl(url);

            } else {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
            }
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("about:blank");
            webView.loadUrl("file:///android_asset/default_error.html");
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            //super.onReceivedError(view, request, error);
        }
    });

    //Load language
    String language = Locale.getDefault().getDisplayLanguage();
    if (language.equals("ar")) {
        webView.loadUrl(WebViewInfoWebDownloadController.CRISIS_HUB_DEFAULT_URL_AR);
    } else if (language.equals("fa")) {
        webView.loadUrl(WebViewInfoWebDownloadController.CRISIS_HUB_DEFAULT_URL_FA);
    } else {
        webView.loadUrl(WebViewInfoWebDownloadController.CRISIS_HUB_DEFAULT_URL);
    }

    return view;
}

From source file:de.madvertise.android.sdk.MadvertiseMraidView.java

public MadvertiseMraidView(Context context) {
    super(context);
    setVerticalScrollBarEnabled(false);/*from   www . j a  v a2  s .c o m*/
    setHorizontalScrollBarEnabled(false);
    setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    setBackgroundColor(Color.TRANSPARENT);
    WebSettings settings = getSettings();
    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    settings.setJavaScriptEnabled(true);
    //settings.setPluginsEnabled(true);

    // Initialize the default expand properties.
    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    mExpandProperties = new ExpandProperties(metrics.widthPixels, metrics.heightPixels);
    MadvertiseUtil.logMessage(null, Log.INFO,
            "Setting default expandProperties : " + mExpandProperties.toJson().toString());

    // This bridge stays available until this view is destroyed, hence no
    // reloading when displaying new ads is necessary.
    addJavascriptInterface(mBridge, "mraid_bridge");

    setWebViewClient(new WebViewClient() {
        private boolean mError = false;

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, final String url) {
            post(new Runnable() {
                @Override
                public void run() {
                    if (mListener != null) {
                        mListener.onAdClicked();
                    }
                    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url),
                            getContext().getApplicationContext(), MadvertiseActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
            });
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!url.endsWith("mraid.js") && !mError) {
                MadvertiseUtil.logMessage(null, Log.DEBUG, "Setting mraid to default");
                checkReady();

                // Close button in default size for interstitial ads
                if (mPlacementType == MadvertiseUtil.PLACEMENT_TYPE_INTERSTITIAL) {
                    mCloseButton = addCloseButtonToViewGroup(((ViewGroup) getParent()));
                    mCloseButton.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
                }
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            mError = true;
        }
    });

    // Comment this in to enable video tag-capability.
    this.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            MadvertiseUtil.logMessage(null, Log.INFO, "showing VideoView");
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    mVideo = (VideoView) ((FrameLayout) view).getFocusedChild();
                    frame.removeView(mVideo);
                    ((ViewGroup) getParent()).addView(mVideo);

                    // Will also be called onError
                    mVideo.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer player) {
                            player.stop();
                        }
                    });

                    mVideo.setOnErrorListener(new OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            MadvertiseUtil.logMessage(null, Log.WARN, "Error while playing video");

                            if (mListener != null) {
                                mListener.onError(new IOException("Error while playing video"));
                            }

                            // We return false in order to call
                            // onCompletion()
                            return false;
                        }
                    });

                    mVideo.start();
                }
            }
        }

        @Override
        public void onHideCustomView() {
            if (mVideo != null) {
                ((ViewGroup) getParent()).removeView(mVideo);
                if (mVideo.isPlaying()) {
                    mVideo.stopPlayback();
                }
            }
        }
    });
}

From source file:de.geeksfactory.opacclient.frontend.InfoFragment.java

@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
@Override/*from  w w w .  j  a  va 2  s .co  m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = inflater.inflate(R.layout.fragment_info, container, false);
    app = (OpacClient) getActivity().getApplication();

    setHasOptionsMenu(true);

    load();

    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    wvInfo = (WebView) view.findViewById(R.id.wvInfo);

    wvInfo.getSettings().setSupportZoom(true);
    wvInfo.getSettings().setJavaScriptEnabled(true);
    wvInfo.getSettings().setAppCacheMaxSize(5 * 1024 * 1024);
    wvInfo.getSettings().setAppCacheEnabled(true);
    wvInfo.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    wvInfo.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView v, int progress) {
            ProgressBar Pbar = (ProgressBar) view.findViewById(R.id.pbWebProgress);
            if (progress < 100 && Pbar.getVisibility() == View.GONE) {
                Pbar.setVisibility(View.VISIBLE);
            }
            Pbar.setProgress(progress);
            if (progress == 100) {
                Pbar.setVisibility(View.GONE);
            }
        }

    });
    wvInfo.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(app.getLibrary().getData().optString("webviewcontain", "NOPE"))) {
                return false;
            }
            if (getActivity() == null) {
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

    });

    return view;
}

From source file:cn.zhangls.android.weibo.ui.web.WebActivity.java

/**
 * ?/*w w  w  .  j a  v  a 2 s .c  o  m*/
 */
private void initialize() {
    // Appbar 
    setSupportActionBar(mBinding.acWebToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // ? Title
    getSupportActionBar().setTitle("");

    mWeiboUrl = getIntent().getStringExtra(WEIBO_URL);

    //SwipeRefreshLayout
    mBinding.acWebSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mBinding.acWebWebView.reload();
        }
    });
    mBinding.acWebSwipeRefresh.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorAccent));

    mBinding.acWebWebView.loadUrl(mWeiboUrl);
    mBinding.acWebWebView.setWebChromeClient(new WebChromeClient() {
        /**
         * Tell the host application the current progress of loading a page.
         *
         * @param view        The WebView that initiated the callback.
         * @param newProgress Current page loading progress, represented by
         */
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                mBinding.acWebSwipeRefresh.setRefreshing(false);
            }
        }

        /**
         * Notify the host application of a change in the document title.
         *
         * @param view  The WebView that initiated the callback.
         * @param title A String containing the new title of the document.
         */
        @Override
        public void onReceivedTitle(WebView view, String title) {
            getSupportActionBar().setTitle(title);
        }
    });
    mBinding.acWebWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            mBinding.acWebWebView.loadUrl(mWeiboUrl);
            return true;
        }
    });
    // ??javascript
    mBinding.acWebWebView.getSettings().setJavaScriptEnabled(true);
    // 
    mBinding.acWebWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}

From source file:com.usabusi.newsreader.ArticleFragment.java

public void loadUrl(String articleURL) {
    //mNewsArticle = article;
    mCurrArticleURL = articleURL;/*  ww  w.  j  a  v  a  2s .  c o  m*/
    if (mWebView != null) {
        if (!isNetworkAvailable()) { // loading offline
            mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            //Toast.makeText(getActivity().getApplicationContext(), "Offline Cache Mode", Toast.LENGTH_SHORT).show();
        }
        mWebView.loadUrl(articleURL);
        //mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
    }
}

From source file:org.safegees.safegees.gui.fragment.InfoFragment.java

public void setLoaderDependingConnectivity(boolean isConnected) {
    if (webView != null) {
        if (isConnected) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        } else {//from   w  w  w  .  j a va 2  s  . c  o m
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
        }
    }

}