From 3b3a7e18b2466ed6f6faf924f12b6e917e23aa76 Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Wed, 15 Jan 2025 20:16:01 +0100 Subject: [PATCH] Ability to get the temperature --- app/src/main/AndroidManifest.xml | 15 ++- .../one00kb/minipogodynka/MainActivity.java | 108 ++++++++++++++++++ .../minipogodynka/weather/WeatherData.java | 36 ++++++ app/src/main/res/layout/activity_main.xml | 27 +++++ 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/eur/one00kb/minipogodynka/MainActivity.java create mode 100644 app/src/main/java/eur/one00kb/minipogodynka/weather/WeatherData.java create mode 100644 app/src/main/res/layout/activity_main.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5f2c6ea..e9fc751 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,9 +2,22 @@ + + + + + tools:targetApi="35"> + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/eur/one00kb/minipogodynka/MainActivity.java b/app/src/main/java/eur/one00kb/minipogodynka/MainActivity.java new file mode 100644 index 0000000..861b45b --- /dev/null +++ b/app/src/main/java/eur/one00kb/minipogodynka/MainActivity.java @@ -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(); + } +} diff --git a/app/src/main/java/eur/one00kb/minipogodynka/weather/WeatherData.java b/app/src/main/java/eur/one00kb/minipogodynka/weather/WeatherData.java new file mode 100644 index 0000000..06dc3a8 --- /dev/null +++ b/app/src/main/java/eur/one00kb/minipogodynka/weather/WeatherData.java @@ -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; + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..ccf01aa --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,27 @@ + + + + + + + +