From d195434e4f61110a71cd233ccde9b1f5fa2282db Mon Sep 17 00:00:00 2001 From: evilbunny2008 <34211365+evilbunny2008@users.noreply.github.com> Date: Tue, 11 Dec 2018 16:45:40 +1100 Subject: [PATCH] Tweaked the radar/forecast page to refresh webpage radars better --- app/build.gradle | 4 +- .../com/odiousapps/weewxweather/Common.java | 20 ++-- .../com/odiousapps/weewxweather/Forecast.java | 96 ++++++++++++------- .../odiousapps/weewxweather/UpdateCheck.java | 2 +- .../com/odiousapps/weewxweather/Weather.java | 4 +- 5 files changed, 77 insertions(+), 49 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c060549..69ff0d6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.odiousapps.weewxweather" minSdkVersion 19 targetSdkVersion 28 - versionCode 7002 - versionName "0.7.2" + versionCode 7003 + versionName "0.7.3" } buildTypes { release { diff --git a/app/src/main/java/com/odiousapps/weewxweather/Common.java b/app/src/main/java/com/odiousapps/weewxweather/Common.java index 6d51195..729d53c 100644 --- a/app/src/main/java/com/odiousapps/weewxweather/Common.java +++ b/app/src/main/java/com/odiousapps/weewxweather/Common.java @@ -2366,18 +2366,22 @@ void reallyGetWeather(String fromURL) throws Exception } // https://stackoverflow.com/questions/3841317/how-do-i-see-if-wi-fi-is-connected-on-android - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - boolean checkWifiOnAndConnected() + boolean checkConnection() { - WifiManager wifiMgr = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - - if (wifiMgr.isWifiEnabled()) + if(GetBoolPref("onlyWIFI", false)) { - WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); - return wifiInfo.getNetworkId() != -1; + WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + + if (wifiMgr.isWifiEnabled()) + { + WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); + return wifiInfo.getNetworkId() != -1; + } + + return false; } - return false; + return true; } private void sendAlert() diff --git a/app/src/main/java/com/odiousapps/weewxweather/Forecast.java b/app/src/main/java/com/odiousapps/weewxweather/Forecast.java index bb3972b..62f3e7d 100644 --- a/app/src/main/java/com/odiousapps/weewxweather/Forecast.java +++ b/app/src/main/java/com/odiousapps/weewxweather/Forecast.java @@ -17,6 +17,7 @@ import android.view.ViewTreeObserver; import android.webkit.ConsoleMessage; import android.webkit.WebChromeClient; +import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; @@ -279,22 +280,18 @@ private void loadWebView() rl.setVisibility(View.GONE); wv2.setVisibility(View.VISIBLE); - if(wv2.getOriginalUrl() == null) + if(common.checkConnection()) { - Handler mHandler = new Handler(Looper.getMainLooper()); - mHandler.post(new Runnable() - { - @Override - public void run() - { - wv2.loadUrl(common.GetStringPref("RADAR_URL", "")); - swipeLayout.setRefreshing(false); - } - }); - } else { - wv2.reload(); - swipeLayout.setRefreshing(false); + wv2.clearCache(true); + wv2.clearHistory(); + wv2.clearFormData(); + WebSettings webSettings = wv2.getSettings(); + webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); } + + wv2.loadUrl(common.GetStringPref("RADAR_URL", "")); + + swipeLayout.setRefreshing(false); } swipeLayout.setRefreshing(false); @@ -311,15 +308,12 @@ private void reloadWebView(boolean force) if(radar.equals("") || common.GetStringPref("radtype", "image").equals("webpage")) { - if(wv2.getOriginalUrl() == null) - loadWebView(); - else - wv2.reload(); + loadWebView(); swipeLayout.setRefreshing(false); return; } - if(!common.checkWifiOnAndConnected() && !force) + if(!common.checkConnection() && !force) { Common.LogMessage("Not on wifi and not a forced refresh"); swipeLayout.setRefreshing(false); @@ -345,7 +339,15 @@ public void run() e.printStackTrace(); } - swipeLayout.setRefreshing(false); + Handler mHandler = new Handler(Looper.getMainLooper()); + mHandler.post(new Runnable() + { + @Override + public void run() + { + swipeLayout.setRefreshing(false); + } + }); } }); @@ -381,15 +383,15 @@ private void updateScreen() getForecast(false); forecast.setVisibility(View.VISIBLE); im.setVisibility(View.VISIBLE); - rl.setVisibility(View.GONE); - wv2.setVisibility(View.VISIBLE); + rl.setVisibility(View.VISIBLE); + wv2.setVisibility(View.GONE); } else { Common.LogMessage("Displaying radar"); loadWebView(); forecast.setVisibility(View.GONE); im.setVisibility(View.GONE); - rl.setVisibility(View.VISIBLE); - wv2.setVisibility(View.GONE); + rl.setVisibility(View.GONE); + wv2.setVisibility(View.VISIBLE); } swipeLayout.setRefreshing(false); @@ -442,10 +444,10 @@ public void run() return; } - if(!common.checkWifiOnAndConnected() && !force) + if(!common.checkConnection() && !force) { Common.LogMessage("Not on wifi and not a forced refresh"); - swipeLayout.setRefreshing(false); + handlerDone.sendEmptyMessage(0); return; } @@ -477,9 +479,7 @@ public void run() } catch (Exception e) { e.printStackTrace(); } - - swipeLayout.setRefreshing(false); - } + } }); t.start(); @@ -503,7 +503,15 @@ private void generateForecast() @Override public void run() { - swipeLayout.setRefreshing(true); + Handler mHandler = new Handler(Looper.getMainLooper()); + mHandler.post(new Runnable() + { + @Override + public void run() + { + swipeLayout.setRefreshing(true); + } + }); Common.LogMessage("getting json data"); String data; @@ -512,13 +520,22 @@ public void run() data = common.GetStringPref("forecastData", ""); if(data.equals("")) { - String html = ""; + String tmp = ""; if(dark_theme) - html += ""; - html += "Forecast URL not set, edit settings.txt to change"; + tmp += ""; + tmp += "Forecast URL not set, edit settings.txt to change"; - wv1.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); - swipeLayout.setRefreshing(false); + final String html = tmp; + + mHandler.post(new Runnable() + { + @Override + public void run() + { + wv1.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); + swipeLayout.setRefreshing(false); + } + }); return; } @@ -631,7 +648,14 @@ public void run() } } - swipeLayout.setRefreshing(false); + mHandler.post(new Runnable() + { + @Override + public void run() + { + swipeLayout.setRefreshing(false); + } + }); } }); diff --git a/app/src/main/java/com/odiousapps/weewxweather/UpdateCheck.java b/app/src/main/java/com/odiousapps/weewxweather/UpdateCheck.java index 344cd8f..46ca0a9 100644 --- a/app/src/main/java/com/odiousapps/weewxweather/UpdateCheck.java +++ b/app/src/main/java/com/odiousapps/weewxweather/UpdateCheck.java @@ -19,7 +19,7 @@ public void onReceive(final Context c, Intent i) Common common = new Common(c); common.setAlarm("UpdateCheck"); - if(common.GetBoolPref("onlyWIFI") && !common.checkWifiOnAndConnected()) + if(!common.checkConnection()) { Common.LogMessage("Skipping update due to wifi setting."); return; diff --git a/app/src/main/java/com/odiousapps/weewxweather/Weather.java b/app/src/main/java/com/odiousapps/weewxweather/Weather.java index b8e9148..dfb889e 100644 --- a/app/src/main/java/com/odiousapps/weewxweather/Weather.java +++ b/app/src/main/java/com/odiousapps/weewxweather/Weather.java @@ -731,7 +731,7 @@ public void run() return; } - if(!common.checkWifiOnAndConnected() && !force) + if(!common.checkConnection() && !force) { Common.LogMessage("Not on wifi and not a forced refresh"); if(swipeLayout.isRefreshing()) @@ -792,7 +792,7 @@ private void reloadWebView(boolean force) return; } - if(!common.checkWifiOnAndConnected() && !force) + if(!common.checkConnection() && !force) { Common.LogMessage("Not on wifi and not a forced refresh"); swipeLayout.setRefreshing(false);