Skip to content

Commit

Permalink
removed foreground permission since it wasn't used
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Kalderstam <[email protected]>
  • Loading branch information
spacecowboy committed Dec 18, 2024
1 parent e4d2fca commit 235228e
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 76 deletions.
11 changes: 6 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
<!-- To limit syncing to only WiFi -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- For expedited work manager jobs that run in foreground service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<!-- Remove foreground permission which is merged in by workmanager -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" tools:node="remove"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" tools:node="remove"/>

<permission
android:name="${applicationId}.permission.read"
Expand Down Expand Up @@ -199,10 +200,10 @@
android:exported="true"
android:readPermission="${applicationId}.permission.read" />

<!-- Remove foreground service which is merged in by workmanager -->
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge" />
android:name="androidx.work.impl.foreground.SystemForegroundService"
tools:node="remove" />

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,6 @@ class Repository(override val di: DI) : DIAware {

val constraints =
Constraints.Builder()
// This prevents expedited if true
.setRequiresCharging(syncOnlyWhenCharging.value)

if (syncOnlyOnWifi.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import android.annotation.TargetApi
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.ServiceInfo
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.ForegroundInfo
import com.nononsenseapps.feeder.R

private const val SYNC_NOTIFICATION_ID = 42623
private const val SYNC_CHANNEL_ID = "feederSyncNotifications"
private const val SYNC_NOTIFICATION_GROUP = "com.nononsenseapps.feeder.SYNC"

/**
* This is safe to call multiple times
Expand All @@ -34,37 +29,3 @@ private fun createNotificationChannel(

notificationManager.createNotificationChannel(channel)
}

/**
* Necessary for expedited work.
* Pre Android 12 they will run as foreground services, but on Android 12+ they will run as expedited Jobs.
*/
fun createForegroundInfo(
context: Context,
notificationManager: NotificationManagerCompat,
): ForegroundInfo {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(context, notificationManager)
}

val syncingText = context.getString(R.string.syncing)

val notification =
NotificationCompat.Builder(context.applicationContext, SYNC_CHANNEL_ID)
.setContentTitle(syncingText)
.setTicker(syncingText)
.setGroup(SYNC_NOTIFICATION_GROUP)
.setSmallIcon(R.drawable.ic_stat_sync)
.setOngoing(true)
.build()

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ForegroundInfo(
SYNC_NOTIFICATION_ID,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC,
)
} else {
ForegroundInfo(SYNC_NOTIFICATION_ID, notification)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.nononsenseapps.feeder.model.workmanager

import android.content.Context
import androidx.core.app.NotificationManagerCompat
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import com.nononsenseapps.feeder.db.room.BlocklistDao
import com.nononsenseapps.feeder.db.room.ID_UNSET
Expand All @@ -20,13 +18,8 @@ class BlockListWorker(val context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams), DIAware {
override val di: DI by closestDI(context)

private val notificationManager: NotificationManagerCompat by instance()
private val blocklistDao: BlocklistDao by instance()

override suspend fun getForegroundInfo(): ForegroundInfo {
return createForegroundInfo(context, notificationManager)
}

override suspend fun doWork(): Result {
logDebug(LOG_TAG, "Doing work...")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package com.nononsenseapps.feeder.model.workmanager

import android.content.Context
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.OutOfQuotaPolicy
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
Expand Down Expand Up @@ -42,13 +39,8 @@ class FeedSyncer(val context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams), DIAware {
override val di: DI by closestDI(context)

private val notificationManager: NotificationManagerCompat by instance()
private val rssLocalSync: RssLocalSync by instance()

override suspend fun getForegroundInfo(): ForegroundInfo {
return createForegroundInfo(context, notificationManager)
}

override suspend fun doWork(): Result {
var success: Boolean

Expand Down Expand Up @@ -98,7 +90,6 @@ fun requestFeedSync(
val workRequest =
OneTimeWorkRequestBuilder<FeedSyncer>()
.addTag("feeder")
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.keepResultsForAtLeast(5, TimeUnit.MINUTES)
.setConstraints(constraints.build())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package com.nononsenseapps.feeder.model.workmanager

import android.content.Context
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
Expand All @@ -24,14 +22,9 @@ class SyncServiceGetUpdatesWorker(val context: Context, workerParams: WorkerPara
CoroutineWorker(context, workerParams), DIAware {
override val di: DI by closestDI(context)

private val notificationManager: NotificationManagerCompat by instance()
private val syncClient: SyncRestClient by instance()
private val repository: Repository by instance()

override suspend fun getForegroundInfo(): ForegroundInfo {
return createForegroundInfo(context, notificationManager)
}

override suspend fun doWork(): Result {
return try {
Log.d(LOG_TAG, "Doing work")
Expand All @@ -57,7 +50,6 @@ fun scheduleGetUpdates(di: DI) {

val constraints =
Constraints.Builder()
// This prevents expedited if true
.setRequiresCharging(repository.syncOnlyWhenCharging.value)

if (repository.syncOnlyOnWifi.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.nononsenseapps.feeder.model.workmanager

import android.content.Context
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import com.nononsenseapps.feeder.sync.SyncRestClient
import org.kodein.di.DI
Expand All @@ -16,13 +14,8 @@ class SyncServiceSendReadWorker(val context: Context, workerParams: WorkerParame
CoroutineWorker(context, workerParams), DIAware {
override val di: DI by closestDI(context)

private val notificationManager: NotificationManagerCompat by instance()
private val syncClient: SyncRestClient by di.instance()

override suspend fun getForegroundInfo(): ForegroundInfo {
return createForegroundInfo(context, notificationManager)
}

override suspend fun doWork(): Result {
return try {
Log.d(LOG_TAG, "Doing work")
Expand Down

0 comments on commit 235228e

Please sign in to comment.