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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
package com.mapswithme.maps;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import com.mapswithme.maps.base.BaseMwmFragment;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.statistics.PushwooshHelper;
public class MapFragment extends BaseMwmFragment
implements View.OnTouchListener,
SurfaceHolder.Callback
{
// Should correspond to android::MultiTouchAction from Framework.cpp
private static final int NATIVE_ACTION_UP = 0x01;
private static final int NATIVE_ACTION_DOWN = 0x02;
private static final int NATIVE_ACTION_MOVE = 0x03;
private static final int NATIVE_ACTION_CANCEL = 0x04;
// Should correspond to gui::EWidget from skin.hpp
private static final int WIDGET_RULER = 0x01;
private static final int WIDGET_COMPASS = 0x02;
private static final int WIDGET_COPYRIGHT = 0x04;
private static final int WIDGET_SCALE_LABEL = 0x08;
// Should correspond to dp::Anchor from drape_global.hpp
private static final int ANCHOR_CENTER = 0x00;
private static final int ANCHOR_LEFT = 0x01;
private static final int ANCHOR_RIGHT = (ANCHOR_LEFT << 1);
private static final int ANCHOR_TOP = (ANCHOR_RIGHT << 1);
private static final int ANCHOR_BOTTOM = (ANCHOR_TOP << 1);
private static final int ANCHOR_LEFT_TOP = (ANCHOR_LEFT | ANCHOR_TOP);
private static final int ANCHOR_RIGHT_TOP = (ANCHOR_RIGHT | ANCHOR_TOP);
private static final int ANCHOR_LEFT_BOTTOM = (ANCHOR_LEFT | ANCHOR_BOTTOM);
private static final int ANCHOR_RIGHT_BOTTOM = (ANCHOR_RIGHT | ANCHOR_BOTTOM);
// Should correspond to df::TouchEvent::INVALID_MASKED_POINTER from user_event_stream.cpp
private static final int INVALID_POINTER_MASK = 0xFF;
private static final int INVALID_TOUCH_ID = -1;
private int mHeight;
private int mWidth;
private boolean mRequireResize;
private boolean mContextCreated;
private static boolean sWasCopyrightDisplayed;
interface MapRenderingListener
{
void onRenderingInitialized();
void onRenderingRestored();
}
private void setupWidgets(int width, int height)
{
mHeight = height;
mWidth = width;
nativeCleanWidgets();
if (!sWasCopyrightDisplayed)
{
nativeSetupWidget(WIDGET_COPYRIGHT,
UiUtils.dimen(R.dimen.margin_ruler_left),
mHeight - UiUtils.dimen(R.dimen.margin_ruler_bottom),
ANCHOR_LEFT_BOTTOM);
sWasCopyrightDisplayed = true;
}
nativeSetupWidget(WIDGET_RULER,
UiUtils.dimen(R.dimen.margin_ruler_left),
mHeight - UiUtils.dimen(R.dimen.margin_ruler_bottom),
ANCHOR_LEFT_BOTTOM);
if (BuildConfig.DEBUG)
{
nativeSetupWidget(WIDGET_SCALE_LABEL,
UiUtils.dimen(R.dimen.margin_base),
UiUtils.dimen(R.dimen.margin_base),
ANCHOR_LEFT_TOP);
}
setupCompass(UiUtils.getCompassYOffset(getContext()), false);
}
void setupCompass(int offsetY, boolean forceRedraw)
{
nativeSetupWidget(WIDGET_COMPASS,
mWidth - UiUtils.dimen(R.dimen.margin_compass),
offsetY + UiUtils.dimen(R.dimen.margin_compass_top),
ANCHOR_CENTER);
if (forceRedraw && mContextCreated)
nativeApplyWidgets();
}
void setupRuler(int offsetX, int offsetY, boolean forceRedraw)
{
nativeSetupWidget(WIDGET_RULER,
UiUtils.dimen(R.dimen.margin_ruler_left) + offsetX,
mHeight - UiUtils.dimen(R.dimen.margin_ruler_bottom) + offsetY,
ANCHOR_LEFT_BOTTOM);
if (forceRedraw && mContextCreated)
nativeApplyWidgets();
}
private void onRenderingInitialized()
{
final Activity activity = getActivity();
if (isAdded() && activity instanceof MapRenderingListener)
((MapRenderingListener) activity).onRenderingInitialized();
}
private void onRenderingRestored()
{
final Activity activity = getActivity();
if (isAdded() && activity instanceof MapRenderingListener)
((MapRenderingListener) activity).onRenderingRestored();
}
private void reportUnsupported()
{
new AlertDialog.Builder(getActivity())
.setMessage(getString(R.string.unsupported_phone))
.setCancelable(false)
.setPositiveButton(getString(R.string.close), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dlg, int which)
{
getActivity().moveTaskToBack(true);
}
}).show();
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder)
{
final Surface surface = surfaceHolder.getSurface();
if (nativeIsEngineCreated())
{
if (!nativeAttachSurface(surface))
{
reportUnsupported();
return;
}
mContextCreated = true;
mRequireResize = true;
return;
}
mRequireResize = false;
final Rect rect = surfaceHolder.getSurfaceFrame();
setupWidgets(rect.width(), rect.height());
final DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
final float exactDensityDpi = metrics.densityDpi;
boolean firstStart = SplashActivity.isFirstStart();
if (firstStart)
PushwooshHelper.nativeProcessFirstLaunch();
if (!nativeCreateEngine(surface, (int) exactDensityDpi, firstStart))
{
reportUnsupported();
return;
}
mContextCreated = true;
onRenderingInitialized();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height)
{
if (!mContextCreated ||
(!mRequireResize && surfaceHolder.isCreating()))
return;
nativeSurfaceChanged(width, height);
mRequireResize = false;
setupWidgets(width, height);
nativeApplyWidgets();
onRenderingRestored();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder)
{
if (!mContextCreated)
return;
if (getActivity() == null || !getActivity().isChangingConfigurations())
destroyContext();
else
nativeDetachSurface(false);
}
void destroyContext()
{
if (!mContextCreated)
return;
nativeDetachSurface(true);
mContextCreated = false;
}
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
final SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.map_surfaceview);
surfaceView.getHolder().addCallback(this);
}
@Override
public boolean onTouch(View view, MotionEvent event)
{
final int count = event.getPointerCount();
if (count == 0)
return false;
int action = event.getActionMasked();
int pointerIndex = event.getActionIndex();
switch (action)
{
case MotionEvent.ACTION_POINTER_UP:
action = NATIVE_ACTION_UP;
break;
case MotionEvent.ACTION_UP:
action = NATIVE_ACTION_UP;
pointerIndex = 0;
break;
case MotionEvent.ACTION_POINTER_DOWN:
action = NATIVE_ACTION_DOWN;
break;
case MotionEvent.ACTION_DOWN:
action = NATIVE_ACTION_DOWN;
pointerIndex = 0;
break;
case MotionEvent.ACTION_MOVE:
action = NATIVE_ACTION_MOVE;
pointerIndex = INVALID_POINTER_MASK;
break;
case MotionEvent.ACTION_CANCEL:
action = NATIVE_ACTION_CANCEL;
break;
}
switch (count)
{
case 1:
nativeOnTouch(action, event.getPointerId(0), event.getX(), event.getY(), INVALID_TOUCH_ID, 0, 0, 0);
return true;
default:
nativeOnTouch(action,
event.getPointerId(0), event.getX(0), event.getY(0),
event.getPointerId(1), event.getX(1), event.getY(1), pointerIndex);
return true;
}
}
boolean isContextCreated()
{
return mContextCreated;
}
static native void nativeCompassUpdated(double magneticNorth, double trueNorth, boolean forceRedraw);
static native void nativeScalePlus();
static native void nativeScaleMinus();
static native boolean nativeShowMapForUrl(String url);
static native boolean nativeIsEngineCreated();
private static native boolean nativeCreateEngine(Surface surface, int density, boolean firstLaunch);
private static native boolean nativeAttachSurface(Surface surface);
private static native void nativeDetachSurface(boolean destroyContext);
private static native void nativeSurfaceChanged(int w, int h);
private static native void nativeOnTouch(int actionType, int id1, float x1, float y1, int id2, float x2, float y2, int maskedPointer);
private static native void nativeSetupWidget(int widget, float x, float y, int anchor);
private static native void nativeApplyWidgets();
private static native void nativeCleanWidgets();
}
|