Browse Source

Adds notification channels (for Android 8+). Fixes #468.

pull/487/head
Eric Kok 6 years ago
parent
commit
08e3bea0d5
  1. 1
      app/src/main/java/org/transdroid/core/service/AppUpdateJob.java
  2. 17
      app/src/main/java/org/transdroid/core/service/AppUpdateJobRunner.java
  3. 45
      app/src/main/java/org/transdroid/core/service/NotificationChannels.java
  4. 1
      app/src/main/java/org/transdroid/core/service/RssCheckerJob.java
  5. 17
      app/src/main/java/org/transdroid/core/service/RssCheckerJobRunner.java
  6. 1
      app/src/main/java/org/transdroid/core/service/ServerCheckerJob.java
  7. 21
      app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java

1
app/src/main/java/org/transdroid/core/service/AppUpdateJob.java

@ -39,6 +39,7 @@ public class AppUpdateJob extends Job { @@ -39,6 +39,7 @@ public class AppUpdateJob extends Job {
NavigationHelper_ navigationHelper = NavigationHelper_.getInstance_(context);
if (systemSettings.checkForUpdates() && navigationHelper.enableUpdateChecker()) {
Log_.getInstance_(context).d(TAG, "Schedule app update checker job");
NotificationChannels.ensureAppUpdateChannel(context);
scheduledJobId = new JobRequest.Builder(AppUpdateJob.TAG)
.setPeriodic(TimeUnit.DAYS.toMillis(1))
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)

17
app/src/main/java/org/transdroid/core/service/AppUpdateJobRunner.java

@ -16,7 +16,6 @@ @@ -16,7 +16,6 @@
*/
package org.transdroid.core.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
@ -24,7 +23,10 @@ import android.content.Intent; @@ -24,7 +23,10 @@ import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.evernote.android.job.Job;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
@ -170,11 +172,16 @@ public class AppUpdateJobRunner { @@ -170,11 +172,16 @@ public class AppUpdateJobRunner {
private void newNotification(String ticker, String title, String text, String downloadUrl, int notifyID) {
PendingIntent pi = PendingIntent.getActivity(context, notifyID,
new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl)), PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(context).setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(ticker).setContentTitle(title).setContentText(text)
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.CHANNEL_APP_UPDATE)
.setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(ticker)
.setContentTitle(title)
.setContentText(text)
.setLights(notificationSettings.getDesiredLedColour(), 600, 1000)
.setSound(notificationSettings.getSound()).setAutoCancel(true).setContentIntent(pi);
notificationManager.notify(notifyID, builder.getNotification());
.setSound(notificationSettings.getSound())
.setAutoCancel(true)
.setContentIntent(pi);
notificationManager.notify(notifyID, builder.build());
}
}

45
app/src/main/java/org/transdroid/core/service/NotificationChannels.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
package org.transdroid.core.service;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import org.transdroid.R;
class NotificationChannels {
public static final String CHANNEL_APP_UPDATE = "channel_app_update";
public static final String CHANNEL_RSS_CHECKER = "channel_rss_checker";
public static final String CHANNEL_SERVER_CHECKER = "channel_server_checker";
static void ensureAppUpdateChannel(final Context context) {
createChannel(context, CHANNEL_APP_UPDATE, R.string.pref_checkupdates, R.string.pref_checkupdates_info);
}
static void ensureRssCheckerChannel(final Context context) {
createChannel(context, CHANNEL_RSS_CHECKER, R.string.pref_notifications_rss, null);
}
static void ensureServerCheckerChannel(final Context context) {
createChannel(context, CHANNEL_SERVER_CHECKER, R.string.pref_notifications_torrent, null);
}
private static void createChannel(
final Context context,
final String channelId,
final int name,
final Integer description) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
final NotificationChannel channel = new NotificationChannel(channelId, context.getString(name), importance);
if (description != null) {
channel.setDescription(context.getString(description));
}
final NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}

1
app/src/main/java/org/transdroid/core/service/RssCheckerJob.java

@ -40,6 +40,7 @@ public class RssCheckerJob extends Job { @@ -40,6 +40,7 @@ public class RssCheckerJob extends Job {
NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context);
if (notificationSettings.isEnabledForRss()) {
Log_.getInstance_(context).d(TAG, "Schedule rss checker job");
NotificationChannels.ensureRssCheckerChannel(context);
scheduledJobId = new JobRequest.Builder(RssCheckerJob.TAG)
.setPeriodic(notificationSettings.getInvervalInMilliseconds())
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)

17
app/src/main/java/org/transdroid/core/service/RssCheckerJobRunner.java

@ -16,12 +16,14 @@ @@ -16,12 +16,14 @@
*/
package org.transdroid.core.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.evernote.android.job.Job;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
@ -122,14 +124,19 @@ public class RssCheckerJobRunner { @@ -122,14 +124,19 @@ public class RssCheckerJobRunner {
.getActivity(context, 80000, new Intent(context, RssfeedsActivity_.class), PendingIntent.FLAG_UPDATE_CURRENT);
String title = context.getResources().getQuantityString(R.plurals.rss_service_new, unread, Integer.toString(unread));
String forString = Collections2.joinString(hasUnread, ", ");
Notification.Builder builder = new Notification.Builder(context).setSmallIcon(R.drawable.ic_stat_notification).setTicker(title)
.setContentTitle(title).setContentText(context.getString(R.string.rss_service_newfor, forString))
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.CHANNEL_RSS_CHECKER)
.setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(title)
.setContentTitle(title)
.setContentText(context.getString(R.string.rss_service_newfor, forString))
.setNumber(unread).setLights(notificationSettings.getDesiredLedColour(), 600, 1000)
.setSound(notificationSettings.getSound()).setAutoCancel(true).setContentIntent(pi);
.setSound(notificationSettings.getSound())
.setAutoCancel(true)
.setContentIntent(pi);
if (notificationSettings.shouldVibrate()) {
builder.setVibrate(notificationSettings.getDefaultVibratePattern());
}
notificationManager.notify(80001, builder.getNotification());
notificationManager.notify(80001, builder.build());
return Job.Result.SUCCESS;
}

1
app/src/main/java/org/transdroid/core/service/ServerCheckerJob.java

@ -35,6 +35,7 @@ public class ServerCheckerJob extends Job { @@ -35,6 +35,7 @@ public class ServerCheckerJob extends Job {
NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context);
if (notificationSettings.isEnabledForTorrents()) {
Log_.getInstance_(context).d(TAG, "Schedule server checker job");
NotificationChannels.ensureServerCheckerChannel(context);
scheduledJobId = new JobRequest.Builder(ServerCheckerJob.TAG)
.setPeriodic(notificationSettings.getInvervalInMilliseconds())
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)

21
app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java

@ -16,11 +16,11 @@ @@ -16,11 +16,11 @@
*/
package org.transdroid.core.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.evernote.android.job.Job;
import org.androidannotations.annotations.Bean;
@ -175,18 +175,21 @@ public class ServerCheckerJobRunner { @@ -175,18 +175,21 @@ public class ServerCheckerJobRunner {
forString = forString.substring(0, forString.length() - 2);
// Build the basic notification
Notification.Builder builder = new Notification.Builder(context).setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(title).setContentTitle(title).setContentText(forString)
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.CHANNEL_SERVER_CHECKER)
.setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(title)
.setContentTitle(title).setContentText(forString)
.setNumber(affectedTorrents.size())
.setLights(notificationSettings.getDesiredLedColour(), 600, 1000)
.setSound(notificationSettings.getSound()).setAutoCancel(true).setContentIntent(pi);
.setSound(notificationSettings.getSound())
.setAutoCancel(true)
.setContentIntent(pi);
if (notificationSettings.shouldVibrate())
builder.setVibrate(notificationSettings.getDefaultVibratePattern());
// Add at most 5 lines with the affected torrents
Notification notification;
if (android.os.Build.VERSION.SDK_INT >= 16) {
Notification.InboxStyle inbox = new Notification.InboxStyle(builder);
final NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(builder);
if (affectedTorrents.size() < 6) {
for (Torrent affectedTorrent : affectedTorrents) {
inbox.addLine(affectedTorrent.getName());
@ -197,11 +200,9 @@ public class ServerCheckerJobRunner { @@ -197,11 +200,9 @@ public class ServerCheckerJobRunner {
}
inbox.addLine(context.getString(R.string.status_service_andothers, affectedTorrents.get(5).getName()));
}
notification = inbox.build();
} else {
notification = builder.getNotification();
builder.setStyle(inbox);
}
notificationManager.notify(notifyBase + server.getOrder(), notification);
notificationManager.notify(notifyBase + server.getOrder(), builder.build());
}

Loading…
Cancel
Save