Skip to content

Commit 676f4c0

Browse files
committed
api 21 support
1 parent 87fc858 commit 676f4c0

5 files changed

Lines changed: 32 additions & 17 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99

1010
defaultConfig {
1111
applicationId "com.docs.pdfviewer"
12-
minSdk 26
12+
minSdk 21
1313
targetSdk 33
1414
versionCode 1
1515
versionName "1.0"

docwatcher/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
compileSdk 33
1010

1111
defaultConfig {
12-
minSdk 26
12+
minSdk 21
1313
targetSdk 33
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

docwatcher/src/main/java/com/docs/docwatcher/DocView.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.docs.docwatcher
33
import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.pdf.PdfRenderer
6+
import android.os.Build
67
import android.os.ParcelFileDescriptor
78
import android.util.AttributeSet
89
import android.view.LayoutInflater
@@ -90,16 +91,30 @@ class DocView @JvmOverloads constructor(
9091
val cardText = "1 / ${list.size}"
9192
pageText.text = cardText
9293
pageCard.showPagesViewForSomeSeconds(coroutineScope, context)
93-
rv.setOnScrollChangeListener { _, _, _, _, _ ->
94-
val visiblePosition = (rv.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
95-
if(visiblePosition != -1) {
96-
val currPage = visiblePosition + 1
97-
if(!noOfPages.isNullOrEmpty()) {
98-
pageCard.show()
99-
val text = "$currPage / $noOfPages"
100-
pageText.text = text
101-
pageCard.showPagesViewForSomeSeconds(coroutineScope, context)
94+
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
95+
rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
96+
97+
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
98+
super.onScrolled(recyclerView, dx, dy)
99+
handleOnScroll()
102100
}
101+
})
102+
} else {
103+
rv.setOnScrollChangeListener { _, _, _, _, _ ->
104+
handleOnScroll()
105+
}
106+
}
107+
}
108+
109+
private fun handleOnScroll() {
110+
val visiblePosition = (rv.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
111+
if(visiblePosition != -1) {
112+
val currPage = visiblePosition + 1
113+
if(!noOfPages.isNullOrEmpty()) {
114+
pageCard.show()
115+
val text = "$currPage / $noOfPages"
116+
pageText.text = text
117+
pageCard.showPagesViewForSomeSeconds(coroutineScope, context)
103118
}
104119
}
105120
}

docwatcher/src/main/java/com/docs/docwatcher/data/PdfDownloader.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import android.app.DownloadManager
44
import android.content.Context
55
import android.database.ContentObserver
66
import android.net.Uri
7+
import android.os.Build
78
import android.os.Handler
89
import android.os.Looper
9-
import android.util.Log
1010
import androidx.core.net.toUri
1111
import com.docs.docwatcher.DocView
1212

@@ -19,7 +19,11 @@ internal class PdfDownloader(
1919
private val listener: DocView.DownloadListener
2020
) {
2121

22-
private val downloadManager = context.getSystemService(DownloadManager::class.java)
22+
private val downloadManager = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
23+
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
24+
} else {
25+
context.getSystemService(DownloadManager::class.java)
26+
}
2327

2428
fun download(url: String) {
2529
val request = DownloadManager.Request(url.toUri())

docwatcher/src/main/java/com/docs/docwatcher/util/PdfExtFun.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,4 @@ internal fun View.show() {
4343

4444
internal fun View.gone() {
4545
this.visibility = View.GONE
46-
}
47-
48-
internal fun View.invisible() {
49-
this.visibility = View.INVISIBLE
5046
}

0 commit comments

Comments
 (0)