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: c82d59902df3f006a3f5a6e21cdde06933c9f6d2 (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
package com.mapswithme.maps.background;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import androidx.annotation.NonNull;
import androidx.core.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.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()
  {
    if (LightFramework.nativeIsAuthenticated()
        || LightFramework.nativeGetNumberUnsentUGC() < MIN_COUNT_UNSENT_UGC)
    {
      LOGGER.d(TAG, "Authentication notification is rejected. Is user authenticated: " +
                    LightFramework.nativeIsAuthenticated() + ". Number of unsent UGC: " +
                    LightFramework.nativeGetNumberUnsentUGC());
      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;
  }

  private boolean notifySmart()
  {
    if (MwmApplication.backgroundTracker(getApplication()).isForeground())
      return false;

    NotificationCandidate candidate = LightFramework.nativeGetNotification();

    if (candidate == null)
      return false;

    if (candidate.getType() == NotificationCandidate.TYPE_UGC_REVIEW)
    {
      Notifier notifier = Notifier.from(getApplication());
      notifier.notifyLeaveReview((NotificationCandidate.UgcReview) candidate);
      return true;
    }

    return false;
  }

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

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

  private void tryToShowNotification()
  {
    if (!PermissionsUtils.isExternalStorageGranted())
    {
      LOGGER.d(TAG, "Notification is rejected. External storage is not granted.");
      return;
    }

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

    final NotificationExecutor notifyOrder[] =
    {
      this::notifyIsNotAuthenticated,
      this::notifySmart
    };

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