Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MapFragment.java « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40c188be96fc38c314d61f18357c7040fd426e44 (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.mapswithme.maps;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.maps.location.LocationHelper;
import com.mapswithme.util.Config;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

public class MapFragment extends BaseMwmFragment
                      implements View.OnTouchListener,
                                 SurfaceHolder.Callback
{
  public static final String ARG_LAUNCH_BY_DEEP_LINK = "launch_by_deep_link";
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private static final String TAG = MapFragment.class.getSimpleName();

  // 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_FPS_LABEL = 0x08;
  private static final int WIDGET_WATERMARK = 0x10;

  // 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 boolean mLaunchByDeepLink;
  private static boolean sWasCopyrightDisplayed;
  @Nullable
  private String mUiThemeOnPause;
  @NonNull
  private SurfaceView mSurfaceView;

  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;
    }

    setupRuler(0, false);
    setupWatermark(0, false);

    nativeSetupWidget(WIDGET_SCALE_FPS_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)
  {
    int navPadding = UiUtils.dimen(R.dimen.nav_frame_padding);
    int marginX = UiUtils.dimen(R.dimen.margin_compass) + navPadding;
    int marginY = UiUtils.dimen(R.dimen.margin_compass_top) + navPadding;
    nativeSetupWidget(WIDGET_COMPASS,
                      mWidth - marginX,
                      offsetY + marginY,
                      ANCHOR_CENTER);
    if (forceRedraw && mContextCreated)
      nativeApplyWidgets();
  }

  void setupRuler(int offsetY, boolean forceRedraw)
  {
    nativeSetupWidget(WIDGET_RULER,
                      UiUtils.dimen(R.dimen.margin_ruler_left),
                      mHeight - UiUtils.dimen(R.dimen.margin_ruler_bottom) + offsetY,
                      ANCHOR_LEFT_BOTTOM);
    if (forceRedraw && mContextCreated)
      nativeApplyWidgets();
  }

  void setupWatermark(int offsetY, boolean forceRedraw)
  {
    nativeSetupWidget(WIDGET_WATERMARK,
                      mWidth - UiUtils.dimen(R.dimen.margin_watermark_right),
                      mHeight - UiUtils.dimen(R.dimen.margin_watermark_bottom) + offsetY,
                      ANCHOR_RIGHT_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)
  {
    if (isThemeChangingProcess())
    {
      LOGGER.d(TAG, "Activity is being recreated due theme changing, skip 'surfaceCreated' callback");
      return;
    }

    LOGGER.d(TAG, "surfaceCreated, mContextCreated = " + mContextCreated);
    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;

    final boolean firstStart = SplashActivity.isFirstStart();
    if (!nativeCreateEngine(surface, (int) exactDensityDpi, firstStart, mLaunchByDeepLink))
    {
      reportUnsupported();
      return;
    }

    if (firstStart)
    {
      UiThread.runLater(new Runnable()
      {
        @Override
        public void run()
        {
          LocationHelper.INSTANCE.onExitFromFirstRun();
        }
      });
    }

    mContextCreated = true;
    onRenderingInitialized();
  }

  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height)
  {
    if (isThemeChangingProcess())
    {
      LOGGER.d(TAG, "Activity is being recreated due theme changing, skip 'surfaceChanged' callback");
      return;
    }

    LOGGER.d(TAG, "surfaceChanged, mContextCreated = " + mContextCreated);
    if (!mContextCreated ||
        (!mRequireResize && surfaceHolder.isCreating()))
      return;

    nativeSurfaceChanged(width, height);

    mRequireResize = false;
    setupWidgets(width, height);
    nativeApplyWidgets();
    onRenderingRestored();
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder surfaceHolder)
  {
    LOGGER.d(TAG, "surfaceDestroyed");
    destroyContext();
  }

  void destroyContext()
  {
    LOGGER.d(TAG, "destroyContext, mContextCreated = " + mContextCreated +
                  ", isAdded = " + isAdded(), new Throwable());
    if (!mContextCreated || !isAdded())
      return;

    nativeDetachSurface(!getActivity().isChangingConfigurations());
    mContextCreated = false;
  }

  @Override
  public void onCreate(Bundle b)
  {
    super.onCreate(b);
    setRetainInstance(true);
    Bundle args = getArguments();
    if (args != null)
      mLaunchByDeepLink = args.getBoolean(ARG_LAUNCH_BY_DEEP_LINK);
  }

  @Override
  public void onStart()
  {
    super.onStart();
    LOGGER.d(TAG, "onStart");
  }

  private boolean isThemeChangingProcess()
  {
    return mUiThemeOnPause != null && !mUiThemeOnPause.equals(Config.getCurrentUiTheme());
  }

  @Override
  public void onPause()
  {
    mUiThemeOnPause = Config.getCurrentUiTheme();
    nativePauseSurfaceRendering();
    super.onPause();
  }

  @Override
  public void onResume()
  {
    super.onResume();
    nativeResumeSurfaceRendering();
  }

  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
  {
    View view = inflater.inflate(R.layout.fragment_map, container, false);
    mSurfaceView = (SurfaceView) view.findViewById(R.id.map_surfaceview);
    mSurfaceView.getHolder().addCallback(this);
    return view;
  }

  @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,
                                                   boolean isLaunchByDeepLink);
  private static native boolean nativeAttachSurface(Surface surface);
  private static native void nativeDetachSurface(boolean destroyContext);
  private static native void nativePauseSurfaceRendering();
  private static native void nativeResumeSurfaceRendering();
  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();
}