diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml
index 0ff1017482..1810b1fbe4 100644
--- a/OsmAnd/AndroidManifest.xml
+++ b/OsmAnd/AndroidManifest.xml
@@ -866,6 +866,11 @@
+
+
diff --git a/OsmAnd/res/values-ru/phrases.xml b/OsmAnd/res/values-ru/phrases.xml
index 83204daf21..ffc45e83c1 100644
--- a/OsmAnd/res/values-ru/phrases.xml
+++ b/OsmAnd/res/values-ru/phrases.xml
@@ -3470,4 +3470,6 @@
Местонахождение
+ Куча для выжига угля
+
\ No newline at end of file
diff --git a/OsmAnd/res/values/phrases.xml b/OsmAnd/res/values/phrases.xml
index 70624ea6fa..a15a2396e2 100644
--- a/OsmAnd/res/values/phrases.xml
+++ b/OsmAnd/res/values/phrases.xml
@@ -3961,4 +3961,6 @@
Changing table fee: yes
Changing table fee: no
+ Charcoal pile
+
diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml
index a4874a2acf..21fff43abf 100644
--- a/OsmAnd/res/values/strings.xml
+++ b/OsmAnd/res/values/strings.xml
@@ -11,6 +11,7 @@
Thx - Hardy
-->
+ OsmAnd downloading service
Magenta
Icon
diff --git a/OsmAnd/src/net/osmand/plus/NotificationHelper.java b/OsmAnd/src/net/osmand/plus/NotificationHelper.java
index 1fbcbd83b4..01b18a8e77 100644
--- a/OsmAnd/src/net/osmand/plus/NotificationHelper.java
+++ b/OsmAnd/src/net/osmand/plus/NotificationHelper.java
@@ -1,14 +1,5 @@
package net.osmand.plus;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.osmand.plus.notifications.ErrorNotification;
-import net.osmand.plus.notifications.GpxNotification;
-import net.osmand.plus.notifications.NavigationNotification;
-import net.osmand.plus.notifications.OsmandNotification;
-import net.osmand.plus.notifications.OsmandNotification.NotificationType;
-
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
@@ -19,6 +10,16 @@ import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationManagerCompat;
+import net.osmand.plus.notifications.DownloadNotification;
+import net.osmand.plus.notifications.ErrorNotification;
+import net.osmand.plus.notifications.GpxNotification;
+import net.osmand.plus.notifications.NavigationNotification;
+import net.osmand.plus.notifications.OsmandNotification;
+import net.osmand.plus.notifications.OsmandNotification.NotificationType;
+
+import java.util.ArrayList;
+import java.util.List;
+
public class NotificationHelper {
public static final String NOTIFICATION_CHANEL_ID = "osmand_background_service";
@@ -26,6 +27,7 @@ public class NotificationHelper {
private NavigationNotification navigationNotification;
private GpxNotification gpxNotification;
+ private DownloadNotification downloadNotification;
private ErrorNotification errorNotification;
private List all = new ArrayList<>();
@@ -37,9 +39,11 @@ public class NotificationHelper {
private void init() {
navigationNotification = new NavigationNotification(app);
gpxNotification = new GpxNotification(app);
+ downloadNotification = new DownloadNotification(app);
errorNotification = new ErrorNotification(app);
all.add(navigationNotification);
all.add(gpxNotification);
+ all.add(downloadNotification);
}
@NonNull
@@ -59,6 +63,13 @@ public class NotificationHelper {
}
}
+ @Nullable
+ public Notification buildDownloadNotification() {
+ Builder notificationBuilder = downloadNotification.buildNotification(false);
+
+ return notificationBuilder != null ? notificationBuilder.build() : null;
+ }
+
private Notification buildErrorNotification() {
removeNotification(errorNotification.getType());
setTopNotification(errorNotification);
diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java
index 6edecd3a5b..df3fb10251 100644
--- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java
+++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java
@@ -50,6 +50,7 @@ import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.dialogs.CrashBottomSheetDialogFragment;
import net.osmand.plus.dialogs.RateUsBottomSheetDialog;
import net.osmand.plus.download.DownloadIndexesThread;
+import net.osmand.plus.download.DownloadService;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.WaypointHelper;
@@ -98,6 +99,7 @@ public class OsmandApplication extends MultiDexApplication {
Handler uiHandler;
NavigationService navigationService;
+ DownloadService downloadService;
OsmandAidlApi aidlApi;
@@ -506,6 +508,14 @@ public class OsmandApplication extends MultiDexApplication {
this.navigationService = navigationService;
}
+ public DownloadService getDownloadService() {
+ return downloadService;
+ }
+
+ public void setDownloadService(DownloadService downloadService) {
+ this.downloadService = downloadService;
+ }
+
public OsmandAidlApi getAidlApi() {
return aidlApi;
}
@@ -889,6 +899,15 @@ public class OsmandApplication extends MultiDexApplication {
//getNotificationHelper().showNotifications();
}
+ public void startDownloadService() {
+ final Intent serviceIntent = new Intent(this, DownloadService.class);
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ startForegroundService(serviceIntent);
+ } else {
+ startService(serviceIntent);
+ }
+ }
public String getLangTranslation(String l) {
try {
diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java
index 015031c9b8..e0d470db27 100644
--- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java
+++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java
@@ -2,11 +2,7 @@ package net.osmand.plus.download;
import android.annotation.SuppressLint;
import android.app.Activity;
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
-import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
@@ -14,8 +10,6 @@ import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.StatFs;
import android.support.annotation.UiThread;
-import android.support.v4.app.NotificationCompat;
-import android.support.v4.app.NotificationCompat.Builder;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Toast;
@@ -25,7 +19,6 @@ import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.map.WorldRegion;
import net.osmand.map.WorldRegion.RegionParams;
-import net.osmand.plus.NotificationHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.OsmandPreference;
@@ -34,6 +27,7 @@ import net.osmand.plus.Version;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning;
import net.osmand.plus.helpers.DatabaseHelper;
+import net.osmand.plus.notifications.OsmandNotification;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.util.Algorithms;
@@ -53,7 +47,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
@SuppressLint({ "NewApi", "DefaultLocale" })
public class DownloadIndexesThread {
private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class);
- private static final int NOTIFICATION_ID = 45;
+
private OsmandApplication app;
private DownloadEvents uiActivity = null;
@@ -65,8 +59,7 @@ public class DownloadIndexesThread {
private int currentDownloadingItemProgress = 0;
private DownloadResources indexes;
- private Notification notification;
-
+
public interface DownloadEvents {
void newDownloadIndexes();
@@ -109,52 +102,8 @@ public class DownloadIndexesThread {
}
private void updateNotification() {
- if(getCurrentDownloadingItem() != null) {
- BasicProgressAsyncTask, ?, ?, ?> task = getCurrentRunningTask();
- final boolean isFinished = task == null
- || task.getStatus() == AsyncTask.Status.FINISHED;
- Intent contentIntent = new Intent(app, DownloadActivity.class);
- PendingIntent contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- app.getNotificationHelper().createNotificationChannel();
- }
- Builder bld = new NotificationCompat.Builder(app, NotificationHelper.NOTIFICATION_CHANEL_ID);
- String msg = Version.getAppName(app);
- if(!isFinished) {
- msg = task.getDescription();
- }
- StringBuilder contentText = new StringBuilder();
- List ii = getCurrentDownloadingItems();
- for (IndexItem i : ii) {
- if (!isFinished && task.getTag() == i) {
- continue;
- }
- if (contentText.length() > 0) {
- contentText.append(", ");
- }
- contentText.append(i.getVisibleName(app, app.getRegions()));
- contentText.append(" ").append(i.getType().getString(app));
- }
- bld.setContentTitle(msg).setSmallIcon(android.R.drawable.stat_sys_download)
- .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
- .setContentText(contentText.toString())
- .setContentIntent(contentPendingIntent).setOngoing(true);
- int progress = getCurrentDownloadingItemProgress();
- bld.setProgress(100, Math.max(progress, 0), progress < 0);
- notification = bld.build();
- NotificationManager mNotificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.notify(NOTIFICATION_ID, notification);
- } else {
- if(notification != null) {
- NotificationManager mNotificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.cancel(NOTIFICATION_ID);
- notification = null;
- }
- }
-
+ app.getNotificationHelper().refreshNotification(OsmandNotification.NotificationType.DOWNLOAD);
}
-
@UiThread
protected void downloadHasFinished() {
@@ -162,6 +111,9 @@ public class DownloadIndexesThread {
uiActivity.downloadHasFinished();
}
updateNotification();
+ if (app.getDownloadService() != null) {
+ app.getDownloadService().stopService(app);
+ }
}
public void initSettingsFirstMap(WorldRegion reg) {
@@ -216,6 +168,10 @@ public class DownloadIndexesThread {
return res;
}
+ public boolean isDownloading() {
+ return !indexItemDownloading.isEmpty() || currentDownloadingItem != null;
+ }
+
public boolean isDownloading(IndexItem item) {
if(item == currentDownloadingItem) {
return true;
@@ -269,6 +225,9 @@ public class DownloadIndexesThread {
indexItemDownloading.add(item);
}
}
+ if (app.getDownloadService() == null) {
+ app.startDownloadService();
+ }
if (currentDownloadingItem == null) {
execute(new DownloadIndexesAsyncTask());
} else {
diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadService.java b/OsmAnd/src/net/osmand/plus/download/DownloadService.java
new file mode 100644
index 0000000000..32b94ae2bb
--- /dev/null
+++ b/OsmAnd/src/net/osmand/plus/download/DownloadService.java
@@ -0,0 +1,61 @@
+package net.osmand.plus.download;
+
+import android.app.Notification;
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Binder;
+import android.os.IBinder;
+
+import net.osmand.plus.OsmandApplication;
+import net.osmand.plus.notifications.OsmandNotification;
+
+
+public class DownloadService extends Service {
+
+ public static class DownloadServiceBinder extends Binder {
+
+ }
+
+ private DownloadServiceBinder binder = new DownloadServiceBinder();
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return binder;
+ }
+
+ public void stopService(Context ctx) {
+ final Intent serviceIntent = new Intent(ctx, DownloadService.class);
+ ctx.stopService(serviceIntent);
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ final OsmandApplication app = (OsmandApplication) getApplication();
+ app.setDownloadService(this);
+
+ Notification notification = app.getNotificationHelper().buildDownloadNotification();
+ if (notification != null) {
+ startForeground(OsmandNotification.DOWNLOAD_NOTIFICATION_SERVICE_ID, notification);
+ app.getNotificationHelper().refreshNotification(OsmandNotification.NotificationType.DOWNLOAD);
+ }
+ return START_NOT_STICKY;
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ final OsmandApplication app = (OsmandApplication) getApplication();
+ app.setDownloadService(null);
+
+ // remove notification
+ stopForeground(Boolean.TRUE);
+ app.getNotificationHelper().refreshNotification(OsmandNotification.NotificationType.DOWNLOAD);
+ app.runInUIThread(new Runnable() {
+ @Override
+ public void run() {
+ app.getNotificationHelper().refreshNotification(OsmandNotification.NotificationType.DOWNLOAD);
+ }
+ }, 500);
+ }
+}
\ No newline at end of file
diff --git a/OsmAnd/src/net/osmand/plus/notifications/DownloadNotification.java b/OsmAnd/src/net/osmand/plus/notifications/DownloadNotification.java
new file mode 100644
index 0000000000..ca4b0a1850
--- /dev/null
+++ b/OsmAnd/src/net/osmand/plus/notifications/DownloadNotification.java
@@ -0,0 +1,106 @@
+package net.osmand.plus.notifications;
+
+import android.content.Intent;
+import android.os.AsyncTask;
+import android.support.v4.app.NotificationCompat;
+
+import net.osmand.plus.OsmandApplication;
+import net.osmand.plus.Version;
+import net.osmand.plus.base.BasicProgressAsyncTask;
+import net.osmand.plus.download.DownloadActivity;
+import net.osmand.plus.download.DownloadIndexesThread;
+import net.osmand.plus.download.DownloadService;
+import net.osmand.plus.download.IndexItem;
+
+import java.util.List;
+
+public class DownloadNotification extends OsmandNotification {
+
+ public final static String GROUP_NAME = "DOWNLOAD";
+
+ public DownloadNotification(OsmandApplication app) {
+ super(app, GROUP_NAME);
+ }
+
+ @Override
+ public void init() {
+ }
+
+ @Override
+ public NotificationType getType() {
+ return NotificationType.DOWNLOAD;
+ }
+
+ @Override
+ public int getPriority() {
+ return NotificationCompat.PRIORITY_DEFAULT;
+ }
+
+ @Override
+ public boolean isActive() {
+ DownloadService service = app.getDownloadService();
+ return isEnabled() && service != null;
+ }
+
+ @Override
+ public boolean isEnabled() {
+ DownloadIndexesThread downloadThread = app.getDownloadThread();
+ return downloadThread.isDownloading();
+ }
+
+ @Override
+ public Intent getContentIntent() {
+ return new Intent(app, DownloadActivity.class);
+ }
+
+ @Override
+ public NotificationCompat.Builder buildNotification(boolean wearable) {
+ if (!isEnabled()) {
+ return null;
+ }
+ icon = android.R.drawable.stat_sys_download;
+ ongoing = true;
+ DownloadIndexesThread downloadThread = app.getDownloadThread();
+
+ if (downloadThread.getCurrentDownloadingItem() == null) {
+ return null;
+ }
+
+ BasicProgressAsyncTask, ?, ?, ?> task = downloadThread.getCurrentRunningTask();
+ final boolean isFinished = task == null || task.getStatus() == AsyncTask.Status.FINISHED;
+
+ NotificationCompat.Builder notificationBuilder = createBuilder(wearable);
+ String msg = Version.getAppName(app);
+ if (!isFinished) {
+ msg = task.getDescription();
+ }
+ StringBuilder contentText = new StringBuilder();
+ List ii = downloadThread.getCurrentDownloadingItems();
+ for (IndexItem i : ii) {
+ if (!isFinished && task.getTag() == i) {
+ continue;
+ }
+ if (contentText.length() > 0) {
+ contentText.append(", ");
+ }
+ contentText.append(i.getVisibleName(app, app.getRegions()));
+ contentText.append(" ").append(i.getType().getString(app));
+ }
+ notificationBuilder.setContentTitle(msg)
+ .setContentText(contentText.toString())
+ .setOngoing(true);
+ int progress = downloadThread.getCurrentDownloadingItemProgress();
+ notificationBuilder.setProgress(100, Math.max(progress, 0), progress < 0);
+ return notificationBuilder;
+ }
+
+ @Override
+ public int getOsmandNotificationId() {
+ return DOWNLOAD_NOTIFICATION_SERVICE_ID;
+ }
+
+ @Override
+ public int getOsmandWearableNotificationId() {
+ return WEAR_DOWNLOAD_NOTIFICATION_SERVICE_ID;
+ }
+}
\ No newline at end of file
diff --git a/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java b/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java
index b9c5a794e4..af9a3841c9 100644
--- a/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java
+++ b/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java
@@ -1,11 +1,13 @@
package net.osmand.plus.notifications;
+import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
+import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.routing.RoutingHelper;
@@ -41,6 +43,11 @@ public class ErrorNotification extends OsmandNotification {
return true;
}
+ @Override
+ public Intent getContentIntent() {
+ return new Intent(app, MapActivity.class);
+ }
+
@Override
public NotificationCompat.Builder buildNotification(boolean wearable) {
String notificationTitle;
diff --git a/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java
index bd4c5087dc..c56b92027b 100644
--- a/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java
+++ b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java
@@ -14,6 +14,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
+import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.util.Algorithms;
@@ -97,6 +98,11 @@ public class GpxNotification extends OsmandNotification {
return OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null;
}
+ @Override
+ public Intent getContentIntent() {
+ return new Intent(app, MapActivity.class);
+ }
+
@Override
public void onNotificationDismissed() {
if (!wasNoDataDismissed) {
diff --git a/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java b/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java
index 4e981d5822..9ad611000d 100644
--- a/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java
+++ b/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java
@@ -21,6 +21,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper.TargetPoint;
+import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.routing.RouteCalculationResult;
import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo;
import net.osmand.plus.routing.RouteDirectionInfo;
@@ -112,6 +113,11 @@ public class NavigationNotification extends OsmandNotification {
|| (routingHelper.isRoutePlanningMode() && routingHelper.isPauseNavigation());
}
+ @Override
+ public Intent getContentIntent() {
+ return new Intent(app, MapActivity.class);
+ }
+
@Override
public Builder buildNotification(boolean wearable) {
if (!isEnabled()) {
diff --git a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java
index e7ef254fde..96b9b398a4 100644
--- a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java
+++ b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java
@@ -18,11 +18,13 @@ public abstract class OsmandNotification {
public final static int NAVIGATION_NOTIFICATION_SERVICE_ID = 5;
public final static int GPX_NOTIFICATION_SERVICE_ID = 6;
public final static int ERROR_NOTIFICATION_SERVICE_ID = 7;
+ public final static int DOWNLOAD_NOTIFICATION_SERVICE_ID = 8;
public final static int TOP_NOTIFICATION_SERVICE_ID = 100;
public final static int WEAR_NAVIGATION_NOTIFICATION_SERVICE_ID = 1005;
public final static int WEAR_GPX_NOTIFICATION_SERVICE_ID = 1006;
public final static int WEAR_ERROR_NOTIFICATION_SERVICE_ID = 1007;
+ public final static int WEAR_DOWNLOAD_NOTIFICATION_SERVICE_ID = 1008;
protected OsmandApplication app;
@@ -38,6 +40,7 @@ public abstract class OsmandNotification {
GPX,
GPS,
ERROR,
+ DOWNLOAD,
}
public OsmandNotification(OsmandApplication app, String groupName) {
@@ -65,7 +68,7 @@ public abstract class OsmandNotification {
@SuppressLint("InlinedApi")
protected Builder createBuilder(boolean wearable) {
- Intent contentIntent = new Intent(app, MapActivity.class);
+ Intent contentIntent = getContentIntent();
PendingIntent contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
@@ -101,6 +104,8 @@ public abstract class OsmandNotification {
public abstract boolean isEnabled();
+ public abstract Intent getContentIntent();
+
public void setupNotification(Notification notification) {
}