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

AndroidNativeProvider.java « location « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e668c89ec8a7af6d03bc2ec769f242cf3c590d1f (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
package com.mapswithme.maps.location;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapswithme.maps.MwmApplication;
import com.mapswithme.util.LocationUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

class AndroidNativeProvider extends BaseLocationProvider
{
  private final static String TAG = AndroidNativeProvider.class.getSimpleName();
  private final static String[] TRUSTED_PROVIDERS = { LocationManager.NETWORK_PROVIDER,
                                                      LocationManager.GPS_PROVIDER };
  private final static Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.LOCATION);

  @NonNull
  private final LocationManager mLocationManager;
  @NonNull
  private final List<LocationListener> mListeners = new ArrayList<>();

  AndroidNativeProvider(@NonNull LocationFixChecker locationFixChecker)
  {
    super(locationFixChecker);
    mLocationManager = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE);
  }

  @Override
  protected void start()
  {
    LOGGER.d(TAG, "Android native provider is started");
    if (isActive())
      return;

    List<String> providers = getAvailableProviders(mLocationManager);
    if (providers.isEmpty())
    {
      setActive(false);
      return;
    }

    setActive(true);
    for (String provider : providers)
    {
      LocationListener listener = new BaseLocationListener(getLocationFixChecker());
      long interval = LocationHelper.INSTANCE.getInterval();
      LOGGER.d(TAG, "Request Android native provider '" + provider
                    + "' to get locations at this interval = " + interval + " ms");
      mListeners.add(listener);
      try
      {
        mLocationManager.requestLocationUpdates(provider, interval, 0, listener);
      }
      catch (SecurityException e)
      {
        LOGGER.e(TAG, "Dynamic permission ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION is not granted",
                 e);
        setActive(false);
        return;
      }
    }

    LocationHelper.INSTANCE.startSensors();

    Location location = findBestNotExpiredLocation(mLocationManager, providers,
                                                   LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT);
    if (location != null && !getLocationFixChecker().isLocationBetterThanLast(location))
    {
      location = LocationHelper.INSTANCE.getSavedLocation();
      if (location == null || LocationUtils.isExpired(location, LocationHelper.INSTANCE.getSavedLocationTime(),
                                                      LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
      {
        return;
      }
    }

    if (location != null)
      onLocationChanged(location);
  }

  private void onLocationChanged(@NonNull Location location)
  {
    ListIterator<LocationListener> iterator = mListeners.listIterator();
    // All listeners have to be notified only through safe list iterator interface,
    // otherwise ConcurrentModificationException will be obtained, because each listener can
    // cause 'stop' method calling and modifying the collection during this iteration.
    // noinspection WhileLoopReplaceableByForEach
    while (iterator.hasNext())
      iterator.next().onLocationChanged(location);
  }

  @Override
  protected void stop()
  {
    LOGGER.d(TAG, "Android native provider is stopped");
    ListIterator<LocationListener> iterator = mListeners.listIterator();
    // noinspection WhileLoopReplaceableByForEach
    while (iterator.hasNext())
      mLocationManager.removeUpdates(iterator.next());

    mListeners.clear();
    setActive(false);
  }

  @Nullable
  static Location findBestNotExpiredLocation(long expirationMillis)
  {
    final LocationManager manager = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE);
    return findBestNotExpiredLocation(manager,
                                      getAvailableProviders(manager),
                                      expirationMillis);
  }

  @Nullable
  private static Location findBestNotExpiredLocation(LocationManager manager, List<String> providers, long expirationMillis)
  {
    Location res = null;
    try
    {
      for (final String pr : providers)
      {
        final Location last = manager.getLastKnownLocation(pr);
        if (last == null || LocationUtils.isExpired(last, last.getTime(), expirationMillis))
          continue;

        if (res == null || res.getAccuracy() > last.getAccuracy())
          res = last;
      }
    }
    catch (SecurityException e)
    {
      LOGGER.e(TAG, "Dynamic permission ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION is not granted",
               e);
    }
    return res;
  }

  @NonNull
  private static List<String> getAvailableProviders(@NonNull LocationManager locationManager)
  {
    final List<String> res = new ArrayList<>();
    for (String provider : TRUSTED_PROVIDERS)
    {
      if (locationManager.isProviderEnabled(provider))
        res.add(provider);
    }
    return res;
  }
}