Commit a5ccc431329f5720860ec3f52d5792249d48b9bb
1 parent
e8cf6b8b
Get map image if none was selected
Use another server
Showing
3 changed files
with
95 additions
and
12 deletions
Show diff stats
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/PositioningActivity.java
1 | package fr.utbm.lo53.p2017.positionningapp; | 1 | package fr.utbm.lo53.p2017.positionningapp; |
2 | 2 | ||
3 | +import android.graphics.Bitmap; | ||
3 | import android.os.Bundle; | 4 | import android.os.Bundle; |
4 | import android.os.Handler; | 5 | import android.os.Handler; |
5 | import android.support.constraint.ConstraintLayout; | 6 | import android.support.constraint.ConstraintLayout; |
@@ -18,17 +19,22 @@ import com.android.volley.Request; | @@ -18,17 +19,22 @@ import com.android.volley.Request; | ||
18 | import com.android.volley.RequestQueue; | 19 | import com.android.volley.RequestQueue; |
19 | import com.android.volley.Response; | 20 | import com.android.volley.Response; |
20 | import com.android.volley.VolleyError; | 21 | import com.android.volley.VolleyError; |
22 | +import com.android.volley.toolbox.ImageRequest; | ||
21 | import com.android.volley.toolbox.JsonObjectRequest; | 23 | import com.android.volley.toolbox.JsonObjectRequest; |
22 | import com.android.volley.toolbox.Volley; | 24 | import com.android.volley.toolbox.Volley; |
23 | 25 | ||
26 | +import org.json.JSONException; | ||
24 | import org.json.JSONObject; | 27 | import org.json.JSONObject; |
25 | 28 | ||
29 | +import java.util.Random; | ||
30 | + | ||
26 | public class PositioningActivity extends BaseActivity { | 31 | public class PositioningActivity extends BaseActivity { |
27 | 32 | ||
28 | private static final String TAG = "PositioningActivity"; | 33 | private static final String TAG = "PositioningActivity"; |
29 | 34 | ||
30 | private ConstraintLayout start_layout; | 35 | private ConstraintLayout start_layout; |
31 | private TextView textView; | 36 | private TextView textView; |
37 | + private ImageView mapView; | ||
32 | 38 | ||
33 | private Animation hideAnimation; | 39 | private Animation hideAnimation; |
34 | 40 | ||
@@ -37,31 +43,46 @@ public class PositioningActivity extends BaseActivity { | @@ -37,31 +43,46 @@ public class PositioningActivity extends BaseActivity { | ||
37 | 43 | ||
38 | private boolean isLocating = false; | 44 | private boolean isLocating = false; |
39 | 45 | ||
40 | - // Request a string response from the provided URL. | ||
41 | - private JsonObjectRequest stringRequest; | 46 | + private Integer mapId = null; |
42 | 47 | ||
43 | private final Handler handler = new Handler(); | 48 | private final Handler handler = new Handler(); |
44 | private final Runnable runnable = new Runnable() { | 49 | private final Runnable runnable = new Runnable() { |
45 | @Override | 50 | @Override |
46 | public void run() { | 51 | public void run() { |
47 | // Create request | 52 | // Create request |
48 | - stringRequest = new JsonObjectRequest(Request.Method.GET, | 53 | + JsonObjectRequest locateRequest = new JsonObjectRequest(Request.Method.GET, |
49 | getURLSolver().locateURL(), | 54 | getURLSolver().locateURL(), |
50 | null, | 55 | null, |
51 | new Response.Listener<JSONObject>() { | 56 | new Response.Listener<JSONObject>() { |
52 | @Override | 57 | @Override |
53 | public void onResponse(JSONObject response) { | 58 | public void onResponse(JSONObject response) { |
54 | - textView.setText("Response is: "+ response.toString().substring(0,500)); | 59 | + try { |
60 | + float x = (float) response.getDouble("x"); | ||
61 | + float y = (float) response.getDouble("y"); | ||
62 | + int mapId = response.getInt("map_id"); | ||
63 | + if (hasCorrectMap(mapId)) { | ||
64 | + putPoint(x, y); | ||
65 | + textView.setText(""); | ||
66 | + } else { | ||
67 | + getMap(mapId); | ||
68 | + } | ||
69 | + } catch (JSONException e) { | ||
70 | + Log.e(TAG, e.toString()); | ||
71 | + textView.setText("Could not parse the response !"); | ||
72 | + } | ||
55 | } | 73 | } |
56 | }, new Response.ErrorListener() { | 74 | }, new Response.ErrorListener() { |
57 | @Override | 75 | @Override |
58 | public void onErrorResponse(VolleyError error) { | 76 | public void onErrorResponse(VolleyError error) { |
77 | + // TODO: Remove random position | ||
78 | + Random r = new Random(); | ||
79 | + putPoint(r.nextFloat(), r.nextFloat()); | ||
59 | textView.setText("Error: " + error.getMessage()); | 80 | textView.setText("Error: " + error.getMessage()); |
60 | Log.i(TAG, "Error during location request"); | 81 | Log.i(TAG, "Error during location request"); |
61 | } | 82 | } |
62 | }); | 83 | }); |
63 | - Log.v(TAG, "" + stringRequest); | ||
64 | - queue.add(stringRequest); | 84 | + Log.v(TAG, "" + locateRequest); |
85 | + queue.add(locateRequest); | ||
65 | 86 | ||
66 | handler.postDelayed(runnable, 1000); | 87 | handler.postDelayed(runnable, 1000); |
67 | } | 88 | } |
@@ -79,6 +100,8 @@ public class PositioningActivity extends BaseActivity { | @@ -79,6 +100,8 @@ public class PositioningActivity extends BaseActivity { | ||
79 | 100 | ||
80 | textView = (TextView) findViewById(R.id.ID_yolo); | 101 | textView = (TextView) findViewById(R.id.ID_yolo); |
81 | 102 | ||
103 | + mapView = (ImageView) findViewById(R.id.map); | ||
104 | + | ||
82 | queue = Volley.newRequestQueue(this); | 105 | queue = Volley.newRequestQueue(this); |
83 | } | 106 | } |
84 | 107 | ||
@@ -131,4 +154,35 @@ public class PositioningActivity extends BaseActivity { | @@ -131,4 +154,35 @@ public class PositioningActivity extends BaseActivity { | ||
131 | }); | 154 | }); |
132 | hideAnimation = animation; | 155 | hideAnimation = animation; |
133 | } | 156 | } |
157 | + | ||
158 | + private void putPoint(float x, float y) { | ||
159 | + ImageView mapMarker = (ImageView) findViewById(R.id.map_marker); | ||
160 | + float x_on_map = (float) mapView.getWidth() * x; // in px | ||
161 | + float y_on_map = (float) mapView.getHeight() * y; | ||
162 | + mapMarker.setX(mapView.getX() + x_on_map - mapMarker.getWidth()/2); | ||
163 | + mapMarker.setY(mapView.getY() + y_on_map - mapMarker.getHeight()); | ||
164 | + mapMarker.setVisibility(View.VISIBLE); | ||
165 | + } | ||
166 | + | ||
167 | + private boolean hasCorrectMap(int mapId) { | ||
168 | + return (this.mapId != null && this.mapId.equals(mapId)); | ||
169 | + } | ||
170 | + | ||
171 | + private void getMap(int mapId) { | ||
172 | + ImageRequest mapRequest = new ImageRequest( | ||
173 | + getURLSolver().mapDataURL(mapId), | ||
174 | + new Response.Listener<Bitmap>() { | ||
175 | + @Override | ||
176 | + public void onResponse(Bitmap bitmap) { | ||
177 | + mapView.setImageBitmap(bitmap); | ||
178 | + } | ||
179 | + }, 0, 0, null, | ||
180 | + new Response.ErrorListener() { | ||
181 | + public void onErrorResponse(VolleyError error) { | ||
182 | + mapView.setImageResource(R.drawable.map2); | ||
183 | + textView.setText("Can't load map!!!"); | ||
184 | + } | ||
185 | + }); | ||
186 | + queue.add(mapRequest); | ||
187 | + } | ||
134 | } | 188 | } |
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/network/URLSolver.java
@@ -63,4 +63,10 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList | @@ -63,4 +63,10 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList | ||
63 | .appendPath("request"); | 63 | .appendPath("request"); |
64 | return b.build().toString(); | 64 | return b.build().toString(); |
65 | } | 65 | } |
66 | + | ||
67 | + public String mapDataURL(int mapId) { | ||
68 | + Uri.Builder b = baseURL(); | ||
69 | + b.appendPath("static").appendPath("map.png"); | ||
70 | + return b.build().toString(); | ||
71 | + } | ||
66 | } | 72 | } |
app/src/main/res/layout/content_positioning.xml
@@ -9,33 +9,56 @@ | @@ -9,33 +9,56 @@ | ||
9 | tools:showIn="@layout/activity_positioning"> | 9 | tools:showIn="@layout/activity_positioning"> |
10 | 10 | ||
11 | <ImageView | 11 | <ImageView |
12 | - android:id="@+id/imageView" | 12 | + android:id="@+id/map" |
13 | android:layout_width="0dp" | 13 | android:layout_width="0dp" |
14 | android:layout_height="wrap_content" | 14 | android:layout_height="wrap_content" |
15 | - android:layout_marginLeft="8dp" | 15 | + android:layout_marginEnd="8dp" |
16 | + android:layout_marginLeft="0dp" | ||
16 | android:layout_marginRight="8dp" | 17 | android:layout_marginRight="8dp" |
18 | + android:layout_marginStart="8dp" | ||
17 | android:layout_marginTop="8dp" | 19 | android:layout_marginTop="8dp" |
20 | + android:adjustViewBounds="true" | ||
18 | android:scaleType="fitXY" | 21 | android:scaleType="fitXY" |
19 | app:layout_constraintLeft_toLeftOf="parent" | 22 | app:layout_constraintLeft_toLeftOf="parent" |
20 | app:layout_constraintRight_toRightOf="parent" | 23 | app:layout_constraintRight_toRightOf="parent" |
21 | app:layout_constraintTop_toTopOf="parent" | 24 | app:layout_constraintTop_toTopOf="parent" |
22 | - app:srcCompat="@drawable/map" | 25 | + app:srcCompat="@drawable/map2" /> |
26 | + | ||
27 | + <ImageView | ||
28 | + android:id="@+id/map_marker" | ||
29 | + android:layout_width="50dp" | ||
30 | + android:layout_height="50dp" | ||
31 | + android:layout_marginBottom="0dp" | ||
32 | + android:layout_marginEnd="8dp" | ||
33 | + android:layout_marginLeft="0dp" | ||
34 | + android:layout_marginRight="0dp" | ||
23 | android:layout_marginStart="8dp" | 35 | android:layout_marginStart="8dp" |
24 | - android:layout_marginEnd="8dp" /> | 36 | + android:layout_marginTop="0dp" |
37 | + android:adjustViewBounds="false" | ||
38 | + android:cropToPadding="false" | ||
39 | + app:layout_constraintBottom_toBottomOf="@+id/Map" | ||
40 | + app:layout_constraintHorizontal_bias="0.0" | ||
41 | + app:layout_constraintLeft_toLeftOf="parent" | ||
42 | + app:layout_constraintRight_toRightOf="parent" | ||
43 | + app:layout_constraintTop_toTopOf="@+id/Map" | ||
44 | + app:layout_constraintVertical_bias="0.03" | ||
45 | + app:srcCompat="@drawable/map_marker" /> | ||
25 | 46 | ||
26 | <TextView | 47 | <TextView |
27 | android:id="@+id/ID_yolo" | 48 | android:id="@+id/ID_yolo" |
28 | android:layout_width="wrap_content" | 49 | android:layout_width="wrap_content" |
29 | android:layout_height="wrap_content" | 50 | android:layout_height="wrap_content" |
30 | android:text="TextView" | 51 | android:text="TextView" |
31 | - android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> | 52 | + android:textAppearance="@style/TextAppearance.AppCompat.Medium" |
53 | + tools:layout_editor_absoluteX="0dp" | ||
54 | + android:layout_marginTop="0dp" | ||
55 | + app:layout_constraintTop_toBottomOf="@+id/start_layout" /> | ||
32 | 56 | ||
33 | <android.support.constraint.ConstraintLayout | 57 | <android.support.constraint.ConstraintLayout |
34 | android:id="@+id/start_layout" | 58 | android:id="@+id/start_layout" |
35 | android:layout_width="0dp" | 59 | android:layout_width="0dp" |
36 | android:layout_height="0dp" | 60 | android:layout_height="0dp" |
37 | android:onClick="startLocating" | 61 | android:onClick="startLocating" |
38 | - android:visibility="visible" | ||
39 | app:layout_constraintBottom_toBottomOf="parent" | 62 | app:layout_constraintBottom_toBottomOf="parent" |
40 | app:layout_constraintLeft_toLeftOf="parent" | 63 | app:layout_constraintLeft_toLeftOf="parent" |
41 | app:layout_constraintRight_toRightOf="parent" | 64 | app:layout_constraintRight_toRightOf="parent" |