better download progress notification, prevent notification spam

This commit is contained in:
Andrew Dolgov 2011-12-07 18:02:43 +03:00
parent 803afcc6d2
commit 9951991e56
4 changed files with 35 additions and 4 deletions

View File

@ -103,7 +103,7 @@
<string name="dialog_cancel">Cancel</string> <string name="dialog_cancel">Cancel</string>
<string name="syncing_offline_data">Synchronizing offline data...</string> <string name="syncing_offline_data">Synchronizing offline data...</string>
<string name="dialog_offline_switch_prompt">Download unread articles and go offline?</string> <string name="dialog_offline_switch_prompt">Download unread articles and go offline?</string>
<string name="notify_downloading_articles">Downloading articles...</string> <string name="notify_downloading_articles">Downloading articles (%1$d)...</string>
<string name="notify_downloading_init">Starting download...</string> <string name="notify_downloading_init">Starting download...</string>
<string name="notify_downloading_feeds">Downloading feeds...</string> <string name="notify_downloading_feeds">Downloading feeds...</string>
<string name="notify_uploading_sending_data">Sending data to server...</string> <string name="notify_uploading_sending_data">Sending data to server...</string>
@ -113,4 +113,5 @@
<string name="offline_mode">Offline mode</string> <string name="offline_mode">Offline mode</string>
<string name="offline_image_cache_enabled">Cache images</string> <string name="offline_image_cache_enabled">Cache images</string>
<string name="offline_image_cache_enabled_summary">Download images to sdcard. This might significantly increase time it takes to go offline.</string> <string name="offline_image_cache_enabled_summary">Download images to sdcard. This might significantly increase time it takes to go offline.</string>
<string name="notify_downloading_images">Downloading images (%1$d)...</string>
</resources> </resources>

View File

@ -12,7 +12,9 @@ import java.util.Date;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.IntentService; import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo; import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent; import android.content.Intent;
import android.os.Environment; import android.os.Environment;
@ -117,6 +119,26 @@ public class ImageCacheService extends IntentService {
return null; return null;
} }
} }
private void updateNotification(String msg) {
Notification notification = new Notification(R.drawable.icon,
getString(R.string.notify_downloading_title), System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.setLatestEventInfo(this, getString(R.string.notify_downloading_title), msg, contentIntent);
m_nmgr.notify(NOTIFY_DOWNLOADING, notification);
}
private void updateNotification(int msgResId) {
updateNotification(getString(msgResId));
}
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
String url = intent.getStringExtra("url"); String url = intent.getStringExtra("url");
@ -156,6 +178,8 @@ public class ImageCacheService extends IntentService {
m_imagesDownloaded++; m_imagesDownloaded++;
updateNotification(getString(R.string.notify_downloading_images, m_imagesDownloaded));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -75,6 +75,9 @@ public class OfflineDownloadService extends IntentService {
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0); new Intent(this, MainActivity.class), 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.setLatestEventInfo(this, getString(R.string.notify_downloading_title), msg, contentIntent); notification.setLatestEventInfo(this, getString(R.string.notify_downloading_title), msg, contentIntent);
m_nmgr.notify(NOTIFY_DOWNLOADING, notification); m_nmgr.notify(NOTIFY_DOWNLOADING, notification);
@ -115,7 +118,7 @@ public class OfflineDownloadService extends IntentService {
intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(intent); sendBroadcast(intent);
} else { } else {
updateNotification("Downloading images..."); updateNotification(getString(R.string.notify_downloading_images, 0));
} }
m_readableDb.close(); m_readableDb.close();
@ -140,6 +143,8 @@ public class OfflineDownloadService extends IntentService {
private void downloadArticles() { private void downloadArticles() {
Log.d(TAG, "offline: downloading articles... offset=" + m_articleOffset); Log.d(TAG, "offline: downloading articles... offset=" + m_articleOffset);
updateNotification(getString(R.string.notify_downloading_articles, m_articleOffset));
OfflineArticlesRequest req = new OfflineArticlesRequest(this); OfflineArticlesRequest req = new OfflineArticlesRequest(this);
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -195,8 +200,6 @@ public class OfflineDownloadService extends IntentService {
getWritableDb().execSQL("DELETE FROM articles;"); getWritableDb().execSQL("DELETE FROM articles;");
updateNotification(R.string.notify_downloading_articles);
downloadArticles(); downloadArticles();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -50,6 +50,9 @@ public class OfflineUploadService extends IntentService {
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0); new Intent(this, MainActivity.class), 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.setLatestEventInfo(this, getString(R.string.notify_uploading_title), msg, contentIntent); notification.setLatestEventInfo(this, getString(R.string.notify_uploading_title), msg, contentIntent);
m_nmgr.notify(NOTIFY_UPLOADING, notification); m_nmgr.notify(NOTIFY_UPLOADING, notification);