103 lines
4.1 KiB
Kotlin
103 lines
4.1 KiB
Kotlin
package com.qft.optical
|
|
|
|
import android.content.ContentResolver
|
|
import android.content.ContentUris
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.net.Uri
|
|
import android.os.Build
|
|
import android.os.Environment
|
|
import android.provider.MediaStore
|
|
import android.webkit.MimeTypeMap
|
|
import androidx.core.content.FileProvider
|
|
import java.io.File
|
|
|
|
data class ReceivedFile(
|
|
val uri: Uri,
|
|
val name: String,
|
|
val size: Long,
|
|
val mimeType: String,
|
|
val modifiedAtSeconds: Long,
|
|
)
|
|
|
|
/** Lists only the files QFT has finished writing to Downloads/QFT. */
|
|
object ReceivedFileCabinet {
|
|
private const val RELATIVE_QFT_PATH = "Download/QFT/"
|
|
|
|
fun list(context: Context): List<ReceivedFile> =
|
|
if (Build.VERSION.SDK_INT >= 29) listModern(context.contentResolver) else listLegacy(context)
|
|
|
|
fun open(context: Context, file: ReceivedFile) {
|
|
val resolvedMime = mimeFor(file.name, file.mimeType)
|
|
val intent = Intent(Intent.ACTION_VIEW)
|
|
.setDataAndType(file.uri, resolvedMime)
|
|
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
context.startActivity(Intent.createChooser(intent, "打开 ${file.name}"))
|
|
}
|
|
|
|
private fun listModern(resolver: ContentResolver): List<ReceivedFile> {
|
|
val projection = arrayOf(
|
|
MediaStore.Downloads._ID,
|
|
MediaStore.Downloads.DISPLAY_NAME,
|
|
MediaStore.Downloads.SIZE,
|
|
MediaStore.Downloads.MIME_TYPE,
|
|
MediaStore.Downloads.DATE_MODIFIED,
|
|
)
|
|
val out = mutableListOf<ReceivedFile>()
|
|
resolver.query(
|
|
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
|
|
projection,
|
|
"${MediaStore.Downloads.RELATIVE_PATH}=? AND ${MediaStore.Downloads.IS_PENDING}=0",
|
|
arrayOf(RELATIVE_QFT_PATH),
|
|
"${MediaStore.Downloads.DATE_MODIFIED} DESC",
|
|
)?.use { cursor ->
|
|
val idIndex = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID)
|
|
val nameIndex = cursor.getColumnIndexOrThrow(MediaStore.Downloads.DISPLAY_NAME)
|
|
val sizeIndex = cursor.getColumnIndexOrThrow(MediaStore.Downloads.SIZE)
|
|
val mimeIndex = cursor.getColumnIndexOrThrow(MediaStore.Downloads.MIME_TYPE)
|
|
val timeIndex = cursor.getColumnIndexOrThrow(MediaStore.Downloads.DATE_MODIFIED)
|
|
while (cursor.moveToNext()) {
|
|
val id = cursor.getLong(idIndex)
|
|
val name = cursor.getString(nameIndex).orEmpty().ifBlank { "未命名文件" }
|
|
out += ReceivedFile(
|
|
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, id),
|
|
name = name,
|
|
size = cursor.getLong(sizeIndex).coerceAtLeast(0L),
|
|
mimeType = cursor.getString(mimeIndex).orEmpty(),
|
|
modifiedAtSeconds = cursor.getLong(timeIndex),
|
|
)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
@Suppress("DEPRECATION")
|
|
private fun listLegacy(context: Context): List<ReceivedFile> {
|
|
val directory = File(
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
|
|
"QFT",
|
|
)
|
|
return directory.listFiles()
|
|
?.asSequence()
|
|
?.filter { it.isFile }
|
|
?.sortedByDescending { it.lastModified() }
|
|
?.map { file ->
|
|
ReceivedFile(
|
|
uri = FileProvider.getUriForFile(context, "${context.packageName}.files", file),
|
|
name = file.name,
|
|
size = file.length().coerceAtLeast(0L),
|
|
mimeType = mimeFor(file.name, ""),
|
|
modifiedAtSeconds = file.lastModified() / 1000L,
|
|
)
|
|
}
|
|
?.toList()
|
|
.orEmpty()
|
|
}
|
|
|
|
private fun mimeFor(name: String, supplied: String): String {
|
|
if (supplied.isNotBlank() && supplied != "application/octet-stream") return supplied
|
|
val extension = name.substringAfterLast('.', "").lowercase()
|
|
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "application/octet-stream"
|
|
}
|
|
}
|