Commit c006882ea496f23fbcdcd24513556a9c237f4e5f

Authored by florian staine
1 parent 7a4f791c

Connection to server

Add URL helper & connect via setting hostname / port
@@ -53,3 +53,5 @@ google-services.json @@ -53,3 +53,5 @@ google-services.json
53 freeline.py 53 freeline.py
54 freeline/ 54 freeline/
55 freeline_project_description.json 55 freeline_project_description.json
  56 +
  57 +\.idea/
.idea/compiler.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<project version="4">  
3 - <component name="CompilerConfiguration">  
4 - <resourceExtensions />  
5 - <wildcardResourcePatterns>  
6 - <entry name="!?*.java" />  
7 - <entry name="!?*.form" />  
8 - <entry name="!?*.class" />  
9 - <entry name="!?*.groovy" />  
10 - <entry name="!?*.scala" />  
11 - <entry name="!?*.flex" />  
12 - <entry name="!?*.kt" />  
13 - <entry name="!?*.clj" />  
14 - <entry name="!?*.aj" />  
15 - </wildcardResourcePatterns>  
16 - <annotationProcessing>  
17 - <profile default="true" name="Default" enabled="false">  
18 - <processorPath useClasspath="true" />  
19 - </profile>  
20 - </annotationProcessing>  
21 - </component>  
22 -</project>  
23 \ No newline at end of file 0 \ No newline at end of file
.idea/copyright/profiles_settings.xml deleted
1 -<component name="CopyrightManager">  
2 - <settings default="" />  
3 -</component>  
4 \ No newline at end of file 0 \ No newline at end of file
.idea/misc.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<project version="4">  
3 - <component name="EntryPointsManager">  
4 - <entry_points version="2.0" />  
5 - </component>  
6 - <component name="NullableNotNullManager">  
7 - <option name="myDefaultNullable" value="android.support.annotation.Nullable" />  
8 - <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />  
9 - <option name="myNullables">  
10 - <value>  
11 - <list size="4">  
12 - <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />  
13 - <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />  
14 - <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />  
15 - <item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />  
16 - </list>  
17 - </value>  
18 - </option>  
19 - <option name="myNotNulls">  
20 - <value>  
21 - <list size="4">  
22 - <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />  
23 - <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />  
24 - <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />  
25 - <item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />  
26 - </list>  
27 - </value>  
28 - </option>  
29 - </component>  
30 - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">  
31 - <output url="file://$PROJECT_DIR$/build/classes" />  
32 - </component>  
33 - <component name="ProjectType">  
34 - <option name="id" value="Android" />  
35 - </component>  
36 -</project>  
37 \ No newline at end of file 0 \ No newline at end of file
.idea/modules.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<project version="4">  
3 - <component name="ProjectModuleManager">  
4 - <modules>  
5 - <module fileurl="file://$PROJECT_DIR$/PositionningApp.iml" filepath="$PROJECT_DIR$/PositionningApp.iml" />  
6 - <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />  
7 - </modules>  
8 - </component>  
9 -</project>  
10 \ No newline at end of file 0 \ No newline at end of file
.idea/runConfigurations.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<project version="4">  
3 - <component name="RunConfigurationProducerService">  
4 - <option name="ignoredProducers">  
5 - <set>  
6 - <option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />  
7 - <option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />  
8 - <option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />  
9 - </set>  
10 - </option>  
11 - </component>  
12 -</project>  
13 \ No newline at end of file 0 \ No newline at end of file
.idea/vcs.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<project version="4">  
3 - <component name="VcsDirectoryMappings">  
4 - <mapping directory="$PROJECT_DIR$" vcs="Git" />  
5 - <mapping directory="$PROJECT_DIR$" vcs="Git" />  
6 - </component>  
7 -</project>  
8 \ No newline at end of file 0 \ No newline at end of file
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/BaseActivity.java
1 package fr.utbm.lo53.p2017.positionningapp; 1 package fr.utbm.lo53.p2017.positionningapp;
2 2
3 import android.content.Intent; 3 import android.content.Intent;
  4 +import android.os.Bundle;
  5 +import android.preference.PreferenceManager;
  6 +import android.support.annotation.Nullable;
4 import android.support.v7.app.AppCompatActivity; 7 import android.support.v7.app.AppCompatActivity;
5 import android.view.Menu; 8 import android.view.Menu;
6 import android.view.MenuItem; 9 import android.view.MenuItem;
7 10
8 -public class BaseActivity extends AppCompatActivity { 11 +import fr.utbm.lo53.p2017.positionningapp.network.URLSolver;
  12 +
  13 +public abstract class BaseActivity extends AppCompatActivity {
  14 +
  15 + private URLSolver urlSolver;
  16 +
  17 + @Override
  18 + protected void onCreate(@Nullable Bundle savedInstanceState) {
  19 + super.onCreate(savedInstanceState);
  20 + String defaultHostname = getString(R.string.pref_default_hostname);
  21 + String defaultPort = getString(R.string.pref_default_port);
  22 + urlSolver = new URLSolver(PreferenceManager.getDefaultSharedPreferences(this), defaultHostname, defaultPort);
  23 + }
  24 +
  25 + @Override
  26 + protected void onResume() {
  27 + super.onResume();
  28 + PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(getURLSolver());
  29 + }
  30 +
  31 + @Override
  32 + protected void onPause() {
  33 + super.onPause();
  34 + PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(getURLSolver());
  35 + }
9 36
10 @Override 37 @Override
11 public boolean onOptionsItemSelected(MenuItem item) { 38 public boolean onOptionsItemSelected(MenuItem item) {
@@ -40,4 +67,8 @@ public class BaseActivity extends AppCompatActivity { @@ -40,4 +67,8 @@ public class BaseActivity extends AppCompatActivity {
40 return true; 67 return true;
41 } 68 }
42 69
  70 + public URLSolver getURLSolver() {
  71 + return urlSolver;
  72 + }
  73 +
43 } 74 }
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/PositioningActivity.java
@@ -18,9 +18,11 @@ import com.android.volley.Request; @@ -18,9 +18,11 @@ import com.android.volley.Request;
18 import com.android.volley.RequestQueue; 18 import com.android.volley.RequestQueue;
19 import com.android.volley.Response; 19 import com.android.volley.Response;
20 import com.android.volley.VolleyError; 20 import com.android.volley.VolleyError;
21 -import com.android.volley.toolbox.StringRequest; 21 +import com.android.volley.toolbox.JsonObjectRequest;
22 import com.android.volley.toolbox.Volley; 22 import com.android.volley.toolbox.Volley;
23 23
  24 +import org.json.JSONObject;
  25 +
24 public class PositioningActivity extends BaseActivity { 26 public class PositioningActivity extends BaseActivity {
25 27
26 private static final String TAG = "PositioningActivity"; 28 private static final String TAG = "PositioningActivity";
@@ -33,31 +35,35 @@ public class PositioningActivity extends BaseActivity { @@ -33,31 +35,35 @@ public class PositioningActivity extends BaseActivity {
33 // Instantiate the RequestQueue. 35 // Instantiate the RequestQueue.
34 private RequestQueue queue; 36 private RequestQueue queue;
35 37
36 - private String url = "http://www.google.com"; 38 + private boolean isLocating = false;
37 39
38 // Request a string response from the provided URL. 40 // Request a string response from the provided URL.
39 - private StringRequest stringRequest = new StringRequest(Request.Method.GET, url,  
40 - new Response.Listener<String>() {  
41 - @Override  
42 - public void onResponse(String response) {  
43 - // Display the first 500 characters of the response string.  
44 - textView.setText("Response is: "+ response.substring(0,500));  
45 - }  
46 - }, new Response.ErrorListener() {  
47 - @Override  
48 - public void onErrorResponse(VolleyError error) {  
49 - Log.d(TAG, error.getCause().toString());  
50 - textView.clearComposingText();  
51 - }  
52 - }); 41 + private JsonObjectRequest stringRequest;
53 42
54 private final Handler handler = new Handler(); 43 private final Handler handler = new Handler();
55 private final Runnable runnable = new Runnable() { 44 private final Runnable runnable = new Runnable() {
56 @Override 45 @Override
57 public void run() { 46 public void run() {
58 - // Add the request to the RequestQueue. 47 + // Create request
  48 + stringRequest = new JsonObjectRequest(Request.Method.GET,
  49 + getURLSolver().locateURL(),
  50 + null,
  51 + new Response.Listener<JSONObject>() {
  52 + @Override
  53 + public void onResponse(JSONObject response) {
  54 + textView.setText("Response is: "+ response.toString().substring(0,500));
  55 + }
  56 + }, new Response.ErrorListener() {
  57 + @Override
  58 + public void onErrorResponse(VolleyError error) {
  59 + textView.setText("Error: " + error.getMessage());
  60 + Log.i(TAG, "Error during location request");
  61 + }
  62 + });
  63 + Log.v(TAG, "" + stringRequest);
59 queue.add(stringRequest); 64 queue.add(stringRequest);
60 - handler.postDelayed(this, 1000); 65 +
  66 + handler.postDelayed(runnable, 1000);
61 } 67 }
62 }; 68 };
63 69
@@ -69,7 +75,7 @@ public class PositioningActivity extends BaseActivity { @@ -69,7 +75,7 @@ public class PositioningActivity extends BaseActivity {
69 setSupportActionBar(toolbar); 75 setSupportActionBar(toolbar);
70 76
71 start_layout = (ConstraintLayout) findViewById(R.id.start_layout); 77 start_layout = (ConstraintLayout) findViewById(R.id.start_layout);
72 - hideAnimation = createFadeOutAndHideAnimation(); 78 + initFadeOutAndHideAnimation();
73 79
74 textView = (TextView) findViewById(R.id.ID_yolo); 80 textView = (TextView) findViewById(R.id.ID_yolo);
75 81
@@ -77,9 +83,17 @@ public class PositioningActivity extends BaseActivity { @@ -77,9 +83,17 @@ public class PositioningActivity extends BaseActivity {
77 } 83 }
78 84
79 @Override 85 @Override
80 - protected void onDestroy() { 86 + protected void onPause() {
  87 + super.onPause();
81 handler.removeCallbacks(runnable); 88 handler.removeCallbacks(runnable);
82 - super.onDestroy(); 89 + }
  90 +
  91 + @Override
  92 + protected void onResume() {
  93 + super.onResume();
  94 + if (isLocating) {
  95 + handler.post(runnable);
  96 + }
83 } 97 }
84 98
85 @Override 99 @Override
@@ -91,11 +105,17 @@ public class PositioningActivity extends BaseActivity { @@ -91,11 +105,17 @@ public class PositioningActivity extends BaseActivity {
91 } 105 }
92 106
93 public void startLocating(View v) { 107 public void startLocating(View v) {
  108 + if (isLocating) {
  109 + return;
  110 + }
  111 +
  112 + isLocating = true;
94 start_layout.startAnimation(hideAnimation); 113 start_layout.startAnimation(hideAnimation);
  114 +
95 handler.post(runnable); 115 handler.post(runnable);
96 } 116 }
97 117
98 - private Animation createFadeOutAndHideAnimation() { 118 + private void initFadeOutAndHideAnimation() {
99 ImageView hiding_image = (ImageView) findViewById(R.id.hiding_image); 119 ImageView hiding_image = (ImageView) findViewById(R.id.hiding_image);
100 final float initial_alpha = hiding_image.getImageAlpha() / 255; 120 final float initial_alpha = hiding_image.getImageAlpha() / 255;
101 Animation animation = new AlphaAnimation(initial_alpha, 0); 121 Animation animation = new AlphaAnimation(initial_alpha, 0);
@@ -109,6 +129,6 @@ public class PositioningActivity extends BaseActivity { @@ -109,6 +129,6 @@ public class PositioningActivity extends BaseActivity {
109 public void onAnimationRepeat(Animation animation) {} 129 public void onAnimationRepeat(Animation animation) {}
110 public void onAnimationStart(Animation animation) {} 130 public void onAnimationStart(Animation animation) {}
111 }); 131 });
112 - return animation; 132 + hideAnimation = animation;
113 } 133 }
114 } 134 }
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/network/URLSolver.java 0 โ†’ 100644
  1 +package fr.utbm.lo53.p2017.positionningapp.network;
  2 +
  3 +
  4 +import android.content.SharedPreferences;
  5 +import android.net.Uri;
  6 +import android.util.Log;
  7 +
  8 +public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeListener {
  9 +
  10 + private static final String TAG = "URLSolver;";
  11 +
  12 + private static final String HOSTNAME_KEY = "hostname";
  13 + private static final String PORT_KEY = "port";
  14 +
  15 + private final String defaultHostname;
  16 + private final String defaultPort;
  17 +
  18 + private String hostname;
  19 + private int port;
  20 +
  21 + public URLSolver(SharedPreferences sharedPref, String defaultHostname, String defaultPort) {
  22 + this.defaultHostname = defaultHostname;
  23 + this.defaultPort = defaultPort;
  24 +
  25 + updateHostname(sharedPref);
  26 + updatePort(sharedPref);
  27 + }
  28 +
  29 + private void updateHostname(SharedPreferences sharedPref) {
  30 + hostname = sharedPref.getString(HOSTNAME_KEY, defaultHostname);
  31 + Log.i(TAG, "Update host: " + hostname);
  32 + }
  33 +
  34 + private void updatePort(SharedPreferences sharedPref) {
  35 + port = Integer.valueOf(sharedPref.getString(PORT_KEY, defaultPort));
  36 + Log.i(TAG, "Update port: " + port);
  37 + }
  38 +
  39 + @Override
  40 + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  41 + switch (key) {
  42 + case HOSTNAME_KEY: updateHostname(sharedPreferences); break;
  43 + case PORT_KEY: updateHostname(sharedPreferences); break;
  44 + }
  45 + }
  46 +
  47 + private Uri.Builder baseURL() {
  48 + Uri.Builder builder = new Uri.Builder();
  49 + return builder.scheme("http").encodedAuthority(hostname + ":" + port);
  50 + }
  51 +
  52 + public String locateURL(/* TODO: mac ... */) {
  53 + Uri.Builder b = baseURL();
  54 + b.appendPath("positioning")
  55 + .appendPath("request")
  56 + .appendQueryParameter("mac", "TODO"); // TODO
  57 + return b.build().toString();
  58 + }
  59 +
  60 + public String calibrateURL() {
  61 + Uri.Builder b = baseURL();
  62 + b.appendPath("calibration")
  63 + .appendPath("request");
  64 + return b.build().toString();
  65 + }
  66 +}
app/src/main/res/layout/content_calibration.xml
@@ -12,9 +12,12 @@ @@ -12,9 +12,12 @@
12 android:id="@+id/Map" 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_marginEnd="8dp"
15 android:layout_marginLeft="8dp" 16 android:layout_marginLeft="8dp"
16 android:layout_marginRight="8dp" 17 android:layout_marginRight="8dp"
  18 + android:layout_marginStart="8dp"
17 android:layout_marginTop="50dp" 19 android:layout_marginTop="50dp"
  20 + android:adjustViewBounds="true"
18 android:paddingLeft="0dp" 21 android:paddingLeft="0dp"
19 android:paddingTop="0dp" 22 android:paddingTop="0dp"
20 android:scaleType="fitXY" 23 android:scaleType="fitXY"
@@ -22,9 +25,7 @@ @@ -22,9 +25,7 @@
22 app:layout_constraintLeft_toLeftOf="parent" 25 app:layout_constraintLeft_toLeftOf="parent"
23 app:layout_constraintRight_toRightOf="parent" 26 app:layout_constraintRight_toRightOf="parent"
24 app:layout_constraintTop_toTopOf="parent" 27 app:layout_constraintTop_toTopOf="parent"
25 - app:srcCompat="@drawable/map"  
26 - android:layout_marginStart="8dp"  
27 - android:layout_marginEnd="8dp" /> 28 + app:srcCompat="@drawable/map" />
28 29
29 <ImageView 30 <ImageView
30 android:id="@+id/MapMarker" 31 android:id="@+id/MapMarker"
@@ -45,7 +46,7 @@ @@ -45,7 +46,7 @@
45 <Button 46 <Button
46 android:id="@+id/setPointButton" 47 android:id="@+id/setPointButton"
47 style="@style/Widget.AppCompat.Button" 48 style="@style/Widget.AppCompat.Button"
48 - android:layout_width="match_parent" 49 + android:layout_width="0dp"
49 android:layout_height="wrap_content" 50 android:layout_height="wrap_content"
50 android:layout_marginEnd="8dp" 51 android:layout_marginEnd="8dp"
51 android:layout_marginLeft="8dp" 52 android:layout_marginLeft="8dp"
@@ -60,7 +61,7 @@ @@ -60,7 +61,7 @@
60 61
61 <Button 62 <Button
62 android:id="@+id/cancelButton" 63 android:id="@+id/cancelButton"
63 - android:layout_width="match_parent" 64 + android:layout_width="0dp"
64 android:layout_height="wrap_content" 65 android:layout_height="wrap_content"
65 android:layout_marginEnd="8dp" 66 android:layout_marginEnd="8dp"
66 android:layout_marginLeft="8dp" 67 android:layout_marginLeft="8dp"
@@ -74,7 +75,7 @@ @@ -74,7 +75,7 @@
74 75
75 <Button 76 <Button
76 android:id="@+id/measureButton" 77 android:id="@+id/measureButton"
77 - android:layout_width="match_parent" 78 + android:layout_width="0dp"
78 android:layout_height="wrap_content" 79 android:layout_height="wrap_content"
79 android:layout_marginEnd="8dp" 80 android:layout_marginEnd="8dp"
80 android:layout_marginLeft="8dp" 81 android:layout_marginLeft="8dp"
app/src/main/res/values/strings.xml
@@ -84,5 +84,5 @@ @@ -84,5 +84,5 @@
84 <string name="pref_default_hostname">127.0.0.1</string> 84 <string name="pref_default_hostname">127.0.0.1</string>
85 85
86 <string name="pref_title_port">Port</string> 86 <string name="pref_title_port">Port</string>
87 - <string name="default_port">8080</string> 87 + <string name="pref_default_port">8080</string>
88 </resources> 88 </resources>
app/src/main/res/xml/pref_general.xml
@@ -35,14 +35,17 @@ @@ -35,14 +35,17 @@
35 android:selectAllOnFocus="true" 35 android:selectAllOnFocus="true"
36 android:singleLine="true" 36 android:singleLine="true"
37 android:title="@string/pref_title_hostname" /> 37 android:title="@string/pref_title_hostname" />
  38 +
38 <EditTextPreference 39 <EditTextPreference
39 android:layout_width="wrap_content" 40 android:layout_width="wrap_content"
40 android:layout_height="wrap_content" 41 android:layout_height="wrap_content"
41 - android:defaultValue="@string/default_port" 42 + android:defaultValue="@string/pref_default_port"
42 android:key="port" 43 android:key="port"
43 android:selectAllOnFocus="true" 44 android:selectAllOnFocus="true"
44 android:singleLine="true" 45 android:singleLine="true"
45 - android:title="@string/pref_title_port" /> 46 + android:title="@string/pref_title_port"
  47 + android:numeric="integer"
  48 + android:maxLength="5" />
46 49
47 50
48 </PreferenceScreen> 51 </PreferenceScreen>