improve unread notification (fix #645, fix #630)

This commit is contained in:
David Luhmer 2018-08-19 11:53:13 +02:00
parent d0fbb2f37f
commit 4c1351d62d
4 changed files with 75 additions and 47 deletions

View file

@ -38,6 +38,7 @@ import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm; import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.database.model.RssItem; import de.luhmer.owncloudnewsreader.database.model.RssItem;
import de.luhmer.owncloudnewsreader.di.ApiProvider; import de.luhmer.owncloudnewsreader.di.ApiProvider;
@ -50,10 +51,13 @@ import de.luhmer.owncloudnewsreader.helper.SizeAnimator;
import de.luhmer.owncloudnewsreader.interfaces.IPlayPausePodcastClicked; import de.luhmer.owncloudnewsreader.interfaces.IPlayPausePodcastClicked;
import de.luhmer.owncloudnewsreader.model.MediaItem; import de.luhmer.owncloudnewsreader.model.MediaItem;
import de.luhmer.owncloudnewsreader.model.PodcastItem; import de.luhmer.owncloudnewsreader.model.PodcastItem;
import de.luhmer.owncloudnewsreader.notification.NextcloudNotificationManager;
import de.luhmer.owncloudnewsreader.services.OwnCloudSyncService;
import de.luhmer.owncloudnewsreader.services.PodcastDownloadService; import de.luhmer.owncloudnewsreader.services.PodcastDownloadService;
import de.luhmer.owncloudnewsreader.services.PodcastPlaybackService; import de.luhmer.owncloudnewsreader.services.PodcastPlaybackService;
import de.luhmer.owncloudnewsreader.services.podcast.PlaybackService; import de.luhmer.owncloudnewsreader.services.podcast.PlaybackService;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager; import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import de.luhmer.owncloudnewsreader.view.PodcastNotification;
import de.luhmer.owncloudnewsreader.view.PodcastSlidingUpPanelLayout; import de.luhmer.owncloudnewsreader.view.PodcastSlidingUpPanelLayout;
import de.luhmer.owncloudnewsreader.view.ZoomableRelativeLayout; import de.luhmer.owncloudnewsreader.view.ZoomableRelativeLayout;
import de.luhmer.owncloudnewsreader.widget.WidgetProvider; import de.luhmer.owncloudnewsreader.widget.WidgetProvider;
@ -201,9 +205,22 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
WidgetProvider.UpdateWidget(this); WidgetProvider.UpdateWidget(this);
if(NextcloudNotificationManager.IsUnreadRssCountNotificationVisible(this)) {
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(this);
int count = Integer.parseInt(dbConn.getUnreadItemsCountForSpecificFolder(SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS));
NextcloudNotificationManager.ShowUnreadRssItemsNotification(this, count);
if(count == 0) {
NextcloudNotificationManager.RemoveRssItemsNotification(this);
}
}
super.onPause(); super.onPause();
} }
public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) { public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

View file

@ -12,29 +12,11 @@ import de.luhmer.owncloudnewsreader.R;
public class NotificationManagerNewsReader { public class NotificationManagerNewsReader {
private static NotificationManagerNewsReader instance; private static final int NOTIFICATION_ID = 0;
private final int NOTIFICATION_ID = 0; private static final String CHANNEL_ID = "0";
private final String CHANNEL_ID = "0";
private Context context;
public synchronized static NotificationManagerNewsReader getInstance(Context context)
{
if(instance == null)
instance = new NotificationManagerNewsReader(context);
return instance;
}
private NotificationManagerNewsReader(Context context)
{
this.context = context;
//NOTIFICATION_ID = new Random().nextInt();
//NOTIFICATION_ID = new Random().nextInt();
}
public static void ShowUnreadRssItemsNotification(Context context, String title, String tickerMessage, String message) {
public void ShowMessage(String title, String tickerMessage, String message)
{
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, "") new NotificationCompat.Builder(context, "")
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
@ -45,21 +27,6 @@ public class NotificationManagerNewsReader {
.setContentText(message); .setContentText(message);
/*
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("http://android-er.blogspot.com/")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
*/
Intent notificationIntent = new Intent(context, NewsReaderListActivity.class); Intent notificationIntent = new Intent(context, NewsReaderListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent); builder.setContentIntent(contentIntent);
@ -79,7 +46,7 @@ public class NotificationManagerNewsReader {
} }
// Remove notification // Remove notification
public void RemoveNotification() { public void RemoveNotification(Context context) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID); manager.cancel(NOTIFICATION_ID);
} }

View file

@ -6,8 +6,11 @@ import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider; import android.support.v4.content.FileProvider;
@ -19,10 +22,13 @@ import java.io.File;
import de.luhmer.owncloudnewsreader.BuildConfig; import de.luhmer.owncloudnewsreader.BuildConfig;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity; import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R; import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.services.OwnCloudSyncService;
public class NextcloudNotificationManager { public class NextcloudNotificationManager {
private static final int ID_DownloadSingleImageComplete = 10; private static final int ID_DownloadSingleImageComplete = 10;
private static final int UNREAD_RSS_ITEMS_NOTIFICATION_ID = 246;
public static void ShowNotificationDownloadSingleImageComplete(Context context, File imagePath) { public static void ShowNotificationDownloadSingleImageComplete(Context context, File imagePath) {
String channelDownloadImage = context.getString(R.string.action_img_download); String channelDownloadImage = context.getString(R.string.action_img_download);
@ -136,6 +142,47 @@ public class NextcloudNotificationManager {
public static void ShowUnreadRssItemsNotification(Context context, int newItemsCount) {
Resources res = context.getResources();
String tickerMessage = res.getQuantityString(R.plurals.notification_new_items_ticker, newItemsCount, newItemsCount);
String contentText = res.getQuantityString(R.plurals.notification_new_items_text, newItemsCount, newItemsCount);
String title = context.getString(R.string.app_name);
String channelId = context.getString(R.string.app_name);
NotificationManager notificationManager = getNotificationManagerAndCreateChannel(context, channelId);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setTicker(tickerMessage)
.setContentTitle(title)
//.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentText(contentText);
Intent notificationIntent = new Intent(context, NewsReaderListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, UNREAD_RSS_ITEMS_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
notificationManager.notify(UNREAD_RSS_ITEMS_NOTIFICATION_ID, builder.build());
}
public static boolean IsUnreadRssCountNotificationVisible(Context context) {
Intent notificationIntent = new Intent(context, OwnCloudSyncService.class);
PendingIntent test = PendingIntent.getActivity(context, UNREAD_RSS_ITEMS_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return test != null;
}
public static void RemoveRssItemsNotification(Context context) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(UNREAD_RSS_ITEMS_NOTIFICATION_ID);
}

View file

@ -52,6 +52,7 @@ import de.luhmer.owncloudnewsreader.database.model.Feed;
import de.luhmer.owncloudnewsreader.database.model.Folder; import de.luhmer.owncloudnewsreader.database.model.Folder;
import de.luhmer.owncloudnewsreader.di.ApiProvider; import de.luhmer.owncloudnewsreader.di.ApiProvider;
import de.luhmer.owncloudnewsreader.helper.NotificationManagerNewsReader; import de.luhmer.owncloudnewsreader.helper.NotificationManagerNewsReader;
import de.luhmer.owncloudnewsreader.notification.NextcloudNotificationManager;
import de.luhmer.owncloudnewsreader.reader.InsertIntoDatabase; import de.luhmer.owncloudnewsreader.reader.InsertIntoDatabase;
import de.luhmer.owncloudnewsreader.reader.nextcloud.ItemStateSync; import de.luhmer.owncloudnewsreader.reader.nextcloud.ItemStateSync;
import de.luhmer.owncloudnewsreader.reader.nextcloud.RssItemObservable; import de.luhmer.owncloudnewsreader.reader.nextcloud.RssItemObservable;
@ -299,13 +300,9 @@ public class OwnCloudSyncService extends Service {
// If another app is opened show a notification // If another app is opened show a notification
if (!foregroundActivityPackageName.equals(getPackageName())) { if (!foregroundActivityPackageName.equals(getPackageName())) {
Resources res = getResources(); if (mPrefs.getBoolean(SettingsActivity.CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING, true)) {
String tickerText = res.getQuantityString(R.plurals.notification_new_items_ticker, newItemsCount, newItemsCount); NextcloudNotificationManager.ShowUnreadRssItemsNotification(OwnCloudSyncService.this, newItemsCount);
String contentText = res.getQuantityString(R.plurals.notification_new_items_text, newItemsCount, newItemsCount); }
String title = getString(R.string.app_name);
if (mPrefs.getBoolean(SettingsActivity.CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING, true))//Default is true
NotificationManagerNewsReader.getInstance(OwnCloudSyncService.this).ShowMessage(title, tickerText, contentText);
} }
} }