分享新文件前先删除原有文件

pull/1006/head
gedoor 4 years ago
parent 334356ce92
commit e3eee57d80
  1. 4
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 10
      app/src/main/java/io/legado/app/service/DownloadService.kt
  3. 12
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  4. 12
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt
  5. 8
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -2,6 +2,7 @@ package io.legado.app.constant
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.provider.Settings import android.provider.Settings
import io.legado.app.BuildConfig
import io.legado.app.R import io.legado.app.R
import splitties.init.appCtx import splitties.init.appCtx
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -103,4 +104,7 @@ object AppConst {
var versionCode: Long = 0L, var versionCode: Long = 0L,
var versionName: String = "" var versionName: String = ""
) )
const val authority = BuildConfig.APPLICATION_ID + ".fileProvider"
} }

@ -12,7 +12,6 @@ import android.os.Looper
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import io.legado.app.BuildConfig
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
@ -133,14 +132,9 @@ class DownloadService : BaseService() {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0版本以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0版本以上
val uriForFile: Uri = val contentUrl = FileProvider.getUriForFile(this, AppConst.authority, file)
FileProvider.getUriForFile(
this,
"${BuildConfig.APPLICATION_ID}.fileProvider",
file
)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.setDataAndType(uriForFile, "application/vnd.android.package-archive") intent.setDataAndType(contentUrl, "application/vnd.android.package-archive")
} else { } else {
val uri: Uri = Uri.fromFile(file) val uri: Uri = Uri.fromFile(file)
intent.setDataAndType(uri, "application/vnd.android.package-archive") intent.setDataAndType(uri, "application/vnd.android.package-archive")

@ -5,8 +5,8 @@ import android.content.Intent
import android.text.TextUtils import android.text.TextUtils
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.BuildConfig
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
@ -164,14 +164,12 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
fun shareSelection(sources: List<BookSource>, success: ((intent: Intent) -> Unit)) { fun shareSelection(sources: List<BookSource>, success: ((intent: Intent) -> Unit)) {
execute { execute {
val tmpSharePath = "${context.filesDir}/shareBookSource.json"
FileUtils.delete(tmpSharePath)
val intent = Intent(Intent.ACTION_SEND) val intent = Intent(Intent.ACTION_SEND)
val file = FileUtils.createFileWithReplace("${context.filesDir}/shareBookSource.json") val file = FileUtils.createFileWithReplace(tmpSharePath)
file.writeText(GSON.toJson(sources)) file.writeText(GSON.toJson(sources))
val fileUri = FileProvider.getUriForFile( val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file)
context,
BuildConfig.APPLICATION_ID + ".fileProvider",
file
)
intent.type = "text/*" intent.type = "text/*"
intent.putExtra(Intent.EXTRA_STREAM, fileUri) intent.putExtra(Intent.EXTRA_STREAM, fileUri)
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION

@ -5,8 +5,8 @@ import android.content.Intent
import android.text.TextUtils import android.text.TextUtils
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.BuildConfig
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
@ -106,14 +106,12 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
fun shareSelection(sources: List<RssSource>, success: ((intent: Intent) -> Unit)) { fun shareSelection(sources: List<RssSource>, success: ((intent: Intent) -> Unit)) {
execute { execute {
val tmpSharePath = "${context.filesDir}/shareRssSource.json"
FileUtils.delete(tmpSharePath)
val intent = Intent(Intent.ACTION_SEND) val intent = Intent(Intent.ACTION_SEND)
val file = FileUtils.createFileWithReplace("${context.filesDir}/shareRssSource.json") val file = FileUtils.createFileWithReplace(tmpSharePath)
file.writeText(GSON.toJson(sources)) file.writeText(GSON.toJson(sources))
val fileUri = FileProvider.getUriForFile( val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file)
context,
BuildConfig.APPLICATION_ID + ".fileProvider",
file
)
intent.type = "text/*" intent.type = "text/*"
intent.putExtra(Intent.EXTRA_STREAM, fileUri) intent.putExtra(Intent.EXTRA_STREAM, fileUri)
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION

@ -21,8 +21,8 @@ import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import io.legado.app.BuildConfig
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppConst
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -158,11 +158,7 @@ fun Context.shareWithQr(text: String, title: String = getString(R.string.share))
fOut.flush() fOut.flush()
fOut.close() fOut.close()
file.setReadable(true, false) file.setReadable(true, false)
val contentUri = FileProvider.getUriForFile( val contentUri = FileProvider.getUriForFile(this, AppConst.authority, file)
this,
"${BuildConfig.APPLICATION_ID}.fileProvider",
file
)
val intent = Intent(Intent.ACTION_SEND) val intent = Intent(Intent.ACTION_SEND)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(Intent.EXTRA_STREAM, contentUri) intent.putExtra(Intent.EXTRA_STREAM, contentUri)

Loading…
Cancel
Save