From a7518e9ed24eccdca3778b8bd02f98ddfa5d2d6d Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 30 Mar 2020 11:25:40 +0200 Subject: [PATCH] adding a service with a notification --- app/src/main/AndroidManifest.xml | 3 + .../voicerecorder/activities/MainActivity.kt | 9 +++ .../voicerecorder/helpers/Constants.kt | 3 + .../voicerecorder/services/RecorderService.kt | 57 +++++++++++++++++++ app/src/main/res/values-sk/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 74 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dc69dcc..d4ba8dc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ android:installLocation="auto"> + @@ -63,5 +64,7 @@ android:label="@string/frequently_asked_questions" android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity" /> + + diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt index 38a37db..05355aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt @@ -19,6 +19,7 @@ import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.voicerecorder.BuildConfig import com.simplemobiletools.voicerecorder.R +import com.simplemobiletools.voicerecorder.services.RecorderService import kotlinx.android.synthetic.main.activity_main.* import java.io.File import java.io.IOException @@ -139,6 +140,10 @@ class MainActivity : SimpleActivity() { duration = 0 timer = Timer() timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000) + + Intent(this@MainActivity, RecorderService::class.java).apply { + startService(this) + } } catch (e: IOException) { showErrorToast(e) } @@ -148,6 +153,10 @@ class MainActivity : SimpleActivity() { private fun stopRecording() { timer.cancel() + Intent(this@MainActivity, RecorderService::class.java).apply { + stopService(this) + } + recorder?.apply { stop() release() diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt new file mode 100644 index 0000000..41ef7cd --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/Constants.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.voicerecorder.helpers + +const val RECORDER_RUNNING_NOTIF_ID = 10000 diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt new file mode 100644 index 0000000..4004a6a --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt @@ -0,0 +1,57 @@ +package com.simplemobiletools.voicerecorder.services + +import android.annotation.TargetApi +import android.app.* +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.IBinder +import androidx.core.app.NotificationCompat +import com.simplemobiletools.commons.extensions.getLaunchIntent +import com.simplemobiletools.commons.helpers.isOreoPlus +import com.simplemobiletools.voicerecorder.R +import com.simplemobiletools.voicerecorder.activities.SplashActivity +import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID + +class RecorderService : Service() { + override fun onBind(intent: Intent?): IBinder? = null + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + super.onStartCommand(intent, flags, startId) + startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification()) + return START_NOT_STICKY + } + + @TargetApi(Build.VERSION_CODES.O) + private fun showNotification(): Notification { + val channelId = "simple_recorder" + val label = getString(R.string.app_name) + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + if (isOreoPlus()) { + val importance = NotificationManager.IMPORTANCE_DEFAULT + NotificationChannel(channelId, label, importance).apply { + setSound(null, null) + notificationManager.createNotificationChannel(this) + } + } + + val builder = NotificationCompat.Builder(this) + .setContentTitle(label) + .setContentText(getString(R.string.recording)) + .setSmallIcon(R.drawable.ic_mic_vector) + .setContentIntent(getOpenAppIntent()) + .setPriority(Notification.PRIORITY_DEFAULT) + .setSound(null) + .setOngoing(true) + .setAutoCancel(true) + .setChannelId(channelId) + + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + return builder.build() + } + + private fun getOpenAppIntent(): PendingIntent { + val intent = getLaunchIntent() ?: Intent(this, SplashActivity::class.java) + return PendingIntent.getActivity(this, RECORDER_RUNNING_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT) + } +} diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index 5123dbe..b2bceb1 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -2,4 +2,5 @@ Jednoduchý nahrávač hlasu Nahrávač hlasu Nahrávka bola uložená ako \n\"%s\" + Nahráva sa diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b52dcfc..d002b3f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,4 +2,5 @@ Simple Voice Recorder Voice Recorder Recording saved successfully as\n\"%s\" + Recording