pull/1303/head
gedoor 3 years ago
parent cc4c429957
commit cc16a8e2d2
  1. 25
      app/src/main/java/io/legado/app/data/entities/BaseSource.kt
  2. 13
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt
  4. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  5. 17
      app/src/main/java/io/legado/app/ui/login/RuleUiLoginDialog.kt
  6. 18
      app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt
  7. 11
      app/src/main/java/io/legado/app/utils/EncoderUtils.kt
  8. 5
      app/src/main/res/menu/audio_play.xml

@ -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
}
}

@ -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)
}

@ -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

@ -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)
})
}
}
}

@ -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")
}
}
}
}

@ -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<Int, Pair<Int?, Uri?>?>() {
@ -25,3 +26,20 @@ class SelectImageContract : ActivityResultContract<Int, Pair<Int?, Uri?>?>() {
}
}
class StartActivityForResult(private val cls: Class<*>) :
ActivityResultContract<Intent.() -> 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)
}
}

@ -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)
}

@ -20,4 +20,9 @@
android:title="@string/copy_play_url"
app:showAsAction="never" />
<item
android:id="@+id/menu_edit_source"
android:title="@string/edit_book_source"
app:showAsAction="never" />
</menu>
Loading…
Cancel
Save