From e3eee57d80340acb46225f788b24e56214df588f Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 21 May 2021 10:30:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E4=BA=AB=E6=96=B0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=89=8D=E5=85=88=E5=88=A0=E9=99=A4=E5=8E=9F=E6=9C=89=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/constant/AppConst.kt | 4 ++++ .../java/io/legado/app/service/DownloadService.kt | 10 ++-------- .../app/ui/book/source/manage/BookSourceViewModel.kt | 12 +++++------- .../app/ui/rss/source/manage/RssSourceViewModel.kt | 12 +++++------- .../java/io/legado/app/utils/ContextExtensions.kt | 8 ++------ 5 files changed, 18 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index d7018c996..4938d3e15 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -2,6 +2,7 @@ package io.legado.app.constant import android.annotation.SuppressLint import android.provider.Settings +import io.legado.app.BuildConfig import io.legado.app.R import splitties.init.appCtx import java.text.SimpleDateFormat @@ -103,4 +104,7 @@ object AppConst { var versionCode: Long = 0L, var versionName: String = "" ) + + const val authority = BuildConfig.APPLICATION_ID + ".fileProvider" + } diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index 134ca55eb..990271808 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -12,7 +12,6 @@ import android.os.Looper import androidx.core.app.NotificationCompat import androidx.core.content.FileProvider import androidx.core.os.bundleOf -import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.AppConst @@ -133,14 +132,9 @@ class DownloadService : BaseService() { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0版本以上 - val uriForFile: Uri = - FileProvider.getUriForFile( - this, - "${BuildConfig.APPLICATION_ID}.fileProvider", - file - ) + val contentUrl = FileProvider.getUriForFile(this, AppConst.authority, file) 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 { val uri: Uri = Uri.fromFile(file) intent.setDataAndType(uri, "application/vnd.android.package-archive") diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 92fb7e4fa..1432429ee 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -5,8 +5,8 @@ import android.content.Intent import android.text.TextUtils import androidx.core.content.FileProvider import androidx.documentfile.provider.DocumentFile -import io.legado.app.BuildConfig import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource @@ -164,14 +164,12 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun shareSelection(sources: List, success: ((intent: Intent) -> Unit)) { execute { + val tmpSharePath = "${context.filesDir}/shareBookSource.json" + FileUtils.delete(tmpSharePath) val intent = Intent(Intent.ACTION_SEND) - val file = FileUtils.createFileWithReplace("${context.filesDir}/shareBookSource.json") + val file = FileUtils.createFileWithReplace(tmpSharePath) file.writeText(GSON.toJson(sources)) - val fileUri = FileProvider.getUriForFile( - context, - BuildConfig.APPLICATION_ID + ".fileProvider", - file - ) + val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file) intent.type = "text/*" intent.putExtra(Intent.EXTRA_STREAM, fileUri) intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index a89dc2e7f..408ca10f8 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -5,8 +5,8 @@ import android.content.Intent import android.text.TextUtils import androidx.core.content.FileProvider import androidx.documentfile.provider.DocumentFile -import io.legado.app.BuildConfig import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.data.appDb import io.legado.app.data.entities.RssSource import io.legado.app.help.DefaultData @@ -106,14 +106,12 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) fun shareSelection(sources: List, success: ((intent: Intent) -> Unit)) { execute { + val tmpSharePath = "${context.filesDir}/shareRssSource.json" + FileUtils.delete(tmpSharePath) val intent = Intent(Intent.ACTION_SEND) - val file = FileUtils.createFileWithReplace("${context.filesDir}/shareRssSource.json") + val file = FileUtils.createFileWithReplace(tmpSharePath) file.writeText(GSON.toJson(sources)) - val fileUri = FileProvider.getUriForFile( - context, - BuildConfig.APPLICATION_ID + ".fileProvider", - file - ) + val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file) intent.type = "text/*" intent.putExtra(Intent.EXTRA_STREAM, fileUri) intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index d6644e35a..c1d5e50e3 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -21,8 +21,8 @@ import androidx.core.content.ContextCompat import androidx.core.content.FileProvider import androidx.core.content.edit import androidx.preference.PreferenceManager -import io.legado.app.BuildConfig import io.legado.app.R +import io.legado.app.constant.AppConst import java.io.File import java.io.FileOutputStream @@ -158,11 +158,7 @@ fun Context.shareWithQr(text: String, title: String = getString(R.string.share)) fOut.flush() fOut.close() file.setReadable(true, false) - val contentUri = FileProvider.getUriForFile( - this, - "${BuildConfig.APPLICATION_ID}.fileProvider", - file - ) + val contentUri = FileProvider.getUriForFile(this, AppConst.authority, file) val intent = Intent(Intent.ACTION_SEND) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.putExtra(Intent.EXTRA_STREAM, contentUri)