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

LocationHelper.java « location « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4260590856c370f9fdaff3b801c1abc064103283 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package com.mapswithme.maps.location;

import android.app.Activity;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.ads.Banner;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.util.Config;
import com.mapswithme.util.Listeners;
import com.mapswithme.util.LocationUtils;
import com.mapswithme.util.PermissionsUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.permissions.PermissionsResult;

public enum LocationHelper
{
  INSTANCE;

  // These constants should correspond to values defined in platform/location.hpp
  // Leave 0-value as no any error.
  public static final int ERROR_NOT_SUPPORTED = 1;
  public static final int ERROR_DENIED = 2;
  public static final int ERROR_GPS_OFF = 3;
  public static final int ERROR_UNKNOWN = 0;

  private static final long INTERVAL_FOLLOW_AND_ROTATE_MS = 3000;
  private static final long INTERVAL_FOLLOW_MS = 1000;
  private static final long INTERVAL_NOT_FOLLOW_MS = 3000;
  private static final long INTERVAL_NAVIGATION_VEHICLE_MS = 500;

  // TODO (trashkalmar): Correct value
  private static final long INTERVAL_NAVIGATION_BICYCLE_MS = 1000;
  private static final long INTERVAL_NAVIGATION_PEDESTRIAN_MS = 1000;

  @NonNull
  private final TransitionListener mOnTransition = new TransitionListener();

  @NonNull
  private final LocationListener mCoreLocationListener = new LocationListener()
  {
    @Override
    public void onLocationUpdated(Location location)
    {
      // If we are still in the first run mode, i.e. user is staying on the first run screens,
      // not on the map, we mustn't post location update to the core. Only this preserving allows us
      // to play nice zoom animation once a user will leave first screens and will see a map.
      if (mInFirstRun)
      {
        mLogger.d(TAG, "Location update is obtained and must be ignored, " +
                       "because the app is in a first run mode");
        return;
      }

      nativeLocationUpdated(location.getTime(),
                            location.getLatitude(),
                            location.getLongitude(),
                            location.getAccuracy(),
                            location.getAltitude(),
                            location.getSpeed(),
                            location.getBearing());

      if (mUiCallback != null)
        mUiCallback.onLocationUpdated(location);
    }

    @Override
    public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy)
    {
      if (mCompassData == null)
        mCompassData = new CompassData();

      mCompassData.update(magneticNorth, trueNorth);

      if (mUiCallback != null)
        mUiCallback.onCompassUpdated(mCompassData);
    }


    @Override
    public void onLocationError(int errorCode)
    {
      mLogger.d(TAG, "onLocationError errorCode = " + errorCode, new Throwable());

      nativeOnLocationError(errorCode);
      mLogger.d(TAG, "nativeOnLocationError errorCode = " + errorCode +
                ", current state = " + LocationState.nameOf(getMyPositionMode()));

      if (mUiCallback == null)
        return;

      mUiCallback.onLocationError();
    }

    @Override
    public String toString()
    {
      return "LocationHelper.mCoreLocationListener";
    }
  };

  private final static String TAG = LocationHelper.class.getSimpleName();
  private final Logger mLogger = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.LOCATION);
  @NonNull
  private final Listeners<LocationListener> mListeners = new Listeners<>();
  private Location mSavedLocation;
  private MapObject mMyPosition;
  private long mSavedLocationTime;
  @NonNull
  private final SensorHelper mSensorHelper = new SensorHelper();
  @Nullable
  private BaseLocationProvider mLocationProvider;
  @Nullable
  private UiCallback mUiCallback;
  private long mInterval;
  private CompassData mCompassData;
  private boolean mInFirstRun;

  @SuppressWarnings("FieldCanBeLocal")
  private final LocationState.ModeChangeListener mMyPositionModeListener =
      new LocationState.ModeChangeListener()
  {
    @Override
    public void onMyPositionModeChanged(int newMode)
    {
      notifyMyPositionModeChanged(newMode);
      mLogger.d(TAG, "onMyPositionModeChanged mode = " + LocationState.nameOf(newMode));

      if (mUiCallback == null)
      {
        mLogger.d(TAG, "UI is not ready to listen my position changes, i.e. it's not attached yet.");
        return;
      }

      switch (newMode)
      {
        case LocationState.NOT_FOLLOW_NO_POSITION:
          // In the first run mode, the NOT_FOLLOW_NO_POSITION state doesn't mean that location
          // is actually not found.
          if (mInFirstRun)
          {
            mLogger.i(TAG, "It's the first run, so this state should be skipped");
            return;
          }

          stop();
          if (LocationUtils.areLocationServicesTurnedOn())
            notifyLocationNotFound();
          break;
      }
    }
  };

  @UiThread
  public void initialize()
  {
    initProvider();
    LocationState.nativeSetListener(mMyPositionModeListener);
    MwmApplication.backgroundTracker().addListener(mOnTransition);
  }

  private void initProvider()
  {
    mLogger.d(TAG, "initProvider", new Throwable());
    final MwmApplication application = MwmApplication.get();
    final boolean containsGoogleServices = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(application) == ConnectionResult.SUCCESS;
    final boolean googleServicesTurnedInSettings = Config.useGoogleServices();
    if (containsGoogleServices && googleServicesTurnedInSettings)
    {
      mLogger.d(TAG, "Use fused provider.");
      mLocationProvider = new GoogleFusedLocationProvider(new FusedLocationFixChecker());
    }
    else
    {
      initNativeProvider();
    }
  }

  void initNativeProvider()
  {
    mLogger.d(TAG, "Use native provider");
    mLocationProvider = new AndroidNativeProvider(new DefaultLocationFixChecker());
  }

  public void onLocationUpdated(@NonNull Location location)
  {
    mSavedLocation = location;
    mMyPosition = null;
    mSavedLocationTime = System.currentTimeMillis();
  }

  /**
   * @return MapObject.MY_POSITION, null if location is not yet determined or "My position" button is switched off.
   */
  @Nullable
  public MapObject getMyPosition()
  {
    if (!LocationState.isTurnedOn())
    {
      mMyPosition = null;
      return null;
    }

    if (mSavedLocation == null)
      return null;

    if (mMyPosition == null)
      mMyPosition = new MapObject(MapObject.MY_POSITION, "", "", "", mSavedLocation.getLatitude(),
          mSavedLocation.getLongitude(), "", null, false, "");

    return mMyPosition;
  }

  /**
   * <p>Obtains last known saved location. It depends on "My position" button mode and is erased on "No follow, no position" one.
   * <p>If you need the location regardless of the button's state, use {@link #getLastKnownLocation()}.
   * @return {@code null} if no location is saved or "My position" button is in "No follow, no position" mode.
   */
  @Nullable
  public Location getSavedLocation() { return mSavedLocation; }

  public long getSavedLocationTime() { return mSavedLocationTime; }

  public void switchToNextMode()
  {
    mLogger.d(TAG, "switchToNextMode()");
    LocationState.nativeSwitchToNextMode();
  }

  /**
   * Indicates about whether a location provider is polling location updates right now or not.
   * @see BaseLocationProvider#isActive()
   */
  public boolean isActive()
  {
    return mLocationProvider != null && mLocationProvider.isActive();
  }

  void notifyCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy)
  {
    for (LocationListener listener : mListeners)
      listener.onCompassUpdated(time, magneticNorth, trueNorth, accuracy);
    mListeners.finishIterate();
  }

  void notifyLocationUpdated()
  {
    if (mSavedLocation == null)
    {
      mLogger.d(TAG, "No saved location - skip");
      return;
    }

    for (LocationListener listener : mListeners)
      listener.onLocationUpdated(mSavedLocation);
    mListeners.finishIterate();

    // TODO: consider to create callback mechanism to transfer 'ROUTE_IS_FINISHED' event from
    // the core to the platform code (https://jira.mail.ru/browse/MAPSME-3675),
    // because calling the native method 'nativeIsRouteFinished'
    // too often can result in poor UI performance.
    if (RoutingController.get().isNavigating() && Framework.nativeIsRouteFinished())
    {
      mLogger.d(TAG, "End point is reached");
      restart();
      RoutingController.get().cancel();
    }
  }

  private void notifyLocationUpdated(LocationListener listener)
  {
    mLogger.d(TAG, "notifyLocationUpdated(), listener: " + listener);

    if (mSavedLocation == null)
    {
      mLogger.d(TAG, "No saved location - skip");
      return;
    }

    listener.onLocationUpdated(mSavedLocation);
  }

  private void notifyLocationError(int errCode)
  {
    mLogger.d(TAG, "notifyLocationError(): " + errCode);

    for (LocationListener listener : mListeners)
      listener.onLocationError(errCode);
    mListeners.finishIterate();
  }

  private void notifyMyPositionModeChanged(int newMode)
  {
    mLogger.d(TAG, "notifyMyPositionModeChanged(): " + LocationState.nameOf(newMode) , new Throwable());

    if (mUiCallback != null)
      mUiCallback.onMyPositionModeChanged(newMode);
  }

  private void notifyLocationNotFound()
  {
    mLogger.d(TAG, "notifyLocationNotFound()");
    if (mUiCallback != null)
      mUiCallback.onLocationNotFound();
  }

  /**
   * Registers listener about location changes.
   *
   * @param listener    listener to register.
   * @param forceUpdate instantly notify given listener about available location, if any.
   */
  @UiThread
  public void addListener(@NonNull LocationListener listener, boolean forceUpdate)
  {
    mLogger.d(TAG, "addListener(): " + listener + ", forceUpdate: " + forceUpdate);
    mLogger.d(TAG, " - listener count was: " + mListeners.getSize());

    mListeners.register(listener);

    if (forceUpdate)
      notifyLocationUpdated(listener);
  }

  @UiThread
  /**
   * Removes given location listener.
   * @param listener listener to unregister.
   */
  public void removeListener(@NonNull LocationListener listener)
  {
    mLogger.d(TAG, "removeListener(), listener: " + listener);
    mLogger.d(TAG, " - listener count was: " + mListeners.getSize());
    mListeners.unregister(listener);
  }

  void startSensors()
  {
    mSensorHelper.start();
  }

  void resetMagneticField(Location location)
  {
    mSensorHelper.resetMagneticField(mSavedLocation, location);
  }

  private void calcLocationUpdatesInterval()
  {
    mLogger.d(TAG, "calcLocationUpdatesInterval()");
    if (RoutingController.get().isNavigating())
    {
      mLogger.d(TAG, "calcLocationUpdatesInterval(), it's navigation mode");
      final @Framework.RouterType int router = Framework.nativeGetRouter();
      switch (router)
      {
      case Framework.ROUTER_TYPE_PEDESTRIAN:
        mInterval = INTERVAL_NAVIGATION_PEDESTRIAN_MS;
        break;

      case Framework.ROUTER_TYPE_VEHICLE:
        mInterval = INTERVAL_NAVIGATION_VEHICLE_MS;
        break;

      case Framework.ROUTER_TYPE_BICYCLE:
        mInterval = INTERVAL_NAVIGATION_BICYCLE_MS;
        break;

      default:
        throw new IllegalArgumentException("Unsupported router type: " + router);
      }

      return;
    }

    int mode = getMyPositionMode();
    switch (mode)
    {
      case LocationState.FOLLOW:
        mInterval = INTERVAL_FOLLOW_MS;
        break;

      case LocationState.FOLLOW_AND_ROTATE:
        mInterval = INTERVAL_FOLLOW_AND_ROTATE_MS;
        break;

      default:
        mInterval = INTERVAL_NOT_FOLLOW_MS;
        break;
    }
  }

  long getInterval()
  {
    return mInterval;
  }

  /**
   * Stops the current provider. Then initialize the location provider again,
   * because location settings could be changed and a new location provider can be used,
   * such as Google fused provider. And we think that Google fused provider is preferable
   * for the most cases. And starts the initialized location provider.
   *
   * @see #start()
   *
   */
  public void restart()
  {
    mLogger.d(TAG, "restart()");
    checkProviderInitialization();
    stopInternal();
    initProvider();
    start();
  }

  /**
   * Adds the {@link #mCoreLocationListener} to listen location updates and notify UI.
   * Notifies about {@link #ERROR_DENIED} if there are no enabled location providers.
   * Calculates minimum time interval for location updates.
   * Starts polling location updates.
   */
  public void start()
  {
    checkProviderInitialization();
    //noinspection ConstantConditions
    if (mLocationProvider.isActive())
      throw new AssertionError("Location provider '" + mLocationProvider + "' must be stopped first");

    addListener(mCoreLocationListener, true);

    if (!LocationUtils.checkProvidersAvailability())
    {
      // No need to notify about an error in first run mode
      if (!mInFirstRun)
        notifyLocationError(ERROR_DENIED);
      return;
    }

    long oldInterval = mInterval;
    mLogger.d(TAG, "Old time interval (ms): " + oldInterval);
    calcLocationUpdatesInterval();
    mLogger.d(TAG, "start(), params: " + mInterval);
    startInternal();
  }

  /**
   * Stops the polling location updates, i.e. removes the {@link #mCoreLocationListener} and stops
   * the current active provider.
   */
  private void stop()
  {
    mLogger.d(TAG, "stop()");
    checkProviderInitialization();
    //noinspection ConstantConditions
    if (!mLocationProvider.isActive())
    {
      mLogger.i(TAG, "Provider '" + mLocationProvider + "' is already stopped");
      return;
    }

    removeListener(mCoreLocationListener);
    stopInternal();
  }

  /**
   * Actually starts location polling.
   */
  private void startInternal()
  {
    mLogger.d(TAG, "startInternal(), current provider is '" + mLocationProvider
                   + "' , my position mode = " + LocationState.nameOf(getMyPositionMode())
                   + ", mInFirstRun = " + mInFirstRun);
    if (!PermissionsUtils.isLocationGranted())
    {
      mLogger.w(TAG, "Dynamic permission ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION is not granted",
                new Throwable());
      return;
    }
    checkProviderInitialization();
    //noinspection ConstantConditions
    mLocationProvider.start();
    mLogger.d(TAG, mLocationProvider.isActive() ? "SUCCESS" : "FAILURE");

    if (mLocationProvider.isActive())
    {
      if (!mInFirstRun && getMyPositionMode() == LocationState.NOT_FOLLOW_NO_POSITION)
        switchToNextMode();
    }
  }

  private void checkProviderInitialization()
  {
    if (mLocationProvider == null)
    {
      String error = "A location provider must be initialized!";
      mLogger.e(TAG, error, new Throwable());
      throw new AssertionError(error);
    }
  }

  /**
   * Actually stops location polling.
   */
  private void stopInternal()
  {
    mLogger.d(TAG, "stopInternal()");
    checkProviderInitialization();
    //noinspection ConstantConditions
    mLocationProvider.stop();
    mSensorHelper.stop();
  }

  /**
   * Attach UI to helper.
   */
  @UiThread
  public void attach(@NonNull UiCallback callback)
  {
    mLogger.d(TAG, "attach() callback = " + callback);

    if (mUiCallback != null)
    {
      mLogger.d(TAG, " - already attached. Skip.");
      return;
    }

    mUiCallback = callback;

    Utils.keepScreenOn(true, mUiCallback.getActivity().getWindow());

    mUiCallback.onMyPositionModeChanged(getMyPositionMode());
    if (mCompassData != null)
      mUiCallback.onCompassUpdated(mCompassData);

    checkProviderInitialization();
    //noinspection ConstantConditions
    if (mLocationProvider.isActive())
    {
      mLogger.d(TAG, "attach() provider '" + mLocationProvider + "' is active, just add the listener");
      addListener(mCoreLocationListener, true);
    }
    else
    {
      restart();
    }
  }

  /**
   * Detach UI from helper.
   */
  @UiThread
  public void detach(boolean delayed)
  {
    mLogger.d(TAG, "detach(), delayed: " + delayed);

    if (mUiCallback == null)
    {
      mLogger.d(TAG, " - already detached. Skip.");
      return;
    }

    Utils.keepScreenOn(false, mUiCallback.getActivity().getWindow());
    mUiCallback = null;
    stop();
  }

  @UiThread
  public void onEnteredIntoFirstRun()
  {
    mLogger.i(TAG, "onEnteredIntoFirstRun");
    mInFirstRun = true;
  }

  @UiThread
  public void onExitFromFirstRun()
  {
    mLogger.i(TAG, "onExitFromFirstRun");
    if (!mInFirstRun)
      throw new AssertionError("Must be called only after 'onEnteredIntoFirstRun' method!");

    mInFirstRun = false;

    if (getMyPositionMode() != LocationState.NOT_FOLLOW_NO_POSITION)
      throw new AssertionError("My position mode must be equal NOT_FOLLOW_NO_POSITION");

    // If there is a location we need just to pass it to the listeners, so that
    // my position state machine will be switched to the FOLLOW state.
    Location location = getSavedLocation();
    if (location != null)
    {
      notifyLocationUpdated();
      mLogger.d(TAG, "Current location is available, so play the nice zoom animation");
      Framework.nativeZoomToPoint(location.getLatitude(), location.getLongitude(), 14, true);
      return;
    }

    checkProviderInitialization();
    // If the location hasn't been obtained yet we need to switch to the next mode and wait for locations.
    // Otherwise, try to restart location updates polling.
    // noinspection ConstantConditions
    if (mLocationProvider.isActive())
      switchToNextMode();
    else
      restart();
  }

  /**
   * Obtains last known location regardless of "My position" button state.
   * @return {@code null} on failure.
   */
  @Nullable
  public Location getLastKnownLocation(long expirationMillis)
  {
    if (mSavedLocation != null)
      return mSavedLocation;

    return AndroidNativeProvider.findBestNotExpiredLocation(expirationMillis);
  }

  @Nullable
  public Location getLastKnownLocation()
  {
    return getLastKnownLocation(LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG);
  }

  @Nullable
  public CompassData getCompassData()
  {
    return mCompassData;
  }

  @LocationState.Value
  public int getMyPositionMode()
  {
    return LocationState.nativeGetMode();
  }

  private static native void nativeOnLocationError(int errorCode);
  private static native void nativeLocationUpdated(long time, double lat, double lon, float accuracy,
                                                   double altitude, float speed, float bearing);
  static native float[] nativeUpdateCompassSensor(int ind, float[] arr);

  public interface UiCallback
  {
    Activity getActivity();
    void onMyPositionModeChanged(int newMode);
    void onLocationUpdated(@NonNull Location location);
    void onCompassUpdated(@NonNull CompassData compass);
    void onLocationError();
    void onLocationNotFound();
  }
}