Skip to content

Commit 49303ab

Browse files
Merge pull request #845 from prathamk22/Auto_Delete_Expired_Downloads
Auto Delete after 15 days service added
2 parents f6c9604 + 35e5ae3 commit 49303ab

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

app/src/main/java/com/codingblocks/cbonlineapp/dashboard/DashboardActivity.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import com.codingblocks.cbonlineapp.tracks.LearningTracksActivity
3434
import com.codingblocks.cbonlineapp.util.Components
3535
import com.codingblocks.cbonlineapp.util.JWTUtils
3636
import com.codingblocks.cbonlineapp.util.UNAUTHORIZED
37+
import com.codingblocks.cbonlineapp.util.DELETE_DOWNLOADED_VIDEO
38+
import com.codingblocks.cbonlineapp.util.DeleteDownloadedFiles
39+
import java.util.concurrent.TimeUnit
40+
import com.google.common.util.concurrent.ListenableFuture
41+
import androidx.work.*
3742
import com.codingblocks.cbonlineapp.util.extensions.colouriseToolbar
3843
import com.codingblocks.cbonlineapp.util.extensions.loadImage
3944
import com.codingblocks.cbonlineapp.util.extensions.observer
@@ -85,6 +90,7 @@ class DashboardActivity : BaseCBActivity(),
8590
toggle.syncState()
8691
dashboardNavigation.setNavigationItemSelectedListener(this)
8792
dashboardBottomNav.setOnNavigationItemSelectedListener(this)
93+
checkDownloadDataWM()
8894
initializeUI(vm.isLoggedIn ?: false)
8995
vm.errorLiveData.observer(this) { error ->
9096
when (error) {
@@ -266,6 +272,26 @@ class DashboardActivity : BaseCBActivity(),
266272
}
267273
}
268274

275+
private fun checkDownloadDataWM(){
276+
val wm = WorkManager.getInstance()
277+
278+
//Will get if Auto delete downloaded video request is already started or not
279+
val future: ListenableFuture<List<WorkInfo>> = wm.getWorkInfosByTag(DELETE_DOWNLOADED_VIDEO)
280+
val list: List<WorkInfo> = future.get()
281+
282+
//Request to delete video files which expired after 15 days
283+
val request: PeriodicWorkRequest =
284+
PeriodicWorkRequestBuilder<DeleteDownloadedFiles>(1, TimeUnit.DAYS)
285+
.addTag(DELETE_DOWNLOADED_VIDEO)
286+
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 20, TimeUnit.SECONDS)
287+
.build()
288+
289+
//If found empty then it is not started or cancelled for whatever reason, will add request to start it
290+
if(list.isEmpty()){
291+
wm.enqueue(request)
292+
}
293+
}
294+
269295
private fun checkForUpdates() {
270296
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
271297

app/src/main/java/com/codingblocks/cbonlineapp/util/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,5 @@ const val REFRESH_TOKEN = "refresh_token"
105105
const val CONTEST_ID = "contestId"
106106

107107
const val CODE_ID = "codeId"
108+
109+
const val DELETE_DOWNLOADED_VIDEO = "deleteDownloadedVideo"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codingblocks.cbonlineapp.util
2+
3+
import android.content.Context
4+
import android.os.Environment
5+
import androidx.work.CoroutineWorker
6+
import androidx.work.WorkerParameters
7+
import com.codingblocks.cbonlineapp.database.ContentDao
8+
import com.codingblocks.cbonlineapp.database.models.ContentModel
9+
import org.koin.core.KoinComponent
10+
import org.koin.core.inject
11+
import java.io.File
12+
import java.util.*
13+
14+
class DeleteDownloadedFiles(context: Context, private val workerParameters: WorkerParameters) :
15+
CoroutineWorker(context, workerParameters), KoinComponent {
16+
17+
val contentDao: ContentDao by inject()
18+
19+
override suspend fun doWork(): Result {
20+
val list:List<ContentModel> = contentDao.getDownloads(true)
21+
22+
list.forEach {item->
23+
val folderFile = File(applicationContext.getExternalFilesDir(Environment.getDataDirectory().absolutePath), "/${item.contentLecture.lectureId}")
24+
val fileDate = Date(folderFile.lastModified())
25+
val expiringDate = Date(fileDate.time + 15)
26+
27+
//Difference is number of days between expiring date and today.
28+
val diff = (expiringDate.time - Date().time)/(24 * 60 * 60 * 1000)
29+
if (diff<=0){
30+
MediaUtils.deleteRecursive(folderFile)
31+
contentDao.updateContentWithVideoId(item.contentLecture.lectureId, 0)
32+
}
33+
}
34+
return Result.success()
35+
}
36+
}

app/src/main/java/com/codingblocks/cbonlineapp/util/DownloadWorker.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DownloadWorker(context: Context, private val workerParameters: WorkerParam
4141
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
4242
}
4343

44-
val contentDao: ContentDao by inject()
44+
private val contentDao: ContentDao by inject()
4545

4646
// override fun onStopped() {
4747
// super.onStopped()
@@ -77,6 +77,7 @@ class DownloadWorker(context: Context, private val workerParameters: WorkerParam
7777
setAutoCancel(false)
7878
}
7979
)
80+
8081
notificationManager.notify(downloadData.notificationId, downloadData.notificationBuilder.build())
8182
val response: Response<JsonObject>
8283
response = withContext(Dispatchers.IO) { CBOnlineLib.api.getOtp(downloadData.videoId, downloadData.sectionId, downloadData.attemptId, true) }

0 commit comments

Comments
 (0)