pull/69/head
kunfei 5 years ago
parent 33a1a5ccef
commit c8f2ece30f
  1. 55
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  2. 32
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt

@ -12,6 +12,7 @@ import android.view.Gravity
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -20,11 +21,13 @@ import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.help.FileHelp
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.ui.book.read.Help import io.legado.app.ui.book.read.Help
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
@ -32,6 +35,7 @@ import kotlinx.android.synthetic.main.dialog_read_bg_text.*
import kotlinx.android.synthetic.main.item_bg_image.view.* import kotlinx.android.synthetic.main.item_bg_image.view.*
import org.jetbrains.anko.sdk27.listeners.onCheckedChange import org.jetbrains.anko.sdk27.listeners.onCheckedChange
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.io.File
class BgTextConfigDialog : DialogFragment() { class BgTextConfigDialog : DialogFragment() {
@ -133,17 +137,10 @@ class BgTextConfigDialog : DialogFragment() {
} }
private fun selectImage() { private fun selectImage() {
PermissionsCompat.Builder(this) val intent = Intent(Intent.ACTION_GET_CONTENT)
.addPermissions(Permissions.READ_EXTERNAL_STORAGE, Permissions.WRITE_EXTERNAL_STORAGE) intent.addCategory(Intent.CATEGORY_OPENABLE)
.rationale(R.string.bg_image_per) intent.type = "image/*"
.onGranted { startActivityForResult(intent, resultSelectBg)
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, resultSelectBg)
Unit
}
.request()
} }
class BgAdapter(context: Context) : class BgAdapter(context: Context) :
@ -170,11 +167,37 @@ class BgTextConfigDialog : DialogFragment() {
when (requestCode) { when (requestCode) {
resultSelectBg -> { resultSelectBg -> {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
data?.data?.let { data?.data?.let { uri ->
FileUtils.getPath(requireContext(), it)?.let { path -> if (DocumentFile.isDocumentUri(requireContext(), uri)) {
ReadBookConfig.getConfig().setBg(2, path) val doc = DocumentFile.fromSingleUri(requireContext(), uri)
ReadBookConfig.upBg() doc?.let {
postEvent(Bus.UP_CONFIG, false) var file = requireContext().getExternalFilesDir(null)
?: requireContext().filesDir
file =
FileHelp.getFile(file.absolutePath + File.separator + "bg" + File.separator + doc.name)
DocumentUtils.readBytes(requireContext(), uri)?.let {
file.writeBytes(it)
ReadBookConfig.getConfig().setBg(2, file.absolutePath)
ReadBookConfig.upBg()
postEvent(Bus.UP_CONFIG, false)
}
}
} else {
PermissionsCompat.Builder(this)
.addPermissions(
Permissions.READ_EXTERNAL_STORAGE,
Permissions.WRITE_EXTERNAL_STORAGE
)
.rationale(R.string.bg_image_per)
.onGranted {
FileUtils.getPath(requireContext(), uri)?.let { path ->
ReadBookConfig.getConfig().setBg(2, path)
ReadBookConfig.upBg()
postEvent(Bus.UP_CONFIG, false)
}
Unit
}
.request()
} }
} }
} }

@ -6,25 +6,24 @@ import android.net.Uri
object DocumentUtils { object DocumentUtils {
@JvmStatic @JvmStatic
@Throws(Exception::class)
fun writeText(context: Context, data: String, fileUri: Uri): Boolean { fun writeText(context: Context, data: String, fileUri: Uri): Boolean {
return writeBytes(context, data.toByteArray(), fileUri) return writeBytes(context, data.toByteArray(), fileUri)
} }
@JvmStatic @JvmStatic
@Throws(Exception::class)
fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean { fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean {
try { context.contentResolver.openOutputStream(fileUri)?.let {
context.contentResolver.openOutputStream(fileUri)?.let { it.write(data)
it.write(data) it.close()
it.close() return true
return true
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
} }
return false return false
} }
@JvmStatic @JvmStatic
@Throws(Exception::class)
fun readText(context: Context, uri: Uri): String? { fun readText(context: Context, uri: Uri): String? {
readBytes(context, uri)?.let { readBytes(context, uri)?.let {
return String(it) return String(it)
@ -33,17 +32,14 @@ object DocumentUtils {
} }
@JvmStatic @JvmStatic
@Throws(Exception::class)
fun readBytes(context: Context, uri: Uri): ByteArray? { fun readBytes(context: Context, uri: Uri): ByteArray? {
try { context.contentResolver.openInputStream(uri)?.let {
context.contentResolver.openInputStream(uri)?.let { val len: Int = it.available()
val len: Int = it.available() val buffer = ByteArray(len)
val buffer = ByteArray(len) it.read(buffer)
it.read(buffer) it.close()
it.close() return buffer
return buffer
}
} catch (e: Exception) {
e.printStackTrace()
} }
return null return null
} }

Loading…
Cancel
Save