use NotificationCompat to get rid of some version checks

pull/149/head
Unpublished 6 years ago
parent 0edc7a8629
commit 7f66558566

@ -4,11 +4,11 @@ import android.app.Notification;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
public class ServiceExternal extends Service { public class ServiceExternal extends Service {
private final static String ACTION_ENABLE = "eu.faircode.email.ENABLE"; private final static String ACTION_ENABLE = "eu.faircode.email.ENABLE";
@ -61,12 +61,8 @@ public class ServiceExternal extends Service {
return null; return null;
} }
private Notification.Builder getNotification() { private NotificationCompat.Builder getNotification() {
Notification.Builder builder; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
builder = new Notification.Builder(this, "service");
else
builder = new Notification.Builder(this);
builder builder
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24) .setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
@ -75,7 +71,7 @@ public class ServiceExternal extends Service {
.setShowWhen(false) .setShowWhen(false)
.setPriority(Notification.PRIORITY_MIN) .setPriority(Notification.PRIORITY_MIN)
.setCategory(Notification.CATEGORY_STATUS) .setCategory(Notification.CATEGORY_STATUS)
.setVisibility(Notification.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET);
return builder; return builder;
} }

@ -23,7 +23,6 @@ import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Person;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -132,6 +131,7 @@ import javax.net.ssl.SSLException;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleService; import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
@ -449,7 +449,7 @@ public class ServiceSynchronize extends LifecycleService {
return START_STICKY; return START_STICKY;
} }
private Notification.Builder getNotificationService(TupleAccountStats stats) { private NotificationCompat.Builder getNotificationService(TupleAccountStats stats) {
if (stats == null) if (stats == null)
stats = lastStats; stats = lastStats;
if (stats == null) if (stats == null)
@ -461,11 +461,7 @@ public class ServiceSynchronize extends LifecycleService {
PendingIntent pi = PendingIntent.getService(this, PI_WHY, intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pi = PendingIntent.getService(this, PI_WHY, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build notification // Build notification
Notification.Builder builder; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
builder = new Notification.Builder(this, "service");
else
builder = new Notification.Builder(this);
builder builder
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24) .setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
@ -476,10 +472,10 @@ public class ServiceSynchronize extends LifecycleService {
.setShowWhen(false) .setShowWhen(false)
.setPriority(Notification.PRIORITY_MIN) .setPriority(Notification.PRIORITY_MIN)
.setCategory(Notification.CATEGORY_STATUS) .setCategory(Notification.CATEGORY_STATUS)
.setVisibility(Notification.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET);
if (stats.operations > 0) if (stats.operations > 0)
builder.setStyle(new Notification.BigTextStyle().setSummaryText( builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(
getResources().getQuantityString( getResources().getQuantityString(
R.plurals.title_notification_operations, stats.operations, stats.operations))); R.plurals.title_notification_operations, stats.operations, stats.operations)));
@ -524,11 +520,7 @@ public class ServiceSynchronize extends LifecycleService {
String channelName = (account == 0 ? "notification" : EntityAccount.getNotificationChannelName(account)); String channelName = (account == 0 ? "notification" : EntityAccount.getNotificationChannelName(account));
// Build public notification // Build public notification
Notification.Builder pbuilder; NotificationCompat.Builder pbuilder = new NotificationCompat.Builder(this, channelName);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
pbuilder = new Notification.Builder(this);
else
pbuilder = new Notification.Builder(this, channelName);
pbuilder pbuilder
.setSmallIcon(R.drawable.baseline_email_white_24) .setSmallIcon(R.drawable.baseline_email_white_24)
@ -539,17 +531,13 @@ public class ServiceSynchronize extends LifecycleService {
.setDeleteIntent(piClear) .setDeleteIntent(piClear)
.setPriority(Notification.PRIORITY_DEFAULT) .setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_STATUS) .setCategory(Notification.CATEGORY_STATUS)
.setVisibility(Notification.VISIBILITY_PUBLIC); .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (!TextUtils.isEmpty(accountName)) if (!TextUtils.isEmpty(accountName))
pbuilder.setSubText(accountName); pbuilder.setSubText(accountName);
// Build notification // Build notification
Notification.Builder builder; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelName);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
builder = new Notification.Builder(this);
else
builder = new Notification.Builder(this, channelName);
builder builder
.setSmallIcon(R.drawable.baseline_email_white_24) .setSmallIcon(R.drawable.baseline_email_white_24)
@ -583,7 +571,7 @@ public class ServiceSynchronize extends LifecycleService {
builder.setOnlyAlertOnce(true); builder.setOnlyAlertOnce(true);
} else } else
builder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN); builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
if (pro) { if (pro) {
DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT); DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
@ -596,7 +584,7 @@ public class ServiceSynchronize extends LifecycleService {
sb.append("<br>"); sb.append("<br>");
} }
builder.setStyle(new Notification.BigTextStyle() builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(HtmlHelper.fromHtml(sb.toString())) .bigText(HtmlHelper.fromHtml(sb.toString()))
.setSummaryText(title)); .setSummaryText(title));
} }
@ -635,26 +623,23 @@ public class ServiceSynchronize extends LifecycleService {
PendingIntent piTrash = PendingIntent.getService(this, PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piTrash = PendingIntent.getService(this, PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action.Builder actionSeen = new Notification.Action.Builder( NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder(
R.drawable.baseline_visibility_24, R.drawable.baseline_visibility_24,
getString(R.string.title_action_seen), getString(R.string.title_action_seen),
piSeen); piSeen);
Notification.Action.Builder actionArchive = new Notification.Action.Builder( NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder(
R.drawable.baseline_archive_24, R.drawable.baseline_archive_24,
getString(R.string.title_action_archive), getString(R.string.title_action_archive),
piArchive); piArchive);
Notification.Action.Builder actionTrash = new Notification.Action.Builder( NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder(
R.drawable.baseline_delete_24, R.drawable.baseline_delete_24,
getString(R.string.title_action_trash), getString(R.string.title_action_trash),
piTrash); piTrash);
Notification.Builder mbuilder; NotificationCompat.Builder mbuilder;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) mbuilder = new NotificationCompat.Builder(this, channelName);
mbuilder = new Notification.Builder(this);
else
mbuilder = new Notification.Builder(this, channelName);
String folderName = message.folderDisplay == null String folderName = message.folderDisplay == null
? Helper.localizeFolderName(this, message.folderName) ? Helper.localizeFolderName(this, message.folderName)
@ -701,32 +686,25 @@ public class ServiceSynchronize extends LifecycleService {
sb.append(text); sb.append(text);
sb.append("</em>"); sb.append("</em>");
} }
mbuilder.setStyle(new Notification.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString()))); mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString())));
} catch (IOException ex) { } catch (IOException ex) {
Log.e(ex); Log.e(ex);
mbuilder.setStyle(new Notification.BigTextStyle().bigText(ex.toString())); mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString()));
} }
if (info.hasPhoto()) if (info.hasPhoto())
mbuilder.setLargeIcon(info.getPhotoBitmap()); mbuilder.setLargeIcon(info.getPhotoBitmap());
if (info.hasLookupUri()) if (info.hasLookupUri())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) mbuilder.addPerson(info.getLookupUri().toString());
mbuilder.addPerson(new Person.Builder()
.setUri(info.getLookupUri().toString())
.build());
else
mbuilder.addPerson(info.getLookupUri().toString());
if (message.accountColor != null) { if (message.accountColor != null) {
mbuilder.setColor(message.accountColor); mbuilder.setColor(message.accountColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) mbuilder.setColorized(true);
mbuilder.setColorized(true);
} }
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) mbuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
mbuilder.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
notifications.add(mbuilder.build()); notifications.add(mbuilder.build());
} }
@ -734,11 +712,11 @@ public class ServiceSynchronize extends LifecycleService {
return notifications; return notifications;
} }
private Notification.Builder getNotificationError(String title, Throwable ex) { private NotificationCompat.Builder getNotificationError(String title, Throwable ex) {
return getNotificationError("error", title, ex, true); return getNotificationError("error", title, ex, true);
} }
private Notification.Builder getNotificationError(String channel, String title, Throwable ex, boolean debug) { private NotificationCompat.Builder getNotificationError(String channel, String title, Throwable ex, boolean debug) {
// Build pending intent // Build pending intent
Intent intent = new Intent(this, ActivitySetup.class); Intent intent = new Intent(this, ActivitySetup.class);
if (debug) if (debug)
@ -748,11 +726,7 @@ public class ServiceSynchronize extends LifecycleService {
this, ActivitySetup.REQUEST_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT); this, ActivitySetup.REQUEST_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build notification // Build notification
Notification.Builder builder; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
builder = new Notification.Builder(this, channel);
else
builder = new Notification.Builder(this);
builder builder
.setSmallIcon(R.drawable.baseline_warning_white_24) .setSmallIcon(R.drawable.baseline_warning_white_24)
@ -764,9 +738,9 @@ public class ServiceSynchronize extends LifecycleService {
.setPriority(Notification.PRIORITY_MAX) .setPriority(Notification.PRIORITY_MAX)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.setCategory(Notification.CATEGORY_ERROR) .setCategory(Notification.CATEGORY_ERROR)
.setVisibility(Notification.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET);
builder.setStyle(new Notification.BigTextStyle() builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Helper.formatThrowable(ex, false, "\n"))); .bigText(Helper.formatThrowable(ex, false, "\n")));
return builder; return builder;

Loading…
Cancel
Save