11package org.thoughtcrime.securesms.attachments
22
33import android.content.Context
4- import android.widget.Toast
54import androidx.compose.ui.unit.IntSize
65import androidx.hilt.work.HiltWorker
76import androidx.work.BackoffPolicy
@@ -17,8 +16,6 @@ import dagger.Lazy
1716import dagger.assisted.Assisted
1817import dagger.assisted.AssistedInject
1918import kotlinx.coroutines.CancellationException
20- import kotlinx.coroutines.Dispatchers
21- import kotlinx.coroutines.withContext
2219import network.loki.messenger.BuildConfig
2320import okhttp3.HttpUrl.Companion.toHttpUrl
2421import okio.BufferedSource
@@ -31,8 +28,8 @@ import org.session.libsession.utilities.TextSecurePreferences
3128import org.session.libsession.utilities.recipients.RemoteFile.Companion.toRemoteFile
3229import org.session.libsignal.exceptions.NonRetryableException
3330import org.session.libsignal.utilities.Log
31+ import org.thoughtcrime.securesms.debugmenu.DebugLogGroup
3432import org.thoughtcrime.securesms.util.BitmapUtil
35- import org.thoughtcrime.securesms.util.CurrentActivityObserver
3633import org.thoughtcrime.securesms.util.DateUtils.Companion.secondsToInstant
3734import org.thoughtcrime.securesms.util.ImageUtils
3835import java.time.Duration
@@ -54,23 +51,14 @@ class AvatarReuploadWorker @AssistedInject constructor(
5451 private val configFactory : ConfigFactoryProtocol ,
5552 private val avatarUploadManager : Lazy <AvatarUploadManager >,
5653 private val localEncryptedFileInputStreamFactory : LocalEncryptedFileInputStream .Factory ,
57- private val fileServerApi : FileServerApi ,
58- private val prefs : TextSecurePreferences ,
59- private val currentActivityObserver : CurrentActivityObserver ,
54+ private val fileServerApi : FileServerApi
6055) : CoroutineWorker(context, params) {
6156
6257 /* *
6358 * Log the given message and show a toast if in debug mode
6459 */
65- private suspend inline fun logAndToast (message : String , e : Throwable ? = null) {
66- Log .d(TAG , message, e)
67-
68- val context = currentActivityObserver.currentActivity.value ? : return
69- if (prefs.debugAvatarReupload || BuildConfig .DEBUG ) {
70- withContext(Dispatchers .Main ) {
71- Toast .makeText(context, " AvatarReupload[debug only]: $message " , Toast .LENGTH_SHORT ).show()
72- }
73- }
60+ private fun log (message : String , e : Throwable ? = null) {
61+ Log .d(DebugLogGroup .AVATAR .label, " Avatar Reupload: $message " , e)
7462 }
7563
7664 override suspend fun doWork (): Result {
@@ -79,13 +67,13 @@ class AvatarReuploadWorker @AssistedInject constructor(
7967 }
8068
8169 if (profile == null ) {
82- logAndToast (" No profile picture set; nothing to do." )
70+ log (" No profile picture set; nothing to do." )
8371 return Result .success()
8472 }
8573
8674 val localFile = AvatarDownloadManager .computeFileName(context, profile)
8775 if (! localFile.exists()) {
88- logAndToast (" Avatar file is missing locally; nothing to do." )
76+ log (" Avatar file is missing locally; nothing to do." )
8977 return Result .success()
9078 }
9179
@@ -94,7 +82,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
9482 // Check if the file exists and whether we need to do reprocessing, if we do, we reprocess and re-upload
9583 localEncryptedFileInputStreamFactory.create(localFile).use { stream ->
9684 if (stream.meta.hasPermanentDownloadError) {
97- logAndToast (" Permanent download error for current avatar; nothing to do." )
85+ log (" Permanent download error for current avatar; nothing to do." )
9886 return Result .success()
9987 }
10088
@@ -103,7 +91,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
10391 val source = stream.source().buffer()
10492
10593 if ((lastUpdated != null && needsReProcessing(source)) || lastUpdated == null ) {
106- logAndToast (" About to start reuploading avatar." )
94+ log (" About to start reuploading avatar." )
10795 val attachment = attachmentProcessor.processAvatar(
10896 data = source.use { it.readByteArray() },
10997 ) ? : return Result .failure()
@@ -118,22 +106,22 @@ class AvatarReuploadWorker @AssistedInject constructor(
118106 } catch (e: CancellationException ) {
119107 throw e
120108 } catch (e: NonRetryableException ) {
121- logAndToast (" Non-retryable error while reuploading avatar." , e)
109+ log (" Non-retryable error while reuploading avatar." , e)
122110 return Result .failure()
123111 } catch (e: Exception ) {
124- logAndToast (" Error while reuploading avatar." , e)
112+ log (" Error while reuploading avatar." , e)
125113 return Result .retry()
126114 }
127115
128- logAndToast (" Successfully reuploaded avatar." )
116+ log (" Successfully reuploaded avatar." )
129117 return Result .success()
130118 }
131119 }
132120
133121 // Otherwise, we only need to renew the same avatar on the server
134122 val parsed = fileServerApi.parseAttachmentUrl(profile.url.toHttpUrl())
135123
136- logAndToast (" Renewing user avatar on ${parsed.fileServer} " )
124+ log (" Renewing user avatar on ${parsed.fileServer} " )
137125 try {
138126 fileServerApi.renew(
139127 fileId = parsed.fileId,
@@ -149,7 +137,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
149137 val now = Instant .now()
150138 if (fileExpiry?.isBefore(now) == true ||
151139 (lastUpdated?.isBefore(now.minus(Duration .ofDays(12 )))) == true ) {
152- logAndToast (" FileServer renew failed, trying to upload" , e)
140+ log (" FileServer renew failed, trying to upload" , e)
153141 val pictureData =
154142 localEncryptedFileInputStreamFactory.create(localFile).use { stream ->
155143 check(! stream.meta.hasPermanentDownloadError) {
@@ -166,18 +154,18 @@ class AvatarReuploadWorker @AssistedInject constructor(
166154 } catch (e: CancellationException ) {
167155 throw e
168156 } catch (e: Exception ) {
169- logAndToast (" Error while reuploading avatar after renew failed." , e)
157+ log (" Error while reuploading avatar after renew failed." , e)
170158 return Result .failure()
171159 }
172160
173- logAndToast (" Successfully reuploaded avatar after renew failed." )
161+ log (" Successfully reuploaded avatar after renew failed." )
174162 } else {
175- logAndToast ( " Not reuploading avatar after renew failed; last updated too recent." )
163+ log ( " Not reuploading avatar after renew failed; last updated too recent." )
176164 }
177165
178166 return Result .success()
179167 } else {
180- logAndToast (" Error while renewing avatar. Retrying..." , e)
168+ log (" Error while renewing avatar. Retrying..." , e)
181169 return Result .retry()
182170 }
183171 }
0 commit comments