Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / WebViewFragment.java
1 package com.moiseum.wolnelektury.view;
2
3 import android.os.Bundle;
4 import android.support.v4.app.Fragment;
5 import android.view.View;
6 import android.webkit.WebSettings;
7 import android.webkit.WebView;
8 import android.webkit.WebViewClient;
9 import android.widget.Button;
10 import android.widget.TextView;
11
12 import com.moiseum.wolnelektury.R;
13 import com.moiseum.wolnelektury.base.AbstractFragment;
14
15 import butterknife.BindView;
16
17 /**
18  * @author golonkos.
19  */
20
21 public class WebViewFragment extends AbstractFragment {
22
23         private static final String PARAM_URL = "PARAM_URL";
24
25         @BindView(R.id.wvAbout)
26         WebView wvPage;
27         @BindView(R.id.btnBack)
28         Button btnBack;
29         @BindView(R.id.btnRefresh)
30         Button btnRefresh;
31         @BindView(R.id.btnNext)
32         Button btnNext;
33         @BindView(R.id.tvPageError)
34         TextView tvPageError;
35
36         private boolean loadFailed;
37
38         public static Fragment newInstance(String url) {
39                 WebViewFragment fragment = new WebViewFragment();
40                 Bundle args = new Bundle(1);
41                 args.putString(PARAM_URL, url);
42                 fragment.setArguments(args);
43                 return fragment;
44         }
45
46         @Override
47         public int getLayoutResourceId() {
48                 return R.layout.fragment_web_view;
49         }
50
51         @Override
52         public void prepareView(View view, Bundle savedInstanceState) {
53                 initWebView();
54         }
55
56         private void initWebView() {
57                 wvPage.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
58                 wvPage.getSettings().setJavaScriptEnabled(true);
59                 wvPage.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
60                 wvPage.setWebViewClient(new WebViewClient() {
61
62                         @Override
63                         public boolean shouldOverrideUrlLoading(WebView view, String url) {
64                                 wvPage.loadUrl(url);
65                                 return true;
66                         }
67
68                         @Override
69                         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
70                                 if (tvPageError == null) {
71                                         return;
72                                 }
73                                 loadFailed = true;
74                                 tvPageError.setVisibility(View.VISIBLE);
75                         }
76
77                         @Override
78                         public void onPageFinished(WebView view, String url) {
79                                 super.onPageFinished(view, url);
80                                 if (tvPageError == null) {
81                                         return;
82                                 }
83                                 if (!loadFailed) {
84                                         tvPageError.setVisibility(View.GONE);
85                                 }
86                                 btnBack.setEnabled(wvPage.canGoBack());
87                                 btnNext.setEnabled(wvPage.canGoForward());
88                         }
89                 });
90                 String url = getArguments().getString(PARAM_URL);
91                 wvPage.loadUrl(url);
92
93                 btnRefresh.setOnClickListener(new View.OnClickListener() {
94                         @Override
95                         public void onClick(View v) {
96                                 loadFailed = false;
97                                 wvPage.reload();
98                         }
99                 });
100
101                 btnBack.setOnClickListener(new View.OnClickListener() {
102                         @Override
103                         public void onClick(View v) {
104                                 wvPage.goBack();
105                         }
106                 });
107
108                 btnNext.setOnClickListener(new View.OnClickListener() {
109                         @Override
110                         public void onClick(View v) {
111                                 wvPage.goForward();
112                         }
113                 });
114         }
115 }