Commit 41d92f29f85f6970ff621e0a63858793b6c28179
1 parent
afe23868
Add MAC address
Plus hide map and marker before positioning
Showing
4 changed files
with
55 additions
and
4 deletions
Show diff stats
app/src/main/AndroidManifest.xml
@@ -3,6 +3,8 @@ | @@ -3,6 +3,8 @@ | ||
3 | package="fr.utbm.lo53.p2017.positionningapp"> | 3 | package="fr.utbm.lo53.p2017.positionningapp"> |
4 | 4 | ||
5 | <uses-permission android:name="android.permission.INTERNET" /> | 5 | <uses-permission android:name="android.permission.INTERNET" /> |
6 | + <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | ||
7 | + | ||
6 | <application | 8 | <application |
7 | android:allowBackup="true" | 9 | android:allowBackup="true" |
8 | android:icon="@drawable/map_marker" | 10 | android:icon="@drawable/map_marker" |
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.content.Context; | ||
3 | import android.graphics.Bitmap; | 4 | import android.graphics.Bitmap; |
5 | +import android.net.wifi.WifiInfo; | ||
6 | +import android.net.wifi.WifiManager; | ||
7 | +import android.os.Build; | ||
4 | import android.os.Bundle; | 8 | import android.os.Bundle; |
5 | import android.os.Handler; | 9 | import android.os.Handler; |
6 | import android.support.constraint.ConstraintLayout; | 10 | import android.support.constraint.ConstraintLayout; |
@@ -26,6 +30,10 @@ import com.android.volley.toolbox.Volley; | @@ -26,6 +30,10 @@ import com.android.volley.toolbox.Volley; | ||
26 | import org.json.JSONException; | 30 | import org.json.JSONException; |
27 | import org.json.JSONObject; | 31 | import org.json.JSONObject; |
28 | 32 | ||
33 | +import java.net.NetworkInterface; | ||
34 | +import java.net.SocketException; | ||
35 | +import java.util.Collections; | ||
36 | +import java.util.List; | ||
29 | import java.util.Random; | 37 | import java.util.Random; |
30 | 38 | ||
31 | public class PositioningActivity extends BaseActivity { | 39 | public class PositioningActivity extends BaseActivity { |
@@ -51,7 +59,7 @@ public class PositioningActivity extends BaseActivity { | @@ -51,7 +59,7 @@ public class PositioningActivity extends BaseActivity { | ||
51 | public void run() { | 59 | public void run() { |
52 | // Create request | 60 | // Create request |
53 | JsonObjectRequest locateRequest = new JsonObjectRequest(Request.Method.GET, | 61 | JsonObjectRequest locateRequest = new JsonObjectRequest(Request.Method.GET, |
54 | - getURLSolver().locateURL(), | 62 | + getURLSolver().locateURL(getMacAddress()), |
55 | null, | 63 | null, |
56 | new Response.Listener<JSONObject>() { | 64 | new Response.Listener<JSONObject>() { |
57 | @Override | 65 | @Override |
@@ -128,6 +136,7 @@ public class PositioningActivity extends BaseActivity { | @@ -128,6 +136,7 @@ public class PositioningActivity extends BaseActivity { | ||
128 | } | 136 | } |
129 | 137 | ||
130 | public void startLocating(View v) { | 138 | public void startLocating(View v) { |
139 | + Log.i(TAG, v.toString()); | ||
131 | if (isLocating) { | 140 | if (isLocating) { |
132 | return; | 141 | return; |
133 | } | 142 | } |
@@ -162,6 +171,7 @@ public class PositioningActivity extends BaseActivity { | @@ -162,6 +171,7 @@ public class PositioningActivity extends BaseActivity { | ||
162 | mapMarker.setX(mapView.getX() + x_on_map - mapMarker.getWidth()/2); | 171 | mapMarker.setX(mapView.getX() + x_on_map - mapMarker.getWidth()/2); |
163 | mapMarker.setY(mapView.getY() + y_on_map - mapMarker.getHeight()); | 172 | mapMarker.setY(mapView.getY() + y_on_map - mapMarker.getHeight()); |
164 | mapMarker.setVisibility(View.VISIBLE); | 173 | mapMarker.setVisibility(View.VISIBLE); |
174 | + mapView.setVisibility(mapView.VISIBLE); | ||
165 | } | 175 | } |
166 | 176 | ||
167 | private boolean hasCorrectMap(int mapId) { | 177 | private boolean hasCorrectMap(int mapId) { |
@@ -175,6 +185,7 @@ public class PositioningActivity extends BaseActivity { | @@ -175,6 +185,7 @@ public class PositioningActivity extends BaseActivity { | ||
175 | @Override | 185 | @Override |
176 | public void onResponse(Bitmap bitmap) { | 186 | public void onResponse(Bitmap bitmap) { |
177 | mapView.setImageBitmap(bitmap); | 187 | mapView.setImageBitmap(bitmap); |
188 | + mapView.setVisibility(mapView.VISIBLE); | ||
178 | } | 189 | } |
179 | }, 0, 0, null, | 190 | }, 0, 0, null, |
180 | new Response.ErrorListener() { | 191 | new Response.ErrorListener() { |
@@ -185,4 +196,40 @@ public class PositioningActivity extends BaseActivity { | @@ -185,4 +196,40 @@ public class PositioningActivity extends BaseActivity { | ||
185 | }); | 196 | }); |
186 | queue.add(mapRequest); | 197 | queue.add(mapRequest); |
187 | } | 198 | } |
199 | + | ||
200 | + protected String getMacAddress() { | ||
201 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | ||
202 | + // Get MAC address prior to android 6.0 | ||
203 | + WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); | ||
204 | + WifiInfo wInfo = wifiManager.getConnectionInfo(); | ||
205 | + return wInfo.getMacAddress(); | ||
206 | + } else { | ||
207 | + // Removed in Android 6.0 -> manually get MAC address | ||
208 | + try { | ||
209 | + List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); | ||
210 | + for (NetworkInterface nif : all) { | ||
211 | + if (!nif.getName().equalsIgnoreCase("wlan0")) continue; | ||
212 | + | ||
213 | + byte[] macBytes = nif.getHardwareAddress(); | ||
214 | + if (macBytes == null) { | ||
215 | + return ""; | ||
216 | + } | ||
217 | + | ||
218 | + StringBuilder res1 = new StringBuilder(); | ||
219 | + for (byte b : macBytes) { | ||
220 | + //res1.append(Integer.toHexString(b & 0xFF) + ":"); | ||
221 | + res1.append(String.format("%02X:", b)); | ||
222 | + } | ||
223 | + | ||
224 | + if (res1.length() > 0) { | ||
225 | + res1.deleteCharAt(res1.length() - 1); | ||
226 | + } | ||
227 | + return res1.toString(); | ||
228 | + } | ||
229 | + } catch (SocketException e) { | ||
230 | + e.printStackTrace(); | ||
231 | + } | ||
232 | + return "02:00:00:00:00:00"; | ||
233 | + } | ||
234 | + } | ||
188 | } | 235 | } |
app/src/main/java/fr/utbm/lo53/p2017/positionningapp/network/URLSolver.java
@@ -49,11 +49,11 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList | @@ -49,11 +49,11 @@ public class URLSolver implements SharedPreferences.OnSharedPreferenceChangeList | ||
49 | return builder.scheme("http").encodedAuthority(hostname + ":" + port); | 49 | return builder.scheme("http").encodedAuthority(hostname + ":" + port); |
50 | } | 50 | } |
51 | 51 | ||
52 | - public String locateURL(/* TODO: mac ... */) { | 52 | + public String locateURL(String mac) { |
53 | Uri.Builder b = baseURL(); | 53 | Uri.Builder b = baseURL(); |
54 | b.appendPath("positioning") | 54 | b.appendPath("positioning") |
55 | .appendPath("request") | 55 | .appendPath("request") |
56 | - .appendQueryParameter("mac", "TODO"); // TODO | 56 | + .appendQueryParameter("mac", mac); |
57 | return b.build().toString(); | 57 | return b.build().toString(); |
58 | } | 58 | } |
59 | 59 |
app/src/main/res/layout/content_positioning.xml
@@ -19,10 +19,11 @@ | @@ -19,10 +19,11 @@ | ||
19 | android:layout_marginTop="8dp" | 19 | android:layout_marginTop="8dp" |
20 | android:adjustViewBounds="true" | 20 | android:adjustViewBounds="true" |
21 | android:scaleType="fitXY" | 21 | android:scaleType="fitXY" |
22 | + android:visibility="invisible" | ||
22 | app:layout_constraintLeft_toLeftOf="parent" | 23 | app:layout_constraintLeft_toLeftOf="parent" |
23 | app:layout_constraintRight_toRightOf="parent" | 24 | app:layout_constraintRight_toRightOf="parent" |
24 | app:layout_constraintTop_toTopOf="parent" | 25 | app:layout_constraintTop_toTopOf="parent" |
25 | - app:srcCompat="@drawable/map2" /> | 26 | + app:srcCompat="@drawable/map" /> |
26 | 27 | ||
27 | <ImageView | 28 | <ImageView |
28 | android:id="@+id/map_marker" | 29 | android:id="@+id/map_marker" |
@@ -36,6 +37,7 @@ | @@ -36,6 +37,7 @@ | ||
36 | android:layout_marginTop="0dp" | 37 | android:layout_marginTop="0dp" |
37 | android:adjustViewBounds="false" | 38 | android:adjustViewBounds="false" |
38 | android:cropToPadding="false" | 39 | android:cropToPadding="false" |
40 | + android:visibility="invisible" | ||
39 | app:layout_constraintBottom_toBottomOf="@+id/Map" | 41 | app:layout_constraintBottom_toBottomOf="@+id/Map" |
40 | app:layout_constraintHorizontal_bias="0.0" | 42 | app:layout_constraintHorizontal_bias="0.0" |
41 | app:layout_constraintLeft_toLeftOf="parent" | 43 | app:layout_constraintLeft_toLeftOf="parent" |