move ongoing notification to extra class

This commit is contained in:
Milos Kozak 2020-04-30 17:56:40 +02:00
parent 18820441a4
commit ddd58c37a3
6 changed files with 65 additions and 72 deletions

View file

@ -1,21 +1,13 @@
package info.nightscout.androidaps; package info.nightscout.androidaps;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import androidx.annotation.ColorRes;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.crashlytics.android.Crashlytics; import com.crashlytics.android.Crashlytics;
@ -68,10 +60,6 @@ public class MainApp extends DaggerApplication {
static DatabaseHelper sDatabaseHelper = null; static DatabaseHelper sDatabaseHelper = null;
private String CHANNEL_ID = "AndroidAPS-Ongoing"; // TODO: move to OngoingNotificationProvider (and dagger)
private int ONGOING_NOTIFICATION_ID = 4711; // TODO: move to OngoingNotificationProvider (and dagger)
private Notification notification; // TODO: move to OngoingNotificationProvider (and dagger)
@Inject PluginStore pluginStore; @Inject PluginStore pluginStore;
@Inject public HasAndroidInjector injector; @Inject public HasAndroidInjector injector;
@Inject AAPSLogger aapsLogger; @Inject AAPSLogger aapsLogger;
@ -95,7 +83,6 @@ public class MainApp extends DaggerApplication {
sInstance = this; sInstance = this;
sResources = getResources(); sResources = getResources();
LocaleHelper.INSTANCE.update(this); LocaleHelper.INSTANCE.update(this);
generateEmptyNotification();
sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class); sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class);
Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> { Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
@ -230,42 +217,6 @@ public class MainApp extends DaggerApplication {
return firebaseAnalytics; return firebaseAnalytics;
} }
// global Notification has been moved to MainApp because PersistentNotificationPlugin is initialized too late
private void generateEmptyNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setSmallIcon(resourceHelper.getNotificationIcon())
.setLargeIcon(resourceHelper.decodeResource(resourceHelper.getIcon()));
builder.setContentTitle(resourceHelper.gs(R.string.loading));
Intent resultIntent = new Intent(this, MainApp.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = builder.build();
mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification);
}
public int notificationId() {
return ONGOING_NOTIFICATION_ID;
}
public String channelId() {
return CHANNEL_ID;
}
public void setNotification(Notification notification) {
this.notification = notification;
}
public Notification getNotification() {
return notification;
}
@Override @Override
public void onTerminate() { public void onTerminate() {
aapsLogger.debug(LTag.CORE, "onTerminate"); aapsLogger.debug(LTag.CORE, "onTerminate");

View file

@ -4,12 +4,12 @@ import android.app.Service
import android.content.Intent import android.content.Intent
import android.os.IBinder import android.os.IBinder
import dagger.android.DaggerService import dagger.android.DaggerService
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.events.EventAppExit import info.nightscout.androidaps.events.EventAppExit
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.androidNotification.NotificationHolder
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import javax.inject.Inject import javax.inject.Inject
@ -21,8 +21,8 @@ class DummyService : DaggerService() {
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var mainApp: MainApp
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var notificationHolder: NotificationHolder
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
@ -32,14 +32,14 @@ class DummyService : DaggerService() {
super.onCreate() super.onCreate()
// TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service? // TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service?
// As onCreate() is not called every time a service is started, copied to onStartCommand(). // As onCreate() is not called every time a service is started, copied to onStartCommand().
startForeground(mainApp.notificationId(), mainApp.notification) startForeground(notificationHolder.notificationID, notificationHolder.notification)
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventAppExit::class.java) .toObservable(EventAppExit::class.java)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.subscribe({ .subscribe({
aapsLogger.debug(LTag.CORE, "EventAppExit received") aapsLogger.debug(LTag.CORE, "EventAppExit received")
stopSelf() stopSelf()
}) { fabricPrivacy.logException(it) } }) { fabricPrivacy::logException }
) )
} }
@ -52,7 +52,7 @@ class DummyService : DaggerService() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
startForeground(mainApp.notificationId(), mainApp.notification) startForeground(notificationHolder.notificationID, notificationHolder.notification)
return Service.START_STICKY return Service.START_STICKY
} }
} }

View file

@ -12,7 +12,6 @@ import androidx.core.app.TaskStackBuilder
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.Constants import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainActivity import info.nightscout.androidaps.MainActivity
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.events.* import info.nightscout.androidaps.events.*
@ -28,6 +27,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.androidNotification.NotificationHolder
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
@ -45,7 +45,7 @@ class PersistentNotificationPlugin @Inject constructor(
private var iobCobCalculatorPlugin: IobCobCalculatorPlugin, private var iobCobCalculatorPlugin: IobCobCalculatorPlugin,
private var rxBus: RxBusWrapper, private var rxBus: RxBusWrapper,
private var context: Context, private var context: Context,
private var mainApp: MainApp private var notificationHolder: NotificationHolder
) : PluginBase(PluginDescription() ) : PluginBase(PluginDescription()
.mainType(PluginType.GENERAL) .mainType(PluginType.GENERAL)
.neverVisible(true) .neverVisible(true)
@ -109,7 +109,7 @@ class PersistentNotificationPlugin @Inject constructor(
private fun createNotificationChannel() { private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(mainApp.channelId(), mainApp.channelId() as CharSequence, NotificationManager.IMPORTANCE_HIGH) val channel = NotificationChannel(notificationHolder.channelID, notificationHolder.channelID as CharSequence, NotificationManager.IMPORTANCE_HIGH)
mNotificationManager.createNotificationChannel(channel) mNotificationManager.createNotificationChannel(channel)
} }
} }
@ -176,20 +176,20 @@ class PersistentNotificationPlugin @Inject constructor(
val msgReadIntent = Intent() val msgReadIntent = Intent()
.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
.setAction(READ_ACTION) .setAction(READ_ACTION)
.putExtra(CONVERSATION_ID, mainApp.notificationId()) .putExtra(CONVERSATION_ID, notificationHolder.notificationID)
.setPackage(PACKAGE) .setPackage(PACKAGE)
val msgReadPendingIntent = PendingIntent.getBroadcast(context, val msgReadPendingIntent = PendingIntent.getBroadcast(context,
mainApp.notificationId(), notificationHolder.notificationID,
msgReadIntent, msgReadIntent,
PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.FLAG_UPDATE_CURRENT)
val msgReplyIntent = Intent() val msgReplyIntent = Intent()
.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
.setAction(REPLY_ACTION) .setAction(REPLY_ACTION)
.putExtra(CONVERSATION_ID, mainApp.notificationId()) .putExtra(CONVERSATION_ID, notificationHolder.notificationID)
.setPackage(PACKAGE) .setPackage(PACKAGE)
val msgReplyPendingIntent = PendingIntent.getBroadcast( val msgReplyPendingIntent = PendingIntent.getBroadcast(
context, context,
mainApp.notificationId(), notificationHolder.notificationID,
msgReplyIntent, msgReplyIntent,
PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.FLAG_UPDATE_CURRENT)
// Build a RemoteInput for receiving voice input from devices // Build a RemoteInput for receiving voice input from devices
@ -205,7 +205,7 @@ class PersistentNotificationPlugin @Inject constructor(
} else { } else {
line1 = resourceHelper.gs(R.string.noprofileset) line1 = resourceHelper.gs(R.string.noprofileset)
} }
val builder = NotificationCompat.Builder(context, mainApp.channelId()) val builder = NotificationCompat.Builder(context, notificationHolder.channelID)
builder.setOngoing(true) builder.setOngoing(true)
builder.setOnlyAlertOnce(true) builder.setOnlyAlertOnce(true)
builder.setCategory(NotificationCompat.CATEGORY_STATUS) builder.setCategory(NotificationCompat.CATEGORY_STATUS)
@ -231,7 +231,7 @@ class PersistentNotificationPlugin @Inject constructor(
builder.setContentIntent(resultPendingIntent) builder.setContentIntent(resultPendingIntent)
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = builder.build() val notification = builder.build()
mNotificationManager.notify(mainApp.notificationId(), notification) mNotificationManager.notify(notificationHolder.notificationID, notification)
mainApp.notification = notification notificationHolder.notification = notification
} }
} }

View file

@ -6,17 +6,17 @@ import android.media.AudioManager
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.IBinder import android.os.IBinder
import dagger.android.DaggerService import dagger.android.DaggerService
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.utils.androidNotification.NotificationHolder
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import javax.inject.Inject import javax.inject.Inject
class AlarmSoundService : DaggerService() { class AlarmSoundService : DaggerService() {
@Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var mainApp: MainApp @Inject lateinit var notificationHolder: NotificationHolder
private var player: MediaPlayer? = null private var player: MediaPlayer? = null
private var resourceId = R.raw.error private var resourceId = R.raw.error
@ -26,11 +26,11 @@ class AlarmSoundService : DaggerService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
aapsLogger.debug(LTag.CORE, "onCreate") aapsLogger.debug(LTag.CORE, "onCreate")
startForeground(mainApp.notificationId(), mainApp.notification) startForeground(notificationHolder.notificationID, notificationHolder.notification)
} }
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
startForeground(mainApp.notificationId(), mainApp.notification) startForeground(notificationHolder.notificationID, notificationHolder.notification)
player?.let { if (it.isPlaying) it.stop() } player?.let { if (it.isPlaying) it.stop() }

View file

@ -12,7 +12,6 @@ import android.os.IBinder
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import com.google.android.gms.location.LocationServices import com.google.android.gms.location.LocationServices
import dagger.android.DaggerService import dagger.android.DaggerService
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.EventAppExit import info.nightscout.androidaps.events.EventAppExit
import info.nightscout.androidaps.events.EventLocationChange import info.nightscout.androidaps.events.EventLocationChange
@ -21,6 +20,7 @@ import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.androidNotification.NotificationHolder
import info.nightscout.androidaps.utils.sharedPreferences.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
@ -32,7 +32,7 @@ class LocationService : DaggerService() {
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var mainApp: MainApp @Inject lateinit var notificationHolder: NotificationHolder
@Inject lateinit var lastLocationDataContainer: LastLocationDataContainer @Inject lateinit var lastLocationDataContainer: LastLocationDataContainer
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
@ -73,13 +73,13 @@ class LocationService : DaggerService() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
startForeground(mainApp.notificationId(), mainApp.notification) startForeground(notificationHolder.notificationID, notificationHolder.notification)
return Service.START_STICKY return Service.START_STICKY
} }
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
startForeground(mainApp.notificationId(), mainApp.notification) startForeground(notificationHolder.notificationID, notificationHolder.notification)
// Get last location once until we get regular update // Get last location once until we get regular update
LocationServices.getFusedLocationProviderClient(this).lastLocation.addOnSuccessListener { LocationServices.getFusedLocationProviderClient(this).lastLocation.addOnSuccessListener {

View file

@ -0,0 +1,42 @@
package info.nightscout.androidaps.utils.androidNotification
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.TaskStackBuilder
import info.nightscout.androidaps.MainActivity
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.resources.ResourceHelper
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NotificationHolder @Inject constructor(
private val resourceHelper: ResourceHelper,
private val context: Context
) {
val channelID = "AndroidAPS-Ongoing"
val notificationID = 4711
var notification: Notification
init {
val stackBuilder = TaskStackBuilder.create(context)
.addParentStack(MainActivity::class.java)
.addNextIntent(Intent(context, MainApp::class.java))
val builder = NotificationCompat.Builder(context, channelID)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setSmallIcon(resourceHelper.getNotificationIcon())
.setLargeIcon(resourceHelper.decodeResource(resourceHelper.getIcon()))
.setContentTitle(resourceHelper.gs(R.string.loading))
.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT))
notification = builder.build()
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(notificationID, notification)
}
}