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

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

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Environment;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.mapswithme.maps.BuildConfig;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.background.AppBackgroundTracker;
import com.mapswithme.util.Constants;
import com.mapswithme.util.StorageUtils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.FileLogger;
import com.mapswithme.util.log.Logger;

public final class TrackRecorder
{
  private static final AlarmManager sAlarmManager = (AlarmManager)MwmApplication.get().getSystemService(Context.ALARM_SERVICE);
  private static final Intent sAlarmIntent = new Intent("com.mapswithme.maps.TRACK_RECORDER_ALARM");
  private static final long WAKEUP_INTERVAL_MS = 20000;
  private static final long STARTUP_AWAIT_INTERVAL_MS = 5000;

  private static final String LOCATION_TIMEOUT_STORED_KEY = "TrackRecordLastAwaitTimeout";
  private static final long LOCATION_TIMEOUT_MIN_MS = 5000;
  private static final long LOCATION_TIMEOUT_MAX_MS = 80000;

  private static final Runnable sStartupAwaitProc = new Runnable()
  {
    @Override
    public void run()
    {
      restartAlarmIfEnabled();
    }
  };

  private static Boolean sEnableLogging;
  @Nullable
  private static Logger sLogger;

  private static final LocationListener sLocationListener = new LocationListener.Simple()
  {
    @Override
    public void onLocationUpdated(Location location)
    {
      log("onLocationUpdated()");
      setAwaitTimeout(LOCATION_TIMEOUT_MIN_MS);
      LocationHelper.INSTANCE.onLocationUpdated(location);
      TrackRecorderWakeService.stop();
    }

    @Override
    public void onLocationError(int errorCode)
    {
      log("onLocationError() errorCode: " + errorCode);

      // Unrecoverable error occured: GPS disabled or inaccessible
      setEnabled(false);
    }
  };

  private TrackRecorder() {}

  public static void init()
  {
    log("--------------------------------");
    log("init()");

    MwmApplication.backgroundTracker().addListener(new AppBackgroundTracker.OnTransitionListener()
    {
      @Override
      public void onTransit(boolean foreground)
      {
        TrackRecorder.log("Transit to foreground: " + foreground);

        UiThread.cancelDelayedTasks(sStartupAwaitProc);
        if (foreground)
          TrackRecorderWakeService.stop();
        else
          restartAlarmIfEnabled();
      }
    });

    if (nativeIsEnabled())
      UiThread.runLater(sStartupAwaitProc, STARTUP_AWAIT_INTERVAL_MS);
    else
      stop();
  }

  private static PendingIntent getAlarmIntent()
  {
    return PendingIntent.getBroadcast(MwmApplication.get(), 0, sAlarmIntent, 0);
  }

  private static void restartAlarmIfEnabled()
  {
    TrackRecorder.log("restartAlarmIfEnabled()");
    if (nativeIsEnabled())
      sAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + WAKEUP_INTERVAL_MS, getAlarmIntent());
  }

  private static void stop()
  {
    TrackRecorder.log("stop(). Cancel awake timer");
    sAlarmManager.cancel(getAlarmIntent());
    TrackRecorderWakeService.stop();
  }

  public static boolean isEnabled()
  {
    return nativeIsEnabled();
  }

  public static void setEnabled(boolean enabled)
  {
    log("setEnabled(): " + enabled);

    setAwaitTimeout(LOCATION_TIMEOUT_MIN_MS);
    nativeSetEnabled(enabled);

    if (enabled)
      restartAlarmIfEnabled();
    else
      stop();
  }

  public static int getDuration()
  {
    return nativeGetDuration();
  }

  public static void setDuration(int hours)
  {
    nativeSetDuration(hours);
  }

  static void onWakeAlarm()
  {
    log("onWakeAlarm(). Enabled: " + nativeIsEnabled());

    UiThread.cancelDelayedTasks(sStartupAwaitProc);

    if (nativeIsEnabled() && !MwmApplication.backgroundTracker().isForeground())
      TrackRecorderWakeService.start();
    else
      stop();
  }

  static long getAwaitTimeout()
  {
    return MwmApplication.prefs().getLong(LOCATION_TIMEOUT_STORED_KEY, LOCATION_TIMEOUT_MIN_MS);
  }

  private static void setAwaitTimeout(long timeout)
  {
    log("setAwaitTimeout(): " + timeout);

    if (timeout != getAwaitTimeout())
      MwmApplication.prefs().edit().putLong(LOCATION_TIMEOUT_STORED_KEY, timeout).apply();
  }

  static void incrementAwaitTimeout()
  {
    long current = getAwaitTimeout();
    long next = current * 2;
    if (next > LOCATION_TIMEOUT_MAX_MS)
      next = LOCATION_TIMEOUT_MAX_MS;

    if (next != current)
      setAwaitTimeout(next);
  }

  static void onServiceStarted()
  {
    TrackRecorder.log("onServiceStarted(). Scheduled to be run on UI thread...");

    UiThread.run(new Runnable()
    {
      @Override
      public void run()
      {
        TrackRecorder.log("onServiceStarted(): actually runs here");
        LocationHelper.INSTANCE.addListener(sLocationListener, false);
      }
    });
  }

  static void onServiceStopped()
  {
    TrackRecorder.log("onServiceStopped(). Scheduled to be run on UI thread...");

    UiThread.run(new Runnable()
    {
      @Override
      public void run()
      {
        TrackRecorder.log("onServiceStopped(): actually runs here");
        LocationHelper.INSTANCE.removeListener(sLocationListener);

        if (!MwmApplication.backgroundTracker().isForeground())
          restartAlarmIfEnabled();
      }
    });
  }

  static void log(String message)
  {
    if (sEnableLogging == null)
      sEnableLogging = ("debug".equals(BuildConfig.BUILD_TYPE) || "beta".equals(BuildConfig.BUILD_TYPE));

    if (!sEnableLogging)
      return;

    synchronized (TrackRecorder.class)
    {
      if (sLogger == null)
      {
        String externalDir = StorageUtils.getExternalFilesDir();
        if (!TextUtils.isEmpty(externalDir))
          sLogger = new FileLogger(externalDir + "/gps-tracker.log");
      }
    }

    if (sLogger != null)
      sLogger.d(message);
  }

  private static native void nativeSetEnabled(boolean enable);
  private static native boolean nativeIsEnabled();
  private static native void nativeSetDuration(int hours);
  private static native int nativeGetDuration();
}