PositioningActivity.java
8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package fr.utbm.lo53.p2017.positionningapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class PositioningActivity extends BaseActivity {
private static final String TAG = "PositioningActivity";
private ConstraintLayout start_layout;
private ImageView mapView;
private ImageView imageLoading;
private Animation hideAnimation;
// Instantiate the RequestQueue.
private RequestQueue queue;
private boolean isLocating = false;
private Integer mapId = null;
private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
@Override
public void run() {
// Create request
JsonObjectRequest locateRequest = new JsonObjectRequest(Request.Method.GET,
getURLSolver().locateURL(getMacAddress()),
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
float x = (float) response.getDouble("x");
float y = (float) response.getDouble("y");
int mapId = response.getInt("map");
imageLoading.setVisibility(View.GONE);
if (hasCorrectMap(mapId)) {
putPoint(x, y);
} else {
getMap(mapId);
}
} catch (JSONException e) {
Log.e(TAG, "Could not parse the response: " + e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Remove random position
Random r = new Random();
putPoint(r.nextFloat(), r.nextFloat());
Snackbar.make(mapView, "Error during positioning request", Snackbar.LENGTH_LONG).show();
}
});
Log.v(TAG, "" + locateRequest);
queue.add(locateRequest);
handler.postDelayed(runnable, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_positioning);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
start_layout = (ConstraintLayout) findViewById(R.id.start_layout);
initFadeOutAndHideAnimation();
mapView = (ImageView) findViewById(R.id.map);
imageLoading = (ImageView) findViewById(R.id.img_loading);
queue = Volley.newRequestQueue(this);
}
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(runnable);
}
@Override
protected void onResume() {
super.onResume();
if (isLocating) {
handler.post(runnable);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.open_calibration_activity);
item.setVisible(true);
return true;
}
public void startLocating(View v) {
Log.i(TAG, v.toString());
if (isLocating) {
return;
}
isLocating = true;
final Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
rotate.setDuration(1500);
imageLoading.setVisibility(View.VISIBLE);
imageLoading.startAnimation(rotate);
start_layout.startAnimation(hideAnimation);
handler.post(runnable);
}
private void initFadeOutAndHideAnimation() {
ImageView hiding_image = (ImageView) findViewById(R.id.hiding_image);
final float initial_alpha = hiding_image.getImageAlpha() / 255;
Animation animation = new AlphaAnimation(initial_alpha, 0);
animation.setInterpolator(new AccelerateInterpolator());
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
start_layout.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationStart(Animation animation) {}
});
hideAnimation = animation;
}
private void putPoint(float x, float y) {
ImageView mapMarker = (ImageView) findViewById(R.id.map_marker);
float x_on_map = (float) mapView.getWidth() * x; // in px
float y_on_map = (float) mapView.getHeight() * y;
mapMarker.setX(mapView.getX() + x_on_map - mapMarker.getWidth()/2);
mapMarker.setY(mapView.getY() + y_on_map - mapMarker.getHeight());
mapMarker.setVisibility(View.VISIBLE);
mapView.setVisibility(mapView.VISIBLE);
}
private boolean hasCorrectMap(int mapId) {
return (this.mapId != null && this.mapId.equals(mapId));
}
private void getMap(int mapId) {
mapView.setVisibility(mapView.INVISIBLE);
ImageRequest mapRequest = new ImageRequest(
getURLSolver().mapBytesURL(mapId),
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mapView.setImageBitmap(bitmap);
mapView.setVisibility(mapView.VISIBLE);
Snackbar.make(mapView, "You need to set a point before measurement.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Snackbar.make(mapView, "Can't load map !", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
queue.add(mapRequest);
}
protected String getMacAddress() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Get MAC address prior to android 6.0
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
return wInfo.getMacAddress();
} else {
// Removed in Android 6.0 -> manually get MAC address
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
//res1.append(Integer.toHexString(b & 0xFF) + ":");
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (SocketException e) {
e.printStackTrace();
}
return "02:00:00:00:00:00";
}
}
}