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

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

Loading…
Cancel
Save