Commit 21d351b286470973151eda1624f562334ccb0b8c

Authored by FatBaz
Committed by florian staine
1 parent a5ccc431

Add map_id spinner, Calibration request, correct Map_Marker positioning

app/src/main/java/fr/utbm/lo53/p2017/positionningapp/Calibration.java
1 1 package fr.utbm.lo53.p2017.positionningapp;
2 2  
3   -import android.content.Intent;
4 3 import android.os.Bundle;
5   -import android.support.design.widget.FloatingActionButton;
6 4 import android.support.design.widget.Snackbar;
7   -import android.support.v7.app.AppCompatActivity;
8 5 import android.support.v7.widget.Toolbar;
9   -import android.view.MotionEvent;
10   -import android.view.View;
  6 +import android.util.Log;
11 7 import android.view.Menu;
12 8 import android.view.MenuItem;
  9 +import android.view.MotionEvent;
  10 +import android.view.View;
  11 +import android.widget.AdapterView;
  12 +import android.widget.ArrayAdapter;
13 13 import android.widget.Button;
14 14 import android.widget.ImageView;
  15 +import android.widget.Spinner;
  16 +
  17 +import com.android.volley.Request;
  18 +import com.android.volley.RequestQueue;
  19 +import com.android.volley.Response;
  20 +import com.android.volley.VolleyError;
  21 +import com.android.volley.toolbox.JsonObjectRequest;
  22 +import com.android.volley.toolbox.JsonRequest;
  23 +import com.android.volley.toolbox.Volley;
  24 +
  25 +import org.json.JSONException;
  26 +import org.json.JSONObject;
15 27  
16 28 public class Calibration extends BaseActivity {
  29 + public static final String TAG = "Calibration";
17 30 float height,width;
  31 + long map_id;
  32 + private ImageView MapMarker;
  33 + private RequestQueue queue;
  34 +
18 35 @Override
19 36 protected void onCreate(Bundle savedInstanceState) {
20 37  
... ... @@ -24,11 +41,19 @@ public class Calibration extends BaseActivity {
24 41 setSupportActionBar(toolbar);
25 42  
26 43 final ImageView Map = (ImageView) findViewById(R.id.Map);
27   - final ImageView MapMarker = (ImageView) findViewById(R.id.MapMarker);
  44 + MapMarker = (ImageView) findViewById(R.id.MapMarker);
28 45 final Button setPointButton = (Button) findViewById(R.id.setPointButton);
29 46 final Button cancelButton = (Button) findViewById(R.id.cancelButton);
30 47 final Button measureButton = (Button) findViewById(R.id.measureButton);
  48 + final Spinner spinner = (Spinner) findViewById(R.id.spinner);
  49 + queue = Volley.newRequestQueue(this);
  50 +
  51 + ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.map_choices, android.R.layout.simple_spinner_dropdown_item);
  52 + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  53 + spinner.setAdapter(adapter);
31 54  
  55 + Map.setVisibility(Map.INVISIBLE);
  56 + setPointButton.setVisibility(setPointButton.INVISIBLE);
32 57 MapMarker.setVisibility(MapMarker.INVISIBLE);
33 58 cancelButton.setVisibility(cancelButton.INVISIBLE);
34 59 measureButton.setVisibility(measureButton.INVISIBLE);
... ... @@ -41,12 +66,13 @@ public class Calibration extends BaseActivity {
41 66 /*motionEvent.getAction()==motionEvent.ACTION_MOVE &&*/
42 67 if( setPointButton.getVisibility() == setPointButton.INVISIBLE && motionEvent.getX() <= Map.getWidth() && motionEvent.getY() <= Map.getHeight() && motionEvent.getX() >= 0 && motionEvent.getY() >= 0) {
43 68 MapMarker.setVisibility(MapMarker.VISIBLE);
44   - height = motionEvent.getX();
45   - width = motionEvent.getY();
46   - Snackbar.make(view, " X : " + height + " Y : " + width, Snackbar.LENGTH_LONG)
47   - .setAction("Action", null).show();
48   - MapMarker.setX(motionEvent.getX()- (view.getWidth()- Map.getWidth())/2 -40);//- MapMarker.getWidth()/2);
49   - MapMarker.setY(motionEvent.getY()+ (view.getHeight()/5));//+(MapMarker.getWidth()/(5/4)));
  69 + width = motionEvent.getX() / Map.getWidth();
  70 + height = motionEvent.getY() / Map.getHeight();
  71 + /*Snackbar.make(view, " X : " + height + " Y : " + width, Snackbar.LENGTH_LONG)
  72 + .setAction("Action", null).show();*/
  73 + Log.i(TAG, String.format("Width: %f, height: %f", width, height));
  74 + MapMarker.setX(motionEvent.getX() - MapMarker.getWidth()/2 + Map.getX());
  75 + MapMarker.setY(motionEvent.getY() - MapMarker.getHeight() + Map.getY());
50 76 }
51 77 return true;
52 78 }
... ... @@ -58,15 +84,7 @@ public class Calibration extends BaseActivity {
58 84 setPointButton.setVisibility(setPointButton.INVISIBLE);
59 85 cancelButton.setVisibility(cancelButton.VISIBLE);
60 86 measureButton.setVisibility(measureButton.VISIBLE);
61   - }
62   - });
63   - measureButton.setOnClickListener(new View.OnClickListener() {
64   - @Override
65   - public void onClick(View v) {
66   - if(MapMarker.getVisibility()==MapMarker.INVISIBLE) {
67   - Snackbar.make(v, "You need to set a point before measurement.", Snackbar.LENGTH_LONG)
68   - .setAction("Action", null).show();
69   - }
  87 + spinner.setVisibility(spinner.INVISIBLE);
70 88 }
71 89 });
72 90 cancelButton.setOnClickListener(new View.OnClickListener() {
... ... @@ -76,6 +94,22 @@ public class Calibration extends BaseActivity {
76 94 cancelButton.setVisibility(cancelButton.INVISIBLE);
77 95 measureButton.setVisibility(measureButton.INVISIBLE);
78 96 MapMarker.setVisibility(MapMarker.INVISIBLE);
  97 + spinner.setVisibility(spinner.VISIBLE);
  98 + }
  99 + });
  100 + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  101 + @Override
  102 + public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  103 + Log.i(TAG, String.format("Pos: %d, ID %d", position, id));
  104 + map_id = id;
  105 + Map.setVisibility(Map.VISIBLE);
  106 + setPointButton.setVisibility(setPointButton.VISIBLE);
  107 + }
  108 +
  109 + @Override
  110 + public void onNothingSelected(AdapterView<?> parent) {
  111 + Map.setVisibility(Map.INVISIBLE);
  112 + setPointButton.setVisibility(setPointButton.INVISIBLE);
79 113 }
80 114 });
81 115 }
... ... @@ -88,8 +122,42 @@ public class Calibration extends BaseActivity {
88 122 return true;
89 123 }
90 124  
91   - public void sendCalibrateRequest(View v) {
92   - Snackbar.make(v, "Coucou, X :" + height + " Y : " + width, Snackbar.LENGTH_LONG)
93   - .setAction("Action", null).show();
  125 + public void sendCalibrateRequest(final View v) {
  126 + if(MapMarker.getVisibility()==MapMarker.INVISIBLE) {
  127 + Snackbar.make(v, "You need to set a point before measurement.", Snackbar.LENGTH_LONG)
  128 + .setAction("Action", null).show();
  129 + } else {
  130 + JsonRequest request = new JsonObjectRequest(Request.Method.GET,getURLSolver().calibrateURL(width,height,map_id),null,
  131 + new Response.Listener<JSONObject>() {
  132 + @Override
  133 + public void onResponse(JSONObject response) {
  134 + try {
  135 + String x = response.getString("calibration");
  136 + if(x.equals("try_again")){
  137 + Snackbar.make(v, "An error with the server occurred, please try again", Snackbar.LENGTH_LONG)
  138 + .setAction("Action", null).show();
  139 + }else{
  140 + Snackbar.make(v, "Calibration completed", Snackbar.LENGTH_LONG)
  141 + .setAction("Action", null).show();
  142 + }
  143 + } catch (JSONException e) {
  144 + Log.e(TAG, e.toString());
  145 + Snackbar.make(v, "Could not parse the response !", Snackbar.LENGTH_LONG)
  146 + .setAction("Action", null).show();
  147 + }
  148 + }
  149 + },
  150 + new Response.ErrorListener() {
  151 + @Override
  152 + public void onErrorResponse(VolleyError error) {
  153 + Snackbar.make(v, "Error: " + error.getMessage(), Snackbar.LENGTH_LONG)
  154 + .setAction("Action", null).show();
  155 + Log.i(TAG, "Error during calibration request");
  156 + }
  157 + });
  158 + queue.add(request);
  159 + Log.i("TAG", String.format("Request : %s", request.getUrl()));
  160 + }
94 161 }
  162 +
95 163 }
... ...
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/network/URLSolver.java
... ... @@ -40,7 +40,7 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList
40 40 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
41 41 switch (key) {
42 42 case HOSTNAME_KEY: updateHostname(sharedPreferences); break;
43   - case PORT_KEY: updateHostname(sharedPreferences); break;
  43 + case PORT_KEY: updatePort(sharedPreferences); break;
44 44 }
45 45 }
46 46  
... ... @@ -57,10 +57,13 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList
57 57 return b.build().toString();
58 58 }
59 59  
60   - public String calibrateURL() {
  60 + public String calibrateURL(float x, float y, long map_id) {
61 61 Uri.Builder b = baseURL();
62 62 b.appendPath("calibration")
63   - .appendPath("request");
  63 + .appendPath("request")
  64 + .appendQueryParameter("x", String.format("%f",x))
  65 + .appendQueryParameter("y", String.format("%f",y))
  66 + .appendQueryParameter("map_id", String.format("%d", map_id));
64 67 return b.build().toString();
65 68 }
66 69  
... ...
app/src/main/res/layout/activity_calibration.xml
... ... @@ -4,7 +4,6 @@
4 4 xmlns:tools="http://schemas.android.com/tools"
5 5 android:layout_width="match_parent"
6 6 android:layout_height="match_parent"
7   - android:background="#000000"
8 7 tools:context="fr.utbm.lo53.p2017.positionningapp.Calibration">
9 8  
10 9 <android.support.design.widget.AppBarLayout
... ...
app/src/main/res/layout/content_calibration.xml
... ... @@ -29,22 +29,19 @@
29 29  
30 30 <ImageView
31 31 android:id="@+id/MapMarker"
32   - android:layout_width="0dp"
  32 + android:layout_width="50dp"
33 33 android:layout_height="50dp"
34 34 android:layout_marginEnd="8dp"
35 35 android:layout_marginLeft="8dp"
36 36 android:layout_marginRight="8dp"
37 37 android:layout_marginStart="8dp"
38   - android:adjustViewBounds="false"
  38 + android:adjustViewBounds="true"
39 39 android:cropToPadding="false"
40   - app:layout_constraintHorizontal_bias="0.15"
  40 + app:layout_constraintBottom_toBottomOf="@+id/Map"
41 41 app:layout_constraintLeft_toLeftOf="parent"
42 42 app:layout_constraintRight_toRightOf="parent"
43   - app:srcCompat="@drawable/map_marker"
44 43 app:layout_constraintTop_toTopOf="@+id/Map"
45   - android:layout_marginTop="8dp"
46   - app:layout_constraintBottom_toBottomOf="@+id/Map"
47   - android:layout_marginBottom="8dp" />
  44 + app:srcCompat="@drawable/map_marker" />
48 45  
49 46 <Button
50 47 android:id="@+id/setPointButton"
... ... @@ -93,4 +90,15 @@
93 90 android:layout_marginBottom="8dp"
94 91 app:layout_constraintBottom_toTopOf="@+id/cancelButton" />
95 92  
  93 + <Spinner
  94 + android:id="@+id/spinner"
  95 + android:layout_width="368dp"
  96 + android:layout_height="wrap_content"
  97 + android:layout_marginBottom="8dp"
  98 + app:layout_constraintBottom_toTopOf="@+id/measureButton"
  99 + android:layout_marginRight="8dp"
  100 + app:layout_constraintRight_toRightOf="parent"
  101 + android:layout_marginLeft="8dp"
  102 + app:layout_constraintLeft_toLeftOf="parent" />
  103 +
96 104 </android.support.constraint.ConstraintLayout>
... ...
app/src/main/res/layout/content_positioning.xml
... ... @@ -41,7 +41,7 @@
41 41 app:layout_constraintLeft_toLeftOf="parent"
42 42 app:layout_constraintRight_toRightOf="parent"
43 43 app:layout_constraintTop_toTopOf="@+id/Map"
44   - app:layout_constraintVertical_bias="0.03"
  44 + app:layout_constraintVertical_bias="0.0"
45 45 app:srcCompat="@drawable/map_marker" />
46 46  
47 47 <TextView
... ...
app/src/main/res/values/strings.xml
... ... @@ -64,6 +64,12 @@
64 64 <item>3</item>
65 65 </string-array>
66 66  
  67 + <string-array name="map_choices">
  68 + <item>0</item>
  69 + <item>1</item>
  70 + <item>2</item>
  71 + </string-array>
  72 +
67 73 <string-array name="multi_select_list_preference_default_value" />
68 74  
69 75 <string name="pref_title_system_sync_settings">System sync settings</string>
... ...