MapboxOfflineDownloaderService extends Service implements OfflineRegionObserver, OnDownloadMapListener { protected boolean persistOfflineMapTask(@Nullable Intent intent) { persistOfflineMapTaskCalled = true; if (intent == null) { return false; } Bundle extras = intent.getExtras(); if (extras != null && extras.containsKey(Constants.PARCELABLE_KEY_SERVICE_ACTION)) { final SERVICE_ACTION serviceAction = (SERVICE_ACTION) extras.get(Constants.PARCELABLE_KEY_SERVICE_ACTION); if (extras.containsKey(Constants.PARCELABLE_KEY_MAP_UNIQUE_NAME) && extras.containsKey(Constants.PARCELABLE_KEY_MAPBOX_ACCESS_TOKEN)) { final String mapUniqueName = extras.getString(Constants.PARCELABLE_KEY_MAP_UNIQUE_NAME); mapBoxAccessToken = extras.getString(Constants.PARCELABLE_KEY_MAPBOX_ACCESS_TOKEN); Mapbox.getInstance(this, mapBoxAccessToken); MapBoxDownloadTask downloadTask = new MapBoxDownloadTask(); downloadTask.setMapName(mapUniqueName); downloadTask.setMapBoxAccessToken(mapBoxAccessToken); if (serviceAction == SERVICE_ACTION.DOWNLOAD_MAP) { if (extras.containsKey(Constants.PARCELABLE_KEY_STYLE_URL) && extras.containsKey(Constants.PARCELABLE_KEY_MAX_ZOOM) && extras.containsKey(Constants.PARCELABLE_KEY_MIN_ZOOM) && extras.containsKey(Constants.PARCELABLE_KEY_TOP_LEFT_BOUND) && extras.containsKey(Constants.PARCELABLE_KEY_TOP_RIGHT_BOUND) && extras.containsKey(Constants.PARCELABLE_KEY_BOTTOM_RIGHT_BOUND) && extras.containsKey(Constants.PARCELABLE_KEY_BOTTOM_LEFT_BOUND)) { downloadTask.setPackageName("kl"); downloadTask.setMapBoxStyleUrl(extras.getString(Constants.PARCELABLE_KEY_STYLE_URL)); downloadTask.setMaxZoom(ObjectCoercer.coerceNumberObjectToDoublePrimitive(extras.get(Constants.PARCELABLE_KEY_MAX_ZOOM))); downloadTask.setMinZoom(ObjectCoercer.coerceNumberObjectToDoublePrimitive(extras.get(Constants.PARCELABLE_KEY_MIN_ZOOM))); downloadTask.setTopLeftBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_TOP_LEFT_BOUND)); downloadTask.setTopRightBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_TOP_RIGHT_BOUND)); downloadTask.setBottomRightBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_BOTTOM_RIGHT_BOUND)); downloadTask.setBottomLeftBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_BOTTOM_LEFT_BOUND)); realmDatabase.deletePendingOfflineMapDownloadsWithSimilarNames(mapUniqueName); MapBoxDownloadTask.constructMapBoxOfflineQueueTask(downloadTask); return true; } } else if (serviceAction == SERVICE_ACTION.DELETE_MAP) { MapBoxDeleteTask deleteTask = new MapBoxDeleteTask(); deleteTask.setMapBoxAccessToken(mapBoxAccessToken); deleteTask.setMapName(mapUniqueName); MapBoxDeleteTask.constructMapBoxOfflineQueueTask(deleteTask); return true; } else { final String taskType = extras.getString(Constants.PARCELABLE_KEY_DELETE_TASK_TYPE, ""); if (!TextUtils.isEmpty(taskType)) { if (taskType.equals(MapBoxOfflineQueueTask.TASK_TYPE_DOWNLOAD) && mostRecentMapNameUpdate.equals(mapUniqueName)) { MapBoxOfflineResourcesDownloader mapBoxOfflineResourcesDownloader = MapBoxOfflineResourcesDownloader.getInstance(this, mapBoxAccessToken); showProgressNotification(mostRecentMapNameUpdate, mostRecentPercentageUpdate, false); mapBoxOfflineResourcesDownloader.pauseMapDownload(mapUniqueName, new OnPauseMapDownloadCallback() { @Override public void onPauseSuccess() { MapBoxOfflineResourcesDownloader .getInstance(MapboxOfflineDownloaderService.this, mapBoxAccessToken) .deleteMap(mapUniqueName, new OfflineRegion.OfflineRegionDeleteCallback() { @Override public void onDelete() { if (deleteTaskFromRealmDatabase(taskType, mapUniqueName)) { sendBroadcast(SERVICE_ACTION_RESULT.SUCCESSFUL, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD); showDownloadCompleteNotification(String.format(getString(R.string.notification_stopped_download_title), mapUniqueName), getString(R.string.notification_stopped_download_content)); } else { sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, getString(R.string.map_delete_task_error)); } releaseQueueToPerformOtherJobs(); performNextTask(); } @Override public void onError(String error) { sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, error); } }); } @Override public void onPauseError(String error) { sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, String.format(getString(R.string.error_broadcast_for_download_pause), error)); } }); } else { if (deleteTaskFromRealmDatabase(taskType, mapUniqueName)) { sendBroadcast(SERVICE_ACTION_RESULT.SUCCESSFUL, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD); } else { sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, String.format(getString(R.string.error_broadcast_for_download_pause), "Map could not be found")); } } } } } } return false; } MapboxOfflineDownloaderService(); @Override int onStartCommand(@Nullable Intent intent, int flags, int startId); @Nullable @Override IBinder onBind(Intent intent); @Override void onStatusChanged(@NonNull OfflineRegionStatus status, @NonNull OfflineRegion offlineRegion); @Override void onError(String error); @Override void onError(@NonNull String reason, @Nullable String message); @Override void mapboxTileCountLimitExceeded(long limit); @Override void onDestroy(); static final String KEY_RESULT_STATUS; static final String KEY_RESULT_MESSAGE; static final String KEY_RESULTS_PARENT_ACTION; static final String MY_PREFERENCES; static final String PREFERENCE_MAPBOX_ACCESS_TOKEN; static final int PROGRESS_NOTIFICATION_ID; static final int REQUEST_ID_STOP_MAP_DOWNLOAD; static int LAST_DOWNLOAD_COMPLETE_NOTIFICATION_ID; static int LAST_DOWNLOAD_ERROR_NOTIFICATION_ID; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean onStartCommandCalled; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean persistOfflineMapTaskCalled; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean performNextTaskCalled; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean observeOfflineRegionCalled; }
@Test public void persistOfflineMapTaskShouldReturnFalseWhenGivenNullIntent() { Intent sampleExtra = null; assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleExtra)); }
@Test public void persistOfflineMapTaskShouldReturnFalseWhenGivenNullIntentExtras() { Intent sampleExtra = new Intent(); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleExtra)); sampleExtra.putExtra("SOME EXTRA", ""); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleExtra)); }
@Test public void persistOfflineMapTaskShouldReturnTrueWhenGivenValidDeleteTask() { Intent sampleServiceIntent = createMapboxOfflineDownloaderServiceIntent(); sampleServiceIntent = createSampleDeleteIntent(sampleServiceIntent); assertEquals(true, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleServiceIntent)); }
@Test public void persistOfflineMapTaskShouldReturnTrueWhenGivenValidDownloadTask() throws NoSuchFieldException, IllegalAccessException { Intent sampleServiceIntent = createMapboxOfflineDownloaderServiceIntent(); sampleServiceIntent = createSampleDownloadIntent(sampleServiceIntent); insertValueInPrivateField(mapboxOfflineDownloaderService, "realmDatabase", RealmDatabase.init(context)); assertEquals(true, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleServiceIntent)); }
@Test public void persistOfflineMapTaskShouldReturnFalseWhenGivenInvalidDownloadTask() { Intent sampleServiceIntent = createMapboxOfflineDownloaderServiceIntent(); sampleServiceIntent = createSampleDownloadIntent(sampleServiceIntent); Intent sampleInvalidIntent = (Intent) sampleServiceIntent.clone(); sampleInvalidIntent.removeExtra(Constants.PARCELABLE_KEY_STYLE_URL); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleInvalidIntent)); sampleInvalidIntent = (Intent) sampleInvalidIntent.clone(); sampleInvalidIntent.removeExtra(Constants.PARCELABLE_KEY_MIN_ZOOM); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleInvalidIntent)); sampleInvalidIntent = (Intent) sampleInvalidIntent.clone(); sampleInvalidIntent.removeExtra(Constants.PARCELABLE_KEY_MAX_ZOOM); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleInvalidIntent)); sampleInvalidIntent = (Intent) sampleInvalidIntent.clone(); sampleInvalidIntent.removeExtra(Constants.PARCELABLE_KEY_TOP_LEFT_BOUND); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleInvalidIntent)); sampleInvalidIntent = (Intent) sampleInvalidIntent.clone(); sampleInvalidIntent.removeExtra(Constants.PARCELABLE_KEY_BOTTOM_RIGHT_BOUND); assertEquals(false, mapboxOfflineDownloaderService.persistOfflineMapTask(sampleInvalidIntent)); }