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;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import androidx.annotation.ColorRes;
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 com.crashlytics.android.Crashlytics;
@ -68,10 +60,6 @@ public class MainApp extends DaggerApplication {
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 public HasAndroidInjector injector;
@Inject AAPSLogger aapsLogger;
@ -95,7 +83,6 @@ public class MainApp extends DaggerApplication {
sInstance = this;
sResources = getResources();
LocaleHelper.INSTANCE.update(this);
generateEmptyNotification();
sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class);
Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
@ -230,42 +217,6 @@ public class MainApp extends DaggerApplication {
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
public void onTerminate() {
aapsLogger.debug(LTag.CORE, "onTerminate");

View file

@ -4,12 +4,12 @@ import android.app.Service
import android.content.Intent
import android.os.IBinder
import dagger.android.DaggerService
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.events.EventAppExit
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.androidNotification.NotificationHolder
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
@ -21,8 +21,8 @@ class DummyService : DaggerService() {
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var mainApp: MainApp
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var notificationHolder: NotificationHolder
private val disposable = CompositeDisposable()
@ -32,14 +32,14 @@ class DummyService : DaggerService() {
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?
// 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
.toObservable(EventAppExit::class.java)
.observeOn(Schedulers.io())
.subscribe({
aapsLogger.debug(LTag.CORE, "EventAppExit received")
stopSelf()
}) { fabricPrivacy.logException(it) }
}) { fabricPrivacy::logException }
)
}
@ -52,7 +52,7 @@ class DummyService : DaggerService() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
startForeground(mainApp.notificationId(), mainApp.notification)
startForeground(notificationHolder.notificationID, notificationHolder.notification)
return Service.START_STICKY
}
}

View file

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

View file

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

View file

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