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

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

import android.annotation.TargetApi;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import androidx.annotation.NonNull;

import com.crashlytics.android.Crashlytics;
import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.Trigger;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.util.Utils;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class ConnectivityJobScheduler implements ConnectivityListener
{
  private static final int SCHEDULE_PERIOD_IN_HOURS = 1;

  @NonNull
  private final ConnectivityListener mMasterConnectivityListener;

  public ConnectivityJobScheduler(@NonNull MwmApplication context)
  {
    mMasterConnectivityListener = Utils.isLollipopOrLater()
                                  ? createNativeJobScheduler(context)
                                  : createCompatJobScheduler(context);
  }

  @NonNull
  private ConnectivityListener createCompatJobScheduler(@NonNull MwmApplication context)
  {
    int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
    boolean isAvailable = status == ConnectionResult.SUCCESS;
    return isAvailable ? new ConnectivityListenerCompat(context) : new ConnectivityListenerStub();
  }

  @NonNull
  private ConnectivityListener createNativeJobScheduler(@NonNull MwmApplication context)
  {
    return new NativeConnectivityListener(context);
  }

  @Override
  public void listen()
  {
    mMasterConnectivityListener.listen();
  }

  @NonNull
  public static ConnectivityJobScheduler from(@NonNull Context context)
  {
    MwmApplication application = (MwmApplication) context.getApplicationContext();
    return (ConnectivityJobScheduler) application.getConnectivityListener();
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private static class NativeConnectivityListener implements ConnectivityListener
  {
    @NonNull
    private final JobScheduler mJobScheduler;
    @NonNull
    private final Context mContext;

    NativeConnectivityListener(@NonNull Context context)
    {
      mContext = context;
      JobScheduler jobScheduler = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
      Objects.requireNonNull(jobScheduler);
      mJobScheduler = jobScheduler;
    }

    @Override
    public void listen()
    {
      ComponentName component = new ComponentName(mContext, NativeJobService.class);
      int jobId = JobIdMap.getId(NativeJobService.class);
      JobInfo jobInfo = new JobInfo
          .Builder(jobId, component)
          .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
          .setPersisted(true)
          .setMinimumLatency(TimeUnit.HOURS.toMillis(SCHEDULE_PERIOD_IN_HOURS))
          .build();
      mJobScheduler.schedule(jobInfo);
    }
  }

  private static class ConnectivityListenerCompat implements ConnectivityListener
  {
    @NonNull
    private final FirebaseJobDispatcher mJobDispatcher;

    ConnectivityListenerCompat(@NonNull MwmApplication context)
    {
      mJobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
    }

    @Override
    public void listen()
    {
      String tag = String.valueOf(JobIdMap.getId(FirebaseJobService.class));
      int executionWindowStart = (int) TimeUnit.HOURS.toSeconds(SCHEDULE_PERIOD_IN_HOURS);
      Job job = mJobDispatcher.newJobBuilder()
                              .setTag(tag)
                              .setService(FirebaseJobService.class)
                              .setConstraints(Constraint.ON_ANY_NETWORK)
                              .setLifetime(Lifetime.FOREVER)
                              .setTrigger(Trigger.executionWindow(executionWindowStart, ++executionWindowStart))
                              .build();
      mJobDispatcher.mustSchedule(job);
    }
  }

  private static class ConnectivityListenerStub implements ConnectivityListener
  {
    ConnectivityListenerStub()
    {
      IllegalStateException exception = new IllegalStateException("Play services doesn't exist on" +
                                                                  " the device");
      Crashlytics.logException(exception);
    }

    @Override
    public void listen()
    {
      /* Do nothing */
    }
  }

}