From 8d1199925f793e6fbe2c5577c7a164a41fdd9063 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 24 Mar 2021 20:41:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 4 - .../permission => constant}/Permissions.kt | 2 +- .../app/help/permission/ActivitySource.kt | 20 -- .../app/help/permission/FragmentSource.kt | 19 -- .../permission/OnPermissionsDeniedCallback.kt | 7 - .../OnPermissionsGrantedCallback.kt | 7 - .../permission/OnPermissionsResultCallback.kt | 9 - .../OnRequestPermissionsResultCallback.kt | 10 - .../app/help/permission/PermissionActivity.kt | 76 ------- .../app/help/permission/PermissionsCompat.kt | 91 -------- .../io/legado/app/help/permission/Request.kt | 204 ------------------ .../app/help/permission/RequestManager.kt | 69 ------ .../app/help/permission/RequestPlugins.kt | 20 -- .../app/help/permission/RequestSource.kt | 12 -- .../app/ui/book/info/BookInfoViewModel.kt | 6 +- .../ui/book/info/edit/BookInfoEditActivity.kt | 51 ++--- .../app/ui/book/local/ImportBookActivity.kt | 20 +- .../ui/book/read/config/BgTextConfigDialog.kt | 37 +--- .../legado/app/ui/config/BackupRestoreUi.kt | 38 ++-- .../app/ui/config/OtherConfigFragment.kt | 31 ++- .../app/ui/config/ThemeConfigFragment.kt | 89 +++----- .../io/legado/app/ui/filepicker/FilePicker.kt | 24 +-- .../app/ui/rss/read/ReadRssViewModel.kt | 2 +- .../app/ui/widget/font/FontSelectDialog.kt | 14 +- 24 files changed, 111 insertions(+), 751 deletions(-) rename app/src/main/java/io/legado/app/{help/permission => constant}/Permissions.kt (98%) delete mode 100644 app/src/main/java/io/legado/app/help/permission/ActivitySource.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/FragmentSource.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/OnPermissionsDeniedCallback.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/OnPermissionsGrantedCallback.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/OnPermissionsResultCallback.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/OnRequestPermissionsResultCallback.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/PermissionActivity.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/Request.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/RequestManager.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/RequestPlugins.kt delete mode 100644 app/src/main/java/io/legado/app/help/permission/RequestSource.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 85914dd9d..9a149a441 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -171,10 +171,6 @@ - - = WeakReference(activity) - - override val context: Context? - get() = actRef.get() - - override fun startActivity(intent: Intent) { - actRef.get()?.startActivity(intent) - } - -} diff --git a/app/src/main/java/io/legado/app/help/permission/FragmentSource.kt b/app/src/main/java/io/legado/app/help/permission/FragmentSource.kt deleted file mode 100644 index b66e11b98..000000000 --- a/app/src/main/java/io/legado/app/help/permission/FragmentSource.kt +++ /dev/null @@ -1,19 +0,0 @@ -package io.legado.app.help.permission - -import android.content.Context -import android.content.Intent -import androidx.fragment.app.Fragment - -import java.lang.ref.WeakReference - -internal class FragmentSource(fragment: Fragment) : RequestSource { - - private val fragRef: WeakReference = WeakReference(fragment) - - override val context: Context? - get() = fragRef.get()?.requireContext() - - override fun startActivity(intent: Intent) { - fragRef.get()?.startActivity(intent) - } -} diff --git a/app/src/main/java/io/legado/app/help/permission/OnPermissionsDeniedCallback.kt b/app/src/main/java/io/legado/app/help/permission/OnPermissionsDeniedCallback.kt deleted file mode 100644 index 4a51881b0..000000000 --- a/app/src/main/java/io/legado/app/help/permission/OnPermissionsDeniedCallback.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.legado.app.help.permission - -interface OnPermissionsDeniedCallback { - - fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array) - -} diff --git a/app/src/main/java/io/legado/app/help/permission/OnPermissionsGrantedCallback.kt b/app/src/main/java/io/legado/app/help/permission/OnPermissionsGrantedCallback.kt deleted file mode 100644 index 59f6977d4..000000000 --- a/app/src/main/java/io/legado/app/help/permission/OnPermissionsGrantedCallback.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.legado.app.help.permission - -interface OnPermissionsGrantedCallback { - - fun onPermissionsGranted(requestCode: Int) - -} diff --git a/app/src/main/java/io/legado/app/help/permission/OnPermissionsResultCallback.kt b/app/src/main/java/io/legado/app/help/permission/OnPermissionsResultCallback.kt deleted file mode 100644 index 3d7afa600..000000000 --- a/app/src/main/java/io/legado/app/help/permission/OnPermissionsResultCallback.kt +++ /dev/null @@ -1,9 +0,0 @@ -package io.legado.app.help.permission - -interface OnPermissionsResultCallback { - - fun onPermissionsGranted(requestCode: Int) - - fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array) - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/permission/OnRequestPermissionsResultCallback.kt b/app/src/main/java/io/legado/app/help/permission/OnRequestPermissionsResultCallback.kt deleted file mode 100644 index 3674461bc..000000000 --- a/app/src/main/java/io/legado/app/help/permission/OnRequestPermissionsResultCallback.kt +++ /dev/null @@ -1,10 +0,0 @@ -package io.legado.app.help.permission - -import android.content.Intent - -interface OnRequestPermissionsResultCallback { - - fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) - - fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) -} diff --git a/app/src/main/java/io/legado/app/help/permission/PermissionActivity.kt b/app/src/main/java/io/legado/app/help/permission/PermissionActivity.kt deleted file mode 100644 index bd27e56e9..000000000 --- a/app/src/main/java/io/legado/app/help/permission/PermissionActivity.kt +++ /dev/null @@ -1,76 +0,0 @@ -package io.legado.app.help.permission - -import android.content.Intent -import android.net.Uri -import android.os.Bundle -import android.provider.Settings -import android.view.KeyEvent -import androidx.appcompat.app.AppCompatActivity -import androidx.core.app.ActivityCompat -import io.legado.app.R -import io.legado.app.utils.toastOnUi - -class PermissionActivity : AppCompatActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - when (intent.getIntExtra(KEY_INPUT_REQUEST_TYPE, Request.TYPE_REQUEST_PERMISSION)) { - Request.TYPE_REQUEST_PERMISSION//权限请求 - -> { - val requestCode = intent.getIntExtra(KEY_INPUT_PERMISSIONS_CODE, 1000) - val permissions = intent.getStringArrayExtra(KEY_INPUT_PERMISSIONS) - if (permissions != null) { - ActivityCompat.requestPermissions(this, permissions, requestCode) - } else { - finish() - } - } - Request.TYPE_REQUEST_SETTING//跳转到设置界面 - -> try { - val settingIntent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - settingIntent.data = Uri.fromParts("package", packageName, null) - startActivityForResult(settingIntent, Request.TYPE_REQUEST_SETTING) - } catch (e: Exception) { - toastOnUi(R.string.tip_cannot_jump_setting_page) - finish() - } - - } - } - - override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults) - RequestPlugins.sRequestCallback?.onRequestPermissionsResult(requestCode, permissions, grantResults) - finish() - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - RequestPlugins.sRequestCallback?.onActivityResult(requestCode, resultCode, data) - finish() - } - - override fun startActivity(intent: Intent) { - super.startActivity(intent) - overridePendingTransition(0, 0) - } - - override fun finish() { - super.finish() - overridePendingTransition(0, 0) - } - - override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { - return if (keyCode == KeyEvent.KEYCODE_BACK) { - true - } else super.onKeyDown(keyCode, event) - } - - companion object { - - const val KEY_INPUT_REQUEST_TYPE = "KEY_INPUT_REQUEST_TYPE" - const val KEY_INPUT_PERMISSIONS_CODE = "KEY_INPUT_PERMISSIONS_CODE" - const val KEY_INPUT_PERMISSIONS = "KEY_INPUT_PERMISSIONS" - } -} diff --git a/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt b/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt deleted file mode 100644 index 1d118fd0e..000000000 --- a/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt +++ /dev/null @@ -1,91 +0,0 @@ -package io.legado.app.help.permission - -import androidx.annotation.StringRes -import androidx.appcompat.app.AppCompatActivity -import androidx.fragment.app.Fragment -import java.util.* - -@Suppress("unused") -class PermissionsCompat private constructor() { - - private var request: Request? = null - - fun request() { - RequestManager.pushRequest(request) - } - - companion object { - // 检查权限, 如果已经拥有返回 true - fun check(activity: AppCompatActivity, vararg permissions: String): Boolean { - val request = Request(activity) - val pers = ArrayList() - pers.addAll(listOf(*permissions)) - val data = request.getDeniedPermissions(pers.toTypedArray()) - return data == null - } - } - - class Builder { - private val request: Request - - constructor(activity: AppCompatActivity) { - request = Request(activity) - } - - constructor(fragment: Fragment) { - request = Request(fragment) - } - - fun addPermissions(vararg permissions: String): Builder { - request.addPermissions(*permissions) - return this - } - - fun requestCode(requestCode: Int): Builder { - request.setRequestCode(requestCode) - return this - } - - fun onGranted(callback: (requestCode: Int) -> Unit): Builder { - request.setOnGrantedCallback(object : OnPermissionsGrantedCallback { - override fun onPermissionsGranted(requestCode: Int) { - callback(requestCode) - } - }) - return this - } - - fun onDenied(callback: (requestCode: Int, deniedPermissions: Array) -> Unit): Builder { - request.setOnDeniedCallback(object : OnPermissionsDeniedCallback { - override fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array) { - callback(requestCode, deniedPermissions) - } - }) - return this - } - - fun rationale(rationale: CharSequence): Builder { - request.setRationale(rationale) - return this - } - - fun rationale(@StringRes resId: Int): Builder { - request.setRationale(resId) - return this - } - - fun build(): PermissionsCompat { - val compat = PermissionsCompat() - compat.request = request - return compat - } - - fun request(): PermissionsCompat { - val compat = build() - compat.request = request - compat.request() - return compat - } - } - -} diff --git a/app/src/main/java/io/legado/app/help/permission/Request.kt b/app/src/main/java/io/legado/app/help/permission/Request.kt deleted file mode 100644 index 7a26f521f..000000000 --- a/app/src/main/java/io/legado/app/help/permission/Request.kt +++ /dev/null @@ -1,204 +0,0 @@ -package io.legado.app.help.permission - -import android.content.Intent -import android.content.pm.PackageManager -import android.os.Build -import androidx.annotation.StringRes -import androidx.appcompat.app.AlertDialog -import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat -import androidx.fragment.app.Fragment -import io.legado.app.R -import io.legado.app.utils.startActivity -import java.util.* - -internal class Request : OnRequestPermissionsResultCallback { - - internal val requestTime: Long - private var requestCode: Int = TYPE_REQUEST_PERMISSION - private var source: RequestSource? = null - private var permissions: ArrayList? = null - private var grantedCallback: OnPermissionsGrantedCallback? = null - private var deniedCallback: OnPermissionsDeniedCallback? = null - private var rationaleResId: Int = 0 - private var rationale: CharSequence? = null - - private var rationaleDialog: AlertDialog? = null - - private val deniedPermissions: Array? - get() { - return getDeniedPermissions(this.permissions?.toTypedArray()) - } - - constructor(activity: AppCompatActivity) { - source = ActivitySource(activity) - permissions = ArrayList() - requestTime = System.currentTimeMillis() - } - - constructor(fragment: Fragment) { - source = FragmentSource(fragment) - permissions = ArrayList() - requestTime = System.currentTimeMillis() - } - - fun addPermissions(vararg permissions: String) { - this.permissions?.addAll(listOf(*permissions)) - } - - fun setRequestCode(requestCode: Int) { - this.requestCode = requestCode - } - - fun setOnGrantedCallback(callback: OnPermissionsGrantedCallback) { - grantedCallback = callback - } - - fun setOnDeniedCallback(callback: OnPermissionsDeniedCallback) { - deniedCallback = callback - } - - fun setRationale(@StringRes resId: Int) { - rationaleResId = resId - rationale = null - } - - fun setRationale(rationale: CharSequence) { - this.rationale = rationale - rationaleResId = 0 - } - - fun start() { - RequestPlugins.setOnRequestPermissionsCallback(this) - - val deniedPermissions = deniedPermissions - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { - if (deniedPermissions == null) { - onPermissionsGranted(requestCode) - } else { - val rationale = - if (rationaleResId != 0) source?.context?.getText(rationaleResId) else rationale - if (rationale != null) { - showSettingDialog(rationale) { - onPermissionsDenied( - requestCode, - deniedPermissions - ) - } - } else { - onPermissionsDenied(requestCode, deniedPermissions) - } - } - } else { - if (deniedPermissions != null) { - source?.context?.startActivity { - putExtra(PermissionActivity.KEY_INPUT_REQUEST_TYPE, TYPE_REQUEST_PERMISSION) - putExtra(PermissionActivity.KEY_INPUT_PERMISSIONS_CODE, requestCode) - putExtra(PermissionActivity.KEY_INPUT_PERMISSIONS, deniedPermissions) - } - } else { - onPermissionsGranted(requestCode) - } - } - } - - fun clear() { - grantedCallback = null - deniedCallback = null - } - - fun getDeniedPermissions(permissions: Array?): Array? { - if (permissions != null) { - val deniedPermissionList = ArrayList() - for (permission in permissions) { - if (source?.context?.let { - ContextCompat.checkSelfPermission( - it, - permission - ) - } != PackageManager.PERMISSION_GRANTED - ) { - deniedPermissionList.add(permission) - } - } - val size = deniedPermissionList.size - if (size > 0) { - return deniedPermissionList.toTypedArray() - } - } - return null - } - - private fun showSettingDialog(rationale: CharSequence, cancel: () -> Unit) { - rationaleDialog?.dismiss() - source?.context?.let { - runCatching { - rationaleDialog = AlertDialog.Builder(it) - .setTitle(R.string.dialog_title) - .setMessage(rationale) - .setPositiveButton(R.string.dialog_setting) { _, _ -> - it.startActivity { - putExtra( - PermissionActivity.KEY_INPUT_REQUEST_TYPE, - TYPE_REQUEST_SETTING - ) - } - } - .setNegativeButton(R.string.dialog_cancel) { _, _ -> cancel() } - .show() - } - } - } - - private fun onPermissionsGranted(requestCode: Int) { - try { - grantedCallback?.onPermissionsGranted(requestCode) - } catch (ignore: Exception) { - } - - RequestPlugins.sResultCallback?.onPermissionsGranted(requestCode) - } - - private fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array) { - try { - deniedCallback?.onPermissionsDenied(requestCode, deniedPermissions) - } catch (ignore: Exception) { - } - - RequestPlugins.sResultCallback?.onPermissionsDenied(requestCode, deniedPermissions) - } - - override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResults: IntArray - ) { - val deniedPermissions = getDeniedPermissions(permissions) - if (deniedPermissions != null) { - val rationale = - if (rationaleResId != 0) source?.context?.getText(rationaleResId) else rationale - if (rationale != null) { - showSettingDialog(rationale) { onPermissionsDenied(requestCode, deniedPermissions) } - } else { - onPermissionsDenied(requestCode, deniedPermissions) - } - } else { - onPermissionsGranted(requestCode) - } - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - val deniedPermissions = deniedPermissions - if (deniedPermissions == null) { - onPermissionsGranted(this.requestCode) - } else { - onPermissionsDenied(this.requestCode, deniedPermissions) - } - } - - companion object { - const val TYPE_REQUEST_PERMISSION = 1 - const val TYPE_REQUEST_SETTING = 2 - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/permission/RequestManager.kt b/app/src/main/java/io/legado/app/help/permission/RequestManager.kt deleted file mode 100644 index eb28b9e6c..000000000 --- a/app/src/main/java/io/legado/app/help/permission/RequestManager.kt +++ /dev/null @@ -1,69 +0,0 @@ -package io.legado.app.help.permission - -import android.os.Handler -import android.os.Looper -import java.util.* - -internal object RequestManager : OnPermissionsResultCallback { - - private var requests: Stack? = null - private var request: Request? = null - - private val handler = Handler(Looper.getMainLooper()) - - private val requestRunnable = Runnable { - request?.start() - } - - private val isCurrentRequestInvalid: Boolean - get() = request?.let { System.currentTimeMillis() - it.requestTime > 5 * 1000L } ?: true - - init { - RequestPlugins.setOnPermissionsResultCallback(this) - } - - fun pushRequest(request: Request?) { - if (request == null) return - - if (requests == null) { - requests = Stack() - } - - requests?.let { - val index = it.indexOf(request) - if (index >= 0) { - val to = it.size - 1 - if (index != to) { - @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") - Collections.swap(requests, index, to) - } - } else { - it.push(request) - } - - if (!it.empty() && isCurrentRequestInvalid) { - this.request = it.pop() - handler.post(requestRunnable) - } - } - } - - private fun startNextRequest() { - request?.clear() - request = null - - requests?.let { - request = if (it.empty()) null else it.pop() - request?.let { handler.post(requestRunnable) } - } - } - - override fun onPermissionsGranted(requestCode: Int) { - startNextRequest() - } - - override fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array) { - startNextRequest() - } - -} diff --git a/app/src/main/java/io/legado/app/help/permission/RequestPlugins.kt b/app/src/main/java/io/legado/app/help/permission/RequestPlugins.kt deleted file mode 100644 index 16370193f..000000000 --- a/app/src/main/java/io/legado/app/help/permission/RequestPlugins.kt +++ /dev/null @@ -1,20 +0,0 @@ -package io.legado.app.help.permission - -internal object RequestPlugins { - - @Volatile - var sRequestCallback: OnRequestPermissionsResultCallback? = null - - @Volatile - var sResultCallback: OnPermissionsResultCallback? = null - - fun setOnRequestPermissionsCallback(callback: OnRequestPermissionsResultCallback) { - sRequestCallback = callback - } - - fun setOnPermissionsResultCallback(callback: OnPermissionsResultCallback) { - sResultCallback = callback - } - - -} diff --git a/app/src/main/java/io/legado/app/help/permission/RequestSource.kt b/app/src/main/java/io/legado/app/help/permission/RequestSource.kt deleted file mode 100644 index a822ff5ad..000000000 --- a/app/src/main/java/io/legado/app/help/permission/RequestSource.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.legado.app.help.permission - -import android.content.Context -import android.content.Intent - -interface RequestSource { - - val context: Context? - - fun startActivity(intent: Intent) - -} diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 8348dca02..3cb698ef3 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -68,7 +68,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { toastOnUi(R.string.error_get_book_info) } } ?: let { - chapterListData.postValue(null) + chapterListData.postValue(emptyList()) toastOnUi(R.string.error_no_source) } } @@ -104,11 +104,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { toastOnUi(R.string.chapter_list_empty) } }.onError { - chapterListData.postValue(null) + chapterListData.postValue(emptyList()) toastOnUi(R.string.error_get_chapter_list) } } ?: let { - chapterListData.postValue(null) + chapterListData.postValue(emptyList()) toastOnUi(R.string.error_no_source) } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index 4d16ba6bd..840b2f54e 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -1,19 +1,18 @@ package io.legado.app.ui.book.info.edit import android.app.Activity -import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.documentfile.provider.DocumentFile import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Permissions import io.legado.app.data.entities.Book import io.legado.app.databinding.ActivityBookInfoEditBinding -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.ui.book.changecover.ChangeCoverDialog import io.legado.app.utils.* import java.io.File @@ -22,8 +21,6 @@ class BookInfoEditActivity : VMBaseActivity(), ChangeCoverDialog.CallBack { - private val resultSelectCover = 132 - override val viewModel: BookInfoEditViewModel by viewModels() @@ -97,10 +94,9 @@ class BookInfoEditActivity : } private fun selectImage() { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "image/*" - startActivityForResult(intent, resultSelectCover) + registerForActivityResult(ActivityResultContracts.GetContent()) { + coverChangeTo(it) + }.launch("image/*") } override fun coverChangeTo(coverUrl: String) { @@ -123,37 +119,18 @@ class BookInfoEditActivity : } ?: toastOnUi("获取文件出错") } } else { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - RealPathUtil.getPath(this, uri)?.let { path -> - val imgFile = File(path) - if (imgFile.exists()) { - var file = this.externalFilesDir - file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) - file.writeBytes(imgFile.readBytes()) - coverChangeTo(file.absolutePath) - } + registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + RealPathUtil.getPath(this, uri)?.let { path -> + val imgFile = File(path) + if (imgFile.exists()) { + var file = this.externalFilesDir + file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) + file.writeBytes(imgFile.readBytes()) + coverChangeTo(file.absolutePath) } } - .request() + }.launch(Permissions.Group.STORAGE) } } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (requestCode) { - resultSelectCover -> { - if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - coverChangeTo(uri) - } - } - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index 776d980c5..9cb291e96 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -8,6 +8,7 @@ import android.os.Bundle import android.provider.DocumentsContract import android.view.Menu import android.view.MenuItem +import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.documentfile.provider.DocumentFile @@ -15,11 +16,10 @@ import androidx.lifecycle.LiveData import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.Permissions import io.legado.app.data.appDb import io.legado.app.databinding.ActivityImportBookBinding import io.legado.app.help.AppConfig -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.theme.backgroundColor import io.legado.app.ui.filepicker.FilePicker import io.legado.app.ui.filepicker.FilePickerDialog @@ -148,16 +148,12 @@ class ImportBookActivity : VMBaseActivity { binding.tvEmptyMsg.visible() - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - rootDoc = null - subDocs.clear() - path = lastPath - upPath() - } - .request() + registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + rootDoc = null + subDocs.clear() + path = lastPath + upPath() + }.launch(Permissions.Group.STORAGE) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 7b4eee738..e9d8775de 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -8,17 +8,17 @@ import android.graphics.Color import android.net.Uri import android.os.Bundle import android.view.* +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import com.jaredrummler.android.colorpicker.ColorPickerDialog import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.constant.EventBus +import io.legado.app.constant.Permissions import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.databinding.DialogReadBgTextBinding import io.legado.app.databinding.ItemBgImageBinding import io.legado.app.help.ReadBookConfig -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.bottomBackground import io.legado.app.lib.theme.getPrimaryTextColor @@ -40,7 +40,6 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { } private val binding by viewBinding(DialogReadBgTextBinding::bind) - private val requestCodeBg = 123 private val requestCodeExport = 131 private val requestCodeImport = 132 private val configFileName = "readConfig.zip" @@ -191,10 +190,9 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { } private fun selectImage() { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "image/*" - startActivityForResult(intent, requestCodeBg) + registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it) + }.launch("image/*") } @Suppress("BlockingMethodInNonBlockingContext") @@ -371,13 +369,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) when (requestCode) { - requestCodeBg -> if (resultCode == RESULT_OK) { - data?.data?.let { uri -> - setBgFromUri(uri) - } - } requestCodeImport -> if (resultCode == RESULT_OK) { data?.data?.let { uri -> importConfig(uri) @@ -407,20 +399,13 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { } ?: toastOnUi("获取文件出错") } } else { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - RealPathUtil.getPath(requireContext(), uri)?.let { path -> - ReadBookConfig.durConfig.setCurBg(2, path) - ReadBookConfig.upBg() - postEvent(EventBus.UP_CONFIG, false) - } + registerForActivityResult(ActivityResultContracts.RequestPermission()) { + RealPathUtil.getPath(requireContext(), uri)?.let { path -> + ReadBookConfig.durConfig.setCurBg(2, path) + ReadBookConfig.upBg() + postEvent(EventBus.UP_CONFIG, false) } - .request() + }.launch(Permissions.READ_EXTERNAL_STORAGE) } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt index d538cc7a8..17c067fd3 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt @@ -3,14 +3,14 @@ package io.legado.app.ui.config import android.app.Activity.RESULT_OK import android.content.Intent import android.net.Uri +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import androidx.fragment.app.Fragment import io.legado.app.R +import io.legado.app.constant.Permissions import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.help.storage.Backup import io.legado.app.help.storage.BookWebDav import io.legado.app.help.storage.ImportOldData @@ -56,18 +56,14 @@ object BackupRestoreUi { fragment: Fragment, path: String ) { - PermissionsCompat.Builder(fragment) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - Coroutine.async { - AppConfig.backupPath = path - Backup.backup(fragment.requireContext(), path) - }.onSuccess { - fragment.toastOnUi(R.string.backup_success) - } + fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + Coroutine.async { + AppConfig.backupPath = path + Backup.backup(fragment.requireContext(), path) + }.onSuccess { + fragment.toastOnUi(R.string.backup_success) } - .request() + }.launch(Permissions.Group.STORAGE) } fun selectBackupFolder(fragment: Fragment, requestCode: Int = selectFolderRequestCode) { @@ -103,17 +99,13 @@ object BackupRestoreUi { } private fun restoreUsePermission(fragment: Fragment, path: String) { - PermissionsCompat.Builder(fragment) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - Coroutine.async { - AppConfig.backupPath = path - Restore.restoreDatabase(path) - Restore.restoreConfig(path) - } + fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + Coroutine.async { + AppConfig.backupPath = path + Restore.restoreDatabase(path) + Restore.restoreConfig(path) } - .request() + }.launch(Permissions.Group.STORAGE) } fun importOldData(fragment: Fragment) { diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index b44909711..5d94d55e9 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -10,18 +10,18 @@ import android.net.Uri import android.os.Bundle import android.os.Process import android.view.View +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import androidx.preference.ListPreference import androidx.preference.Preference import io.legado.app.R import io.legado.app.base.BasePreferenceFragment import io.legado.app.constant.EventBus +import io.legado.app.constant.Permissions import io.legado.app.constant.PreferKey import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.ATH @@ -224,25 +224,18 @@ class OtherConfigFragment : BasePreferenceFragment(), } ?: toastOnUi("获取文件出错") } } else { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - RealPathUtil.getPath(requireContext(), uri)?.let { path -> - val imgFile = File(path) - if (imgFile.exists()) { - var file = requireContext().externalFilesDir - file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) - file.writeBytes(imgFile.readBytes()) - putPrefString(PreferKey.defaultCover, file.absolutePath) - CoverImageView.upDefaultCover() - } + registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + RealPathUtil.getPath(requireContext(), uri)?.let { path -> + val imgFile = File(path) + if (imgFile.exists()) { + var file = requireContext().externalFilesDir + file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) + file.writeBytes(imgFile.readBytes()) + putPrefString(PreferKey.defaultCover, file.absolutePath) + CoverImageView.upDefaultCover() } } - .request() + }.launch(Permissions.Group.STORAGE) } } diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 834e53eb9..4074f32b6 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -1,8 +1,6 @@ package io.legado.app.ui.config import android.annotation.SuppressLint -import android.app.Activity -import android.content.Intent import android.content.SharedPreferences import android.net.Uri import android.os.Build @@ -11,19 +9,19 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import androidx.preference.Preference import io.legado.app.R import io.legado.app.base.BasePreferenceFragment import io.legado.app.constant.AppConst import io.legado.app.constant.EventBus +import io.legado.app.constant.Permissions import io.legado.app.constant.PreferKey import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig import io.legado.app.help.LauncherIconHelp import io.legado.app.help.ThemeConfig -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.ATH @@ -38,9 +36,6 @@ import java.io.File class ThemeConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { - private val requestCodeBgImage = 234 - private val requestCodeBgImageN = 342 - override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) if (Build.VERSION.SDK_INT < 26) { @@ -186,26 +181,42 @@ class ThemeConfigFragment : BasePreferenceFragment(), "themeList" -> ThemeListDialog().show(childFragmentManager, "themeList") "saveDayTheme", "saveNightTheme" -> saveThemeAlert(key) PreferKey.bgImage -> if (getPrefString(PreferKey.bgImage).isNullOrEmpty()) { - selectImage(requestCodeBgImage) + registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImage) { + upTheme(false) + } + }.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImage) upTheme(false) } else { - selectImage(requestCodeBgImage) + registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImage) { + upTheme(false) + } + }.launch("image/*") } } } PreferKey.bgImageN -> if (getPrefString(PreferKey.bgImageN).isNullOrEmpty()) { - selectImage(requestCodeBgImageN) + registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImageN) { + upTheme(false) + } + }.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImageN) upTheme(true) } else { - selectImage(requestCodeBgImageN) + registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImageN) { + upTheme(false) + } + }.launch("image/*") } } } @@ -213,13 +224,6 @@ class ThemeConfigFragment : BasePreferenceFragment(), return super.onPreferenceTreeClick(preference) } - private fun selectImage(requestCode: Int) { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "image/*" - startActivityForResult(intent, requestCode) - } - @SuppressLint("InflateParams") private fun saveThemeAlert(key: String) { alert(R.string.theme_name) { @@ -279,46 +283,19 @@ class ThemeConfigFragment : BasePreferenceFragment(), } ?: toastOnUi("获取文件出错") } } else { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - RealPathUtil.getPath(requireContext(), uri)?.let { path -> - val imgFile = File(path) - if (imgFile.exists()) { - var file = requireContext().externalFilesDir - file = FileUtils.createFileIfNotExist(file, preferenceKey, imgFile.name) - file.writeBytes(imgFile.readBytes()) - putPrefString(preferenceKey, file.absolutePath) - upPreferenceSummary(preferenceKey, file.absolutePath) - success() - } + registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + RealPathUtil.getPath(requireContext(), uri)?.let { path -> + val imgFile = File(path) + if (imgFile.exists()) { + var file = requireContext().externalFilesDir + file = FileUtils.createFileIfNotExist(file, preferenceKey, imgFile.name) + file.writeBytes(imgFile.readBytes()) + putPrefString(preferenceKey, file.absolutePath) + upPreferenceSummary(preferenceKey, file.absolutePath) + success() } } - .request() - } - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (requestCode) { - requestCodeBgImage -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - setBgFromUri(uri, PreferKey.bgImage) { - upTheme(false) - } - } - } - requestCodeBgImageN -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - setBgFromUri(uri, PreferKey.bgImageN) { - upTheme(true) - } - } - } + }.launch(Permissions.Group.STORAGE) } } diff --git a/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt b/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt index ff099f9ff..68151ce0f 100644 --- a/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt +++ b/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt @@ -2,11 +2,11 @@ package io.legado.app.ui.filepicker import android.content.Intent import android.os.Build +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import io.legado.app.R -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat +import io.legado.app.constant.Permissions import io.legado.app.lib.dialogs.alert @Suppress("unused") @@ -247,23 +247,15 @@ object FilePicker { } private fun checkPermissions(fragment: Fragment, success: (() -> Unit)? = null) { - PermissionsCompat.Builder(fragment) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - success?.invoke() - } - .request() + fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + success?.invoke() + }.launch(Permissions.Group.STORAGE) } private fun checkPermissions(activity: AppCompatActivity, success: (() -> Unit)? = null) { - PermissionsCompat.Builder(activity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - success?.invoke() - } - .request() + activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + success?.invoke() + }.launch(Permissions.Group.STORAGE) } private fun typesOfExtensions(allowExtensions: Array): Array { diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index f4cc85fff..cf3574f2e 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -53,7 +53,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), rssArticle = rssStar?.toRssArticle() ?: appDb.rssArticleDao.get(origin, link) rssArticle?.let { rssArticle -> if (!rssArticle.description.isNullOrBlank()) { - contentLiveData.postValue(rssArticle.description) + contentLiveData.postValue(rssArticle.description!!) } else { rssSource?.let { val ruleContent = it.ruleContent diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index 004d3204b..8f6b7bf2f 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -8,16 +8,16 @@ import android.view.LayoutInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.widget.Toolbar import androidx.documentfile.provider.DocumentFile import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment +import io.legado.app.constant.Permissions import io.legado.app.constant.PreferKey import io.legado.app.databinding.DialogFontSelectBinding import io.legado.app.help.AppConfig -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.filepicker.FilePicker @@ -162,13 +162,9 @@ class FontSelectDialog : BaseDialogFragment(), } private fun loadFontFilesByPermission(path: String) { - PermissionsCompat.Builder(this@FontSelectDialog) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - loadFontFiles(path) - } - .request() + registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + loadFontFiles(path) + }.launch(Permissions.Group.STORAGE) } private fun loadFontFiles(path: String) {