Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented scheduled worker for record schedule checker
- Loading branch information
1 parent
9f06739
commit c3b1d22
Showing
10 changed files
with
157 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[*] | ||
insert_final_newline = true | ||
[*.{kt,kts}] | ||
indent_size=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.freshdigitable.upnpsample | ||
|
||
import android.app.Application | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.os.Build | ||
import androidx.core.app.NotificationManagerCompat | ||
import androidx.work.Configuration | ||
import androidx.work.ListenableWorker | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerFactory | ||
import androidx.work.WorkerParameters | ||
|
||
class App : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
val conf = Configuration.Builder() | ||
.setWorkerFactory(object : WorkerFactory() { | ||
override fun createWorker( | ||
appContext: Context, | ||
workerClassName: String, | ||
workerParameters: WorkerParameters | ||
): ListenableWorker? { | ||
return RecordScheduleCheckWorker(appContext, workerParameters) | ||
} | ||
}) | ||
.build() | ||
WorkManager.initialize(this, conf) | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val notificationChannel = | ||
NotificationChannel( | ||
getString(R.string.notif_schedule_checker_channel_id), | ||
getString(R.string.notif_schedule_checker_name), | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
).apply { | ||
description = getString(R.string.notif_schedule_checker_description) | ||
} | ||
NotificationManagerCompat.from(this) | ||
.createNotificationChannel(notificationChannel) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/freshdigitable/upnpsample/RecordScheduleCheckWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.freshdigitable.upnpsample | ||
|
||
import android.content.Context | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.WorkerParameters | ||
|
||
class RecordScheduleCheckWorker( | ||
private val context: Context, | ||
param: WorkerParameters | ||
) : CoroutineWorker(context, param) { | ||
override suspend fun doWork(): Result { | ||
withDevice { | ||
val recordScheduleList = getRecordScheduleList() | ||
MainActivity.logd("recordSchedule", recordScheduleList) | ||
val mediaAlert = recordScheduleList.result | ||
.filter { it.hasWarnings } | ||
.joinToString { it.title } | ||
if (mediaAlert.isEmpty()) { | ||
return Result.success() | ||
} | ||
val notif = NotificationCompat.Builder( | ||
context, | ||
context.getString(R.string.notif_schedule_checker_channel_id) | ||
) | ||
.setSmallIcon(R.drawable.ic_warning_black_24dp) | ||
.setContentTitle("録画が失敗しそうです") | ||
.setContentText("notice: $mediaAlert") | ||
.build() | ||
NotificationManagerCompat.from(context) | ||
.notify(0, notif) | ||
} | ||
return Result.success() | ||
} | ||
|
||
private suspend inline fun withDevice(block: NasneDevice.() -> Unit) { | ||
val nasneDeviceProvider = NasneDeviceProvider(context) | ||
nasneDeviceProvider.init() | ||
val nasneDevice = nasneDeviceProvider.findDevice() | ||
block(nasneDevice) | ||
nasneDeviceProvider.dispose() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M1,21h22L12,2 1,21zM13,18h-2v-2h2v2zM13,14h-2v-4h2v4z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<resources> | ||
<string name="app_name">upnp sample</string> | ||
<string name="notif_schedule_checker_channel_id" translatable="false">scheduleChecker</string> | ||
<string name="notif_schedule_checker_name" translatable="false">scheduleChecker</string> | ||
<string name="notif_schedule_checker_description">scheduleChecker description</string> | ||
</resources> |