Create and use notification channels on Android Oreo and higher.

This commit is contained in:
Isira Seneviratne 2020-09-22 05:35:48 +05:30
parent 17ce6110c2
commit dbbedc7ab1
4 changed files with 47 additions and 11 deletions

View File

@ -24,6 +24,7 @@ import android.text.TextUtils;
import android.util.Log;
import cc.mvdan.accesspoint.WifiApControl;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.NotificationHelper;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.UpdateService;
@ -468,7 +469,7 @@ public class SwapService extends Service {
Intent intent = new Intent(this, SwapWorkflowActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return new NotificationCompat.Builder(this)
return new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_SWAPS)
.setContentTitle(getText(R.string.local_repo_running))
.setContentText(getText(R.string.touch_to_configure_local_repo))
.setSmallIcon(R.drawable.ic_nearby)

View File

@ -1,6 +1,8 @@
package org.fdroid.fdroid;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -12,28 +14,35 @@ import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.View;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import org.fdroid.fdroid.data.App;
import org.fdroid.fdroid.views.AppDetailsActivity;
import org.fdroid.fdroid.views.main.MainActivity;
import java.util.ArrayList;
import java.util.Arrays;
@SuppressWarnings("LineLength")
class NotificationHelper {
public class NotificationHelper {
public static final String CHANNEL_SWAPS = "swap-channel";
public static final String CHANNEL_INSTALLS = "install-channel";
public static final String CHANNEL_UPDATES = "update-channel";
static final String BROADCAST_NOTIFICATIONS_ALL_UPDATES_CLEARED = "org.fdroid.fdroid.installer.notifications.allupdates.cleared";
static final String BROADCAST_NOTIFICATIONS_ALL_INSTALLED_CLEARED = "org.fdroid.fdroid.installer.notifications.allinstalled.cleared";
@ -60,6 +69,25 @@ class NotificationHelper {
appUpdateStatusManager = AppUpdateStatusManager.getInstance(context);
notificationManager = NotificationManagerCompat.from(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationChannel installChannel = new NotificationChannel(CHANNEL_INSTALLS,
context.getString(R.string.notification_channel_installs_title),
NotificationManager.IMPORTANCE_LOW);
installChannel.setDescription(context.getString(R.string.notification_channel_installs_description));
final NotificationChannel swapChannel = new NotificationChannel(CHANNEL_SWAPS,
context.getString(R.string.notification_channel_swaps_title),
NotificationManager.IMPORTANCE_LOW);
swapChannel.setDescription(context.getString(R.string.notification_channel_swaps_description));
final NotificationChannel updateChannel = new NotificationChannel(CHANNEL_UPDATES,
context.getString(R.string.notification_channel_updates_title),
NotificationManager.IMPORTANCE_LOW);
updateChannel.setDescription(context.getString(R.string.notification_channel_updates_description));
notificationManager.createNotificationChannels(Arrays.asList(installChannel, swapChannel, updateChannel));
}
IntentFilter filter = new IntentFilter();
filter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_LIST_CHANGED);
filter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_ADDED);
@ -299,7 +327,7 @@ class NotificationHelper {
Bitmap iconLarge = getLargeIconForEntry(entry);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
new NotificationCompat.Builder(context, CHANNEL_UPDATES)
.setAutoCancel(true)
.setContentTitle(getSingleItemTitleString(app, status))
.setContentText(getSingleItemContentString(app, status))
@ -384,7 +412,7 @@ class NotificationHelper {
PendingIntent piAction = PendingIntent.getActivity(context, 0, intentObject, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
new NotificationCompat.Builder(context, CHANNEL_UPDATES)
.setAutoCancel(!useStackedNotifications())
.setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(context, R.color.fdroid_blue))
@ -412,7 +440,7 @@ class NotificationHelper {
Bitmap iconLarge = getLargeIconForEntry(entry);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
new NotificationCompat.Builder(context, CHANNEL_INSTALLS)
.setAutoCancel(true)
.setLargeIcon(iconLarge)
.setSmallIcon(R.drawable.ic_notification)
@ -464,7 +492,7 @@ class NotificationHelper {
PendingIntent piAction = PendingIntent.getActivity(context, 0, intentObject, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
new NotificationCompat.Builder(context, CHANNEL_INSTALLS)
.setAutoCancel(!useStackedNotifications())
.setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(context, R.color.fdroid_blue))

View File

@ -258,7 +258,7 @@ public class UpdateService extends JobIntentService {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(this)
notificationBuilder = new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_UPDATES)
.setSmallIcon(R.drawable.ic_refresh_white)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)

View File

@ -642,6 +642,13 @@ This often occurs with apps installed via Google Play or other sources, if they
<string name="notification_action_cancel">Cancel</string>
<string name="notification_action_install">Install</string>
<string name="notification_channel_installs_title">Installations</string>
<string name="notification_channel_installs_description">Displays app installation notifications.</string>
<string name="notification_channel_swaps_title">Swaps</string>
<string name="notification_channel_swaps_description">Displays P2P app swapping notifications.</string>
<string name="notification_channel_updates_title">Updates</string>
<string name="notification_channel_updates_description">Displays app and repository update notifications.</string>
<!-- Used by the TTS engine when showing a category "Chip" in the search box -->
<string name="tts_category_name">Category %1$s</string>