diff --git a/AirKoality/AirKoality.iml b/AirKoality/AirKoality.iml
new file mode 100644
index 0000000..09b1fda
--- /dev/null
+++ b/AirKoality/AirKoality.iml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/AirKoality/app/app.iml b/AirKoality/app/app.iml
new file mode 100644
index 0000000..e1cae17
--- /dev/null
+++ b/AirKoality/app/app.iml
@@ -0,0 +1,179 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ generateDebugSources
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/AirKoality/app/build.gradle b/AirKoality/app/build.gradle
index d0f98bf..b781b68 100644
--- a/AirKoality/app/build.gradle
+++ b/AirKoality/app/build.gradle
@@ -29,4 +29,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
+ implementation 'android.arch.persistence.room:runtime:1.1.1'
+ annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/db/room/AirKoalityDB.java b/AirKoality/app/src/main/java/at/fhj/airkoality/db/room/AirKoalityDB.java
new file mode 100644
index 0000000..ab52a9b
--- /dev/null
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/db/room/AirKoalityDB.java
@@ -0,0 +1,26 @@
+package at.fhj.airkoality.db.room;
+
+import android.arch.persistence.room.Database;
+import android.arch.persistence.room.Room;
+import android.arch.persistence.room.RoomDatabase;
+import android.content.Context;
+
+import at.fhj.airkoality.model.Location;
+
+@Database(entities = {Location.class}, version = 1,exportSchema = false)
+public abstract class AirKoalityDB extends RoomDatabase {
+
+ private static AirKoalityDB instance;
+
+ public abstract LocationDAO locationDAO();
+
+
+ public static AirKoalityDB getDatabase(Context context){
+ if (instance == null) {
+ instance = Room.databaseBuilder(context, AirKoalityDB.class, "airkoality").allowMainThreadQueries().build();
+ }
+ return instance;
+ }
+
+
+}
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/db/room/LocationDAO.java b/AirKoality/app/src/main/java/at/fhj/airkoality/db/room/LocationDAO.java
new file mode 100644
index 0000000..16eab3a
--- /dev/null
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/db/room/LocationDAO.java
@@ -0,0 +1,28 @@
+package at.fhj.airkoality.db.room;
+
+import android.arch.persistence.room.Dao;
+import android.arch.persistence.room.Insert;
+import android.arch.persistence.room.OnConflictStrategy;
+import android.arch.persistence.room.Query;
+
+import java.util.List;
+
+import at.fhj.airkoality.model.Location;
+
+@Dao
+public interface LocationDAO {
+
+ @Query("SELECT * FROM location")
+ List getAll();
+
+ @Query("SELECT * FROM location WHERE location = :locationName")
+ Location getLocationWithName(String locationName);
+
+ @Insert(onConflict = OnConflictStrategy.REPLACE)
+ void addLocation(Location location);
+
+ @Insert(onConflict = OnConflictStrategy.REPLACE)
+ void addAll(List locations);
+
+
+}
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/model/Location.java b/AirKoality/app/src/main/java/at/fhj/airkoality/model/Location.java
index 8543cfd..5c51198 100644
--- a/AirKoality/app/src/main/java/at/fhj/airkoality/model/Location.java
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/model/Location.java
@@ -1,16 +1,27 @@
package at.fhj.airkoality.model;
+import android.arch.persistence.room.ColumnInfo;
+import android.arch.persistence.room.Entity;
+import android.arch.persistence.room.PrimaryKey;
+import android.support.annotation.NonNull;
+
+@Entity(tableName = "location")
public class Location {
+ @PrimaryKey
+ @NonNull
+ @ColumnInfo(name= "location")
private String location;
+ @ColumnInfo(name= "city")
private String city;
- private String county;
+ @ColumnInfo(name= "country")
+ private String country;
- public Location(String location, String city, String county) {
+ public Location(String location, String city, String country) {
this.location = location;
this.city = city;
- this.county = county;
+ this.country = country;
}
public String getLocation() {
@@ -29,12 +40,12 @@ public class Location {
this.city = city;
}
- public String getCounty() {
- return county;
+ public String getCountry() {
+ return country;
}
- public void setCounty(String county) {
- this.county = county;
+ public void setCountry(String county) {
+ this.country = county;
}
@Override
@@ -42,7 +53,7 @@ public class Location {
return "Location{" +
"location='" + location + '\'' +
", city='" + city + '\'' +
- ", county='" + county + '\'' +
+ ", county='" + country + '\'' +
'}';
}
}
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsClient.java b/AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsClient.java
new file mode 100644
index 0000000..7b7b398
--- /dev/null
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsClient.java
@@ -0,0 +1,118 @@
+
+package at.fhj.airkoality.network;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+
+import javax.net.ssl.HttpsURLConnection;
+
+
+/**
+ * General workflow for connections with UrlConnection:
+ *
+ * The connection object is created by invoking the openConnection method on a URL.
+ * The setup parameters and general request properties are manipulated.
+ * The actual connection to the remote object is made, using the connect method.
+ * The remote object becomes available. The header fields and the contents of the remote object can be accessed.
+ *
+ */
+public class HttpsClient {
+ public String get(URL url) throws IOException {
+ InputStream stream = null;
+ HttpsURLConnection connection = null;
+ String result = null;
+ try {
+ connection = (HttpsURLConnection) url.openConnection();
+ connection.setReadTimeout(3000);
+ connection.setConnectTimeout(3000);
+
+ //this is a get request
+ connection.setRequestMethod("GET");
+ //indicate that this connection wants to read from the connection
+ connection.setDoInput(true);
+
+ //connect and therefore initiate network communication
+ connection.connect();
+
+ int responseCode = connection.getResponseCode();
+ if (responseCode != HttpsURLConnection.HTTP_OK) {
+ throw new IOException("HTTP error code: " + responseCode);
+ }
+ stream = connection.getInputStream();
+
+ if(stream != null) {
+ BufferedReader r = new BufferedReader(new InputStreamReader(stream));
+ StringBuilder total = new StringBuilder();
+ for (String line; (line = r.readLine()) != null; ) {
+ total.append(line).append('\n');
+ }
+ result = total.toString();
+ }
+ } finally {
+ if (stream != null) {
+ stream.close();
+ }
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ return result;
+ }
+
+ public String post(URL url, String body) throws IOException {
+ InputStream stream = null;
+ HttpsURLConnection connection = null;
+ String result = null;
+ try {
+ connection = (HttpsURLConnection) url.openConnection();
+ connection.setReadTimeout(3000);
+ connection.setConnectTimeout(3000);
+
+ //this is a post request
+ connection.setRequestMethod("POST");
+ //indicate that this connection wants to read from the connection
+ connection.setDoOutput(true);
+
+ //write request body to connection object
+ OutputStream outputStream = connection.getOutputStream();
+ OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
+ outputStreamWriter.write(body);
+ outputStreamWriter.flush();
+ outputStreamWriter.close();
+ outputStream.close();
+
+ //connect and therefore initiate network communication
+ connection.connect();
+
+ int responseCode = connection.getResponseCode();
+ if (responseCode != HttpsURLConnection.HTTP_OK) {
+ throw new IOException("HTTP error code: " + responseCode);
+ }
+
+ stream = connection.getInputStream();
+
+ if(stream != null) {
+ BufferedReader r = new BufferedReader(new InputStreamReader(stream));
+ StringBuilder total = new StringBuilder();
+ for (String line; (line = r.readLine()) != null; ) {
+ total.append(line).append('\n');
+ }
+ result = total.toString();
+ }
+
+ } finally {
+ if (stream != null) {
+ stream.close();
+ }
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ return result;
+ }
+}
\ No newline at end of file
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsGetTask.java b/AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsGetTask.java
new file mode 100644
index 0000000..58131b9
--- /dev/null
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsGetTask.java
@@ -0,0 +1,60 @@
+package at.fhj.airkoality.network;
+
+import android.os.AsyncTask;
+import android.util.Log;
+
+import java.io.IOException;
+import java.net.URL;
+
+//Async task has 3 generic types for:
+//Params
+//Progress
+//Result
+public class HttpsGetTask extends AsyncTask {
+ private static final String TAG = "HttpsGetTask";
+ private RequestCallback callback;
+
+ public HttpsGetTask(RequestCallback callback) {
+ this.callback = callback;
+ }
+
+ //Do preparation here
+ //this is executed on UI thread, so no network communication here!
+ @Override
+ protected void onPreExecute() {
+ Log.d(TAG, "Preparing ...");
+ if(callback != null) {
+ callback.onRequestStart();
+ }
+ }
+
+ //this runs in background
+ //will be called after onPreExecute
+ @Override
+ protected String doInBackground(String... params) {
+ Log.d(TAG, "Doing background work ...");
+ HttpsClient httpsClient = new HttpsClient();
+ String result = null;
+ try {
+ result = httpsClient.get(new URL(params[0]));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ //will be called after doInBackground
+ //runs on UI thread
+ //gets the result of doInBackground as its parameter
+ @Override
+ protected void onPostExecute(String result) {
+ if(callback != null)
+ callback.onResult(result);
+ }
+
+ //will be called if publishProgress is called during doInBackground
+ //runs on UI thread
+ @Override
+ protected void onProgressUpdate(Void... values) {
+ }
+}
\ No newline at end of file
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/network/RequestCallback.java b/AirKoality/app/src/main/java/at/fhj/airkoality/network/RequestCallback.java
new file mode 100644
index 0000000..7da5194
--- /dev/null
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/network/RequestCallback.java
@@ -0,0 +1,7 @@
+package at.fhj.airkoality.network;
+
+public interface RequestCallback {
+ void onRequestStart();
+
+ void onResult(String result);
+}
diff --git a/AirKoality/app/src/main/java/at/fhj/airkoality/ui/fragment/LocationListFragment.java b/AirKoality/app/src/main/java/at/fhj/airkoality/ui/fragment/LocationListFragment.java
index 8264278..85f28c4 100644
--- a/AirKoality/app/src/main/java/at/fhj/airkoality/ui/fragment/LocationListFragment.java
+++ b/AirKoality/app/src/main/java/at/fhj/airkoality/ui/fragment/LocationListFragment.java
@@ -15,6 +15,7 @@ import java.util.ArrayList;
import java.util.List;
import at.fhj.airkoality.R;
+import at.fhj.airkoality.db.room.AirKoalityDB;
import at.fhj.airkoality.model.Location;
import at.fhj.airkoality.ui.adapter.LocationListAdapter;
@@ -40,7 +41,12 @@ public class LocationListFragment extends Fragment implements LocationListAdapte
locations.add(new Location("Daheim7", "Graz" , "Österreich"));
locations.add(new Location("Daheim8", "Graz" , "Österreich"));
- LocationListAdapter adapter = new LocationListAdapter(locations, this);
+
+ AirKoalityDB.getDatabase(getContext()).locationDAO().addAll(locations);
+
+ LocationListAdapter adapter = new LocationListAdapter(AirKoalityDB.getDatabase(getContext()).locationDAO().getAll(),this);
+
+
locationList.setAdapter(adapter);
locationList.setLayoutManager(new LinearLayoutManager(getContext()));