修改弃用方法

pull/921/head
gedoor 4 years ago
parent 4e28806c2b
commit 16d3a60c7f
  1. 28
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  2. 106
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  3. 30
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  4. 17
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  5. 25
      app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt
  6. 26
      app/src/main/java/io/legado/app/ui/book/toc/TocActivityResult.kt
  7. 30
      app/src/main/java/io/legado/app/ui/filepicker/FilePickerActivity.kt
  8. 58
      app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt
  9. 25
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt
  10. 9
      app/src/main/java/io/legado/app/utils/ActivityExtensions.kt
  11. 2
      app/src/main/res/layout/activity_rss_artivles.xml

@ -1,7 +1,6 @@
package io.legado.app.ui.audio
import android.app.Activity
import android.content.Intent
import android.graphics.drawable.Drawable
import android.icu.text.SimpleDateFormat
import android.os.Build
@ -25,7 +24,7 @@ import io.legado.app.help.ImageLoader
import io.legado.app.lib.dialogs.alert
import io.legado.app.service.help.AudioPlay
import io.legado.app.ui.book.changesource.ChangeSourceDialog
import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.book.toc.TocActivityResult
import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.ui.widget.seekbar.SeekBarChangeListener
import io.legado.app.utils.*
@ -42,7 +41,6 @@ class AudioPlayActivity :
override val viewModel: AudioPlayViewModel
by viewModels()
private var requestCodeChapter = 8461
private var adjustProgress = false
private val progressTimeFormat by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -51,6 +49,13 @@ class AudioPlayActivity :
java.text.SimpleDateFormat("mm:ss", Locale.getDefault())
}
}
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
it?.let {
if (it.first != AudioPlay.durChapterIndex) {
AudioPlay.skipTo(this, it.first)
}
}
}
override fun getViewBinding(): ActivityAudioPlayBinding {
return ActivityAudioPlayBinding.inflate(layoutInflater)
@ -107,9 +112,7 @@ class AudioPlayActivity :
})
binding.ivChapter.setOnClickListener {
AudioPlay.book?.let {
startActivityForResult<ChapterListActivity>(requestCodeChapter) {
putExtra("bookUrl", it.bookUrl)
}
tocActivityResult.launch(it.bookUrl)
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
@ -183,19 +186,6 @@ class AudioPlayActivity :
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
requestCodeChapter -> data?.getIntExtra("index", AudioPlay.durChapterIndex)?.let {
if (it != AudioPlay.durChapterIndex) {
AudioPlay.skipTo(this, it)
}
}
}
}
}
override fun observeLiveBus() {
observeEvent<Boolean>(EventBus.MEDIA_BUTTON) {
if (it) {

@ -1,7 +1,6 @@
package io.legado.app.ui.book.info
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
@ -9,6 +8,7 @@ import android.view.Menu
import android.view.MenuItem
import android.widget.CheckBox
import android.widget.LinearLayout
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
@ -35,7 +35,7 @@ import io.legado.app.ui.book.info.edit.BookInfoEditActivity
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.book.toc.TocActivityResult
import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.utils.*
@ -47,9 +47,37 @@ class BookInfoActivity :
ChangeSourceDialog.CallBack,
ChangeCoverDialog.CallBack {
private val requestCodeChapterList = 568
private val requestCodeInfoEdit = 562
private val requestCodeRead = 432
private val tocActivityResult = registerForActivityResult(TocActivityResult()) {
it?.let {
viewModel.bookData.value?.let { book ->
if (book.durChapterIndex != it.first) {
book.durChapterIndex = it.first
book.durChapterPos = it.second
}
startReadActivity(book)
}
} ?: let {
if (!viewModel.inBookshelf) {
viewModel.delBook()
}
}
}
private val readBookResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == RESULT_OK) {
viewModel.inBookshelf = true
upTvBookshelf()
}
}
private val infoEditResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == RESULT_OK) {
viewModel.upEditBook()
}
}
override val viewModel: BookInfoViewModel by viewModels()
@ -82,11 +110,10 @@ class BookInfoActivity :
R.id.menu_edit -> {
if (viewModel.inBookshelf) {
viewModel.bookData.value?.let {
startActivityForResult<BookInfoEditActivity>(
requestCodeInfoEdit
) {
putExtra("bookUrl", it.bookUrl)
}
infoEditResult.launch(
Intent(this, BookInfoEditActivity::class.java)
.putExtra("bookUrl", it.bookUrl)
)
}
} else {
toastOnUi(R.string.after_add_bookshelf)
@ -303,11 +330,7 @@ class BookInfoActivity :
return
}
viewModel.bookData.value?.let {
startActivityForResult<ChapterListActivity>(
requestCodeChapterList
) {
putExtra("bookUrl", it.bookUrl)
}
tocActivityResult.launch(it.bookUrl)
}
}
@ -327,19 +350,17 @@ class BookInfoActivity :
private fun startReadActivity(book: Book) {
when (book.type) {
BookType.audio -> startActivityForResult<AudioPlayActivity>(
requestCodeRead
) {
putExtra("bookUrl", book.bookUrl)
putExtra("inBookshelf", viewModel.inBookshelf)
}
else -> startActivityForResult<ReadBookActivity>(
requestCodeRead
) {
putExtra("bookUrl", book.bookUrl)
putExtra("inBookshelf", viewModel.inBookshelf)
putExtra("key", IntentDataHelp.putData(book))
}
BookType.audio -> readBookResult.launch(
Intent(this, AudioPlayActivity::class.java)
.putExtra("bookUrl", book.bookUrl)
.putExtra("inBookshelf", viewModel.inBookshelf)
)
else -> readBookResult.launch(
Intent(this, ReadBookActivity::class.java)
.putExtra("bookUrl", book.bookUrl)
.putExtra("inBookshelf", viewModel.inBookshelf)
.putExtra("key", IntentDataHelp.putData(book))
)
}
}
@ -381,33 +402,4 @@ class BookInfoActivity :
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
requestCodeInfoEdit ->
if (resultCode == Activity.RESULT_OK) {
viewModel.upEditBook()
}
requestCodeChapterList ->
if (resultCode == Activity.RESULT_OK) {
viewModel.bookData.value?.let {
data?.getIntExtra("index", it.durChapterIndex)?.let { index ->
if (it.durChapterIndex != index) {
it.durChapterIndex = index
it.durChapterPos = 0
}
startReadActivity(it)
}
}
} else {
if (!viewModel.inBookshelf) {
viewModel.delBook()
}
}
requestCodeRead -> if (resultCode == Activity.RESULT_OK) {
viewModel.inBookshelf = true
upTvBookshelf()
}
}
}
}

@ -1,11 +1,11 @@
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
@ -22,7 +22,12 @@ class BookInfoEditActivity :
VMBaseActivity<ActivityBookInfoEditBinding, BookInfoEditViewModel>(),
ChangeCoverDialog.CallBack {
private val resultSelectCover = 132
private val selectCoverResult =
registerForActivityResult(ActivityResultContracts.GetContent()) {
it?.let { uri ->
coverChangeTo(uri)
}
}
override val viewModel: BookInfoEditViewModel
by viewModels()
@ -60,7 +65,7 @@ class BookInfoEditActivity :
}
}
tvSelectCover.setOnClickListener {
selectImage()
selectCoverResult.launch("image/*")
}
tvRefreshCover.setOnClickListener {
viewModel.book?.customCoverUrl = tieCoverUrl.text?.toString()
@ -96,13 +101,6 @@ class BookInfoEditActivity :
}
}
private fun selectImage() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, resultSelectCover)
}
override fun coverChangeTo(coverUrl: String) {
viewModel.book?.customCoverUrl = coverUrl
binding.tieCoverUrl.setText(coverUrl)
@ -144,16 +142,4 @@ class BookInfoEditActivity :
}
}
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)
}
}
}
}
}
}

@ -46,7 +46,7 @@ import io.legado.app.ui.book.read.page.entities.PageDirection
import io.legado.app.ui.book.read.page.provider.TextPageFactory
import io.legado.app.ui.book.searchContent.SearchContentActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.book.toc.TocActivityResult
import io.legado.app.ui.login.SourceLogin
import io.legado.app.ui.replace.ReplaceRuleActivity
import io.legado.app.ui.replace.edit.ReplaceEditActivity
@ -71,13 +71,10 @@ class ReadBookActivity : ReadBookBaseActivity(),
ColorPickerDialogListener {
private val tocActivity =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
it.data?.let { data ->
data.getIntExtra("index", ReadBook.durChapterIndex).let { index ->
if (index != ReadBook.durChapterIndex) {
val chapterPos = data.getIntExtra("chapterPos", 0)
viewModel.openChapter(index, chapterPos)
}
registerForActivityResult(TocActivityResult()) {
it?.let {
if (it.first != ReadBook.durChapterIndex) {
viewModel.openChapter(it.first, it.second)
}
}
}
@ -731,9 +728,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
*/
override fun openChapterList() {
ReadBook.book?.let {
tocActivity.launch(Intent(this, ChapterListActivity::class.java).apply {
putExtra("bookUrl", it.bookUrl)
})
tocActivity.launch(it.bookUrl)
}
}

@ -1,6 +1,5 @@
package io.legado.app.ui.book.source.debug
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
@ -12,10 +11,8 @@ import io.legado.app.databinding.ActivitySourceDebugBinding
import io.legado.app.help.LocalConfig
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.qrcode.QrCodeActivity
import io.legado.app.ui.qrcode.QrCodeResult
import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.startActivityForResult
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.launch
@ -26,7 +23,11 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
private lateinit var adapter: BookSourceDebugAdapter
private lateinit var searchView: SearchView
private val qrRequestCode = 101
private val qrCodeResult = registerForActivityResult(QrCodeResult()) {
it?.let {
startSearch(it)
}
}
override fun getViewBinding(): ActivitySourceDebugBinding {
return ActivitySourceDebugBinding.inflate(layoutInflater)
@ -96,7 +97,7 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_scan -> {
startActivityForResult<QrCodeActivity>(qrRequestCode)
qrCodeResult.launch(null)
}
R.id.menu_help -> showHelp()
}
@ -108,16 +109,4 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
TextDialog.show(supportFragmentManager, text, TextDialog.MD)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
qrRequestCode -> {
if (resultCode == RESULT_OK) {
data?.getStringExtra("result")?.let {
startSearch(it)
}
}
}
}
}
}

@ -0,0 +1,26 @@
package io.legado.app.ui.book.toc
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
class TocActivityResult : ActivityResultContract<String, Pair<Int, Int>?>() {
override fun createIntent(context: Context, input: String?): Intent {
return Intent(context, ChapterListActivity::class.java)
.putExtra("bookUrl", input)
}
override fun parseResult(resultCode: Int, intent: Intent?): Pair<Int, Int>? {
if (resultCode == RESULT_OK) {
intent?.let {
return Pair(
it.getIntExtra("index", 0),
it.getIntExtra("chapterPos", 0)
)
}
}
return null
}
}

@ -1,13 +1,11 @@
package io.legado.app.ui.filepicker
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContract
import android.webkit.MimeTypeMap
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.CallSuper
import io.legado.app.R
import io.legado.app.base.BaseActivity
import io.legado.app.constant.Theme
@ -28,7 +26,7 @@ class FilePickerActivity :
onResult(Intent().setData(it))
}
private val selectDoc = registerForActivityResult(GetContent()) {
private val selectDoc = registerForActivityResult(ActivityResultContracts.OpenDocument()) {
onResult(Intent().setData(it))
}
@ -65,7 +63,7 @@ class FilePickerActivity :
} else {
selectDoc.launch(typesOfExtensions(allowExtensions))
}
2 -> if (mode == FilePicker.DIRECTORY) {
1 -> if (mode == FilePicker.DIRECTORY) {
checkPermissions {
FilePickerDialog.show(
supportFragmentManager,
@ -117,7 +115,14 @@ class FilePickerActivity :
when (it) {
"*" -> types.add("*/*")
"txt", "xml" -> types.add("text/*")
else -> types.add("application/$it")
else -> {
val mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension("json")
if (mime == null) {
types.add("application/octet-stream")
} else {
types.add(mime)
}
}
}
}
}
@ -129,17 +134,4 @@ class FilePickerActivity :
finish()
}
class GetContent : ActivityResultContract<Array<String>, Uri>() {
@CallSuper
override fun createIntent(context: Context, input: Array<String>): Intent {
return Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.putExtra(Intent.EXTRA_MIME_TYPES, input)
.setType("*/*")
}
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
return if (intent == null || resultCode != RESULT_OK) null else intent.data
}
}
}

@ -1,38 +1,47 @@
package io.legado.app.ui.rss.article
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityRssArtivlesBinding
import io.legado.app.ui.rss.source.edit.RssSourceEditActivity
import io.legado.app.utils.gone
import io.legado.app.utils.startActivityForResult
import io.legado.app.utils.visible
class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewModel>() {
override val viewModel: RssSortViewModel
by viewModels()
private val editSource = 12319
private val fragments = linkedMapOf<String, RssArticlesFragment>()
private lateinit var adapter: TabFragmentPageAdapter
private val upSourceResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == RESULT_OK) {
viewModel.initData(intent) {
upFragments()
}
}
}
override fun getViewBinding(): ActivityRssArtivlesBinding {
return ActivityRssArtivlesBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
adapter = TabFragmentPageAdapter(supportFragmentManager)
binding.tabLayout.setupWithViewPager(binding.viewPager)
adapter = TabFragmentPageAdapter()
binding.viewPager.adapter = adapter
TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
tab.text = fragments.keys.elementAt(position)
}
viewModel.titleLiveData.observe(this, {
binding.titleBar.title = it
})
@ -49,9 +58,10 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_edit_source -> viewModel.rssSource?.sourceUrl?.let {
startActivityForResult<RssSourceEditActivity>(editSource) {
putExtra("data", it)
}
upSourceResult.launch(
Intent(this, RssSourceEditActivity::class.java)
.putExtra("data", it)
)
}
R.id.menu_clear -> {
viewModel.url?.let {
@ -79,35 +89,15 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
adapter.notifyDataSetChanged()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
editSource -> if (resultCode == Activity.RESULT_OK) {
viewModel.initData(intent) {
upFragments()
}
}
}
}
private inner class TabFragmentPageAdapter(fm: FragmentManager) :
FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItemPosition(`object`: Any): Int {
return POSITION_NONE
}
private inner class TabFragmentPageAdapter : FragmentStateAdapter(this) {
override fun getPageTitle(position: Int): CharSequence {
return fragments.keys.elementAt(position)
override fun getItemCount(): Int {
return fragments.size
}
override fun getItem(position: Int): Fragment {
override fun createFragment(position: Int): Fragment {
return fragments.values.elementAt(position)
}
override fun getCount(): Int {
return fragments.size
}
}
}

@ -1,7 +1,6 @@
package io.legado.app.ui.rss.source.edit
import android.app.Activity
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.Gravity
@ -20,7 +19,7 @@ import io.legado.app.help.LocalConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.theme.ATH
import io.legado.app.ui.qrcode.QrCodeActivity
import io.legado.app.ui.qrcode.QrCodeResult
import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity
import io.legado.app.ui.widget.KeyboardToolPop
import io.legado.app.ui.widget.dialog.TextDialog
@ -34,9 +33,15 @@ class RssSourceEditActivity :
private var mSoftKeyboardTool: PopupWindow? = null
private var mIsSoftKeyBoardShowing = false
private val qrRequestCode = 101
private val adapter = RssSourceEditAdapter()
private val sourceEntities: ArrayList<EditEntity> = ArrayList()
private val qrCodeResult = registerForActivityResult(QrCodeResult()) {
it?.let {
viewModel.importSource(it) { source: RssSource ->
upRecyclerView(source)
}
}
}
override fun getViewBinding(): ActivityRssSourceEditBinding {
return ActivityRssSourceEditBinding.inflate(layoutInflater)
@ -107,7 +112,7 @@ class RssSourceEditActivity :
}
}
R.id.menu_copy_source -> sendToClip(GSON.toJson(getRssSource()))
R.id.menu_qr_code_camera -> startActivityForResult<QrCodeActivity>(qrRequestCode)
R.id.menu_qr_code_camera -> qrCodeResult.launch(null)
R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) }
R.id.menu_share_str -> share(GSON.toJson(getRssSource()))
R.id.menu_share_qr -> shareWithQr(
@ -269,16 +274,4 @@ class RssSourceEditActivity :
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
qrRequestCode -> if (resultCode == RESULT_OK) {
data?.getStringExtra("result")?.let {
viewModel.importSource(it) { source: RssSource ->
upRecyclerView(source)
}
}
}
}
}
}

@ -1,20 +1,11 @@
package io.legado.app.utils
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowInsets
import android.view.WindowMetrics
inline fun <reified A : Activity> Activity.startActivityForResult(
requestCode: Int,
configIntent: Intent.() -> Unit = {}
) {
startActivityForResult(Intent(this, A::class.java).apply(configIntent), requestCode)
}
fun Activity.getSize(): DisplayMetrics {
val displayMetrics = DisplayMetrics()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

@ -16,7 +16,7 @@
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Loading…
Cancel
Save