191 lines
7.2 KiB
Java
191 lines
7.2 KiB
Java
package com.example.airquality;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.view.MenuInflater;
|
|
import android.view.View;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.activity.EdgeToEdge;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.appcompat.widget.Toolbar;
|
|
import androidx.core.graphics.Insets;
|
|
import androidx.core.view.ViewCompat;
|
|
import androidx.core.view.WindowInsetsCompat;
|
|
|
|
import com.android.volley.Request;
|
|
import com.android.volley.RequestQueue;
|
|
import com.android.volley.Response;
|
|
import com.android.volley.VolleyError;
|
|
import com.android.volley.toolbox.JsonObjectRequest;
|
|
import com.android.volley.toolbox.Volley;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class AQActivity extends AppCompatActivity {
|
|
private static final Map<String, double[]> cityCoordinates = new HashMap<>();
|
|
static {
|
|
cityCoordinates.put("Subotica", new double[]{46.1, 19.6667});
|
|
cityCoordinates.put("Novi Sad", new double[]{45.2517, 19.8369});
|
|
cityCoordinates.put("Beograd", new double[]{44.804, 20.4651});
|
|
cityCoordinates.put("San Francisko", new double[]{37.7749, -122.4194});
|
|
cityCoordinates.put("Sidnej", new double[]{-33.8679, 151.2073});
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
EdgeToEdge.enable(this);
|
|
setContentView(R.layout.activity_aqactivity);
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
// Using toolbar as ActionBar
|
|
setSupportActionBar(toolbar);
|
|
|
|
// Display application icon in the toolbar
|
|
if (getSupportActionBar() != null) {
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
getSupportActionBar().setLogo(R.drawable.weather_clear_symbolic);
|
|
getSupportActionBar().setDisplayUseLogoEnabled(true);
|
|
}
|
|
|
|
loadAQ();
|
|
}
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
loadAQ();
|
|
}
|
|
|
|
public boolean onCreateOptionsMenu(android.view.Menu menu) {
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.menu_main, menu);
|
|
return super.onCreateOptionsMenu(menu);
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(android.view.MenuItem item) {
|
|
switch (item.getItemId()) {
|
|
case R.id.menu_aq:
|
|
Intent intentAQ = new Intent(this, AQActivity.class);
|
|
startActivity(intentAQ);
|
|
return true;
|
|
case R.id.menu_settings:
|
|
Intent intent = new Intent(this, SettingsActivity.class);
|
|
startActivity(intent);
|
|
return true;
|
|
default:
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
}
|
|
private String getAQIDescription(int aqi) {
|
|
switch (aqi) {
|
|
case 1: return "Good";
|
|
case 2: return "Fair";
|
|
case 3: return "Moderate";
|
|
case 4: return "Poor";
|
|
case 5: return "Very Poor";
|
|
default: return "Unknown";
|
|
}
|
|
}
|
|
public void loadAQ(){
|
|
String city = PreferencesManager.getCity(this);
|
|
double[] coords = cityCoordinates.get(city);
|
|
if (coords == null) {
|
|
Toast.makeText(this, "Unknown city: " + city, Toast.LENGTH_SHORT).show();
|
|
return;
|
|
}
|
|
String apiKey = getString(R.string.weather_api_key);
|
|
String url = String.format(
|
|
"https://api.openweathermap.org/data/2.5/air_pollution?lat=%.6f&lon=%.6f&appid=%s",
|
|
coords[0], coords[1], apiKey
|
|
);
|
|
|
|
RequestQueue queue = Volley.newRequestQueue(this);
|
|
JsonObjectRequest request = new JsonObjectRequest(
|
|
Request.Method.GET,
|
|
url,
|
|
null,
|
|
new Response.Listener<JSONObject>() {
|
|
@Override
|
|
public void onResponse(JSONObject response) {
|
|
try {
|
|
// Get JSON fields
|
|
JSONObject listItem = response.getJSONArray("list").getJSONObject(0);
|
|
int aqi = listItem.getJSONObject("main").getInt("aqi");
|
|
|
|
View rootLayout = findViewById(R.id.main);
|
|
|
|
int colorResId;
|
|
switch (aqi) {
|
|
case 1:
|
|
colorResId = R.color.aqi_good;
|
|
break;
|
|
case 2:
|
|
colorResId = R.color.aqi_fair;
|
|
break;
|
|
case 3:
|
|
colorResId = R.color.aqi_moderate;
|
|
break;
|
|
case 4:
|
|
colorResId = R.color.aqi_poor;
|
|
break;
|
|
case 5:
|
|
colorResId = R.color.aqi_very_poor;
|
|
break;
|
|
default:
|
|
colorResId = android.R.color.background_light;
|
|
}
|
|
|
|
rootLayout.setBackgroundColor(getResources().getColor(colorResId));
|
|
|
|
JSONObject components = listItem.getJSONObject("components");
|
|
double pm25 = components.getDouble("pm2_5");
|
|
double pm10 = components.getDouble("pm10");
|
|
double o3 = components.getDouble("o3");
|
|
double co = components.getDouble("co");
|
|
double no2 = components.getDouble("no2");
|
|
|
|
|
|
TextView tvCity = findViewById(R.id.tvCity);
|
|
TextView tvAQ = findViewById(R.id.tvAQ);
|
|
TextView tvComponents = findViewById(R.id.tvComponents);
|
|
|
|
|
|
String city = PreferencesManager.getCity(AQActivity.this);
|
|
|
|
|
|
tvCity.setText("City: " + city);
|
|
tvAQ.setText("AQI Index: " + aqi + " (" + getAQIDescription(aqi) + ")");
|
|
tvComponents.setText(
|
|
String.format("PM2.5: %.2f µg/m³\nPM10: %.2f µg/m³\nO₃: %.2f ppb\nCO: %.2f ppm\nNO₂: %.2f ppb",
|
|
pm25, pm10, o3, co, no2)
|
|
);
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
Toast.makeText(AQActivity.this, "Failed to parse air data", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
},
|
|
new Response.ErrorListener() {
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
Toast.makeText(AQActivity.this, "Failed to load air pollution data", Toast.LENGTH_SHORT).show();
|
|
error.printStackTrace();
|
|
}
|
|
}
|
|
);
|
|
|
|
queue.add(request);
|
|
|
|
}
|
|
} |