|
@@ -60,6 +60,7 @@ import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil
|
|
import com.nextcloud.talk.utils.preferences.AppPreferences
|
|
import com.nextcloud.talk.utils.preferences.AppPreferences
|
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
import okhttp3.OkHttpClient
|
|
import okhttp3.OkHttpClient
|
|
|
|
+import java.io.File
|
|
import javax.inject.Inject
|
|
import javax.inject.Inject
|
|
|
|
|
|
@AutoInjector(NextcloudTalkApplication::class)
|
|
@AutoInjector(NextcloudTalkApplication::class)
|
|
@@ -91,6 +92,9 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
lateinit var roomToken: String
|
|
lateinit var roomToken: String
|
|
lateinit var conversationName: String
|
|
lateinit var conversationName: String
|
|
lateinit var currentUser: User
|
|
lateinit var currentUser: User
|
|
|
|
+ private var isChunkedUploading = false
|
|
|
|
+ private var file: File? = null
|
|
|
|
+ private var chunkedFileUploader: ChunkedFileUploader? = null
|
|
|
|
|
|
@Suppress("Detekt.TooGenericExceptionCaught")
|
|
@Suppress("Detekt.TooGenericExceptionCaught")
|
|
override fun doWork(): Result {
|
|
override fun doWork(): Result {
|
|
@@ -120,28 +124,30 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
|
|
|
|
val sourceFileUri = Uri.parse(sourceFile)
|
|
val sourceFileUri = Uri.parse(sourceFile)
|
|
fileName = FileUtils.getFileName(sourceFileUri, context)
|
|
fileName = FileUtils.getFileName(sourceFileUri, context)
|
|
- val file = FileUtils.getFileFromUri(context, sourceFileUri)
|
|
|
|
|
|
+ file = FileUtils.getFileFromUri(context, sourceFileUri)
|
|
val remotePath = getRemotePath(currentUser)
|
|
val remotePath = getRemotePath(currentUser)
|
|
val uploadSuccess: Boolean
|
|
val uploadSuccess: Boolean
|
|
|
|
|
|
initNotificationSetup()
|
|
initNotificationSetup()
|
|
-
|
|
|
|
|
|
+ file?.let { isChunkedUploading = it.length() > CHUNK_UPLOAD_THRESHOLD_SIZE }
|
|
if (file == null) {
|
|
if (file == null) {
|
|
uploadSuccess = false
|
|
uploadSuccess = false
|
|
- } else if (file.length() > CHUNK_UPLOAD_THRESHOLD_SIZE) {
|
|
|
|
- Log.d(TAG, "starting chunked upload because size is " + file.length())
|
|
|
|
|
|
+ } else if (isChunkedUploading) {
|
|
|
|
+ Log.d(TAG, "starting chunked upload because size is " + file!!.length())
|
|
|
|
|
|
initNotificationWithPercentage()
|
|
initNotificationWithPercentage()
|
|
val mimeType = context.contentResolver.getType(sourceFileUri)?.toMediaTypeOrNull()
|
|
val mimeType = context.contentResolver.getType(sourceFileUri)?.toMediaTypeOrNull()
|
|
|
|
|
|
- uploadSuccess = ChunkedFileUploader(
|
|
|
|
|
|
+ chunkedFileUploader = ChunkedFileUploader(
|
|
okHttpClient,
|
|
okHttpClient,
|
|
currentUser,
|
|
currentUser,
|
|
roomToken,
|
|
roomToken,
|
|
metaData,
|
|
metaData,
|
|
this
|
|
this
|
|
- ).upload(
|
|
|
|
- file,
|
|
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ uploadSuccess = chunkedFileUploader!!.upload(
|
|
|
|
+ file!!,
|
|
mimeType,
|
|
mimeType,
|
|
remotePath
|
|
remotePath
|
|
)
|
|
)
|
|
@@ -164,6 +170,9 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
if (uploadSuccess) {
|
|
if (uploadSuccess) {
|
|
mNotifyManager?.cancel(notificationId)
|
|
mNotifyManager?.cancel(notificationId)
|
|
return Result.success()
|
|
return Result.success()
|
|
|
|
+ } else if (isStopped) {
|
|
|
|
+ // since work is cancelled the result would be ignored anyways
|
|
|
|
+ return Result.failure()
|
|
}
|
|
}
|
|
|
|
|
|
Log.e(TAG, "Something went wrong when trying to upload file")
|
|
Log.e(TAG, "Something went wrong when trying to upload file")
|
|
@@ -195,6 +204,15 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
mNotifyManager!!.notify(notificationId, notification)
|
|
mNotifyManager!!.notify(notificationId, notification)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ override fun onStopped() {
|
|
|
|
+ if (file != null && isChunkedUploading) {
|
|
|
|
+ chunkedFileUploader?.abortUpload {
|
|
|
|
+ mNotifyManager?.cancel(notificationId)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ super.onStopped()
|
|
|
|
+ }
|
|
|
|
+
|
|
private fun initNotificationSetup() {
|
|
private fun initNotificationSetup() {
|
|
mNotifyManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
mNotifyManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
mBuilder = NotificationCompat.Builder(
|
|
mBuilder = NotificationCompat.Builder(
|
|
@@ -206,13 +224,17 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
|
|
|
|
private fun initNotificationWithPercentage() {
|
|
private fun initNotificationWithPercentage() {
|
|
notification = mBuilder!!
|
|
notification = mBuilder!!
|
|
- .setContentTitle(context.resources.getString(R.string.nc_upload_in_progess))
|
|
|
|
|
|
+ .setContentTitle(getResourceString(context, R.string.nc_upload_in_progess))
|
|
.setContentText(getNotificationContentText(ZERO_PERCENT))
|
|
.setContentText(getNotificationContentText(ZERO_PERCENT))
|
|
.setSmallIcon(R.drawable.upload_white)
|
|
.setSmallIcon(R.drawable.upload_white)
|
|
.setOngoing(true)
|
|
.setOngoing(true)
|
|
.setProgress(HUNDRED_PERCENT, ZERO_PERCENT, false)
|
|
.setProgress(HUNDRED_PERCENT, ZERO_PERCENT, false)
|
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
.setContentIntent(getIntentToOpenConversation())
|
|
.setContentIntent(getIntentToOpenConversation())
|
|
|
|
+ .addAction(
|
|
|
|
+ R.drawable.ic_cancel_white_24dp, getResourceString(context, R.string.nc_cancel),
|
|
|
|
+ getCancelUploadIntent()
|
|
|
|
+ )
|
|
.build()
|
|
.build()
|
|
|
|
|
|
notificationId = SystemClock.uptimeMillis().toInt()
|
|
notificationId = SystemClock.uptimeMillis().toInt()
|
|
@@ -221,7 +243,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
|
|
|
|
private fun getNotificationContentText(percentage: Int): String {
|
|
private fun getNotificationContentText(percentage: Int): String {
|
|
return String.format(
|
|
return String.format(
|
|
- context.resources.getString(R.string.nc_upload_notification_text),
|
|
|
|
|
|
+ getResourceString(context, R.string.nc_upload_notification_text),
|
|
getShortenedFileName(),
|
|
getShortenedFileName(),
|
|
conversationName,
|
|
conversationName,
|
|
percentage
|
|
percentage
|
|
@@ -236,6 +258,11 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private fun getCancelUploadIntent(): PendingIntent {
|
|
|
|
+ return WorkManager.getInstance(applicationContext)
|
|
|
|
+ .createCancelPendingIntent(id)
|
|
|
|
+ }
|
|
|
|
+
|
|
private fun getIntentToOpenConversation(): PendingIntent? {
|
|
private fun getIntentToOpenConversation(): PendingIntent? {
|
|
val bundle = Bundle()
|
|
val bundle = Bundle()
|
|
val intent = Intent(context, MainActivity::class.java)
|
|
val intent = Intent(context, MainActivity::class.java)
|
|
@@ -257,9 +284,9 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
}
|
|
}
|
|
|
|
|
|
private fun showFailedToUploadNotification() {
|
|
private fun showFailedToUploadNotification() {
|
|
- val failureTitle = context.resources.getString(R.string.nc_upload_failed_notification_title)
|
|
|
|
|
|
+ val failureTitle = getResourceString(context, R.string.nc_upload_failed_notification_title)
|
|
val failureText = String.format(
|
|
val failureText = String.format(
|
|
- context.resources.getString(R.string.nc_upload_failed_notification_text),
|
|
|
|
|
|
+ getResourceString(context, R.string.nc_upload_failed_notification_text),
|
|
fileName
|
|
fileName
|
|
)
|
|
)
|
|
notification = mBuilder!!
|
|
notification = mBuilder!!
|
|
@@ -275,6 +302,10 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
mNotifyManager!!.notify(SystemClock.uptimeMillis().toInt(), notification)
|
|
mNotifyManager!!.notify(SystemClock.uptimeMillis().toInt(), notification)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private fun getResourceString(context: Context, resourceId: Int): String {
|
|
|
|
+ return context.resources.getString(resourceId)
|
|
|
|
+ }
|
|
|
|
+
|
|
companion object {
|
|
companion object {
|
|
private val TAG = UploadAndShareFilesWorker::class.simpleName
|
|
private val TAG = UploadAndShareFilesWorker::class.simpleName
|
|
private const val DEVICE_SOURCE_FILE = "DEVICE_SOURCE_FILE"
|
|
private const val DEVICE_SOURCE_FILE = "DEVICE_SOURCE_FILE"
|
|
@@ -301,6 +332,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
REQUEST_PERMISSION
|
|
REQUEST_PERMISSION
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
+
|
|
Build.VERSION.SDK_INT > Build.VERSION_CODES.Q -> {
|
|
Build.VERSION.SDK_INT > Build.VERSION_CODES.Q -> {
|
|
activity.requestPermissions(
|
|
activity.requestPermissions(
|
|
arrayOf(
|
|
arrayOf(
|
|
@@ -309,6 +341,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
|
|
REQUEST_PERMISSION
|
|
REQUEST_PERMISSION
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
+
|
|
else -> {
|
|
else -> {
|
|
activity.requestPermissions(
|
|
activity.requestPermissions(
|
|
arrayOf(
|
|
arrayOf(
|