From cc16a8e2d226a34c9f4c609c4d3606ce301e34b3 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 7 Sep 2021 23:33:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/entities/BaseSource.kt | 25 +++++++++---------- .../app/ui/book/audio/AudioPlayActivity.kt | 13 ++++++++++ .../app/ui/book/audio/AudioPlayViewModel.kt | 8 ++++++ .../app/ui/book/read/ReadBookActivity.kt | 6 ++--- .../legado/app/ui/login/RuleUiLoginDialog.kt | 17 +++++++------ .../app/utils/ActivityResultContracts.kt | 18 +++++++++++++ .../java/io/legado/app/utils/EncoderUtils.kt | 11 +++----- app/src/main/res/menu/audio_play.xml | 5 ++++ 8 files changed, 72 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt index 4f403b613..fed5ef9c2 100644 --- a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt @@ -84,11 +84,12 @@ interface BaseSource : JsExtensions { */ fun getLoginInfo(): String? { try { + val key = AppConst.androidId.encodeToByteArray(0, 8) val cache = CacheManager.get("userInfo_${getStoreUrl()}") ?: return null - val byteArrayB = Base64.decode(cache, Base64.DEFAULT) - val byteArrayA = EncoderUtils.decryptAES(byteArrayB, AppConst.androidId.toByteArray()) + val encodeBytes = Base64.decode(cache, Base64.DEFAULT) + val decodeBytes = EncoderUtils.decryptAES(encodeBytes, key) ?: return null - return String(byteArrayA) + return String(decodeBytes) } catch (e: Exception) { e.printStackTrace() return null @@ -102,18 +103,16 @@ interface BaseSource : JsExtensions { /** * 保存用户信息,aes加密 */ - fun putLoginInfo(info: String) { - try { - val data = Base64.encodeToString( - EncoderUtils.decryptAES( - info.toByteArray(), - AppConst.androidId.toByteArray() - ), - Base64.DEFAULT - ) - CacheManager.put("userInfo_${getStoreUrl()}", data) + fun putLoginInfo(info: String): Boolean { + return try { + val key = (AppConst.androidId).encodeToByteArray(0, 8) + val encodeBytes = EncoderUtils.encryptAES(info.toByteArray(), key) + val encodeStr = Base64.encodeToString(encodeBytes, Base64.DEFAULT) + CacheManager.put("userInfo_${getStoreUrl()}", encodeStr) + true } catch (e: Exception) { e.printStackTrace() + false } } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index 8295927de..7dfcab3b5 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -25,6 +25,7 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.model.AudioPlay import io.legado.app.service.AudioPlayService import io.legado.app.ui.book.changesource.ChangeSourceDialog +import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.widget.image.CoverImageView @@ -59,6 +60,13 @@ class AudioPlayActivity : } } } + private val sourceEditResult = + registerForActivityResult(StartActivityForResult(BookSourceEditActivity::class.java)) { + it ?: return@registerForActivityResult + if (it.resultCode == RESULT_OK) { + viewModel.upSource() + } + } override fun onActivityCreated(savedInstanceState: Bundle?) { binding.titleBar.transparent() @@ -94,6 +102,11 @@ class AudioPlayActivity : } } R.id.menu_copy_audio_url -> sendToClip(AudioPlayService.url) + R.id.menu_edit_source -> AudioPlay.bookSource?.let { + sourceEditResult.launch { + putExtra("data", it.bookSourceUrl) + } + } } return super.onCompatOptionsItemSelected(item) } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt index 69cd49f9e..af933a8fa 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt @@ -82,6 +82,14 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } + fun upSource() { + execute { + AudioPlay.book?.let { book -> + AudioPlay.bookSource = appDb.bookSourceDao.getBookSource(book.origin) + } + } + } + fun changeTo(book1: Book) { execute { var oldTocSize: Int = book1.totalChapterNum diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index e3b3b2c7b..5a6344804 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -74,7 +74,7 @@ class ReadBookActivity : ReadBookBaseActivity(), } } private val sourceEditActivity = - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + registerForActivityResult(StartActivityForResult(BookSourceEditActivity::class.java)) { it ?: return@registerForActivityResult if (it.resultCode == RESULT_OK) { viewModel.upBookSource { @@ -732,9 +732,9 @@ class ReadBookActivity : ReadBookBaseActivity(), override fun openSourceEditActivity() { ReadBook.bookSource?.let { - sourceEditActivity.launch(Intent(this, BookSourceEditActivity::class.java).apply { + sourceEditActivity.launch { putExtra("data", it.bookSourceUrl) - }) + } } } diff --git a/app/src/main/java/io/legado/app/ui/login/RuleUiLoginDialog.kt b/app/src/main/java/io/legado/app/ui/login/RuleUiLoginDialog.kt index e84fa6c96..0d89e5587 100644 --- a/app/src/main/java/io/legado/app/ui/login/RuleUiLoginDialog.kt +++ b/app/src/main/java/io/legado/app/ui/login/RuleUiLoginDialog.kt @@ -105,14 +105,15 @@ class RuleUiLoginDialog : BaseDialogFragment() { bookSource.removeLoginInfo() return@setOnMenuItemClickListener true } - bookSource.putLoginInfo(GSON.toJson(loginData)) - bookSource.getLoginJs()?.let { - try { - bookSource.evalJS(it) - toastOnUi(R.string.success) - } catch (e: Exception) { - e.printStackTrace() - toastOnUi(e.localizedMessage ?: "ERROR") + if (bookSource.putLoginInfo(GSON.toJson(loginData))) { + bookSource.getLoginJs()?.let { + try { + bookSource.evalJS(it) + toastOnUi(R.string.success) + } catch (e: Exception) { + e.printStackTrace() + toastOnUi(e.localizedMessage ?: "ERROR") + } } } } diff --git a/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt b/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt index 108c28bd0..5aebe1890 100644 --- a/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt +++ b/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt @@ -4,6 +4,7 @@ import android.app.Activity.RESULT_OK import android.content.Context import android.content.Intent import android.net.Uri +import androidx.activity.result.ActivityResult import androidx.activity.result.contract.ActivityResultContract class SelectImageContract : ActivityResultContract?>() { @@ -24,4 +25,21 @@ class SelectImageContract : ActivityResultContract?>() { return null } +} + +class StartActivityForResult(private val cls: Class<*>) : + ActivityResultContract Unit, ActivityResult>() { + + override fun createIntent(context: Context, input: Intent.() -> Unit): Intent { + val intent = Intent(context, cls) + intent.apply(input) + return intent + } + + override fun parseResult( + resultCode: Int, intent: Intent? + ): ActivityResult { + return ActivityResult(resultCode, intent) + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt index 66cf8a021..9bf400bad 100644 --- a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt +++ b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt @@ -120,7 +120,7 @@ object EncoderUtils { fun decryptAES( data: ByteArray?, key: ByteArray?, - transformation: String = "AES/ECB/PKCS5Padding", + transformation: String = "DES/ECB/PKCS5Padding", iv: ByteArray? = null ): ByteArray? { return symmetricTemplate(data, key, "AES", transformation, iv, false) @@ -154,15 +154,12 @@ object EncoderUtils { else { val keySpec = SecretKeySpec(key, algorithm) val cipher = Cipher.getInstance(transformation) + val mode = if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE if (iv == null || iv.isEmpty()) { - cipher.init(if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE, keySpec) + cipher.init(mode, keySpec) } else { val params: AlgorithmParameterSpec = IvParameterSpec(iv) - cipher.init( - if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE, - keySpec, - params - ) + cipher.init(mode, keySpec, params) } cipher.doFinal(data) } diff --git a/app/src/main/res/menu/audio_play.xml b/app/src/main/res/menu/audio_play.xml index a313756ce..133837ec5 100644 --- a/app/src/main/res/menu/audio_play.xml +++ b/app/src/main/res/menu/audio_play.xml @@ -20,4 +20,9 @@ android:title="@string/copy_play_url" app:showAsAction="never" /> + + \ No newline at end of file