a/app/src/main/java/com/codesys/forge/MainActivity.java | b/app/src/main/java/com/codesys/forge/MainActivity.java | ||
---|---|---|---|
... |
... |
||
66 | 66 | ||
67 | public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { |
67 | public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { |
68 | private Context myContext; |
68 | private Context myContext; |
69 | private WebView myWebView; |
69 | private WebView myWebView; |
70 | private static final String TAG = "Main"; |
70 | private static final String TAG = "Main"; |
71 | private String currentURL; |
||
72 | private BottomNavigationView bottomnav; |
71 | private BottomNavigationView bottomnav; |
73 | private SwipeRefreshLayout refreshLayout; |
72 | private SwipeRefreshLayout refreshLayout; |
74 | private Boolean xNews; |
73 | private ViewTreeObserver.OnScrollChangedListener onScrollChangedListener; |
75 | private Boolean xProjects; |
74 | // |
76 | private Boolean xTalk; |
75 | // When rotating the device, onCreate is rerun, and the instance is recreated. |
77 | private ViewTreeObserver.OnScrollChangedListener onScrollChangedListener; |
76 | // We make everything that should survive this layout change static. |
77 | // |
||
78 | private static String currentURL = "https://forge.codesys.com/forge/community-feed"; |
||
79 | private static Boolean xOffline; |
||
80 | private static Boolean xNews; |
||
81 | private static Boolean xProjects; |
||
78 | private Boolean xOffline; |
82 | private static Boolean xTalk; |
79 | 83 | ||
80 | // cache directory |
84 | // cache directory |
81 | private File getLocalCacheDir() { |
85 | private File getLocalCacheDir() { |
82 | File cacheDir = new File(MainActivity.this.getCacheDir(), "cache"); |
86 | File cacheDir = new File(MainActivity.this.getCacheDir(), "cache"); |
83 | cacheDir.mkdirs(); |
87 | cacheDir.mkdirs(); |
... |
... |
||
140 | 144 | ||
141 | 145 | ||
142 | 146 | ||
143 | protected void loadUrl(String Url, String Title) { |
147 | protected void loadUrl(String Url, String Title) { |
144 | refreshLayout.setRefreshing(true); |
148 | refreshLayout.setRefreshing(true); |
149 | currentURL = Url; |
||
145 | myWebView.loadUrl(Url); |
150 | myWebView.loadUrl(Url); |
146 | } |
151 | } |
147 | 152 | ||
148 | private boolean getNetworkState() { |
153 | private boolean getNetworkState() { |
149 | ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
154 | ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
... |
... |
||
264 | if(getNetworkState()) { |
269 | if(getNetworkState()) { |
265 | setOnline(); |
270 | setOnline(); |
266 | } |
271 | } |
267 | else |
272 | else |
268 | { |
273 | { |
269 | setOnline(); |
274 | setOffline(); |
270 | } |
275 | } |
271 | 276 | ||
272 | myWebView.loadUrl(currentURL); |
277 | myWebView.loadUrl(currentURL); |
273 | } |
278 | } |
274 | 279 | ||
... |
... |
||
312 | myUseragent += " cforge"; |
317 | myUseragent += " cforge"; |
313 | myWebView.getSettings().setUserAgentString(myUseragent); |
318 | myWebView.getSettings().setUserAgentString(myUseragent); |
314 | 319 | ||
315 | // reload page |
320 | // reload page |
316 | refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe); |
321 | refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe); |
317 | refreshLayout.setOnRefreshListener(this); |
322 | refreshLayout.setOnRefreshListener(this); |
318 | myWebView.loadUrl("https://forge.codesys.com"); |
||
319 | 323 | ||
320 | // Override URL Loading |
324 | // Override URL Loading |
321 | myWebView.setWebViewClient(new WebViewClient() { |
325 | myWebView.setWebViewClient(new WebViewClient() { |
322 | public String getFilenameFromURL(String url) |
326 | public String getFilenameFromURL(String url) |
323 | { |
327 | { |
... |
... |
||
394 | Uri url = request.getUrl(); |
398 | Uri url = request.getUrl(); |
395 | String filename = getFilenameFromURL(url.toString()); |
399 | String filename = getFilenameFromURL(url.toString()); |
396 | 400 | ||
397 | // remote request |
401 | // remote request |
398 | if (url.toString().startsWith("https://forge.codesys.com")) { |
402 | if (url.toString().startsWith("https://forge.codesys.com")) { |
403 | if(!getNetworkState()) { |
||
404 | xOffline = true; |
||
405 | } |
||
406 | |||
399 | if (!getOffline()) |
407 | if (!getOffline()) |
400 | { |
408 | { |
401 | // online => cache all files |
409 | // online => cache all files |
402 | downloadCacheFile(filename, url.toString()); |
410 | downloadCacheFile(filename, url.toString()); |
403 | return super.shouldInterceptRequest(view, request); |
411 | return super.shouldInterceptRequest(view, request); |
... |
... |
||
422 | return null; |
430 | return null; |
423 | } |
431 | } |
424 | 432 | ||
425 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
433 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
426 | // load local file, stating that we're offline |
434 | // load local file, stating that we're offline |
435 | /* |
||
436 | File cacheDir = getLocalCacheDir(); |
||
437 | File fileName = new File(cacheDir, "default.html"); |
||
438 | view.loadUrl("file://" + fileName.toString()); |
||
439 | */ |
||
427 | refreshLayout.setRefreshing(false); |
440 | refreshLayout.setRefreshing(false); |
428 | setOffline(); |
441 | setOffline(); |
429 | } |
442 | } |
430 | }); |
443 | }); |
431 | 444 | ||
... |
... |
||
454 | // |
467 | // |
455 | // Add navigation bar |
468 | // Add navigation bar |
456 | // |
469 | // |
457 | bottomnav = findViewById(R.id.bottomNavigationView); |
470 | bottomnav = findViewById(R.id.bottomNavigationView); |
458 | bottomnav.setOnNavigationItemSelectedListener(navListener); |
471 | bottomnav.setOnNavigationItemSelectedListener(navListener); |
459 | loadUrl("https://forge.codesys.com/forge/community-feed", "Community"); |
472 | ReLoadWebView(currentURL); |
460 | } |
473 | } |
461 | 474 | ||
462 | // back button |
475 | // back button |
463 | @Override |
476 | @Override |
464 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
477 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
... |
... |
||
517 | case R.id.talk: |
530 | case R.id.talk: |
518 | loadUrl("https://forge.codesys.com/forge/talk", "Talk"); |
531 | loadUrl("https://forge.codesys.com/forge/talk", "Talk"); |
519 | break; |
532 | break; |
520 | } |
533 | } |
521 | menuItem.setChecked(true); |
534 | menuItem.setChecked(true); |
535 | // getNews(); |
||
536 | // notification(); |
||
537 | |||
522 | return true; |
538 | return true; |
523 | } |
539 | } |
524 | }; |
540 | }; |
525 | } |
541 | } |