Skip to content

Commit

Permalink
More dark theme code, and added forecasts from metoffice.gov.uk
Browse files Browse the repository at this point in the history
  • Loading branch information
evilbunny2008 committed Nov 28, 2018
1 parent 326725d commit fc8190b
Show file tree
Hide file tree
Showing 58 changed files with 152 additions and 21 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 6006
versionName "0.6.6"
versionCode 6007
versionName "0.6.7"
}
buildTypes {
release {
Expand Down
90 changes: 83 additions & 7 deletions app/src/main/java/com/odiousapps/weewxweather/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,83 @@ RemoteViews buildUpdate(Context context)
return views;
}

String[] processMET(String data)
{
return processMET(data, false);
}

String[] processMET(String data, boolean showHeader)
{
if(data == null || data.equals(""))
return null;

//boolean metric = GetBoolPref("metric", true);
long mdate = (long)GetIntPref("rssCheck", 0) * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String lastupdate = sdf.format(mdate);

String tmp;
StringBuilder out = new StringBuilder();
String desc;

try
{
desc = data.split("<title>", 2)[1].split(" weather - Met Office</title>",2)[0].trim();

tmp = "<div style='font-size:12pt;'>" + lastupdate + "</div>";
out.append(tmp);
tmp = "<table style='width:100%;'>\n";
out.append(tmp);

String[] forecasts = data.split("<ul id=\"dayNav\"", 2)[1].split("</ul>", 2)[0].split("<li");
for(int i = 1; i < forecasts.length; i++)
{
String date = forecasts[i].split("data-tab-id=\"", 2)[1].split("\"")[0].trim();

sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
mdate = sdf.parse(date).getTime();
sdf = new SimpleDateFormat("EEEE", Locale.getDefault());
date = sdf.format(mdate);

String icon = "https://beta.metoffice.gov.uk" + forecasts[i].split("<img class=\"icon\"")[1].split("src=\"")[1].split("\">")[0].trim();
String fileName = "met" + icon.substring(icon.lastIndexOf('/') + 1, icon.length()).replaceAll("\\.svg$", "\\.png");
String min = forecasts[i].split("<span class=\"tab-temp-low\"", 2)[1].split("\">")[1].split("</span>")[0].trim();
String max = forecasts[i].split("<span class=\"tab-temp-high\"", 2)[1].split("\">")[1].split("</span>")[0].trim();
String text = forecasts[i].split("<div class=\"summary-text", 2)[1].split("\">", 3)[2]
.split("</div>", 2)[0].replaceAll("</span>", "").replaceAll("<span>", "");

tmp = "<tr><td style='width:10%;' rowspan='2'>" + "<img width='40px' src='" + fileName + "'></td>";
out.append(tmp);

tmp = "<td style='width:80%;'><b>" + date + "</b></td>";
out.append(tmp);

tmp = "<td style='width:10%;text-align:right;'><b>" + max + "</b></td></tr>";
out.append(tmp);

tmp = "<tr><td style='width:80%;'>" + text + "</td>";
out.append(tmp);

tmp = "<td style='text-align:right;vertical-align:top;'>" + min + "</td></tr>";
out.append(tmp);

if(showHeader)
{
tmp = "<tr><td style='font-size:10pt;' colspan='5'>&nbsp;</td></tr>";
out.append(tmp);
}
}

out.append("</table>");
//LogMessage(out.toString());
} catch (Exception e) {
e.printStackTrace();
return null;
}

return new String[]{out.toString(), desc};
}

String[] processWCA(String data)
{
return processWCA(data, false);
Expand All @@ -357,7 +434,7 @@ String[] processWCA(String data, boolean showHeader)
try
{
String obs = data.split("Forecast issued: ", 2)[1].trim();
obs = obs.split("</span>", 2)[0].trim().replaceAll(" PM ", " pm ").replaceAll(" AM ", " am ");
obs = obs.split("</span>", 2)[0].trim();
//LogMessage("obs == " + obs);

int i = 0, j = obs.indexOf(":");
Expand Down Expand Up @@ -411,7 +488,7 @@ String[] processWCA(String data, boolean showHeader)
Elements div = doc.select("div");
for (j = 0; j < div.size(); j++)
{
String date, text = "", img_url = "", encoded, temp = "", pop = "";
String date, text = "", img_url = "", temp = "", pop = "";

if (div.get(j).className().contains("greybkgrd"))
{
Expand Down Expand Up @@ -461,7 +538,6 @@ else if(div.get(j).select("div").html().contains("Night"))

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bmp = null;

String fileName = "wca" + img_url.substring(img_url.lastIndexOf('/') + 1, img_url.length()).replaceAll("\\.gif$", "\\.png");
//LogMessage("fileName == " + fileName);
Expand Down Expand Up @@ -884,17 +960,17 @@ String[] processBOM(String data, boolean showHeader)
min = round((Double.parseDouble(min) * 9.0 / 5.0) + 32.0) + "&deg;F";
}

if(!max.equals(""))
tmp = "<td style='width:10%;text-align:right;'><b>" + max + "&deg;C</b></td></tr>";
if(!max.equals("&deg;C"))
tmp = "<td style='width:10%;text-align:right;'><b>" + max + "</b></td></tr>";
else
tmp = "<td style='width:10%;text-align:right;'><b>&nbsp;</b></td></tr>";
out.append(tmp);

tmp = "<tr><td>" + text + "</td>";
out.append(tmp);

if(!min.equals(""))
tmp = "<td style='text-align:right;'>" + min + "&deg;C</td></tr>";
if(!min.equals("&deg;C"))
tmp = "<td style='text-align:right;'>" + min + "</td></tr>";
else
tmp = "<td style='text-align:right;'>&nbsp;</td></tr>";
out.append(tmp);
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/odiousapps/weewxweather/Forecast.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if(action != null && action.equals(Common.UPDATE_INTENT))
{
dark_theme = common.GetBoolPref("dark_theme", false);

if (common.GetBoolPref("radarforecast", true))
{
Common.LogMessage("Displaying forecast");
Expand Down Expand Up @@ -637,6 +639,13 @@ private void generateForecast()
updateForecast(content[0], content[1]);
break;
}
case "metoffice.gov.uk":
{
String[] content = common.processMET(data, true);
if(content != null && content.length >= 2)
updateForecast(content[0], content[1]);
break;
}
}
}

Expand Down Expand Up @@ -694,6 +703,9 @@ public void run()
case "weather.gc.ca":
im.setImageResource(R.drawable.wca);
break;
case "metoffice.gov.uk":
im.setImageResource(R.drawable.met);
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte
private Spinner s1;
private Switch show_indoor, metric_forecasts, dark_theme;
private TextView tv;
private boolean use_dark_theme = false;

private ProgressDialog dialog;

Expand All @@ -102,7 +101,6 @@ protected void onCreate(Bundle savedInstanceState)
setContentView(R.layout.main_activity);

common = new Common(this);
use_dark_theme = common.GetBoolPref("dark_theme");

mDrawerLayout = findViewById(R.id.drawer_layout);

Expand Down Expand Up @@ -722,6 +720,10 @@ protected PasswordAuthentication getPasswordAuthentication()
Common.LogMessage("forecast=" + forecast);
Common.LogMessage("fctype=" + fctype);
break;
case "metoffice.gov.uk":
Common.LogMessage("forecast=" + forecast);
Common.LogMessage("fctype=" + fctype);
break;
default:
common.SetStringPref("lastError", "forecast type " + fctype + " is invalid, check your settings file and try again.");
handlerForecast.sendEmptyMessage(0);
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/com/odiousapps/weewxweather/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Stats
private WebView wv;
private SeekBar seekBar;
private boolean dark_theme;
private LinearLayout ll1;
private LinearLayout ll1;

Stats(Common common)
{
Expand Down Expand Up @@ -157,11 +157,23 @@ public void onReceive(Context context, Intent intent)
{
try
{
Common.LogMessage("We have a hit, so we should probably update the screen.");
String action = intent.getAction();
if(action != null && action.equals(Common.UPDATE_INTENT))
updateFields();
else if(action != null && action.equals(Common.EXIT_INTENT))
Common.LogMessage("We have a hit, so we should probably update the screen.");
String action = intent.getAction();
if (action != null && action.equals(Common.UPDATE_INTENT))
{
dark_theme = common.GetBoolPref("dark_theme", false);

if(dark_theme)
{
seekBar.setBackgroundColor(0xff000000);
ll1.setBackgroundColor(0xff000000);
} else {
seekBar.setBackgroundColor(0xffffffff);
ll1.setBackgroundColor(0xffffffff);
}

updateFields();
} else if(action != null && action.equals(Common.EXIT_INTENT))
doPause();
} catch (Exception e) {
e.printStackTrace();
Expand Down
27 changes: 25 additions & 2 deletions app/src/main/java/com/odiousapps/weewxweather/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public boolean onLongClick(View v)
if(vibrator != null)
vibrator.vibrate(250);
Common.LogMessage("wv long press");
loadWebView();
forceRefresh();
return true;
}
});
Expand Down Expand Up @@ -505,10 +505,33 @@ public void run()
String wca = "<img src='wca.png' height='29px'/><br/>";
String tmpfc = "<html>";
if(dark_theme)
tmpfc += "<head><style>body{color: #fff; background-color: #000;}</style></head>";
tmpfc += "<head><style>body{color: #fff; background-color: #000;}</style></head>";
tmpfc += "<body style='text-align:center'>" + wca + content[0] + "</body></html>";
final String fc = tmpfc;

wv.post(new Runnable()
{
@Override
public void run()
{
wv.loadDataWithBaseURL("file:///android_res/drawable/", fc, "text/html", "utf-8", null);
}
});
break;
}
case "metoffice.gov.uk":
{
String[] content = common.processMET(data);
if(content == null || content.length <= 0)
return;

String logo = "<img src='met.png' height='29px'/><br/>";
String tmpfc = "<html>";
if(dark_theme)
tmpfc += "<head><style>body{color: #fff; background-color: #000;}</style></head>";
tmpfc += "<body style='text-align:center'>" + logo + content[0] + "</body></html>";
final String fc = tmpfc;

wv.post(new Runnable()
{
@Override
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/odiousapps/weewxweather/Webcam.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,14 @@ public void onReceive(Context context, Intent intent)
Common.LogMessage("Weather() We have a hit, so we should probably update the screen.");
String action = intent.getAction();
if(action != null && action.equals(Common.UPDATE_INTENT))
reloadWebView(true);
else if(action != null && action.equals(Common.EXIT_INTENT))
{
dark_theme = common.GetBoolPref("dark_theme", false);
if(dark_theme)
iv.setBackgroundColor(0xff000000);
else
iv.setBackgroundColor(0xffffffff);
reloadWebView(true);
} else if(action != null && action.equals(Common.EXIT_INTENT))
doPause();
} catch (Exception e) {
e.printStackTrace();
Expand Down
Binary file modified app/src/main/res/drawable/barometer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/bom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/compass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/droplet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/humidity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/met25.png
Binary file added app/src/main/res/drawable/met26.png
Binary file added app/src/main/res/drawable/met27.png
Binary file added app/src/main/res/drawable/met28.png
Binary file added app/src/main/res/drawable/met29.png
Binary file added app/src/main/res/drawable/met3.png
Binary file added app/src/main/res/drawable/met30.png
Binary file added app/src/main/res/drawable/met4.png
Binary file added app/src/main/res/drawable/met5.png
Binary file added app/src/main/res/drawable/met6.png
Binary file added app/src/main/res/drawable/met7.png
Binary file added app/src/main/res/drawable/met8.png
Binary file added app/src/main/res/drawable/met9.png
Binary file modified app/src/main/res/drawable/moonrise.png
Binary file modified app/src/main/res/drawable/moonset.png
Binary file modified app/src/main/res/drawable/purple.png
Binary file modified app/src/main/res/drawable/sunglasses.png
Binary file modified app/src/main/res/drawable/sunrise.png
Binary file modified app/src/main/res/drawable/sunset.png
Binary file modified app/src/main/res/drawable/temperature.png
Binary file modified app/src/main/res/drawable/umbrella.png
Binary file modified app/src/main/res/drawable/wca.png
Binary file modified app/src/main/res/drawable/windsock.png
Binary file modified app/src/main/res/drawable/wmo.png
Binary file modified app/src/main/res/drawable/wz.png
Binary file modified app/src/main/res/drawable/yrno.png

0 comments on commit fc8190b

Please sign in to comment.