From 119b777377ff1a8fe852e3542a2d068bdd40e372 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 2 Nov 2017 15:05:30 +0300 Subject: do not switch offline if no articles were downloaded allow closing error notification if offline switch failed --- .../main/java/org/fox/ttrss/OnlineActivity.java | 3 - .../fox/ttrss/offline/OfflineDownloadService.java | 65 +++++++++++++--------- .../java/org/fox/ttrss/util/ImageCacheService.java | 3 +- org.fox.ttrss/src/main/res/values/strings.xml | 3 +- 4 files changed, 43 insertions(+), 31 deletions(-) (limited to 'org.fox.ttrss/src/main') diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java index 2f97043f..12ae0622 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java @@ -249,9 +249,6 @@ public class OnlineActivity extends CommonActivity { logout(); // setLoadingStatus(R.string.blank, false); - NotificationManager nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); - nmgr.cancel(OfflineDownloadService.NOTIFY_DOWNLOAD_SUCCESS); - SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = localPrefs.edit(); editor.putBoolean("offline_mode_active", true); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java index 99fda9c3..6ab16bda 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java @@ -46,6 +46,9 @@ public class OfflineDownloadService extends Service { private final String TAG = this.getClass().getSimpleName(); + // enable downloading read articles in debug configuration for testing + private static boolean OFFLINE_DEBUG_READ = false; + public static final int NOTIFY_DOWNLOADING = 1; public static final int NOTIFY_DOWNLOAD_SUCCESS = 2; @@ -99,7 +102,7 @@ public class OfflineDownloadService extends Service { } @SuppressWarnings("deprecation") - private void updateNotification(String msg, int progress, int max, boolean showProgress) { + private void updateNotification(String msg, int progress, int max, boolean showProgress, boolean isError) { Intent intent = new Intent(this, OnlineActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, PI_GENERIC, @@ -113,7 +116,7 @@ public class OfflineDownloadService extends Service { .setSmallIcon(R.drawable.ic_cloud_download) .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher)) - .setOngoing(true) + .setOngoing(!isError) .setOnlyAlertOnce(true); if (showProgress) builder.setProgress(max, progress, max == 0); @@ -137,16 +140,17 @@ public class OfflineDownloadService extends Service { } @SuppressWarnings("deprecation") - private void notifyDownloadSuccess() { + private void notifyDownloadComplete() { Intent intent = new Intent(this, OnlineActivity.class); - intent.setAction(INTENT_ACTION_SWITCH_OFFLINE); + + if (m_articleOffset > 0) { + intent.setAction(INTENT_ACTION_SWITCH_OFFLINE); + } PendingIntent contentIntent = PendingIntent.getActivity(this, PI_SUCCESS, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()) - .setContentTitle(getString(R.string.dialog_offline_success)) - .setContentText(getString(R.string.offline_tap_to_switch)) .setContentIntent(contentIntent) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_notification) @@ -154,8 +158,19 @@ public class OfflineDownloadService extends Service { R.drawable.ic_launcher)) .setOnlyAlertOnce(true) .setPriority(Notification.PRIORITY_HIGH) - .setDefaults(Notification.DEFAULT_ALL); + .setDefaults(Notification.DEFAULT_ALL) + .setAutoCancel(true); + + if (m_articleOffset > 0) { + builder + .setContentTitle(getString(R.string.dialog_offline_success)) + .setContentText(getString(R.string.offline_tap_to_switch)); + } else { + builder + .setContentTitle(getString(R.string.offline_switch_failed)) + .setContentText(getString(R.string.offline_no_articles)); + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setCategory(Notification.CATEGORY_MESSAGE) @@ -168,12 +183,12 @@ public class OfflineDownloadService extends Service { m_nmgr.notify(NOTIFY_DOWNLOAD_SUCCESS, builder.build()); } - private void updateNotification(int msgResId, int progress, int max, boolean showProgress) { - updateNotification(getString(msgResId), progress, max, showProgress); + private void updateNotification(int msgResId, int progress, int max, boolean showProgress, boolean isError) { + updateNotification(getString(msgResId), progress, max, showProgress, isError); } private void downloadFailed() { - m_nmgr.cancel(NOTIFY_DOWNLOADING); + //m_nmgr.cancel(NOTIFY_DOWNLOADING); // TODO send notification to activity? @@ -212,10 +227,8 @@ public class OfflineDownloadService extends Service { intent.addCategory(Intent.CATEGORY_DEFAULT); sendBroadcast(intent);*/ - notifyDownloadSuccess(); + notifyDownloadComplete(); } - } else { - updateNotification(getString(R.string.notify_downloading_images, 0), 0, 0, true); } stopSelf(); @@ -237,7 +250,7 @@ public class OfflineDownloadService extends Service { private void downloadArticles() { Log.d(TAG, "offline: downloading articles... offset=" + m_articleOffset); - updateNotification(getString(R.string.notify_downloading_articles, m_articleOffset), m_articleOffset, m_syncMax, true); + updateNotification(getString(R.string.notify_downloading_articles, m_articleOffset), m_articleOffset, m_syncMax, true, false); OfflineArticlesRequest req = new OfflineArticlesRequest(this); @@ -248,7 +261,7 @@ public class OfflineDownloadService extends Service { put("sid", m_sessionId); put("feed_id", "-4"); - if (BuildConfig.DEBUG) { + if (BuildConfig.DEBUG && OFFLINE_DEBUG_READ) { put("view_mode", "all_articles"); } else { put("view_mode", "unread"); @@ -264,7 +277,7 @@ public class OfflineDownloadService extends Service { private void downloadFeeds() { - updateNotification(R.string.notify_downloading_feeds, 0, 0, true); + updateNotification(R.string.notify_downloading_feeds, 0, 0, true, false); getDatabase().execSQL("DELETE FROM feeds;"); @@ -302,7 +315,7 @@ public class OfflineDownloadService extends Service { getDatabase().execSQL("DELETE FROM articles;"); } catch (Exception e) { e.printStackTrace(); - updateNotification(R.string.offline_switch_error, 0, 0, false); + updateNotification(getErrorMessage(), 0, 0, false, true); downloadFailed(); } } @@ -319,7 +332,7 @@ public class OfflineDownloadService extends Service { downloadFailed(); } } else { - updateNotification(getErrorMessage(), 0, 0, false); + updateNotification(getErrorMessage(), 0, 0, false, true); downloadFailed(); } } @@ -333,7 +346,7 @@ public class OfflineDownloadService extends Service { put("sid", m_sessionId); put("cat_id", "-3"); - if (!BuildConfig.DEBUG) { + if (!BuildConfig.DEBUG && OFFLINE_DEBUG_READ) { put("unread_only", "true"); } } @@ -344,7 +357,7 @@ public class OfflineDownloadService extends Service { private void downloadCategories() { - updateNotification(R.string.notify_downloading_categories, 0, 0, true); + updateNotification(R.string.notify_downloading_categories, 0, 0, true, false); getDatabase().execSQL("DELETE FROM categories;"); @@ -374,7 +387,7 @@ public class OfflineDownloadService extends Service { } catch (Exception e) { e.printStackTrace(); - updateNotification(R.string.offline_switch_error, 0, 0, false); + updateNotification(getErrorMessage(), 0, 0, false, true); downloadFailed(); } } @@ -390,7 +403,7 @@ public class OfflineDownloadService extends Service { downloadFailed(); } } else { - updateNotification(getErrorMessage(), 0, 0, false); + updateNotification(getErrorMessage(), 0, 0, false, true); downloadFailed(); } } @@ -404,7 +417,7 @@ public class OfflineDownloadService extends Service { put("sid", m_sessionId); //put("cat_id", "-3"); - if (!BuildConfig.DEBUG) { + if (!BuildConfig.DEBUG && OFFLINE_DEBUG_READ) { put("unread_only", "true"); } } @@ -524,7 +537,7 @@ public class OfflineDownloadService extends Service { stmtInsert.close(); } catch (Exception e) { - updateNotification(R.string.offline_switch_error, 0, 0, false); + updateNotification(R.string.offline_switch_failed, 0, 0, false, true); Log.d(TAG, "offline: failed: exception when loading articles"); e.printStackTrace(); downloadFailed(); @@ -551,7 +564,7 @@ public class OfflineDownloadService extends Service { } else { Log.d(TAG, "offline: failed: " + getErrorMessage()); - updateNotification(getErrorMessage(), 0, 0, false); + updateNotification(getErrorMessage(), 0, 0, false, true); downloadFailed(); } } @@ -570,7 +583,7 @@ public class OfflineDownloadService extends Service { if (!m_downloadInProgress) { if (m_downloadImages) ImageCacheService.cleanupCache(this, false); - updateNotification(R.string.notify_downloading_init, 0, 0, true); + updateNotification(R.string.notify_downloading_init, 0, 0, true, false); m_downloadInProgress = true; downloadCategories(); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java index 97949c93..3b2f4302 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java @@ -175,7 +175,8 @@ public class ImageCacheService extends IntentService { R.drawable.ic_launcher)) .setOnlyAlertOnce(true) .setPriority(Notification.PRIORITY_HIGH) - .setDefaults(Notification.DEFAULT_ALL); + .setDefaults(Notification.DEFAULT_ALL) + .setAutoCancel(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setCategory(Notification.CATEGORY_MESSAGE) diff --git a/org.fox.ttrss/src/main/res/values/strings.xml b/org.fox.ttrss/src/main/res/values/strings.xml index 00f43b22..719936ac 100755 --- a/org.fox.ttrss/src/main/res/values/strings.xml +++ b/org.fox.ttrss/src/main/res/values/strings.xml @@ -72,7 +72,7 @@ Error: invalid API URL Go offline Go online - Failed to prepare offline mode (see log) + Failed to prepare offline mode Login failed, but you have stored offline data. Would you like to go offline? Offline mode is ready @@ -258,4 +258,5 @@ Reload Try again Tap to switch offline + No articles downloaded -- cgit v1.2.3