Skip to content

Commit

Permalink
Fix old api compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Apr 25, 2020
1 parent 0dc74f9 commit 5fe40ac
Show file tree
Hide file tree
Showing 72 changed files with 229 additions and 151 deletions.
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ android {

defaultConfig {
applicationId 'org.koitharu.kotatsu'
minSdkVersion 21
minSdkVersion 16
maxSdkVersion 20
targetSdkVersion 29
versionCode gitCommits
versionName '0.3'

buildConfigField 'String', 'GIT_BRANCH', "\"${gitBranch}\""

vectorDrawables.useSupportLibrary = true
kapt {
arguments {
arg('room.schemaLocation', "$projectDir/schemas".toString())
Expand Down Expand Up @@ -82,7 +83,7 @@ dependencies {
implementation 'com.github.moxy-community:moxy-ktx:2.1.2'
kapt 'com.github.moxy-community:moxy-compiler:2.1.2'

implementation 'com.squareup.okhttp3:okhttp:4.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
implementation 'com.squareup.okio:okio:2.5.0'
implementation 'org.jsoup:jsoup:1.13.1'

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/koitharu/kotatsu/KotatsuApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class KotatsuApp : Application() {

override fun onCreate() {
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
initKoin()
initCoil()
Thread.setDefaultUncaughtExceptionHandler(AppCrashHandler(applicationContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.koitharu.kotatsu.core.local.cookies

import org.koitharu.kotatsu.core.local.cookies.persistence.CookiePersistor
import okhttp3.Cookie
import okhttp3.HttpUrl
import org.koitharu.kotatsu.core.local.cookies.cache.CookieCache
import org.koitharu.kotatsu.core.local.cookies.persistence.CookiePersistor
import java.util.*

class PersistentCookieJar(
Expand Down Expand Up @@ -72,7 +72,7 @@ class PersistentCookieJar(
fun filterPersistentCookies(cookies: List<Cookie>): List<Cookie> {
val persistentCookies: MutableList<Cookie> = ArrayList()
for (cookie in cookies) {
if (cookie.persistent) {
if (cookie.persistent()) {
persistentCookies.add(cookie)
}
}
Expand All @@ -81,7 +81,7 @@ class PersistentCookieJar(

@JvmStatic
fun isCookieExpired(cookie: Cookie): Boolean {
return cookie.expiresAt < System.currentTimeMillis()
return cookie.expiresAt() < System.currentTimeMillis()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ internal class IdentifiableCookie(val cookie: Cookie) {

override fun equals(other: Any?): Boolean {
if (other !is IdentifiableCookie) return false
return other.cookie.name == cookie.name && other.cookie.domain == cookie.domain
&& other.cookie.path == cookie.path && other.cookie.secure == cookie.secure
&& other.cookie.hostOnly == cookie.hostOnly
return other.cookie.name() == cookie.name() && other.cookie.domain() == cookie.domain()
&& other.cookie.path() == cookie.path() && other.cookie.secure() == cookie.secure()
&& other.cookie.hostOnly() == cookie.hostOnly()
}

override fun hashCode(): Int {
var hash = 17
hash = 31 * hash + cookie.name.hashCode()
hash = 31 * hash + cookie.domain.hashCode()
hash = 31 * hash + cookie.path.hashCode()
hash = 31 * hash + if (cookie.secure) 0 else 1
hash = 31 * hash + if (cookie.hostOnly) 0 else 1
hash = 31 * hash + cookie.name().hashCode()
hash = 31 * hash + cookie.domain().hashCode()
hash = 31 * hash + cookie.path().hashCode()
hash = 31 * hash + if (cookie.secure()) 0 else 1
hash = 31 * hash + if (cookie.hostOnly()) 0 else 1
return hash
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ class SerializableCookie : Serializable {

@Throws(IOException::class)
private fun writeObject(out: ObjectOutputStream) {
out.writeObject(cookie!!.name)
out.writeObject(cookie!!.value)
out.writeLong(if (cookie!!.persistent) cookie!!.expiresAt else NON_VALID_EXPIRES_AT)
out.writeObject(cookie!!.domain)
out.writeObject(cookie!!.path)
out.writeBoolean(cookie!!.secure)
out.writeBoolean(cookie!!.httpOnly)
out.writeBoolean(cookie!!.hostOnly)
out.writeObject(cookie!!.name())
out.writeObject(cookie!!.value())
out.writeLong(if (cookie!!.persistent()) cookie!!.expiresAt() else NON_VALID_EXPIRES_AT)
out.writeObject(cookie!!.domain())
out.writeObject(cookie!!.path())
out.writeBoolean(cookie!!.secure())
out.writeBoolean(cookie!!.httpOnly())
out.writeBoolean(cookie!!.hostOnly())
}

@Throws(IOException::class, ClassNotFoundException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SharedPrefsCookiePersistor(private val sharedPreferences: SharedPreference
private companion object {

fun createCookieKey(cookie: Cookie): String {
return (if (cookie.secure) "https" else "http") + "://" + cookie.domain + cookie.path + "|" + cookie.name
return (if (cookie.secure()) "https" else "http") + "://" + cookie.domain() + cookie.path() + "|" + cookie.name()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.parser

import android.content.Context
import android.net.Uri
import android.os.Build
import androidx.core.net.toFile
import androidx.core.net.toUri
import org.koin.core.KoinComponent
Expand All @@ -14,6 +15,7 @@ import org.koitharu.kotatsu.utils.AlphanumComparator
import org.koitharu.kotatsu.utils.ext.longHashCode
import org.koitharu.kotatsu.utils.ext.readText
import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.sub
import java.io.File
import java.util.*
import java.util.zip.ZipEntry
Expand All @@ -29,8 +31,11 @@ class LocalMangaRepository : MangaRepository, KoinComponent {
sortOrder: SortOrder?,
tag: MangaTag?
): List<Manga> {
val files = context.getExternalFilesDirs("manga")
.flatMap { x -> x?.listFiles(CbzFilter())?.toList().orEmpty() }
val files = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
context.getExternalFilesDirs("manga") + context.filesDir.sub("manga")
} else {
arrayOf(context.getExternalFilesDir("manga"), context.filesDir.sub("manga"))
}.flatMap { x -> x?.listFiles(CbzFilter())?.toList().orEmpty() }
return files.mapNotNull { x -> safe { getFromFile(x) } }
}

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/koitharu/kotatsu/domain/MangaUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.koitharu.kotatsu.domain

import android.graphics.BitmapFactory
import android.util.Size
import android.graphics.Point
import okhttp3.OkHttpClient
import okhttp3.Request
import org.koin.core.KoinComponent
Expand Down Expand Up @@ -29,10 +29,10 @@ object MangaUtils : KoinComponent {
.get()
.build()
val size = client.newCall(request).await().use {
getBitmapSize(it.body?.byteStream())
getBitmapSize(it.body()?.byteStream())
}
return when {
size.width * 2 < size.height -> ReaderMode.WEBTOON
size.x * 2 < size.y -> ReaderMode.WEBTOON
else -> ReaderMode.STANDARD
}
} catch (e: Exception) {
Expand All @@ -44,14 +44,14 @@ object MangaUtils : KoinComponent {
}

@JvmStatic
private fun getBitmapSize(input: InputStream?): Size {
private fun getBitmapSize(input: InputStream?): Point {
val options = BitmapFactory.Options().apply {
inJustDecodeBounds = true
}
BitmapFactory.decodeStream(input, null, options)
val imageHeight: Int = options.outHeight
val imageWidth: Int = options.outWidth
check(imageHeight > 0 && imageWidth > 0)
return Size(imageWidth, imageHeight)
return Point(imageWidth, imageHeight)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class MangaZip(val file: File) {
if (!file.exists()) {
return
}
dir.listFiles()?.forEach {
it?.deleteRecursively()
}
ZipInputStream(file.inputStream()).use { input ->
while (true) {
val entry = input.nextEntry ?: return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.koitharu.kotatsu.ui.browser

import android.graphics.Bitmap
import android.os.Build
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.annotation.RequiresApi
import okhttp3.OkHttpClient
import okhttp3.Request
import org.koin.core.KoinComponent
Expand Down Expand Up @@ -38,6 +40,7 @@ class BrowserClient(private val callback: BrowserCallback) : WebViewClient(), Ko
return url?.let(::doRequest)
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
return request?.url?.toString()?.let(::doRequest)
}
Expand All @@ -47,11 +50,11 @@ class BrowserClient(private val callback: BrowserCallback) : WebViewClient(), Ko
.url(url)
.build()
val response = okHttp.newCall(request).execute()
val ct = response.body?.contentType()
val ct = response.body()?.contentType()
WebResourceResponse(
"${ct?.type}/${ct?.subtype}",
"${ct?.type()}/${ct?.subtype()}",
ct?.charset()?.name() ?: "utf-8",
response.body?.byteStream()
response.body()?.byteStream()
)
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/org/koitharu/kotatsu/ui/common/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ abstract class BaseActivity : MvpAppCompatActivity(), KoinComponent {
}
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
val keyCode = event.keyCode
val action = event.action
val isDown = action == KeyEvent.ACTION_DOWN
return if (keyCode == KeyEvent.KEYCODE_MENU) {
if (isDown) {
onKeyDown(keyCode, event)
} else {
onKeyUp(keyCode, event)
}
} else {
super.dispatchKeyEvent(event)
}
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
//TODO remove. Just for testing
if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
package org.koitharu.kotatsu.ui.common

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager


abstract class BaseFullscreenActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
with(window) {
// addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
statusBarColor = Color.TRANSPARENT
navigationBarColor = Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
}
showSystemUI()
}

protected fun hideSystemUI() {
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // прячем панель навигации
or View.SYSTEM_UI_FLAG_FULLSCREEN // прячем строку состояния
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
)
window.decorView.systemUiVisibility =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // прячем панель навигации
or View.SYSTEM_UI_FLAG_FULLSCREEN // прячем строку состояния
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
)
} else {
(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // прячем панель навигации
or View.SYSTEM_UI_FLAG_FULLSCREEN // прячем строку состояния
)
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package org.koitharu.kotatsu.ui.common.dialog

import android.content.Context
import android.content.DialogInterface
import android.os.Build
import android.os.Environment
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.item_storage.view.*
Expand All @@ -15,6 +17,7 @@ import org.koitharu.kotatsu.utils.ext.inflate
import org.koitharu.kotatsu.utils.ext.longHashCode
import java.io.File

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class StorageSelectDialog private constructor(private val delegate: AlertDialog) :
DialogInterface by delegate {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.koitharu.kotatsu.ui.common.list

import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import java.util.*

class AdapterUpdater<T>(oldList: List<T>, newList: List<T>, getId: (T) -> Long) {

Expand All @@ -12,7 +11,7 @@ class AdapterUpdater<T>(oldList: List<T>, newList: List<T>, getId: (T) -> Long)
getId(oldList[oldItemPosition]) == getId(newList[newItemPosition])

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
Objects.equals(oldList[oldItemPosition], newList[newItemPosition])
oldList[oldItemPosition]?.equals(newList[newItemPosition]) == true

override fun getOldListSize() = oldList.size

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package org.koitharu.kotatsu.ui.common.list

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import okhttp3.internal.toImmutableList
import org.koin.core.KoinComponent
import org.koitharu.kotatsu.utils.ext.replaceWith
import java.util.*
import kotlin.collections.ArrayList

abstract class BaseRecyclerAdapter<T, E>(private val onItemClickListener: OnRecyclerItemClickListener<T>? = null) :
RecyclerView.Adapter<BaseViewHolder<T, E>>(),
KoinComponent {

protected val dataSet = ArrayList<T>() //TODO make private

val items get() = dataSet.toImmutableList()
val items: List<T>
get() = Collections.unmodifiableList(dataSet)

val hasItems get() = dataSet.isNotEmpty()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DownloadNotification(private val context: Context) {
fun fillFrom(manga: Manga) {
builder.setContentTitle(manga.title)
builder.setContentText(context.getString(R.string.manga_downloading_))
builder.setTicker(context.getString(R.string.manga_downloading_))
builder.setProgress(1, 0, true)
builder.setSmallIcon(android.R.drawable.stat_sys_download)
builder.setLargeIcon(null)
Expand All @@ -56,7 +57,7 @@ class DownloadNotification(private val context: Context) {
} else {
val intent = DownloadService.getCancelIntent(context, startId)
builder.addAction(
R.drawable.ic_cross,
R.drawable.ic_cross_compat,
context.getString(android.R.string.cancel),
PendingIntent.getService(
context,
Expand Down
Loading

0 comments on commit 5fe40ac

Please sign in to comment.