diff --git a/android/src/com/mapswithme/maps/background/NotificationService.java b/android/src/com/mapswithme/maps/background/NotificationService.java index a27a93f4c6..0ee96c6959 100644 --- a/android/src/com/mapswithme/maps/background/NotificationService.java +++ b/android/src/com/mapswithme/maps/background/NotificationService.java @@ -9,6 +9,7 @@ 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; @@ -37,8 +38,8 @@ public class NotificationService extends JobIntentService final Intent intent = new Intent(context, NotificationService.class) .setAction(CONNECTIVITY_ACTION); - final int jobId = NotificationService.class.hashCode(); - JobIntentService.enqueueWork(context, NotificationService.class, jobId, intent); + int id = JobIdMap.getId(NotificationService.class); + JobIntentService.enqueueWork(context, NotificationService.class, id, intent); } private boolean notifyIsNotAuthenticated() diff --git a/android/src/com/mapswithme/maps/background/WorkerService.java b/android/src/com/mapswithme/maps/background/WorkerService.java index edbbd7678d..f931924616 100644 --- a/android/src/com/mapswithme/maps/background/WorkerService.java +++ b/android/src/com/mapswithme/maps/background/WorkerService.java @@ -9,6 +9,7 @@ import android.util.Log; import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.editor.Editor; +import com.mapswithme.maps.scheduling.JobIdMap; import com.mapswithme.maps.ugc.UGC; import com.mapswithme.util.CrashlyticsUtils; import com.mapswithme.util.log.Logger; @@ -32,7 +33,7 @@ public class WorkerService extends JobIntentService final Intent intent = new Intent(context, WorkerService.class); intent.setAction(WorkerService.ACTION_UPLOAD_OSM_CHANGES); JobIntentService.enqueueWork(context.getApplicationContext(), WorkerService.class, - WorkerService.class.hashCode(), intent); + JobIdMap.getId(WorkerService.class), intent); } /** @@ -42,7 +43,7 @@ public class WorkerService extends JobIntentService { final Intent intent = new Intent(context, WorkerService.class); intent.setAction(WorkerService.ACTION_UPLOAD_UGC); - final int jobId = WorkerService.class.hashCode(); + final int jobId = JobIdMap.getId(WorkerService.class); JobIntentService.enqueueWork(context, WorkerService.class, jobId, intent); } diff --git a/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java b/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java index 9903c5d42a..76414b1ed4 100644 --- a/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java +++ b/android/src/com/mapswithme/maps/bookmarks/SystemDownloadCompletedReceiver.java @@ -7,6 +7,7 @@ import android.support.annotation.NonNull; import android.support.v4.app.JobIntentService; import com.mapswithme.maps.background.AbstractLogBroadcastReceiver; +import com.mapswithme.maps.scheduling.JobIdMap; public class SystemDownloadCompletedReceiver extends AbstractLogBroadcastReceiver { @@ -24,7 +25,7 @@ public class SystemDownloadCompletedReceiver extends AbstractLogBroadcastReceive if (manager == null) return; intent.setClass(context, SystemDownloadCompletedService.class); - int jobId = SystemDownloadCompletedService.class.hashCode(); + int jobId = JobIdMap.getId(SystemDownloadCompletedService.class); JobIntentService.enqueueWork(context, SystemDownloadCompletedService.class, jobId, intent); } } diff --git a/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java b/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java index b9cf65c50e..c6d7ca1e65 100644 --- a/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java +++ b/android/src/com/mapswithme/maps/location/TrackRecorderWakeService.java @@ -9,6 +9,7 @@ import android.support.v4.app.JobIntentService; import android.util.Log; import com.mapswithme.maps.MwmApplication; +import com.mapswithme.maps.scheduling.JobIdMap; import com.mapswithme.util.CrashlyticsUtils; import com.mapswithme.util.Utils; import com.mapswithme.util.log.Logger; @@ -67,7 +68,7 @@ public class TrackRecorderWakeService extends JobIntentService Context app = context.getApplicationContext(); Intent intent = new Intent(app, TrackRecorderWakeService.class); - final int jobId = TrackRecorderWakeService.class.hashCode(); + final int jobId = JobIdMap.getId(TrackRecorderWakeService.class); if (Utils.isLollipopOrLater()) { JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); diff --git a/android/src/com/mapswithme/maps/scheduling/ConnectivityJobScheduler.java b/android/src/com/mapswithme/maps/scheduling/ConnectivityJobScheduler.java index 0958f0e49c..6a8f02e86c 100644 --- a/android/src/com/mapswithme/maps/scheduling/ConnectivityJobScheduler.java +++ b/android/src/com/mapswithme/maps/scheduling/ConnectivityJobScheduler.java @@ -84,7 +84,7 @@ public class ConnectivityJobScheduler implements ConnectivityListener public void listen() { ComponentName component = new ComponentName(mContext, NativeJobService.class); - int jobId = NativeJobService.class.hashCode(); + int jobId = JobIdMap.getId(NativeJobService.class); JobInfo jobInfo = new JobInfo .Builder(jobId, component) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) @@ -108,7 +108,7 @@ public class ConnectivityJobScheduler implements ConnectivityListener @Override public void listen() { - String tag = String.valueOf(FirebaseJobService.class.hashCode()); + String tag = String.valueOf(JobIdMap.getId(FirebaseJobService.class)); int executionWindowStart = (int) TimeUnit.HOURS.toSeconds(SCHEDULE_PERIOD_IN_HOURS); Job job = mJobDispatcher.newJobBuilder() .setTag(tag) diff --git a/android/src/com/mapswithme/maps/scheduling/JobIdMap.java b/android/src/com/mapswithme/maps/scheduling/JobIdMap.java new file mode 100644 index 0000000000..b5251156e2 --- /dev/null +++ b/android/src/com/mapswithme/maps/scheduling/JobIdMap.java @@ -0,0 +1,39 @@ +package com.mapswithme.maps.scheduling; + +import com.mapswithme.maps.background.NotificationService; +import com.mapswithme.maps.background.WorkerService; +import com.mapswithme.maps.bookmarks.SystemDownloadCompletedService; +import com.mapswithme.maps.location.TrackRecorderWakeService; +import com.mapswithme.util.Utils; + +import java.util.HashMap; +import java.util.Map; + +public class JobIdMap +{ + private static final Map, Integer> MAP = new HashMap<>(); + + static { + MAP.put(Utils.isLollipopOrLater() ? NativeJobService.class : FirebaseJobService.class, calcIdentifier(MAP.size())); + MAP.put(NotificationService.class, calcIdentifier(MAP.size())); + MAP.put(TrackRecorderWakeService.class, calcIdentifier(MAP.size())); + MAP.put(SystemDownloadCompletedService.class, calcIdentifier(MAP.size())); + MAP.put(WorkerService.class, calcIdentifier(MAP.size())); + } + + private static final int ID_BASIC = 1070; + private static final int JOB_TYPE_SHIFTS = 12; + + private static int calcIdentifier(int count) + { + return (count + 1 << JOB_TYPE_SHIFTS) + ID_BASIC; + } + + public static int getId(Class clazz) + { + Integer integer = MAP.get(clazz); + if (integer == null) + throw new IllegalArgumentException("Value not found for args : " + clazz); + return integer; + } +}