@@ -6,17 +6,19 @@ import android.app.PendingIntent
6
6
import android.content.Context
7
7
import android.content.Intent
8
8
import android.os.Build
9
+ import androidx.annotation.RequiresApi
9
10
import androidx.core.app.NotificationCompat
10
- import androidx.core.app.NotificationManagerCompat
11
11
import androidx.room.Room
12
12
import androidx.work.CoroutineWorker
13
+ import androidx.work.ForegroundInfo
14
+ import androidx.work.WorkManager
13
15
import androidx.work.WorkerParameters
14
16
import androidx.work.workDataOf
15
17
import com.github.code.gambit.R
16
18
import com.github.code.gambit.data.entity.chache.FileCacheEntity
19
+ import com.github.code.gambit.data.entity.chache.FileMetaDataCacheEntity
17
20
import com.github.code.gambit.data.entity.network.FileNetworkEntity
18
21
import com.github.code.gambit.data.local.Database
19
- import com.github.code.gambit.data.local.FileDao
20
22
import com.github.code.gambit.data.mapper.cache.FileCacheMapper
21
23
import com.github.code.gambit.data.mapper.network.FileNetworkMapper
22
24
import com.github.code.gambit.data.model.FileMetaData
@@ -33,10 +35,21 @@ import retrofit2.Retrofit
33
35
import retrofit2.converter.gson.GsonConverterFactory
34
36
import timber.log.Timber
35
37
import java.io.File
38
+ import java.lang.Exception
39
+ import java.util.Calendar
36
40
import kotlin.math.pow
37
41
38
42
class FileUploadWorker (ctx : Context , params : WorkerParameters ) : CoroutineWorker(ctx, params) {
39
43
44
+ private val _db =
45
+ Room .databaseBuilder(applicationContext, Database ::class .java, AppConstant .Database .DB_NAME )
46
+ .fallbackToDestructiveMigration()
47
+ .build()
48
+ private val db get() = _db
49
+
50
+ private val fileDao get() = db.fileDao()
51
+ private val fileMetaDataDao get() = db.fileMetaDataDao()
52
+
40
53
/* *
41
54
* @inputData filePath: String is the absolute file path
42
55
* @inputData fileName: String name of the file
@@ -49,20 +62,50 @@ class FileUploadWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker
49
62
val fmd = FileMetaData .fromString(fileMetaDataString)
50
63
val uid = System .currentTimeMillis().toInt()
51
64
val size = String .format(" %.2f" , fmd.size.div(10.0 .pow(6.0 )))
52
- makeStatusNotification(uid, " New file" , " Uploading file ${fmd.name} of size $size MB" , true )
65
+ try {
66
+ val data = FileMetaDataCacheEntity (
67
+ id.toString(),
68
+ Calendar .getInstance().timeInMillis,
69
+ fmd.path,
70
+ fmd.name,
71
+ fmd.size
72
+ )
73
+ fileMetaDataDao.insertFileMetaData(data)
74
+ Timber .tag(" worker" ).i(" added file in db" )
75
+ } catch (err: Exception ) {
76
+ Timber .tag(" worker" ).i(err.localizedMessage)
77
+ }
78
+ setForeground(
79
+ makeStatusNotification(
80
+ uid,
81
+ " New file" ,
82
+ " Uploading file ${fmd.name} of size $size MB"
83
+ )
84
+ )
85
+ /* val test = true
86
+ if (test) {
87
+ delay(60000)
88
+ return Result.success(
89
+ workDataOf(
90
+ AppConstant.Worker.FILE_OUTPUT_KEY to
91
+ FileNetworkEntity("", "", "test-hash", fmd.name, fmd.size, ".any").toString()
92
+ )
93
+ )
94
+ }*/
53
95
val file = File (fmd.path)
54
- val a = System .currentTimeMillis()
55
96
val res = InfuraIPFS ().add.file(file, fmd.name, fmd.name).Hash
56
- val b = System .currentTimeMillis()
57
- // val res = "test-hash-to-avoid-unnecessary-server-call"
58
- Timber .tag(" out" ).i(res)
59
- Timber .tag(" out" ).i(" Took ${b - a} ms" )
60
- val fileDao = getFileDao()
97
+ val fileDao = fileDao
61
98
val fileService = getFileService()
62
- val task = fileService.uploadFile(FileNetworkEntity (" " , " " , res, fmd.name, fmd.size, fmd.name.split(" ." )[1 ]))
99
+ val fne = FileNetworkEntity (" " , " " , res, fmd.name, fmd.size, fmd.name.split(" ." )[1 ])
100
+ val task = fileService.uploadFile(fne)
63
101
fileDao.insertFiles(getFileCacheEntityFromFileNetworkEntity(task))
64
102
val data = workDataOf(AppConstant .Worker .FILE_OUTPUT_KEY to task.toString())
65
- makeStatusNotification(uid, " File Uploaded" , task.toString(), false )
103
+ try {
104
+ fileMetaDataDao.deleteFileMetaData(id.toString())
105
+ Timber .tag(" worker" ).i(" deleted file from db" )
106
+ } catch (err: Exception ) {
107
+ Timber .tag(" worker" ).i(err.localizedMessage)
108
+ }
66
109
return Result .success(data)
67
110
}
68
111
@@ -72,12 +115,6 @@ class FileUploadWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker
72
115
return cacheMapper.mapToEntity(networkMapper.mapFromEntity(fileNetworkEntity))
73
116
}
74
117
75
- private fun getFileDao (): FileDao {
76
- return Room .databaseBuilder(applicationContext, Database ::class .java, AppConstant .Database .DB_NAME )
77
- .fallbackToDestructiveMigration()
78
- .build().fileDao()
79
- }
80
-
81
118
private fun getApiService (): ApiService {
82
119
return Retrofit .Builder ().baseUrl(AppConstant .BASE_URL )
83
120
.addConverterFactory(
@@ -90,55 +127,64 @@ class FileUploadWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker
90
127
}
91
128
92
129
private fun getFileService (): FileService {
93
- return FileServiceImpl (getApiService(), UserManager (applicationContext), LastEvaluatedKeyManager (applicationContext))
130
+ return FileServiceImpl (
131
+ getApiService(),
132
+ UserManager (applicationContext),
133
+ LastEvaluatedKeyManager (applicationContext)
134
+ )
94
135
}
95
136
96
137
/* *
97
138
* @param id Notification id
98
139
* @param title notification title
99
140
* @param message Notification message
100
- * @param progress determines whether to show progress or not
101
141
*/
102
- private fun makeStatusNotification (id : Int , title : String , message : String , progress : Boolean ) {
142
+ private fun makeStatusNotification (id : Int , title : String , message : String ): ForegroundInfo {
103
143
val context = applicationContext
104
144
// Make a channel if necessary
105
145
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
106
146
// Create the NotificationChannel, but only on API 26+ because
107
147
// the NotificationChannel class is new and not in the support library
108
- val name = AppConstant .Notification .CHANNEL_NAME
109
- val description = AppConstant .Notification .CHANNEL_DESCRIPTION
110
- val importance = NotificationManager .IMPORTANCE_HIGH
111
- val channel = NotificationChannel (AppConstant .Notification .CHANNEL_ID , name, importance)
112
- channel.description = description
113
-
114
- // Add the channel
115
- val notificationManager =
116
- context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager ?
117
-
118
- notificationManager?.createNotificationChannel(channel)
148
+ createChannel()
119
149
}
120
150
121
151
val intent = Intent (context, MainActivity ::class .java).apply {
122
152
flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
123
153
}
124
154
125
155
val pendingIntent: PendingIntent = PendingIntent .getActivity(context, 0 , intent, 0 )
126
-
156
+ val cancelIntent =
157
+ WorkManager .getInstance(applicationContext).createCancelPendingIntent(getId())
127
158
// Create the notification
128
159
val builder = NotificationCompat .Builder (context, AppConstant .Notification .CHANNEL_ID )
129
160
.setSmallIcon(R .drawable.ic_launcher_foreground)
130
161
.setContentTitle(title)
131
162
.setContentText(message)
132
163
.setContentIntent(pendingIntent)
164
+ .setProgress(0 , 0 , true )
165
+ .setOngoing(true )
166
+ .addAction(android.R .drawable.ic_delete, " Cancel Upload" , cancelIntent)
133
167
.setPriority(NotificationCompat .PRIORITY_HIGH )
134
168
.setStyle(NotificationCompat .BigTextStyle ().bigText(message))
135
169
.setVibrate(LongArray (0 ))
136
170
137
- if (progress) {
138
- builder.setProgress(0 , 0 , true )
139
- }
171
+ // NotificationManagerCompat.from(applicationContext).notify(1, builder.build())
172
+
173
+ return ForegroundInfo (id, builder.build())
174
+ }
175
+
176
+ @RequiresApi(Build .VERSION_CODES .O )
177
+ fun createChannel () {
178
+ val name = AppConstant .Notification .CHANNEL_NAME
179
+ val description = AppConstant .Notification .CHANNEL_DESCRIPTION
180
+ val importance = NotificationManager .IMPORTANCE_HIGH
181
+ val channel = NotificationChannel (AppConstant .Notification .CHANNEL_ID , name, importance)
182
+ channel.description = description
183
+
184
+ // Add the channel
185
+ val notificationManager =
186
+ applicationContext.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager ?
140
187
141
- // Show the notification
142
- NotificationManagerCompat .from(context).notify(id, builder.build())
188
+ notificationManager?.createNotificationChannel(channel)
143
189
}
144
190
}
0 commit comments