Skip to content

Commit

Permalink
update repo updater
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Oct 13, 2024
1 parent 691016f commit 471136d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
10 changes: 1 addition & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
android:name=".service.DownloadService"
android:foregroundServiceType="dataSync"
android:exported="false" />



<provider
android:name="rikka.shizuku.ShizukuProvider"
android:authorities="${applicationId}.shizuku"
Expand All @@ -110,12 +109,5 @@
android:multiprocess="false"
android:permission="android.permission.INTERACT_ACROSS_USERS_FULL" />

<meta-data
android:name="android.service.foreground"
android:value="true" />

<meta-data
android:name="foregroundServiceType"
android:value="dataSync" />
</application>
</manifest>
39 changes: 9 additions & 30 deletions app/src/main/kotlin/com/dergoogler/mmrl/worker/RepoUpdateWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ import com.dergoogler.mmrl.stub.IRepoManager
import timber.log.Timber

class RepoUpdateWorker(
private val context: Context,
context: Context,
params: WorkerParameters
) : CoroutineWorker(context, params) {

override suspend fun doWork(): Result {
setForeground(createForegroundInfo())

return try {
val updated = updateRepositories()

if (updated) {
createNotification(
title = context.getString(R.string.repo_update_service),
message = context.getString(R.string.repo_update_service_desc)
title = applicationContext.getString(R.string.repo_update_service),
message = applicationContext.getString(R.string.repo_update_service_desc)
)
} else {
createNotification(
title = context.getString(R.string.repo_update_service_failed),
message = context.getString(R.string.repo_update_service_failed_desc)
title = applicationContext.getString(R.string.repo_update_service_failed),
message = applicationContext.getString(R.string.repo_update_service_failed_desc)
)
}
Result.success()
Expand All @@ -44,23 +42,20 @@ class RepoUpdateWorker(
}

private suspend fun updateRepositories(): Boolean {

val database: AppDatabase = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java,
"mmrl"
).build()

val repoDao = database.repoDao()
val repos = database.repoDao().getAll()

val repos = repoDao.getAll()

repos.forEach { repo ->
val repoManager = IRepoManager.build(repo.url)

try {
val response =
repoManager.modules.execute()
val response = repoManager.modules.execute()

if (response.isSuccessful && response.body() != null) {
val modulesJson = response.body()!!
Expand All @@ -78,14 +73,12 @@ class RepoUpdateWorker(
return false
}
}

return true
}

private fun createNotification(title: String, message: String) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val channel = NotificationChannel(
"RepoUpdateChannel",
Expand All @@ -94,26 +87,12 @@ class RepoUpdateWorker(
)
notificationManager.createNotificationChannel(channel)

val notification = NotificationCompat.Builder(context, "RepoUpdateChannel")
val notification = NotificationCompat.Builder(applicationContext, "RepoUpdateChannel")
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.box)
.build()

notificationManager.notify(1, notification)
}

private fun createForegroundInfo(): ForegroundInfo {
val notification = NotificationCompat.Builder(applicationContext, "repo_update_channel")
.setContentTitle("Updating Repositories")
.setSmallIcon(R.drawable.box)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ForegroundInfo(1, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else {
ForegroundInfo(1, notification)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ open class MMRLComponentActivity : ComponentActivity() {
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.build()
)
.build()
Expand Down

0 comments on commit 471136d

Please sign in to comment.