parent
448e25c04d
commit
c3295483e4
@ -1,90 +0,0 @@ |
||||
package io.legado.app.help |
||||
|
||||
import android.content.ContentUris |
||||
import android.content.ContentValues |
||||
import android.net.Uri |
||||
import android.os.Build |
||||
import android.os.Environment |
||||
import android.provider.MediaStore |
||||
import androidx.core.content.FileProvider |
||||
import androidx.documentfile.provider.DocumentFile |
||||
import io.legado.app.constant.AppConst |
||||
import io.legado.app.utils.FileDoc |
||||
import io.legado.app.utils.FileUtils.getMimeType |
||||
import splitties.init.appCtx |
||||
import java.io.File |
||||
import java.util.* |
||||
|
||||
object BookMediaStore { |
||||
private val DOWNLOAD_DIR = |
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) |
||||
|
||||
fun insertBook(doc: DocumentFile): Uri? { |
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
||||
val bookDetails = ContentValues().apply { |
||||
put(MediaStore.Downloads.RELATIVE_PATH, "Download${File.separator}books") |
||||
put(MediaStore.MediaColumns.DISPLAY_NAME, doc.name) |
||||
put(MediaStore.MediaColumns.MIME_TYPE, getMimeType(doc.name!!)) |
||||
put(MediaStore.MediaColumns.SIZE, doc.length()) |
||||
} |
||||
appCtx.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, bookDetails) |
||||
} else { |
||||
val destinyFile = File(DOWNLOAD_DIR, doc.name!!) |
||||
FileProvider.getUriForFile(appCtx, AppConst.authority, destinyFile) |
||||
}?.also { uri -> |
||||
appCtx.contentResolver.openOutputStream(uri).use { outputStream -> |
||||
val brr = ByteArray(1024) |
||||
var len: Int |
||||
val bufferedInputStream = appCtx.contentResolver.openInputStream(doc.uri)!! |
||||
while ((bufferedInputStream.read(brr, 0, brr.size).also { len = it }) != -1) { |
||||
outputStream?.write(brr, 0, len) |
||||
} |
||||
outputStream?.flush() |
||||
bufferedInputStream.close() |
||||
} |
||||
} |
||||
} |
||||
|
||||
fun getBook(name: String): FileDoc? { |
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
||||
val projection = arrayOf( |
||||
MediaStore.Downloads._ID, |
||||
MediaStore.Downloads.DISPLAY_NAME, |
||||
MediaStore.Downloads.SIZE, |
||||
MediaStore.Downloads.DATE_MODIFIED |
||||
) |
||||
val selection = |
||||
"${MediaStore.Downloads.RELATIVE_PATH} like 'Download${File.separator}books${File.separator}%'" |
||||
val sortOrder = "${MediaStore.Downloads.DISPLAY_NAME} ASC" |
||||
appCtx.contentResolver.query( |
||||
MediaStore.Downloads.EXTERNAL_CONTENT_URI, |
||||
projection, |
||||
selection, |
||||
emptyArray(), |
||||
sortOrder |
||||
)?.use { |
||||
val idColumn = it.getColumnIndex(projection[0]) |
||||
val nameColumn = it.getColumnIndex(projection[1]) |
||||
val sizeColumn = it.getColumnIndex(projection[2]) |
||||
val dateColumn = it.getColumnIndex(projection[3]) |
||||
if (it.moveToNext()) { |
||||
val id = it.getLong(idColumn) |
||||
return FileDoc( |
||||
name = it.getString(nameColumn), |
||||
isDir = false, |
||||
size = it.getLong(sizeColumn), |
||||
date = Date(it.getLong(dateColumn)), |
||||
uri = ContentUris.withAppendedId( |
||||
MediaStore.Downloads.EXTERNAL_CONTENT_URI, |
||||
id |
||||
) |
||||
) |
||||
} |
||||
} |
||||
} |
||||
|
||||
return null |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,83 @@ |
||||
package io.legado.app.help.storage |
||||
|
||||
import io.legado.app.R |
||||
import io.legado.app.constant.PreferKey |
||||
import io.legado.app.utils.FileUtils |
||||
import io.legado.app.utils.GSON |
||||
import io.legado.app.utils.fromJsonObject |
||||
import splitties.init.appCtx |
||||
|
||||
abstract class BackupRestore { |
||||
|
||||
private val ignoreConfigPath = FileUtils.getPath(appCtx.filesDir, "restoreIgnore.json") |
||||
val ignoreConfig: HashMap<String, Boolean> by lazy { |
||||
val file = FileUtils.createFileIfNotExist(ignoreConfigPath) |
||||
val json = file.readText() |
||||
GSON.fromJsonObject<HashMap<String, Boolean>>(json) ?: hashMapOf() |
||||
} |
||||
|
||||
//忽略key |
||||
val ignoreKeys = arrayOf( |
||||
"readConfig", |
||||
PreferKey.themeMode, |
||||
PreferKey.bookshelfLayout, |
||||
PreferKey.showRss, |
||||
PreferKey.threadCount, |
||||
PreferKey.defaultBookTreeUri |
||||
) |
||||
|
||||
//忽略标题 |
||||
val ignoreTitle = arrayOf( |
||||
appCtx.getString(R.string.read_config), |
||||
appCtx.getString(R.string.theme_mode), |
||||
appCtx.getString(R.string.bookshelf_layout), |
||||
appCtx.getString(R.string.show_rss), |
||||
appCtx.getString(R.string.thread_count) |
||||
) |
||||
|
||||
//默认忽略keys |
||||
private val ignorePrefKeys = arrayOf( |
||||
PreferKey.themeMode, |
||||
PreferKey.defaultCover, |
||||
PreferKey.defaultCoverDark |
||||
) |
||||
|
||||
//阅读配置 |
||||
private val readPrefKeys = arrayOf( |
||||
PreferKey.readStyleSelect, |
||||
PreferKey.shareLayout, |
||||
PreferKey.hideStatusBar, |
||||
PreferKey.hideNavigationBar, |
||||
PreferKey.autoReadSpeed |
||||
) |
||||
|
||||
|
||||
protected fun keyIsNotIgnore(key: String): Boolean { |
||||
return when { |
||||
ignorePrefKeys.contains(key) -> false |
||||
readPrefKeys.contains(key) && ignoreReadConfig -> false |
||||
PreferKey.themeMode == key && ignoreThemeMode -> false |
||||
PreferKey.bookshelfLayout == key && ignoreBookshelfLayout -> false |
||||
PreferKey.showRss == key && ignoreShowRss -> false |
||||
PreferKey.threadCount == key && ignoreThreadCount -> false |
||||
else -> true |
||||
} |
||||
} |
||||
|
||||
protected val ignoreReadConfig: Boolean |
||||
get() = ignoreConfig["readConfig"] == true |
||||
private val ignoreThemeMode: Boolean |
||||
get() = ignoreConfig[PreferKey.themeMode] == true |
||||
private val ignoreBookshelfLayout: Boolean |
||||
get() = ignoreConfig[PreferKey.bookshelfLayout] == true |
||||
private val ignoreShowRss: Boolean |
||||
get() = ignoreConfig[PreferKey.showRss] == true |
||||
private val ignoreThreadCount: Boolean |
||||
get() = ignoreConfig[PreferKey.threadCount] == true |
||||
|
||||
fun saveIgnoreConfig() { |
||||
val json = GSON.toJson(ignoreConfig) |
||||
FileUtils.createFileIfNotExist(ignoreConfigPath).writeText(json) |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue