Compare commits
19 Commits
LocationCr
...
directions
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b93ddaec5 | ||
|
|
5e218f3b99 | ||
|
|
96f32ccdbc | ||
|
|
d433565fef | ||
|
|
1ca13e3a6b | ||
|
|
9c4eb98fb4 | ||
|
|
610b23b056 | ||
|
|
95a2e4e849 | ||
|
|
74fb4440ed | ||
|
|
17d977bb17 | ||
|
|
b52c5556d2 | ||
|
|
70342c6506 | ||
|
|
311a794a69 | ||
|
|
dbc10014f9 | ||
|
|
1f835a94b0 | ||
|
|
12c8d6a62d | ||
|
|
9e8e9f4cdd | ||
|
|
1d48ada0ab | ||
|
|
6a07ce77f0 |
@@ -26,15 +26,13 @@ android {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
repositories {
|
||||
maven { url "https://jitpack.io" }
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
def room_version = "2.2.5"
|
||||
|
||||
implementation "androidx.room:room-runtime:$room_version"
|
||||
annotationProcessor "androidx.room:room-compiler:$room_version"
|
||||
|
||||
//gson
|
||||
implementation 'com.google.code.gson:gson:2.8.6'
|
||||
|
||||
@@ -52,6 +50,9 @@ dependencies {
|
||||
//osm
|
||||
implementation 'org.osmdroid:osmdroid-android:6.1.8'
|
||||
|
||||
//osm bonus pack
|
||||
implementation 'com.github.MKergall:osmbonuspack:6.6.0'
|
||||
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
|
||||
}
|
||||
6
app/src/main/assets/coupons.json
Normal file
6
app/src/main/assets/coupons.json
Normal file
@@ -0,0 +1,6 @@
|
||||
[
|
||||
{
|
||||
"code": "2345",
|
||||
"reward": "fdasfasdf"
|
||||
}
|
||||
]
|
||||
135
app/src/main/assets/locations.json
Normal file
135
app/src/main/assets/locations.json
Normal file
@@ -0,0 +1,135 @@
|
||||
[
|
||||
{
|
||||
"name":"McDonald's Breda Karnemelkstraat",
|
||||
"coordinates":"51.58596318905295,4.77586651481887",
|
||||
"description":"McDonald's restaurant",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Subway Karnemelkstraat 10 A, 1",
|
||||
"coordinates":"51.58632782568612,4.775219531501114",
|
||||
"description":"4811 KJ Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Wok To Go Halstraat 40",
|
||||
"coordinates":"51.58650312572897,4.777467069592386",
|
||||
"description":"4811HX Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"De Boterhal Grote Markt 19",
|
||||
"coordinates":"51.588069667604756,4.7762685632180695",
|
||||
"description":"4811XL Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Gauchos Grote Markt 33",
|
||||
"coordinates":"51.58859328857082,4.77601349563689",
|
||||
"description":"4811XP Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"The Tosti Club Breda A4, Vlaszak 2",
|
||||
"coordinates":"51.58905632735254,4.780729463841719",
|
||||
"description":"4811GR Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Coffee & Lunch 4 you Oude Vest 31",
|
||||
"coordinates":"51.58741730235298,4.779832967540571",
|
||||
"description":"4811HS Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"t Zusje Breda Vismarktstraat 28",
|
||||
"coordinates":"51.590133292554434,4.773911506170458",
|
||||
"description":"4811WE Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"VR-World Nassaustraat 2",
|
||||
"coordinates":"51.5913566342086,4.786256804321071",
|
||||
"description":"4811DD Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Kees Kroket Houtmarkt 9",
|
||||
"coordinates":"51.58647180329833,4.77741809637617",
|
||||
"description":"KEEEEES KROKET!",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Prison Escape Kloosterlaan 168",
|
||||
"coordinates":"51.59073795635181,4.784917104321059",
|
||||
"description":"4811EE Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"De Koepel - FutureDome Events Nassausingel 26",
|
||||
"coordinates":"51.590431588532105,4.786756741648511",
|
||||
"description":"4811HP Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
|
||||
{
|
||||
"name":"Escaping Breda: Escape Room Games Boschstraat 114",
|
||||
"coordinates":" 51.59110835530862,4.784147222780912",
|
||||
"description":"4811GK Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"MEZZ Breda Keizerstraat 101",
|
||||
"coordinates":"51.58394697737321,4.779757901349616",
|
||||
"description":"4811HL Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Het Klooster Breda Schorsmolenstraat 13",
|
||||
"coordinates":"51.58775443759389,4.765568874365066",
|
||||
"description":"4811VN Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Beach & Lounge club Spider “rooftop bar” Nieuwe Prinsenkade",
|
||||
"coordinates":"51.59212977605884,4.774043765582372",
|
||||
"description":"4811VC Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Koningin Wilhelmina Paviljoen Kraanstraat 4b",
|
||||
"coordinates":"51.590645369292396,4.776045124415531",
|
||||
"description":"4811XV Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Hercules en de Nemeïsche leeuw",
|
||||
"coordinates":"51.59130522182325,4.778161739135078",
|
||||
"description":"4811XJ Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Nassau-Baroniemonument - 1905 - Pierre Cuypers Delpratsingel 1",
|
||||
"coordinates":"51.592530636759136,4.780278353833301",
|
||||
"description":"4811AM Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Station Breda Gravinnen van Nassauboulevard 43",
|
||||
"coordinates":"51.59569850758307,4.780295549958155",
|
||||
"description":"4815CA Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Belcrum Beach Veilingkade 12a",
|
||||
"coordinates":"51.599434239284726,4.76632797992092",
|
||||
"description":"4815HC Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"De Belcrum Watertoren (1935) Speelhuislaan 158",
|
||||
"coordinates":"51.60135351009892,4.7705765989322755",
|
||||
"description":"4815CJ Breda",
|
||||
"imageUrl":"NULL"
|
||||
}
|
||||
]
|
||||
14
app/src/main/assets/routes.json
Normal file
14
app/src/main/assets/routes.json
Normal file
@@ -0,0 +1,14 @@
|
||||
[
|
||||
{
|
||||
"name": "rondje stad",
|
||||
"locations": [
|
||||
{
|
||||
"name": "kees kroket",
|
||||
"coordinates": "2.4654645,6.2342323",
|
||||
"description": "lekkere patatjes"
|
||||
}
|
||||
],
|
||||
"totalDistance": 2.3434,
|
||||
"totalTime": 342342
|
||||
}
|
||||
]
|
||||
@@ -13,6 +13,10 @@ import com.a1.nextlocation.fragments.HomeFragment;
|
||||
import com.a1.nextlocation.fragments.RouteFragment;
|
||||
import com.a1.nextlocation.fragments.SettingsFragment;
|
||||
import com.a1.nextlocation.fragments.StatisticFragment;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
import com.a1.nextlocation.recyclerview.CouponListManager;
|
||||
import com.a1.nextlocation.recyclerview.LocationListManager;
|
||||
import com.a1.nextlocation.recyclerview.RouteListManager;
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
@@ -28,6 +32,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
BottomNavigationView bottomNav = findViewById(R.id.navbar);
|
||||
bottomNav.setOnNavigationItemSelectedListener(navListener);
|
||||
|
||||
LocationListManager.INSTANCE.setContext(this);
|
||||
LocationListManager.INSTANCE.load();
|
||||
CouponListManager.INSTANCE.setContext(this);
|
||||
CouponListManager.INSTANCE.load();
|
||||
RouteListManager.INSTANCE.setContext(this);
|
||||
RouteListManager.INSTANCE.load();
|
||||
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).commit();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Entity(tableName = "coupon")
|
||||
|
||||
public class Coupon {
|
||||
|
||||
/**
|
||||
* fields need to be public for the database to be able to use them
|
||||
*/
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
@ColumnInfo(name = "code")
|
||||
private String code;
|
||||
|
||||
@ColumnInfo(name = "reward")
|
||||
@NonNull
|
||||
private String reward;
|
||||
|
||||
|
||||
@@ -1,43 +1,24 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.Ignore;
|
||||
import androidx.room.PrimaryKey;
|
||||
import androidx.room.TypeConverters;
|
||||
|
||||
import com.a1.nextlocation.data.db.CouponListTypeConverter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity(tableName = "userdata")
|
||||
public class Data {
|
||||
|
||||
|
||||
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
@ColumnInfo(name = "distance_traveled")
|
||||
private float distanceTraveled;
|
||||
|
||||
@ColumnInfo(name = "locations_visited")
|
||||
private int locationsVisited;
|
||||
|
||||
@ColumnInfo(name = "total_time")
|
||||
private int totalTime;
|
||||
|
||||
@TypeConverters(CouponListTypeConverter.class)
|
||||
private List<Coupon> couponList;
|
||||
|
||||
@Ignore
|
||||
private Location nextLocation;
|
||||
|
||||
@Ignore
|
||||
private Location lastLocation;
|
||||
|
||||
@Ignore
|
||||
private Route currentRoute;
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,56 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
public class FileIO {
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.Log;
|
||||
|
||||
public static void readFileData() {
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class FileIO<T> {
|
||||
private final String TAG = FileIO.class.getCanonicalName();
|
||||
|
||||
|
||||
public T readFileData(Context context, String fileName, Type typeOf) {
|
||||
Gson gson = new Gson();
|
||||
AssetManager am = context.getAssets();
|
||||
T res = null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
InputStream is = am.open(fileName);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(is);
|
||||
BufferedReader reader = new BufferedReader(inputStreamReader);
|
||||
String line;
|
||||
while ((line = reader.readLine())!= null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
public static void writeFileData() {
|
||||
|
||||
Log.d(TAG, "readFileData: got string: " + sb.toString());
|
||||
res = gson.fromJson(sb.toString(),typeOf);
|
||||
Log.d(TAG, "readFileData: got object: " + res);
|
||||
|
||||
reader.close();
|
||||
inputStreamReader.close();
|
||||
is.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, "readFileData: exception! " + e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public void writeFileData(T objectToWrite) {
|
||||
//TODO make
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,12 @@ package com.a1.nextlocation.data;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
|
||||
@Entity(tableName = "location")
|
||||
public class Location {
|
||||
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
private String name;
|
||||
|
||||
@@ -22,7 +18,6 @@ public class Location {
|
||||
private String coordinates;
|
||||
private String description;
|
||||
|
||||
@ColumnInfo(name = "image_url")
|
||||
@Nullable
|
||||
private String imageUrl;
|
||||
|
||||
@@ -91,4 +86,8 @@ public class Location {
|
||||
return lat1 + "," + long1;
|
||||
}
|
||||
|
||||
public GeoPoint convertToGeoPoint() {
|
||||
return new GeoPoint(this.getLat(),this.getLong());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +1,25 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
import androidx.room.TypeConverters;
|
||||
|
||||
import com.a1.nextlocation.data.db.LocationListTypeConverter;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity(tableName = "route")
|
||||
public class Route {
|
||||
|
||||
@PrimaryKey
|
||||
|
||||
@NonNull
|
||||
private String name;
|
||||
|
||||
@TypeConverters(LocationListTypeConverter.class)
|
||||
|
||||
private List<Location> locations;
|
||||
|
||||
@ColumnInfo(name = "total_distance")
|
||||
|
||||
private float totalDistance;
|
||||
|
||||
@ColumnInfo(name = "total_time")
|
||||
|
||||
private int totalTime;
|
||||
|
||||
public Route(@NotNull String name) {
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import androidx.room.TypeConverter;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CouponListTypeConverter {
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
@TypeConverter
|
||||
public static List<Coupon> toRoutesList(String data) {
|
||||
if (data == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Type listType = new TypeToken<List<Coupon>>() {}.getType();
|
||||
|
||||
return gson.fromJson(data,listType);
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
public static String fromRoutesList(List<Coupon> list) {
|
||||
return gson.toJson(list);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
import com.a1.nextlocation.data.Data;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.data.db.dao.CouponDao;
|
||||
import com.a1.nextlocation.data.db.dao.DataDao;
|
||||
import com.a1.nextlocation.data.db.dao.LocationDao;
|
||||
import com.a1.nextlocation.data.db.dao.RouteDao;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @author Sem
|
||||
*/
|
||||
@androidx.room.Database(entities = {Coupon.class,Route.class, Location.class, Data.class},version = 1,exportSchema = false)
|
||||
public abstract class Database extends RoomDatabase {
|
||||
|
||||
public abstract RouteDao routeDao();
|
||||
public abstract CouponDao couponDao();
|
||||
public abstract LocationDao locationDao();
|
||||
public abstract DataDao dataDao();
|
||||
|
||||
private static volatile Database INSTANCE;
|
||||
private static final int NUMBER_OF_THREADS = 4;
|
||||
static final ExecutorService databaseWriterExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
|
||||
|
||||
public static Database getDatabase(final Context context) {
|
||||
if (INSTANCE == null){
|
||||
synchronized (Database.class) {
|
||||
if (INSTANCE == null) {
|
||||
|
||||
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
|
||||
Database.class,"next_location_db").addCallback(callback).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private static RoomDatabase.Callback callback = new RoomDatabase.Callback() {
|
||||
@Override
|
||||
public void onCreate(@NonNull SupportSQLiteDatabase db) {
|
||||
super.onCreate(db);
|
||||
|
||||
databaseWriterExecutor.execute(() -> {
|
||||
|
||||
// TODO populate our database here
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import androidx.room.TypeConverter;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationListTypeConverter {
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
@TypeConverter
|
||||
public static List<Location> toLocationList(String data) {
|
||||
if (data == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Type listType = new TypeToken<List<Location>>() {}.getType();
|
||||
|
||||
return gson.fromJson(data,listType);
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
public static String fromLocationList(List<Location> list) {
|
||||
return gson.toJson(list);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface CouponDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertAll(Coupon... coupons);
|
||||
|
||||
@Query("DELETE FROM coupon")
|
||||
void deleteAll();
|
||||
|
||||
@Query("SELECT * FROM coupon")
|
||||
LiveData<List<Coupon>> selectAll();
|
||||
|
||||
@Update
|
||||
public void update(Coupon coupon);
|
||||
|
||||
/*
|
||||
to add an observer to the livedata, you can use the example from https://medium.com/mindorks/using-room-database-with-livedata-android-jetpack-cbf89b677b47
|
||||
*/
|
||||
|
||||
@Query("SELECT * FROM coupon WHERE code = :code LIMIT 1")
|
||||
Coupon selectCouponByCode(String code);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.dao;
|
||||
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.a1.nextlocation.data.Data;
|
||||
|
||||
@Dao
|
||||
public interface DataDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertAll(Data... datas);
|
||||
|
||||
@Update
|
||||
void update(Data data);
|
||||
|
||||
@Query("DELETE FROM userdata")
|
||||
void delete();
|
||||
|
||||
@Query("SELECT * FROM userdata LIMIT 1")
|
||||
Data getData();
|
||||
|
||||
@Query("SELECT * FROM userdata WHERE distance_traveled = :distance LIMIT 1")
|
||||
Data getDataByDistance(float distance);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface LocationDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertAll(Location... locations);
|
||||
|
||||
@Query("DELETE FROM location")
|
||||
void deleteAll();
|
||||
|
||||
@Query("SELECT * FROM location")
|
||||
LiveData<List<Location>> selectAll();
|
||||
|
||||
@Query("SELECT * FROM location WHERE name = :name LIMIT 1")
|
||||
Location getLocationByName(String name);
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.a1.nextlocation.data.Route;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface RouteDao {
|
||||
|
||||
@Insert
|
||||
void insertAll(Route... routes);
|
||||
|
||||
@Query("DELETE FROM route")
|
||||
void deleteAll();
|
||||
|
||||
@Query("SELECT * FROM route")
|
||||
LiveData<List<Route>> getAll();
|
||||
|
||||
@Query("SELECT * FROM route where name = :name LIMIT 1")
|
||||
Route getRouteByName(String name);
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.repositories;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
import com.a1.nextlocation.data.db.dao.CouponDao;
|
||||
import com.a1.nextlocation.data.db.Database;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CouponRepository {
|
||||
private CouponDao mCouponDao;
|
||||
private LiveData<List<Coupon>> mAllCoupons;
|
||||
|
||||
public CouponRepository(Context context) {
|
||||
Database db = Database.getDatabase(context);
|
||||
mCouponDao = db.couponDao();
|
||||
mAllCoupons = mCouponDao.selectAll();
|
||||
}
|
||||
|
||||
public void insert(Coupon... coupons) {
|
||||
mCouponDao.insertAll(coupons);
|
||||
}
|
||||
|
||||
public void update(Coupon coupon) {
|
||||
mCouponDao.update(coupon);
|
||||
}
|
||||
|
||||
public LiveData<List<Coupon>> getAllCoupons() {
|
||||
return mAllCoupons;
|
||||
}
|
||||
|
||||
public Coupon getCoupon(String code) {
|
||||
return mCouponDao.selectCouponByCode(code);
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
mCouponDao.deleteAll();
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.repositories;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.Data;
|
||||
import com.a1.nextlocation.data.db.Database;
|
||||
import com.a1.nextlocation.data.db.dao.DataDao;
|
||||
|
||||
public class DataRepository {
|
||||
private DataDao mDataDao;
|
||||
private Data data;
|
||||
|
||||
public DataRepository(Context context) {
|
||||
Database db = Database.getDatabase(context);
|
||||
mDataDao = db.dataDao();
|
||||
data = mDataDao.getData();
|
||||
}
|
||||
|
||||
public Data getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public Data getDataByDistance(float distance) {
|
||||
return mDataDao.getDataByDistance(distance);
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
mDataDao.delete();
|
||||
}
|
||||
|
||||
public void update(Data data) {
|
||||
mDataDao.update(data);
|
||||
}
|
||||
|
||||
public void insertAll(Data... datas) {
|
||||
mDataDao.insertAll(datas);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.repositories;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.db.Database;
|
||||
import com.a1.nextlocation.data.db.dao.LocationDao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LocationRepository {
|
||||
private LocationDao mLocationDao;
|
||||
private LiveData<List<Location>> mAllLocations;
|
||||
|
||||
public LocationRepository(Context context) {
|
||||
Database db = Database.getDatabase(context);
|
||||
mLocationDao = db.locationDao();
|
||||
mAllLocations = mLocationDao.selectAll();
|
||||
}
|
||||
|
||||
public LiveData<List<Location>> getAllLocations() {
|
||||
return mAllLocations;
|
||||
}
|
||||
|
||||
public Location getLocationByName(String name) {
|
||||
return mLocationDao.getLocationByName(name);
|
||||
}
|
||||
|
||||
public void insertAll(Location... locations) {
|
||||
mLocationDao.insertAll(locations);
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
mLocationDao.deleteAll();
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.a1.nextlocation.data.db.repositories;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.data.db.Database;
|
||||
import com.a1.nextlocation.data.db.dao.RouteDao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RouteRepository {
|
||||
private RouteDao mRouteDao;
|
||||
private LiveData<List<Route>> mAllRoutes;
|
||||
|
||||
public RouteRepository(Context context) {
|
||||
Database db = Database.getDatabase(context);
|
||||
mRouteDao = db.routeDao();
|
||||
mAllRoutes = mRouteDao.getAll();
|
||||
}
|
||||
|
||||
public LiveData<List<Route>> getAllRoutes() {
|
||||
return mAllRoutes;
|
||||
}
|
||||
|
||||
public Route getRouteByName(String name) {
|
||||
return mRouteDao.getRouteByName(name);
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
mRouteDao.deleteAll();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package com.a1.nextlocation.fragments;
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Resources;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
@@ -20,11 +21,18 @@ import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.a1.nextlocation.R;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.recyclerview.CouponListManager;
|
||||
import com.a1.nextlocation.recyclerview.CustomOverlay;
|
||||
import com.a1.nextlocation.recyclerview.LocationListManager;
|
||||
import com.a1.nextlocation.recyclerview.RouteListManager;
|
||||
|
||||
import org.osmdroid.api.IMapController;
|
||||
import org.osmdroid.config.Configuration;
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
import org.osmdroid.views.MapView;
|
||||
import org.osmdroid.views.overlay.Overlay;
|
||||
import org.osmdroid.views.overlay.OverlayItem;
|
||||
import org.osmdroid.views.overlay.compass.CompassOverlay;
|
||||
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
|
||||
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
|
||||
@@ -89,6 +97,19 @@ public class HomeFragment extends Fragment {
|
||||
mLocationOverlay.enableMyLocation();
|
||||
mapView.getOverlays().add(mLocationOverlay);
|
||||
|
||||
CustomOverlay customOverlay = new CustomOverlay(getResources().getDrawable(R.drawable.ic_baseline_location_on_24),mapView);
|
||||
|
||||
for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) {
|
||||
GeoPoint p = new GeoPoint(l.getLat(), l.getLong());
|
||||
OverlayItem overlayItem = new OverlayItem(l.getName(),l.getDescription(), p);
|
||||
|
||||
customOverlay.addOverlayItem(overlayItem);
|
||||
Log.d(TAG, "initMap: " + "succes");
|
||||
}
|
||||
|
||||
|
||||
mapView.getOverlays().add(customOverlay);
|
||||
|
||||
// add the zoom controller
|
||||
IMapController mapController = mapView.getController();
|
||||
mapController.setZoom(15.0);
|
||||
@@ -96,6 +117,7 @@ public class HomeFragment extends Fragment {
|
||||
// add location manager and set the start point
|
||||
LocationManager locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
|
||||
try {
|
||||
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
GeoPoint startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
|
||||
|
||||
@@ -2,19 +2,29 @@ package com.a1.nextlocation.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.a1.nextlocation.R;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
import com.a1.nextlocation.network.DirectionsListener;
|
||||
|
||||
public class RouteFragment extends Fragment {
|
||||
private static final String TAG = RouteFragment.class.getCanonicalName();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
ApiHandler.INSTANCE.addListener(this::onDirectionsAvailable);
|
||||
|
||||
}
|
||||
|
||||
@@ -24,4 +34,21 @@ public class RouteFragment extends Fragment {
|
||||
// Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_route, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
// ApiHandler.INSTANCE.getDirections(8.681436,49.41461,8.687872,49.420318);
|
||||
Route r = new Route("test");
|
||||
r.addLocation(new Location("test",8.681436,49.41461,"route",null));
|
||||
r.addLocation(new Location("test",8.687872,49.420318,"route",null));
|
||||
ApiHandler.INSTANCE.getDirections(r);
|
||||
}
|
||||
|
||||
public void onDirectionsAvailable(DirectionsResult result) {
|
||||
Log.d(TAG, "onDirectionsAvailable: got result! " + result);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
164
app/src/main/java/com/a1/nextlocation/json/DirectionsResult.java
Normal file
164
app/src/main/java/com/a1/nextlocation/json/DirectionsResult.java
Normal file
@@ -0,0 +1,164 @@
|
||||
package com.a1.nextlocation.json;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DirectionsResult {
|
||||
private static final String TAG = DirectionsResult.class.getCanonicalName();
|
||||
private List<DirectionsStep> steps = new ArrayList<>();
|
||||
private double distance;
|
||||
private double duration;
|
||||
private double[][] wayPointCoordinates;
|
||||
|
||||
public List<DirectionsStep> getSteps() {
|
||||
return steps;
|
||||
}
|
||||
|
||||
public void setSteps(List<DirectionsStep> steps) {
|
||||
this.steps = steps;
|
||||
}
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public double getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(double duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public void addStep(DirectionsStep step) {
|
||||
this.steps.add(step);
|
||||
}
|
||||
|
||||
/**
|
||||
* parses a given json string into this object. It gets all the waypoints and steps and combines them so that every step also has the correct coordinates associated with it
|
||||
* @param json the json string to parse.
|
||||
*/
|
||||
public void parse(String json) {
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
JsonObject feature = JsonParser.parseString(json).getAsJsonObject().get("features").getAsJsonArray().get(0).getAsJsonObject();
|
||||
JsonObject properties = feature.get("properties").getAsJsonObject();
|
||||
JsonArray wayPointCoordinates = feature.get("geometry").getAsJsonObject().getAsJsonArray("coordinates");
|
||||
this.wayPointCoordinates = new double[wayPointCoordinates.size()][2];
|
||||
|
||||
|
||||
// fill the way point coordinates list for later use
|
||||
for (int i = 0; i < wayPointCoordinates.size(); i++) {
|
||||
JsonElement j = wayPointCoordinates.get(i);
|
||||
JsonArray arr = j.getAsJsonArray();
|
||||
this.wayPointCoordinates[i][0] = arr.get(0).getAsDouble();
|
||||
this.wayPointCoordinates[i][1] = arr.get(1).getAsDouble();
|
||||
}
|
||||
|
||||
|
||||
JsonArray segments = properties.getAsJsonArray("segments");
|
||||
|
||||
for (JsonElement element : segments) {
|
||||
JsonObject segment = element.getAsJsonObject();
|
||||
|
||||
setDistance(segment.get("distance").getAsDouble());
|
||||
setDuration(segment.get("duration").getAsDouble());
|
||||
|
||||
JsonArray steps = segment.getAsJsonArray("steps");
|
||||
|
||||
for (JsonElement j : steps) {
|
||||
|
||||
DirectionsStep step = gson.fromJson(j,DirectionsStep.class);
|
||||
double lat;
|
||||
double longl;
|
||||
|
||||
// kinda stinky but it works
|
||||
for (int i = 0; i < 2; i++) {
|
||||
lat = this.wayPointCoordinates[step.getWay_points().get(i)][0];
|
||||
longl = this.wayPointCoordinates[step.getWay_points().get(i)][1];
|
||||
step.getWaypoints()[i] = new GeoPoint(lat,longl);
|
||||
}
|
||||
|
||||
addStep(step);
|
||||
Log.d(TAG, "parse: added step" + step);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void parseRoute(String json) {
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
JsonArray routes = JsonParser.parseString(json).getAsJsonObject().getAsJsonArray("routes");
|
||||
for (JsonElement element : routes) {
|
||||
JsonObject route = element.getAsJsonObject();
|
||||
JsonObject summary = route.getAsJsonObject("summary");
|
||||
this.distance = summary.get("distance").getAsDouble();
|
||||
this.duration = summary.get("duration").getAsDouble();
|
||||
|
||||
JsonPrimitive geometry = route.getAsJsonPrimitive("geometry");
|
||||
JsonArray wayPointCoordinates = GeometryDecoder.decodeGeometry(geometry.getAsString(),false);
|
||||
this.wayPointCoordinates = new double[wayPointCoordinates.size()][2];
|
||||
|
||||
|
||||
// fill the way point coordinates list for later use
|
||||
for (int i = 0; i < wayPointCoordinates.size(); i++) {
|
||||
JsonElement j = wayPointCoordinates.get(i);
|
||||
JsonArray arr = j.getAsJsonArray();
|
||||
this.wayPointCoordinates[i][0] = arr.get(0).getAsDouble();
|
||||
this.wayPointCoordinates[i][1] = arr.get(1).getAsDouble();
|
||||
}
|
||||
|
||||
|
||||
JsonArray segments = route.getAsJsonArray("segments");
|
||||
|
||||
for (JsonElement e : segments) {
|
||||
JsonObject segment = e.getAsJsonObject();
|
||||
|
||||
setDistance(segment.get("distance").getAsDouble());
|
||||
setDuration(segment.get("duration").getAsDouble());
|
||||
|
||||
JsonArray steps = segment.getAsJsonArray("steps");
|
||||
|
||||
for (JsonElement j : steps) {
|
||||
|
||||
DirectionsStep step = gson.fromJson(j,DirectionsStep.class);
|
||||
double lat;
|
||||
double longl;
|
||||
|
||||
// kinda stinky but it works
|
||||
for (int i = 0; i < 2; i++) {
|
||||
lat = this.wayPointCoordinates[step.getWay_points().get(i)][0];
|
||||
longl = this.wayPointCoordinates[step.getWay_points().get(i)][1];
|
||||
step.getWaypoints()[i] = new GeoPoint(lat,longl);
|
||||
}
|
||||
|
||||
addStep(step);
|
||||
Log.d(TAG, "parse: added step" + step);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.a1.nextlocation.json;
|
||||
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* pojo class that holds the step object from the api response
|
||||
*/
|
||||
public class DirectionsStep {
|
||||
private double distance;
|
||||
private double duration;
|
||||
private String instruction;
|
||||
private String name;
|
||||
/**
|
||||
* these are the actual waypoints that the step refers to. The first is the beginning of the step, and the second is what it leads to.
|
||||
* The second geopoint is always the first geopoint of the next step in the list of the {@link DirectionsResult} object.
|
||||
*/
|
||||
private GeoPoint[] waypoints = new GeoPoint[2];
|
||||
/**
|
||||
* this is a list of the waypoints that are in the response, it is called way_points so it can be automatically serialized with gson
|
||||
*/
|
||||
private ArrayList<Integer> way_points;
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public double getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(double duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getInstruction() {
|
||||
return instruction;
|
||||
}
|
||||
|
||||
public void setInstruction(String instruction) {
|
||||
this.instruction = instruction;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ArrayList<Integer> getWay_points() {
|
||||
return way_points;
|
||||
}
|
||||
|
||||
public void setWay_points(ArrayList<Integer> way_points) {
|
||||
this.way_points = way_points;
|
||||
}
|
||||
|
||||
public GeoPoint[] getWaypoints() {
|
||||
return waypoints;
|
||||
}
|
||||
|
||||
public void setWaypoints(GeoPoint[] waypoints) {
|
||||
this.waypoints = waypoints;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.a1.nextlocation.json;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
/**
|
||||
* source: https://github.com/GIScience/openrouteservice-docs#geometry-decoding
|
||||
*/
|
||||
public class GeometryDecoder {
|
||||
|
||||
public static JsonArray decodeGeometry(String encodedGeometry, boolean inclElevation) {
|
||||
JsonArray geometry = new JsonArray();
|
||||
int len = encodedGeometry.length();
|
||||
int index = 0;
|
||||
int lat = 0;
|
||||
int lng = 0;
|
||||
int ele = 0;
|
||||
|
||||
while (index < len) {
|
||||
int result = 1;
|
||||
int shift = 0;
|
||||
int b;
|
||||
do {
|
||||
b = encodedGeometry.charAt(index++) - 63 - 1;
|
||||
result += b << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x1f);
|
||||
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
|
||||
|
||||
result = 1;
|
||||
shift = 0;
|
||||
do {
|
||||
b = encodedGeometry.charAt(index++) - 63 - 1;
|
||||
result += b << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x1f);
|
||||
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
|
||||
|
||||
|
||||
if(inclElevation){
|
||||
result = 1;
|
||||
shift = 0;
|
||||
do {
|
||||
b = encodedGeometry.charAt(index++) - 63 - 1;
|
||||
result += b << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x1f);
|
||||
ele += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
|
||||
}
|
||||
|
||||
JsonArray location = new JsonArray();
|
||||
location.add(lat / 1E5);
|
||||
location.add(lng / 1E5);
|
||||
if(inclElevation){
|
||||
location.add((float) (ele / 100));
|
||||
}
|
||||
geometry.add(location);
|
||||
}
|
||||
return geometry;
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,20 @@ import android.util.Log;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public enum ApiHandler {
|
||||
@@ -22,21 +28,22 @@ public enum ApiHandler {
|
||||
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
private final String BASE_URL = "https://api.openrouteservice.org/v2/directions/";
|
||||
private final String API_KEY = "5b3ce3597851110001cf6248d4eee2099f724255918adc71cc502b2a";
|
||||
private final String DIRECTIONS_MODE = "foot_walking";
|
||||
private final String DIRECTIONS_MODE = "foot-walking";
|
||||
private List<DirectionsListener> listeners = new ArrayList<>();
|
||||
|
||||
private OkHttpClient client = new OkHttpClient();
|
||||
|
||||
public Route getDirections(Location startLocation, Location endLocation) {
|
||||
return getDirections(startLocation.getCoordinates(),endLocation.getCoordinates());
|
||||
public void getDirections(Location startLocation, Location endLocation) {
|
||||
getDirections(startLocation.getCoordinates(),endLocation.getCoordinates());
|
||||
}
|
||||
|
||||
public Route getDirections(double startLat, double startLong, double endLat, double endLong) {
|
||||
return getDirections(startLat + "," + startLong, endLat + "," + endLong);
|
||||
public void getDirections(double startLat, double startLong, double endLat, double endLong) {
|
||||
getDirections(startLat + "," + startLong, endLat + "," + endLong);
|
||||
}
|
||||
|
||||
public Route getDirections(String startLocation, String endLocation) {
|
||||
public void getDirections(String startLocation, String endLocation) {
|
||||
|
||||
String requestUrl = BASE_URL + DIRECTIONS_MODE + "?api_key=" + API_KEY + "&start=" +startLocation + "&end=" + endLocation;
|
||||
AtomicReference<Route> res = null;
|
||||
Thread t = new Thread(() -> {
|
||||
|
||||
Request request = new Request.Builder().url(requestUrl).build();
|
||||
@@ -45,6 +52,14 @@ public enum ApiHandler {
|
||||
if (response.body() != null) {
|
||||
String responseString = Objects.requireNonNull(response.body()).string();
|
||||
Log.d(TAG, "getDirections: got response: " + responseString);
|
||||
|
||||
DirectionsResult result = new DirectionsResult();
|
||||
result.parse(responseString);
|
||||
Log.d(TAG, "getDirections: " + result.getSteps().size());
|
||||
|
||||
for (DirectionsListener listener : listeners) {
|
||||
listener.onDirectionsAvailable(result);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
@@ -54,12 +69,59 @@ public enum ApiHandler {
|
||||
|
||||
t.start();
|
||||
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res.get();
|
||||
|
||||
public void addListener(DirectionsListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
public void getDirections(Route route) {
|
||||
// for (int i = 0; i < route.getLocations().size()-1; i+= 2) {
|
||||
// Location start = route.getLocations().get(i);
|
||||
// Location end = route.getLocations().get(i+1);
|
||||
// getDirections(start,end);
|
||||
// }
|
||||
|
||||
ArrayList<double[]> allCoords = new ArrayList<>();
|
||||
for (Location location : route.getLocations()) {
|
||||
allCoords.add(location.getCoordinatesAsDoubles());
|
||||
}
|
||||
|
||||
String body = "{\"coordinates\":" + new Gson().toJson(allCoords) + "}";
|
||||
|
||||
|
||||
String requestUrl = BASE_URL + DIRECTIONS_MODE + "?api_key=" + API_KEY;
|
||||
|
||||
Thread t = new Thread(() -> {
|
||||
|
||||
RequestBody requestBody = RequestBody.create(body,JSON);
|
||||
Request request = new Request.Builder()
|
||||
.url(requestUrl)
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (response.body() != null) {
|
||||
String responseString = Objects.requireNonNull(response.body()).string();
|
||||
Log.d(TAG, "getDirections: got response: " + responseString);
|
||||
|
||||
DirectionsResult result = new DirectionsResult();
|
||||
result.parseRoute(responseString);
|
||||
Log.d(TAG, "getDirections: " + result.getSteps().size());
|
||||
|
||||
for (DirectionsListener listener : listeners) {
|
||||
listener.onDirectionsAvailable(result);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, "getDirections: caught exception: " + e.getLocalizedMessage());
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.a1.nextlocation.network;
|
||||
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
|
||||
public interface DirectionsListener {
|
||||
void onDirectionsAvailable(DirectionsResult result);
|
||||
}
|
||||
@@ -1,23 +1,34 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CouponListManager {
|
||||
public enum CouponListManager {
|
||||
INSTANCE;
|
||||
|
||||
private List<Coupon> coupon;
|
||||
|
||||
public CouponListManager(){
|
||||
private List<Coupon> couponList;
|
||||
private Context context;
|
||||
|
||||
public void setContext(Context context) {
|
||||
this.context = context;
|
||||
this.couponList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Coupon> getCoupon() {
|
||||
return coupon;
|
||||
public List<Coupon> getCouponList() {
|
||||
return couponList;
|
||||
}
|
||||
|
||||
public void setCoupon(List<Coupon> location) {
|
||||
this.coupon = coupon;
|
||||
public Coupon getCoupon(int place) {
|
||||
return couponList.get(place);
|
||||
}
|
||||
|
||||
public void load(){
|
||||
CouponLoader couponLoader = new CouponLoader(this.context);
|
||||
this.couponList = couponLoader.load();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,32 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
public class CouponLoader implements Loader{
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
import com.a1.nextlocation.data.FileIO;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CouponLoader implements Loader<List<Coupon>> {
|
||||
private final Context context;
|
||||
private final String TAG = CouponLoader.class.getCanonicalName();
|
||||
|
||||
public CouponLoader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
public ArrayList<Coupon> load() {
|
||||
FileIO<ArrayList<Coupon>> fileIO = new FileIO<>();
|
||||
ArrayList<Coupon> res = fileIO.readFileData(context, "coupons.json",new TypeToken<ArrayList<Coupon>>(){}.getType());
|
||||
Log.d(TAG, "load: " + res);
|
||||
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
import android.graphics.Point;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import org.osmdroid.api.IMapView;
|
||||
import org.osmdroid.views.MapView;
|
||||
import org.osmdroid.views.overlay.ItemizedOverlay;
|
||||
import org.osmdroid.views.overlay.OverlayItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CustomOverlay extends ItemizedOverlay<OverlayItem> {
|
||||
private final MapView mapView;
|
||||
private ArrayList<OverlayItem> overlayItems = new ArrayList<>();
|
||||
|
||||
public CustomOverlay(Drawable pDefaultMarker, MapView mapView) {
|
||||
super(pDefaultMarker);
|
||||
this.mapView = mapView;
|
||||
|
||||
}
|
||||
|
||||
public void addOverlayItem(OverlayItem item) {
|
||||
overlayItems.add(item);
|
||||
populate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected OverlayItem createItem(int i) {
|
||||
return overlayItems.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return overlayItems.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSnapToItem(int x, int y, Point snapPoint, IMapView mapView) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
public interface Loader {
|
||||
void load();
|
||||
public interface Loader<T> {
|
||||
T load();
|
||||
}
|
||||
|
||||
@@ -1,22 +1,36 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
|
||||
import org.osmdroid.util.GeoPoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationListManager {
|
||||
public enum LocationListManager {
|
||||
INSTANCE;
|
||||
|
||||
private List<Location> location;
|
||||
|
||||
public LocationListManager(){
|
||||
private List<Location> locationList;
|
||||
private Context context;
|
||||
|
||||
public void setContext(Context context) {
|
||||
this.context = context;
|
||||
this.locationList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Location> getLocation() {
|
||||
return location;
|
||||
public List<Location> getLocationList() {
|
||||
return locationList;
|
||||
}
|
||||
|
||||
public void setLocation(List<Location> location) {
|
||||
this.location = location;
|
||||
public Location getLocation(int place) {
|
||||
return locationList.get(place);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
LocationLoader locationLoader = new LocationLoader(this.context);
|
||||
this.locationList = locationLoader.load();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,28 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
public class LocationLoader implements Loader{
|
||||
@Override
|
||||
public void load() {
|
||||
import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.FileIO;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationLoader implements Loader<List<Location>> {
|
||||
private final Context context;
|
||||
|
||||
public LocationLoader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Location> load() {
|
||||
FileIO<ArrayList<Location>> fileIO = new FileIO<>();
|
||||
|
||||
ArrayList<Location> res = fileIO.readFileData(context,"locations.json",new TypeToken<ArrayList<Location>>(){}.getType());
|
||||
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,34 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.Route;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RouteListManager implements Loader{
|
||||
public enum RouteListManager{
|
||||
INSTANCE;
|
||||
|
||||
List<Route> routes;
|
||||
|
||||
public RouteListManager(){
|
||||
private List<Route> routeList;
|
||||
private Context context;
|
||||
|
||||
public void setContext(Context context) {
|
||||
this.context = context;
|
||||
this.routeList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Route> getRoutes() {
|
||||
return routes;
|
||||
|
||||
public List<Route> getRouteList() {
|
||||
return routeList;
|
||||
}
|
||||
|
||||
public void setRoutes(List<Route> routes) {
|
||||
this.routes = routes;
|
||||
public Route getRoute(int place) {
|
||||
return routeList.get(place);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
|
||||
RouteLoader routeLoader = new RouteLoader(this.context);
|
||||
this.routeList = routeLoader.load();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
public class RouteLoader implements Loader {
|
||||
import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.FileIO;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class RouteLoader implements Loader<List<Route>> {
|
||||
private final Context context;
|
||||
|
||||
public RouteLoader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
public ArrayList<Route> load() {
|
||||
|
||||
FileIO<ArrayList<Route>> fileIO = new FileIO<>();
|
||||
ArrayList<Route> res = fileIO.readFileData(context, "routes.json",new TypeToken<ArrayList<Route>>(){}.getType());
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:fillColor="@color/primaryColour"
|
||||
android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zM12,11.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z"/>
|
||||
</vector>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||
<!-- Base application theme. -->
|
||||
<style name="Theme.NextLocation" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||
<style name="Theme.NextLocation" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/purple_200</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||
<!-- Base application theme. -->
|
||||
<style name="Theme.NextLocation" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||
<style name="Theme.NextLocation" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/primaryColour</item>
|
||||
<!-- Secondary brand color. -->
|
||||
|
||||
Reference in New Issue
Block a user