266 lines
8.3 KiB
Java
266 lines
8.3 KiB
Java
package at.fhj.airkoality.ui.activity;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.graphics.Bitmap;
|
|
import android.net.ConnectivityManager;
|
|
import android.net.NetworkInfo;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.view.Menu;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.widget.TextView;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
|
|
import at.fhj.airkoality.R;
|
|
import at.fhj.airkoality.db.room.AirKoalityDB;
|
|
|
|
import at.fhj.airkoality.model.LatestMeasurements;
|
|
import at.fhj.airkoality.model.Measurement;
|
|
import at.fhj.airkoality.network.HttpsGetTask;
|
|
import at.fhj.airkoality.network.RequestCallback;
|
|
import at.fhj.airkoality.util.Util;
|
|
|
|
public class MeasurementActivity extends AppCompatActivity implements RequestCallback {
|
|
|
|
private AirKoalityDB database;
|
|
|
|
private String locationName;
|
|
|
|
private TextView tvLocation;
|
|
private TextView tvBCvalue;
|
|
private TextView tvCOvalue;
|
|
private TextView tvO3value;
|
|
private TextView tvPM10value;
|
|
private TextView tvPM25value;
|
|
private TextView tvSO2value;
|
|
private TextView tvNO2value;
|
|
private View rootView;
|
|
|
|
private NetworkStateReceiver networkStateReceiver;
|
|
|
|
@Override
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_measurement);
|
|
|
|
tvLocation = findViewById(R.id.tvLocation);
|
|
tvBCvalue = findViewById(R.id.tvBCvalue);
|
|
tvCOvalue = findViewById(R.id.tvCOvalue);
|
|
tvO3value = findViewById(R.id.tvO3value);
|
|
tvPM10value = findViewById(R.id.tvPM10value);
|
|
tvPM25value = findViewById(R.id.tvPM25value);
|
|
tvSO2value = findViewById(R.id.tvSO2value);
|
|
tvNO2value = findViewById(R.id.tvNO2value);
|
|
|
|
rootView = findViewById(R.id.rootView);
|
|
|
|
//TODO: get locationname from intent
|
|
locationName = getIntent().getStringExtra("location_name");
|
|
|
|
tvLocation.setText(locationName);
|
|
database = AirKoalityDB.getDatabase(this);
|
|
|
|
networkStateReceiver = new NetworkStateReceiver();
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
setTitle(R.string.latest_measurements);
|
|
|
|
fetchLatestMeasurements(locationName);
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
|
|
|
|
registerReceiver(networkStateReceiver, intentFilter);
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
super.onPause();
|
|
unregisterReceiver(networkStateReceiver);
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean onSupportNavigateUp() {
|
|
onBackPressed();
|
|
return true;
|
|
}
|
|
|
|
private void fetchLatestMeasurements(String locationName) {
|
|
HttpsGetTask httpsGetTask = new HttpsGetTask(this);
|
|
httpsGetTask.execute("https://api.openaq.org/v1/latest?location=" + locationName);
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onRequestStart() {
|
|
}
|
|
|
|
public void updateUI(LatestMeasurements latestMeasurements) {
|
|
if (latestMeasurements != null) {
|
|
runOnUiThread(() -> {
|
|
for (Measurement measurement : latestMeasurements.getMeasurements()) {
|
|
String valueString = "" + measurement.getValue() + " " + measurement.getUnit();
|
|
switch (measurement.getParameter().toLowerCase()) {
|
|
case "bc":
|
|
tvBCvalue.setText(valueString);
|
|
break;
|
|
case "co":
|
|
tvCOvalue.setText(valueString);
|
|
break;
|
|
case "no2":
|
|
tvNO2value.setText(valueString);
|
|
break;
|
|
case "o3":
|
|
tvO3value.setText(valueString);
|
|
break;
|
|
case "pm10":
|
|
tvPM10value.setText(valueString);
|
|
break;
|
|
case "pm25":
|
|
tvPM25value.setText(valueString);
|
|
break;
|
|
case "so2":
|
|
tvSO2value.setText(valueString);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResult(String result) {
|
|
new Thread(() -> {
|
|
LatestMeasurements latestMeasurements;
|
|
if (result == null) {
|
|
latestMeasurements = database.latestMeasurementsDAO().getForLocation(locationName);
|
|
} else {
|
|
latestMeasurements = parseLatestMeasurements(result);
|
|
if (latestMeasurements != null) {
|
|
database.latestMeasurementsDAO().add(latestMeasurements);
|
|
}
|
|
}
|
|
|
|
updateUI(latestMeasurements);
|
|
}).start();
|
|
}
|
|
|
|
private LatestMeasurements parseLatestMeasurements(String json) {
|
|
LatestMeasurements latestMeasurements = null;
|
|
try {
|
|
JSONObject jsonObject = new JSONObject(json);
|
|
JSONArray results = jsonObject.getJSONArray("results");
|
|
JSONObject result = results.getJSONObject(0);
|
|
JSONArray measurementsArray = result.getJSONArray("measurements");
|
|
|
|
|
|
latestMeasurements = new LatestMeasurements();
|
|
latestMeasurements.setLocationName(result.getString("location"));
|
|
|
|
ArrayList<Measurement> measurements = new ArrayList<>();
|
|
for (int i = 0; i < measurementsArray.length(); i++) {
|
|
JSONObject measurementResult = measurementsArray.getJSONObject(i);
|
|
String parameter = measurementResult.getString("parameter");
|
|
double value = measurementResult.getDouble("value");
|
|
String unit = measurementResult.getString("unit");
|
|
measurements.add(new Measurement(parameter, value, unit));
|
|
}
|
|
latestMeasurements.setMeasurements(measurements);
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return latestMeasurements;
|
|
}
|
|
|
|
|
|
class NetworkStateReceiver extends BroadcastReceiver{
|
|
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
if(isConnected(context)) {
|
|
fetchLatestMeasurements(locationName);
|
|
}
|
|
}
|
|
|
|
|
|
private boolean isConnected(Context context){
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
|
|
|
|
return (networkInfo != null && networkInfo.isConnected());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
inflater.inflate(R.menu.menu_measurement, menu);
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()){
|
|
case R.id.action_share:
|
|
shareMeasurements();
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void shareMeasurements() {
|
|
Bitmap bitmap = Util.getBitmapFromView(rootView);
|
|
try {
|
|
Uri uri = Util.savePng(this, bitmap,"measurements.png");
|
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
|
intent.putExtra(Intent.EXTRA_STREAM, uri);
|
|
intent.putExtra(Intent.EXTRA_TEXT, "Messdaten von " + locationName);
|
|
intent.putExtra(Intent.EXTRA_SUBJECT, "AirKoality Daten");
|
|
intent.setType("image/png");
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
startActivity(intent);
|
|
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
}
|