-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0379396
commit ee5c2df
Showing
4 changed files
with
152 additions
and
14 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
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
72 changes: 72 additions & 0 deletions
72
app/src/main/java/com/yausername/dvd/work/YoutubeDLUpdateWorker.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,72 @@ | ||
package com.yausername.dvd.work | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.os.Build | ||
import android.widget.Toast | ||
import androidx.core.app.NotificationCompat | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ForegroundInfo | ||
import androidx.work.WorkerParameters | ||
import com.yausername.dvd.R | ||
import com.yausername.youtubedl_android.YoutubeDL | ||
import com.yausername.youtubedl_android.YoutubeDLUpdater | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
|
||
class YoutubeDLUpdateWorker(appContext: Context, params: WorkerParameters) : | ||
CoroutineWorker(appContext, params) { | ||
|
||
private val notificationManager = | ||
appContext.getSystemService(Context.NOTIFICATION_SERVICE) as | ||
NotificationManager | ||
|
||
|
||
override suspend fun doWork(): Result { | ||
|
||
createNotificationChannel() | ||
val notificationId = id.hashCode() | ||
val notification = NotificationCompat.Builder(applicationContext, | ||
channelId | ||
) | ||
.setSmallIcon(R.mipmap.ic_launcher) | ||
.setContentTitle("Updating youtube-dl") | ||
.build() | ||
|
||
val foregroundInfo = ForegroundInfo(notificationId, notification) | ||
setForeground(foregroundInfo) | ||
|
||
val result = YoutubeDL.getInstance().updateYoutubeDL(applicationContext) | ||
if (result == YoutubeDLUpdater.UpdateStatus.ALREADY_UP_TO_DATE) { | ||
withContext(Dispatchers.Main){ | ||
Toast.makeText(applicationContext, "already up to date", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
return Result.success() | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
var notificationChannel = | ||
notificationManager?.getNotificationChannel(channelId) | ||
if (notificationChannel == null) { | ||
notificationChannel = NotificationChannel( | ||
channelId, | ||
channelName, NotificationManager.IMPORTANCE_LOW | ||
) | ||
notificationChannel.description = | ||
channelDescription | ||
notificationManager?.createNotificationChannel(notificationChannel) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val channelName = "youtube-dl update" | ||
const val channelDescription = "youtube-dl update" | ||
const val channelId = "youtube-dl update" | ||
} | ||
} | ||
|
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