Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
graszka22 committed Jun 17, 2024
1 parent a77e595 commit 84b6898
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
Expand All @@ -28,7 +29,7 @@
android:name=".CameraService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="camera|microphone|mediaProjection">
android:foregroundServiceType="camera|microphone">
</service>

<activity
Expand All @@ -40,7 +41,7 @@
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.FishjamAndroidExample" >
android:theme="@style/Theme.FishjamAndroidExample">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.fishjamandroidexample

import android.app.ForegroundServiceStartNotAllowedException
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
Expand All @@ -12,36 +11,28 @@ import android.os.IBinder
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import timber.log.Timber

class CameraService : Service() {
private fun startForeground() {
// Before starting the service as foreground check that the app has the
// appropriate runtime permissions. In this case, verify that the user has
// granted the CAMERA permission.
val cameraPermission =
ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
if (cameraPermission == PackageManager.PERMISSION_DENIED) {
// Without camera permissions the service cannot run in the foreground
// Consider informing user or updating your app UI if visible.
stopSelf()
return
}

try {
val channelID = "FISHJAM_NOTIFICATION_CHANNEL"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel.
val name = "Fishjam notification channel"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(channelID, name, importance)
// Register the channel with the system. You can't change the importance
// or other notification behaviors after this.
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
val notification =
NotificationCompat.Builder(this, channelID)
// Create the notification to display while the service is running
.setContentTitle("Fishjam is running")
.build()
ServiceCompat.startForeground(
Expand All @@ -55,13 +46,7 @@ class CameraService : Service() {
}
)
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
e is ForegroundServiceStartNotAllowedException
) {
// App not in a valid state to start foreground service
// (e.g. started from bg)
}
// ...
Timber.w("Failed to start foreground service")
}
}

Expand Down

0 comments on commit 84b6898

Please sign in to comment.