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

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

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;

import com.mapswithme.maps.LightFramework;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.maps.scheduling.JobIdMap;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.PermissionsUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

import java.util.concurrent.TimeUnit;

import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static com.mapswithme.maps.MwmApplication.prefs;

public class NotificationService extends JobIntentService
{
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private static final String TAG = NotificationService.class.getSimpleName();
  private static final String LAST_AUTH_NOTIFICATION_TIMESTAMP = "DownloadOrUpdateTimestamp";
  private static final int MIN_COUNT_UNSENT_UGC = 2;
  private static final long MIN_AUTH_EVENT_DELTA_MILLIS = TimeUnit.DAYS.toMillis(5);

  private interface NotificationExecutor
  {
    boolean tryToNotify();
  }

  public static void startOnConnectivityChanged(@NonNull Context context)
  {
    final Intent intent = new Intent(context, NotificationService.class)
        .setAction(CONNECTIVITY_ACTION);

    int id = JobIdMap.getId(NotificationService.class);
    JobIntentService.enqueueWork(context, NotificationService.class, id, intent);
  }

  private boolean notifyIsNotAuthenticated()
  {
    final boolean isAuthenticated = LightFramework.nativeIsAuthenticated();
    final int numberUnsentUgc = LightFramework.nativeGetNumberUnsentUGC();

    if (!PermissionsUtils.isExternalStorageGranted() ||
        !NetworkPolicy.getCurrentNetworkUsageStatus() ||
        isAuthenticated ||
        numberUnsentUgc < MIN_COUNT_UNSENT_UGC)
    {
      LOGGER.d(TAG, "Authentication notification is rejected. External storage granted: " +
                    PermissionsUtils.isExternalStorageGranted() + ". Is user authenticated: " +
                    isAuthenticated + ". Current network usage status: " +
                    NetworkPolicy.getCurrentNetworkUsageStatus() + ". Number of unsent UGC: " +
                    numberUnsentUgc);
      return false;
    }

    // Do not show push when user is in the navigation mode.
    if (MwmApplication.get().arePlatformAndCoreInitialized() &&
        RoutingController.get().isNavigating())
    {
      LOGGER.d(TAG, "Authentication notification is rejected. The user is in navigation mode.");
      return false;
    }

    final long lastEventTimestamp = prefs().getLong(LAST_AUTH_NOTIFICATION_TIMESTAMP, 0);

    if (System.currentTimeMillis() - lastEventTimestamp > MIN_AUTH_EVENT_DELTA_MILLIS)
    {
      LOGGER.d(TAG, "Authentication notification will be sent.");

      prefs().edit()
             .putLong(LAST_AUTH_NOTIFICATION_TIMESTAMP, System.currentTimeMillis())
             .apply();

      Notifier notifier = Notifier.from(getApplication());
      notifier.notifyAuthentication();

      return true;
    }
    LOGGER.d(TAG, "Authentication notification is rejected. Last event timestamp: " +
                  lastEventTimestamp + "Current time milliseconds: " + System.currentTimeMillis());
    return false;
  }

  @Override
  protected void onHandleWork(@NonNull Intent intent)
  {
    final String action = intent.getAction();

    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action))
      onConnectivityChanged();
  }

  private void onConnectivityChanged()
  {
    final NotificationExecutor notifyOrder[] =
    {
        this::notifyIsNotAuthenticated
    };

    // Only one notification should be shown at a time.
    for (NotificationExecutor executor : notifyOrder)
    {
      if (executor.tryToNotify())
        return;
    }
  }
}