diff options
3 files changed, 83 insertions, 3 deletions
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 10ac2e22..73efdcde 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 @@ -3,6 +3,7 @@ package org.fox.ttrss; import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.Dialog; +import android.app.NotificationManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; @@ -150,9 +151,19 @@ public class OnlineActivity extends CommonActivity { // SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE); SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE); - + boolean isOffline = localPrefs.getBoolean("offline_mode_active", false); + if (getIntent().getExtras() != null) { + if (getIntent().getBooleanExtra("forceSwitchOffline", false)) { + + NotificationManager nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + nmgr.cancel(OfflineDownloadService.NOTIFY_DOWNLOAD_SUCCESS); + + isOffline = true; + } + } + Log.d(TAG, "m_isOffline=" + isOffline); setContentView(R.layout.activity_login); @@ -356,7 +367,8 @@ public class OnlineActivity extends CommonActivity { logout(); // setLoadingStatus(R.string.blank, false); - SharedPreferences.Editor editor = m_prefs.edit(); + SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = localPrefs.edit(); editor.putBoolean("offline_mode_active", true); editor.apply(); 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 e2bb20e1..a03dc46e 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,8 @@ public class OfflineDownloadService extends Service { private final String TAG = this.getClass().getSimpleName(); public static final int NOTIFY_DOWNLOADING = 1; + public static final int NOTIFY_DOWNLOAD_SUCCESS = 2; + public static final String INTENT_ACTION_SUCCESS = "org.fox.ttrss.intent.action.DownloadComplete"; public static final String INTENT_ACTION_CANCEL = "org.fox.ttrss.intent.action.Cancel"; @@ -122,6 +124,36 @@ public class OfflineDownloadService extends Service { m_nmgr.notify(NOTIFY_DOWNLOADING, builder.build()); } + @SuppressWarnings("deprecation") + private void notifyDownloadSuccess() { + Intent intent = new Intent(this, OnlineActivity.class); + intent.setAction(INTENT_ACTION_CANCEL); + + intent.putExtra("forceSwitchOffline", true); + + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()) + .setContentTitle(getString(R.string.dialog_offline_success)) + .setContentIntent(contentIntent) + .setWhen(System.currentTimeMillis()) + .setSmallIcon(R.drawable.ic_notification) + .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), + R.drawable.ic_launcher)) + .setOnlyAlertOnce(true); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + builder.setCategory(Notification.CATEGORY_PROGRESS) + .setVibrate(new long[0]) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setColor(0x88b0f0) + .setGroup("org.fox.ttrss"); + } + + 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); } @@ -160,10 +192,13 @@ public class OfflineDownloadService extends Service { editor.apply(); } else { + Intent intent = new Intent(); intent.setAction(INTENT_ACTION_SUCCESS); intent.addCategory(Intent.CATEGORY_DEFAULT); sendBroadcast(intent); + + notifyDownloadSuccess(); } } else { updateNotification(getString(R.string.notify_downloading_images, 0), 0, 0, true); 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 a6d1e12c..a96be7f0 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 @@ -33,6 +33,7 @@ public class ImageCacheService extends IntentService { private final String TAG = this.getClass().getSimpleName(); public static final int NOTIFY_DOWNLOADING = 1; + public static final int NOTIFY_DOWNLOAD_SUCCESS = 2; private static final String CACHE_PATH = "/image-cache/"; @@ -128,7 +129,37 @@ public class ImageCacheService extends IntentService { return null; } } - + + @SuppressWarnings("deprecation") + private void notifyDownloadSuccess() { + Intent intent = new Intent(this, OnlineActivity.class); + + intent.putExtra("forceSwitchOffline", true); + + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()) + .setContentTitle(getString(R.string.dialog_offline_success)) + .setContentIntent(contentIntent) + .setWhen(System.currentTimeMillis()) + .setSmallIcon(R.drawable.ic_notification) + .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), + R.drawable.ic_launcher)) + .setOnlyAlertOnce(true); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + builder.setCategory(Notification.CATEGORY_PROGRESS) + .setVibrate(new long[0]) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setColor(0x88b0f0) + .setGroup("org.fox.ttrss"); + } + + m_nmgr.notify(NOTIFY_DOWNLOAD_SUCCESS, builder.build()); + } + + @SuppressWarnings("deprecation") private void updateNotification(String msg, int progress, int max, boolean showProgress) { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, @@ -222,6 +253,8 @@ public class ImageCacheService extends IntentService { success.setAction(OfflineDownloadService.INTENT_ACTION_SUCCESS); success.addCategory(Intent.CATEGORY_DEFAULT); sendBroadcast(success); + + notifyDownloadSuccess(); } } |