Skip to content

Commit

Permalink
support for assest folder
Browse files Browse the repository at this point in the history
  • Loading branch information
afreakyelf committed Jan 4, 2021
1 parent c1b0975 commit 251184f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added app/src/main/assets/quote.pdf
Binary file not shown.
15 changes: 9 additions & 6 deletions app/src/main/java/com/rajat/pdfviewer/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@


package com.rajat.pdfviewer


import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -11,15 +14,15 @@ class MainActivity : AppCompatActivity() {

open_pdf.setOnClickListener {
startActivity(
PdfViewerActivity.buildIntent(
PdfViewerActivity.launchPdfFromPath(
this,
"url",
false,
"title",
""
"quote.pdf",
"Thailand Trip",
"Trip",
false
)

)
}

}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Jul 11 15:48:36 IST 2020
#Mon Jan 04 10:33:44 IST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
4 changes: 3 additions & 1 deletion pdfViewer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application>
<application
android:requestLegacyExternalStorage="true">
<activity
android:name=".PdfViewerActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
Expand Down
122 changes: 84 additions & 38 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ class PdfViewerActivity : AppCompatActivity() {
const val PERMISSION_CODE = 4040
var engine = PdfEngine.INTERNAL
var enableDownload = true
var isPDFFromPath = false


fun buildIntent(
fun launchPdfFromUrl(
context: Context?,
pdfUrl: String?,
isGoogleEngine: Boolean?,
Expand All @@ -60,6 +61,23 @@ class PdfViewerActivity : AppCompatActivity() {
intent.putExtra(FILE_DIRECTORY, directoryName)
intent.putExtra(ENABLE_FILE_DOWNLOAD, enableDownload)
intent.putExtra(IS_GOOGLE_ENGINE, isGoogleEngine)
isPDFFromPath = false
return intent
}

fun launchPdfFromPath(
context: Context?,
path: String?,
pdfTitle: String?,
directoryName: String?,
enableDownload: Boolean = true
): Intent {
val intent = Intent(context, PdfViewerActivity::class.java)
intent.putExtra(FILE_URL, path)
intent.putExtra(FILE_TITLE, pdfTitle)
intent.putExtra(FILE_DIRECTORY, directoryName)
intent.putExtra(ENABLE_FILE_DOWNLOAD, enableDownload)
isPDFFromPath = true
return intent
}

Expand Down Expand Up @@ -89,14 +107,18 @@ class PdfViewerActivity : AppCompatActivity() {

if (intent.extras!!.containsKey(FILE_URL)) {
fileUrl = intent.extras!!.getString(FILE_URL)
if (checkInternetConnection(this)) {
loadFileFromNetwork(this.fileUrl)
} else {
Toast.makeText(
this,
"No Internet Connection. Please Check your internet connection.",
Toast.LENGTH_SHORT
).show()
if (isPDFFromPath){
initPdfViewerWithPath(this.fileUrl)
}else {
if (checkInternetConnection(this)) {
loadFileFromNetwork(this.fileUrl)
} else {
Toast.makeText(
this,
"No Internet Connection. Please Check your internet connection.",
Toast.LENGTH_SHORT
).show()
}
}
}

Expand Down Expand Up @@ -188,6 +210,26 @@ class PdfViewerActivity : AppCompatActivity() {
onPdfError()
}

enableDownload()

}

private fun initPdfViewerWithPath(filePath: String?) {
if (TextUtils.isEmpty(filePath)) onPdfError()

//Initiating PDf Viewer with URL
try {
pdfView.initWithFile(
com.rajat.pdfviewer.util.FileUtils.fileFromAsset(this,filePath!!),
PdfQuality.NORMAL)
} catch (e: Exception) {
onPdfError()
}

enableDownload()
}

private fun enableDownload() {
//Check permission for download
checkPermissionOnInit()

Expand Down Expand Up @@ -217,7 +259,6 @@ class PdfViewerActivity : AppCompatActivity() {
}

}

}

private fun checkPermissionOnInit() {
Expand Down Expand Up @@ -260,34 +301,39 @@ class PdfViewerActivity : AppCompatActivity() {
if (TextUtils.isEmpty(directoryName)) "/$fileName.pdf" else "/$directoryName/$fileName.pdf"

try {
val downloadUrl = Uri.parse(fileUrl)
val downloadManger = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
val request = DownloadManager.Request(downloadUrl)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or
DownloadManager.Request.NETWORK_MOBILE
)
request.setAllowedOverRoaming(true)
request.setTitle(fileName)
request.setDescription("Downloading $fileName")
request.setVisibleInDownloadsUi(true)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
filePath
)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
registerReceiver(
onComplete,
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
)
if (permissionGranted!!) downloadManger!!.enqueue(request)
} catch (e: Exception) {
Toast.makeText(
this,
"Unable to download file",
Toast.LENGTH_SHORT
).show()
}
if (isPDFFromPath) {
com.rajat.pdfviewer.util.FileUtils.downloadFile(this,fileUrl!!,directoryName!!,fileName)
} else {
val downloadUrl = Uri.parse(fileUrl)
val downloadManger =
getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
val request = DownloadManager.Request(downloadUrl)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or
DownloadManager.Request.NETWORK_MOBILE
)
request.setAllowedOverRoaming(true)
request.setTitle(fileName)
request.setDescription("Downloading $fileName")
request.setVisibleInDownloadsUi(true)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
filePath
)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
registerReceiver(
onComplete,
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
)
if (permissionGranted!!) downloadManger!!.enqueue(request)
}
}catch (e: Exception) {
Toast.makeText(
this,
"Unable to download file",
Toast.LENGTH_SHORT
).show()
}

} catch (e: Exception) {
Log.e("Error", e.toString())
Expand Down
51 changes: 51 additions & 0 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.rajat.pdfviewer.util

import android.content.Context
import android.os.Environment
import android.provider.MediaStore
import android.text.TextUtils
import java.io.*

object FileUtils {
@Throws(IOException::class)
fun fileFromAsset(context: Context, assetName: String): File {
val outFile = File(context.cacheDir, "$assetName")
if (assetName.contains("/")) {
outFile.parentFile.mkdirs()
}
copy(context.assets.open(assetName), outFile)
return outFile
}

@Throws(IOException::class)
fun copy(inputStream: InputStream?, output: File?) {
var outputStream: OutputStream? = null
try {
outputStream = FileOutputStream(output)
var read = 0
val bytes = ByteArray(1024)
while (inputStream!!.read(bytes).also { read = it } != -1) {
outputStream.write(bytes, 0, read)
}
} finally {
try {
inputStream?.close()
} finally {
outputStream?.close()
}
}
}

@Throws(IOException::class)
fun downloadFile(context: Context, assetName: String, filePath: String, fileName: String?){

val dirPath = "${Environment.getExternalStorageDirectory()}/${filePath}"
val outFile = File(dirPath)
//Create New File if not present
if (!outFile.exists()) {
outFile.mkdirs()
}
val outFile1 = File(dirPath, "/$fileName.pdf")
copy(context.assets.open(assetName), outFile1)
}
}

0 comments on commit 251184f

Please sign in to comment.