/
VideoModel.kt
134 lines (126 loc) · 4.72 KB
/
VideoModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 13/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities.views.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.google.firebase.perf.metrics.AddTrace
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.network.HttpDownloader
import com.javinator9889.handwashingreminder.utils.Videos.URI.FILENAME
import com.javinator9889.handwashingreminder.utils.Videos.URI.HASH
import com.javinator9889.handwashingreminder.utils.Videos.URI.URL
import com.javinator9889.handwashingreminder.utils.Videos.URI.VideoList
import com.javinator9889.handwashingreminder.utils.isConnected
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import okio.HashingSink
import okio.buffer
import okio.sink
import timber.log.Timber
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.math.BigInteger
import java.security.MessageDigest
import javax.inject.Inject
private const val LIVEDATA_KEY = "videomodel:livedata"
class VideoModel(
private val state: SavedStateHandle, private val position: Int
) : ViewModel() {
private val cachePath: File =
HandwashingApplication.getInstance().applicationContext.cacheDir
val videos: LiveData<String> = liveData {
emitSource(state.getLiveData(LIVEDATA_KEY, loadVideo()))
}
private suspend fun loadVideo(): String {
var file = File("nonexistent")
Timber.d("Creating a new instance")
coroutineScope {
val video = VideoList[position]
val url = video[URL]
val hash = video[HASH]
val filename = video[FILENAME]
if (url != null && hash != null && filename != null) {
file = downloadVideo(url, hash, filename)
}
}
var isVideoDownloaded = true
if (!file.exists())
isVideoDownloaded = false
if (isVideoDownloaded)
state.set(LIVEDATA_KEY, file.name)
return file.name
}
@AddTrace(name = "videoDownload")
private suspend fun downloadVideo(
url: String,
hash: String,
name: String
): File {
val file = File(cachePath, name)
withContext(Dispatchers.IO) {
if (!isConnected() || !needsToDownloadFile(file, hash))
return@withContext
else
file.createNewFile()
val hashingSink = HashingSink.sha256(file.sink())
do {
val okHttpDownloader = HttpDownloader.newInstance()
with(okHttpDownloader.downloadFile(url)) {
hashingSink.buffer().use {
it.writeAll(this)
}
close()
}
} while (!hashingSink.hash.hex().equals(hash, true))
}
return file
}
private fun needsToDownloadFile(file: File, hash: String): Boolean {
if (!file.exists())
return true
val digest = MessageDigest.getInstance("SHA-256")
val reader: InputStream = FileInputStream(file)
return try {
val bytes = reader.readBytes()
digest.update(bytes)
sameSHA2Hash(hash, digest)
} catch (_: Exception) {
false
} finally {
reader.close()
}
}
private fun sameSHA2Hash(
hash: String,
messageDigest: MessageDigest
): Boolean {
val sha2sum = messageDigest.digest()
val bigInt = BigInteger(1, sha2sum)
val obtainedHash = String.format("%064x", bigInt)
return obtainedHash.equals(hash, true)
}
}
class VideoModelFactory @Inject constructor(private val position: Int) :
ViewModelAssistedFactory<VideoModel> {
override fun create(handle: SavedStateHandle) = VideoModel(handle, position)
}