Skip to content

Commit

Permalink
Tweaked the radar/forecast page to refresh webpage radars better
Browse files Browse the repository at this point in the history
  • Loading branch information
evilbunny2008 committed Dec 11, 2018
1 parent c95fb8d commit d195434
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 12 additions & 8 deletions app/src/main/java/com/odiousapps/weewxweather/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
96 changes: 60 additions & 36 deletions app/src/main/java/com/odiousapps/weewxweather/Forecast.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
});
}
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -477,9 +479,7 @@ public void run()
} catch (Exception e) {
e.printStackTrace();
}

swipeLayout.setRefreshing(false);
}
}
});

t.start();
Expand All @@ -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;
Expand All @@ -512,13 +520,22 @@ public void run()
data = common.GetStringPref("forecastData", "");
if(data.equals(""))
{
String html = "<html>";
String tmp = "<html>";
if(dark_theme)
html += "<head><style>body{color: #fff; background-color: #000;}</style></head>";
html += "<body>Forecast URL not set, edit settings.txt to change</body></html>";
tmp += "<head><style>body{color: #fff; background-color: #000;}</style></head>";
tmp += "<body>Forecast URL not set, edit settings.txt to change</body></html>";

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;
}

Expand Down Expand Up @@ -631,7 +648,14 @@ public void run()
}
}

swipeLayout.setRefreshing(false);
mHandler.post(new Runnable()
{
@Override
public void run()
{
swipeLayout.setRefreshing(false);
}
});
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/odiousapps/weewxweather/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d195434

Please sign in to comment.