diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fb7f4a8..b589d56 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..0c0c338 --- /dev/null +++ b/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 744ff24..f671b92 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -4,10 +4,8 @@ diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..b1077fb --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index bbcbd44..f504a65 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + diff --git a/Screenshot from 2020-07-12 13-30-51.png b/Screenshot from 2020-07-12 13-30-51.png index 150db6e..aed0231 100644 Binary files a/Screenshot from 2020-07-12 13-30-51.png and b/Screenshot from 2020-07-12 13-30-51.png differ diff --git a/app/build.gradle b/app/build.gradle index 2b65014..e7d87f3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,6 @@ android { targetSdkVersion 33 versionCode 2 versionName "1.1" - multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -45,6 +44,5 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' - implementation 'com.android.support:multidex:2.0.0' implementation project(":pdfViewer") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0e63de6..c144461 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,9 @@ - diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png index a571e60..6d4b864 100644 Binary files a/app/src/main/res/mipmap-hdpi/ic_launcher.png and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png index 6dba46d..23139ba 100644 Binary files a/app/src/main/res/mipmap-xhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png index 15ac681..b847d9d 100644 Binary files a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png index f25a419..eb0f417 100644 Binary files a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/pdfViewer/build.gradle b/pdfViewer/build.gradle index 4930028..a8b318e 100644 --- a/pdfViewer/build.gradle +++ b/pdfViewer/build.gradle @@ -8,7 +8,6 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 33 - multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } @@ -46,7 +45,7 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.3' implementation 'com.google.android.material:material:1.7.0' - implementation "androidx.multidex:multidex:2.0.1" + testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' diff --git a/pdfViewer/src/main/AndroidManifest.xml b/pdfViewer/src/main/AndroidManifest.xml index 92dabfd..e9da722 100644 --- a/pdfViewer/src/main/AndroidManifest.xml +++ b/pdfViewer/src/main/AndroidManifest.xml @@ -2,8 +2,6 @@ - - diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt index bcc0b0d..b831b04 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt @@ -24,6 +24,7 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import com.rajat.pdfviewer.util.FileUtils.copyFile import kotlinx.android.synthetic.main.activity_pdf_viewer.* import kotlinx.android.synthetic.main.pdf_view_tool_bar.* import java.io.File @@ -286,11 +287,15 @@ class PdfViewerActivity : AppCompatActivity() { } private fun checkPermissionOnInit() { - if (ContextCompat.checkSelfPermission( - this, - permission.WRITE_EXTERNAL_STORAGE - ) === PackageManager.PERMISSION_GRANTED - ) { + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R){ + if (ContextCompat.checkSelfPermission( + this, + permission.WRITE_EXTERNAL_STORAGE + ) == PackageManager.PERMISSION_GRANTED + ) { + permissionGranted = true + } + }else{ permissionGranted = true } } @@ -326,14 +331,20 @@ class PdfViewerActivity : AppCompatActivity() { if (TextUtils.isEmpty(directoryName)) "/$fileName.pdf" else "/$directoryName/$fileName.pdf" try { - if (isPDFFromPath) { + if (isPDFFromPath) + if (isFromAssets) com.rajat.pdfviewer.util.FileUtils.downloadFile( this, fileUrl!!, directoryName!!, - fileName - ) - } else { + fileName) + else { + copyFile(fileUrl!!, directoryName!!, fileName!!) + Toast.makeText(this, + "PDF successfully saved to $directoryName/$fileName.pdf", + Toast.LENGTH_LONG).show() + } + else { val downloadUrl = Uri.parse(fileUrl) val downloadManger = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager? @@ -366,6 +377,7 @@ class PdfViewerActivity : AppCompatActivity() { "Unable to download file", Toast.LENGTH_SHORT ).show() + e.printStackTrace() } } else { checkPermissionOnInit() @@ -377,14 +389,20 @@ class PdfViewerActivity : AppCompatActivity() { } private fun checkPermission(requestCode: Int) { - if (ContextCompat.checkSelfPermission(this, permission.WRITE_EXTERNAL_STORAGE) - == PackageManager.PERMISSION_DENIED - ) { - ActivityCompat.requestPermissions( - this, arrayOf(permission.WRITE_EXTERNAL_STORAGE), - requestCode - ) - } else { + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + if (ContextCompat.checkSelfPermission(this, permission.WRITE_EXTERNAL_STORAGE) + == PackageManager.PERMISSION_DENIED + ) { + ActivityCompat.requestPermissions( + this, arrayOf(permission.WRITE_EXTERNAL_STORAGE), + requestCode + ) + } else { + permissionGranted = true + downloadPdf() + } + } + else { permissionGranted = true downloadPdf() } @@ -396,10 +414,16 @@ class PdfViewerActivity : AppCompatActivity() { grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) - if (requestCode == PERMISSION_CODE && - grantResults.isNotEmpty() && - grantResults[0] == PackageManager.PERMISSION_GRANTED - ) { + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + if (requestCode == PERMISSION_CODE && + grantResults.isNotEmpty() && + grantResults[0] == PackageManager.PERMISSION_GRANTED + ) { + permissionGranted = true + downloadPdf() + } + } + else { permissionGranted = true downloadPdf() } diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt index 5b4e12f..3b3714d 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt @@ -2,8 +2,8 @@ package com.rajat.pdfviewer.util import android.content.Context import android.os.Environment -import android.provider.MediaStore -import android.text.TextUtils +import android.os.Environment.DIRECTORY_DOWNLOADS +import android.os.Environment.getExternalStoragePublicDirectory import java.io.* object FileUtils { @@ -37,15 +37,22 @@ object FileUtils { } @Throws(IOException::class) - fun downloadFile(context: Context, assetName: String, filePath: String, fileName: String?){ - - val dirPath = "${Environment.getExternalStorageDirectory()}/${filePath}" + 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 + // Create New File if not present if (!outFile.exists()) { outFile.mkdirs() } + val outFile1 = File(dirPath, "/$fileName.pdf") copy(context.assets.open(assetName), outFile1) } -} \ No newline at end of file + + fun copyFile(fullPath: String, destPath: String?, fileName: String) { + val outFile = File(destPath + ?: getExternalStoragePublicDirectory( + DIRECTORY_DOWNLOADS).path, "/$fileName.pdf") + File(fullPath).copyTo(outFile, true) + } +}