-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
followup changes to save file feature
- use MaterialAlertDialogBuilder - save files to matching folders depending on mimeType - show toast Signed-off-by: Marcel Hibbe <[email protected]>
- Loading branch information
Showing
6 changed files
with
118 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/* | ||
* Nextcloud Talk application | ||
* | ||
* @author Andy Scherzinger | ||
* @author Fariba Khandani | ||
* @author Marcel Hibbe | ||
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de> | ||
* Copyright (C) 2021 Marcel Hibbe <[email protected]> | ||
* Copyright (C) 2023 Fariba Khandani <khandani@winworker.de> | ||
* Copyright (C) 2023 Marcel Hibbe <[email protected]> | ||
* | ||
* 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 | ||
|
@@ -25,14 +25,23 @@ package com.nextcloud.talk.jobs | |
import android.content.ContentValues | ||
import android.content.Context | ||
import android.media.MediaScannerConnection | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.provider.MediaStore | ||
import android.provider.MediaStore.Files.FileColumns | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import autodagger.AutoInjector | ||
import com.nextcloud.talk.R | ||
import com.nextcloud.talk.application.NextcloudTalkApplication | ||
import com.nextcloud.talk.utils.Mimetype.AUDIO_PREFIX | ||
import com.nextcloud.talk.utils.Mimetype.IMAGE_PREFIX | ||
import com.nextcloud.talk.utils.Mimetype.VIDEO_PREFIX | ||
import java.io.File | ||
import java.io.IOException | ||
import java.io.OutputStream | ||
|
@@ -50,16 +59,22 @@ class SaveFileToStorageWorker(val context: Context, workerParameters: WorkerPara | |
val contentResolver = context.contentResolver | ||
val mimeType = URLConnection.guessContentTypeFromName(cacheFile.name) | ||
|
||
val appName = applicationContext.resources!!.getString(R.string.nc_app_product_name) | ||
|
||
val values = ContentValues().apply { | ||
if (mimeType.startsWith(IMAGE_PREFIX) || mimeType.startsWith(VIDEO_PREFIX)) { | ||
put(FileColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM + "/" + appName) | ||
} | ||
put(FileColumns.DISPLAY_NAME, cacheFile.name) | ||
put(FileColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS) | ||
|
||
if (mimeType != null) { | ||
put(FileColumns.MIME_TYPE, URLConnection.guessContentTypeFromName(cacheFile.name)) | ||
put(FileColumns.MIME_TYPE, mimeType) | ||
} | ||
} | ||
|
||
val collection = MediaStore.Files.getContentUri("external") | ||
val uri = contentResolver.insert(collection, values) | ||
val collectionUri = getUriByType(mimeType) | ||
|
||
val uri = contentResolver.insert(collectionUri, values) | ||
|
||
uri?.let { fileUri -> | ||
try { | ||
|
@@ -79,16 +94,53 @@ class SaveFileToStorageWorker(val context: Context, workerParameters: WorkerPara | |
// Notify the media scanner about the new file | ||
MediaScannerConnection.scanFile(context, arrayOf(cacheFile.absolutePath), null, null) | ||
|
||
Handler(Looper.getMainLooper()).post { | ||
Toast.makeText( | ||
context, | ||
context.resources.getString(R.string.nc_save_success), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
|
||
return Result.success() | ||
} catch (e: IOException) { | ||
Log.e(TAG, "Something went wrong when trying to save file to internal storage", e) | ||
Handler(Looper.getMainLooper()).post { | ||
Toast.makeText( | ||
context, | ||
context.resources.getString(R.string.nc_common_error_sorry), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
|
||
return Result.failure() | ||
} catch (e: NullPointerException) { | ||
Log.e(TAG, "Something went wrong when trying to save file to internal storage", e) | ||
Handler(Looper.getMainLooper()).post { | ||
Toast.makeText( | ||
context, | ||
context.resources.getString(R.string.nc_common_error_sorry), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
|
||
return Result.failure() | ||
} | ||
} | ||
|
||
private fun getUriByType(mimeType: String): Uri { | ||
return when { | ||
mimeType.startsWith(VIDEO_PREFIX) -> MediaStore.Video.Media.EXTERNAL_CONTENT_URI | ||
mimeType.startsWith(AUDIO_PREFIX) -> MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | ||
mimeType.startsWith(IMAGE_PREFIX) -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI | ||
else -> if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { | ||
Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)) | ||
} else { | ||
MediaStore.Downloads.EXTERNAL_CONTENT_URI | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private val TAG = SaveFileToStorageWorker::class.java.simpleName | ||
const val KEY_FILE_NAME = "KEY_FILE_NAME" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters