diff --git a/mailbox/src/main/AndroidManifest.xml b/mailbox/src/main/AndroidManifest.xml index 50353209690467aa92a8916bf8e0dd04168319ee..9342203e8b08b5809989fe1c470b1e9adf76e6e1 100644 --- a/mailbox/src/main/AndroidManifest.xml +++ b/mailbox/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.briarproject.mailbox"> + <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> + <application android:name=".MailboxApplication" android:allowBackup="true" @@ -10,6 +12,9 @@ android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/Theme.Briarmailbox"> + + <service android:name=".MailboxService" /> + <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt b/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt index fdd09a27045921020538d4c37a139d97b0771cf3..1f3d599bcb1062ae477eff16b3dbd011875e8fe8 100644 --- a/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt +++ b/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt @@ -6,4 +6,9 @@ class MailboxApplication : Application() { val appComponent = DaggerApplicationComponent.create() + override fun onCreate() { + super.onCreate() + MailboxService.startService(this, "Waiting for messages") + } + } \ No newline at end of file diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt b/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt new file mode 100644 index 0000000000000000000000000000000000000000..32e08a70e5e2dec2459e4809a847112ddac04e2f --- /dev/null +++ b/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt @@ -0,0 +1,70 @@ +package org.briarproject.mailbox + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Build.VERSION +import android.os.Build.VERSION_CODES +import android.os.IBinder +import androidx.core.app.NotificationCompat +import androidx.core.content.ContextCompat + +class MailboxService : Service() { + + private val CHANNEL_ID = "Briar Mailbox Service" + private val CHANNEL_NAME = "Briar Mailbox Channel" + + companion object { + + private val ARG_MESSAGE = "message" + + fun startService(context: Context, message: String) { + val startIntent = Intent(context, MailboxService::class.java) + startIntent.putExtra(ARG_MESSAGE, message) + ContextCompat.startForegroundService(context, startIntent) + } + + fun stopService(context: Context) { + val stopIntent = Intent(context, MailboxService::class.java) + context.stopService(stopIntent) + } + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + val input = intent?.getStringExtra(ARG_MESSAGE) + createNotificationChannel() + val notificationIntent = Intent(this, MainActivity::class.java) + val pendingIntent = PendingIntent.getActivity( + this, + 0, notificationIntent, 0 + ) + + val notification = NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("Briar Mailbox running") + .setContentText(input) + .setSmallIcon(R.drawable.ic_launcher_foreground) + .setContentIntent(pendingIntent) + .build() + + startForeground(1, notification) + return START_NOT_STICKY + } + + override fun onBind(intent: Intent): IBinder? { + return null + } + + private fun createNotificationChannel() { + if (VERSION.SDK_INT >= VERSION_CODES.O) { + val serviceChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, + NotificationManager.IMPORTANCE_DEFAULT) + + val manager = getSystemService(NotificationManager::class.java) + manager!!.createNotificationChannel(serviceChannel) + } + } +} \ No newline at end of file