Add notification to download

This commit is contained in:
Victor Shcherb 2015-10-21 23:44:56 +02:00
parent b9ae86969c
commit e54bf89a86
3 changed files with 56 additions and 3 deletions

View file

@ -147,8 +147,7 @@ public class MapActivity extends AccessibleActivity {
// notification.setLatestEventInfo(this, Version.getAppName(app), getString(R.string.go_back_to_osmand), // notification.setLatestEventInfo(this, Version.getAppName(app), getString(R.string.go_back_to_osmand),
// pi); // pi);
int smallIcon = app.getSettings().getApplicationMode().getSmallIconDark(); int smallIcon = app.getSettings().getApplicationMode().getSmallIconDark();
final Builder noti = new NotificationCompat.Builder( final Builder noti = new NotificationCompat.Builder(this).setContentTitle(Version.getAppName(app))
this).setContentTitle(Version.getAppName(app))
.setContentText(getString(R.string.go_back_to_osmand)) .setContentText(getString(R.string.go_back_to_osmand))
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
// .setLargeIcon(Helpers.getBitmap(R.drawable.mirakel, getBaseContext())) // .setLargeIcon(Helpers.getBitmap(R.drawable.mirakel, getBaseContext()))

View file

@ -1,6 +1,10 @@
package net.osmand.plus.download; package net.osmand.plus.download;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -11,13 +15,14 @@ import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.NotificationCompat;
import android.support.v7.app.NotificationCompat.Builder;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -25,6 +30,7 @@ import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.LocalIndexInfo;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TabActivity; import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
@ -190,6 +196,7 @@ public class DownloadActivity extends BaseDownloadActivity {
} }
} }
@Override @Override
@UiThread @UiThread

View file

@ -26,6 +26,9 @@ import org.apache.commons.logging.Log;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -36,12 +39,15 @@ import android.os.AsyncTask.Status;
import android.os.Build; import android.os.Build;
import android.os.StatFs; import android.os.StatFs;
import android.support.annotation.UiThread; import android.support.annotation.UiThread;
import android.support.v7.app.NotificationCompat;
import android.support.v7.app.NotificationCompat.Builder;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
@SuppressLint({ "NewApi", "DefaultLocale" }) @SuppressLint({ "NewApi", "DefaultLocale" })
public class DownloadIndexesThread { public class DownloadIndexesThread {
private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class); private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class);
private static final int NOTIFICATION_ID = 45;
private final Context ctx; private final Context ctx;
private OsmandApplication app; private OsmandApplication app;
@ -54,6 +60,7 @@ public class DownloadIndexesThread {
private int currentDownloadingItemProgress = 0; private int currentDownloadingItemProgress = 0;
private DownloadResources indexes; private DownloadResources indexes;
private Notification notification;
public interface DownloadEvents { public interface DownloadEvents {
@ -88,6 +95,45 @@ public class DownloadIndexesThread {
if (uiActivity != null) { if (uiActivity != null) {
uiActivity.downloadInProgress(); uiActivity.downloadInProgress();
} }
updateNotification();
}
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);
Builder bld = new NotificationCompat.Builder(app);
String msg = Version.getAppName(app);
if(!isFinished) {
msg = task.getDescription();
}
StringBuilder contentText = new StringBuilder();
List<IndexItem> ii = getCurrentDownloadingItems();
for(IndexItem i : ii) {
if(contentText.length() > 0) {
contentText.append(", ");
}
contentText.append(i.getVisibleName(app, app.getRegions()));
}
bld.setContentTitle(msg).setSmallIcon(R.drawable.ic_action_import).
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;
}
}
} }
@ -96,6 +142,7 @@ public class DownloadIndexesThread {
if (uiActivity != null) { if (uiActivity != null) {
uiActivity.downloadHasFinished(); uiActivity.downloadHasFinished();
} }
updateNotification();
} }
@UiThread @UiThread