Ability to get the temperature
This commit is contained in:
@@ -2,9 +2,22 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<!-- There is no need for fine location. Weather stations don't exist on every square meter of
|
||||
the world. -->
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
|
||||
<application
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
tools:targetApi="31" />
|
||||
tools:targetApi="35">
|
||||
<activity android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,108 @@
|
||||
package eur.one00kb.minipogodynka;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Locale;
|
||||
|
||||
import eur.one00kb.minipogodynka.weather.WeatherData;
|
||||
|
||||
public class MainActivity extends Activity implements LocationListener {
|
||||
private static final String API_FORMAT = "https://api.open-meteo" +
|
||||
".com/v1/forecast?latitude=%f&longitude=%f¤t=temperature_2m," +
|
||||
"relative_humidity_2m,rain,surface_pressure,wind_speed_10m";
|
||||
|
||||
private LocationManager mLocManager;
|
||||
private Button mUpdateButton;
|
||||
|
||||
private double mLat, mLong;
|
||||
private WeatherData mData;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mData = new WeatherData();
|
||||
mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
setContentView(R.layout.activity_main);
|
||||
mUpdateButton = findViewById(R.id.btn_update);
|
||||
mUpdateButton.setOnClickListener((button) -> updateWeather());
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private void checkAndRequestPerms() {
|
||||
if (isLocationAllowed()) {
|
||||
requestPermissions(new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION }, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
private void updateWeather() {
|
||||
checkAndRequestPerms();
|
||||
// First, we try to get the location of the user.
|
||||
if (isLocationAllowed()) {
|
||||
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isLocationAllowed() {
|
||||
return checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,
|
||||
android.os.Process.myPid(), android.os.Process.myUid())
|
||||
== PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
private void updateInfo() {
|
||||
TextView test1 = findViewById(R.id.temp_day);
|
||||
TextView test2 = findViewById(R.id.temp_night);
|
||||
test1.setText(mData.getTemperature());
|
||||
test2.setText(mData.getTemperature());
|
||||
}
|
||||
|
||||
private void getJson() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
URL url = new URL(String.format(Locale.ROOT, API_FORMAT, mLat, mLong));
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
reader.close();
|
||||
mData.fromJson(builder.toString());
|
||||
runOnUiThread(this::updateInfo);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
mLat = location.getLatitude();
|
||||
mLong = location.getLongitude();
|
||||
mLocManager.removeUpdates(this);
|
||||
getJson();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package eur.one00kb.minipogodynka.weather;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class WeatherData {
|
||||
private double mTemperature, mWindSpeed, mHumidity, mPressure, mRain;
|
||||
private String mTempUnit, mWindSpeedUnit, mHumidityUnit, mPressureUnit, mRainUnit;
|
||||
|
||||
public void fromJson(String jsonStr) {
|
||||
try {
|
||||
JSONObject json = new JSONObject(jsonStr);
|
||||
JSONObject units = json.getJSONObject("current_units");
|
||||
|
||||
mTempUnit = units.getString("temperature_2m");
|
||||
mWindSpeedUnit = units.getString("wind_speed_10m");
|
||||
mHumidityUnit = units.getString("relative_humidity_2m");
|
||||
mPressureUnit = units.getString("surface_pressure");
|
||||
mRainUnit = units.getString("rain");
|
||||
|
||||
JSONObject current = json.getJSONObject("current");
|
||||
|
||||
mTemperature = current.getDouble("temperature_2m");
|
||||
mWindSpeed = current.getDouble("wind_speed_10m");
|
||||
mHumidity = current.getDouble("relative_humidity_2m");
|
||||
mPressure = current.getDouble("surface_pressure");
|
||||
mRain = current.getDouble("rain");
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getTemperature() {
|
||||
return mTemperature + " " + mTempUnit;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/temp_day"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="0 °C"
|
||||
android:textSize="20sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/temp_night"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="2 °C"
|
||||
android:textSize="20sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_update"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Update" />
|
||||
|
||||
</TableLayout>
|
||||
Reference in New Issue
Block a user