From fd249e549f30d9464c15d83e3f4c28ae6a9ff137 Mon Sep 17 00:00:00 2001 From: bharb Date: Tue, 2 Aug 2022 13:52:35 +0300 Subject: [PATCH 1/7] Fix language --- .idea/codeStyles/Project.xml | 16 ---------- .idea/gradle.xml | 2 +- .idea/misc.xml | 2 +- pdfViewer/build.gradle | 2 +- .../com/rajat/pdfviewer/PdfViewerActivity.kt | 9 ++++-- .../rajat/pdfviewer/util/MyContextWrapper.kt | 32 +++++++++++++++++++ 6 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 3cc336b..88ea3aa 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,22 +1,6 @@ - - diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 744ff24..12c6494 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -7,7 +7,7 @@ - + diff --git a/pdfViewer/build.gradle b/pdfViewer/build.gradle index 9841beb..a6abc66 100644 --- a/pdfViewer/build.gradle +++ b/pdfViewer/build.gradle @@ -6,7 +6,7 @@ android { compileSdkVersion 32 defaultConfig { - minSdkVersion 16 + minSdkVersion 23 targetSdkVersion 32 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt index 4c28a97..d5ab752 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt @@ -23,6 +23,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.Languages import kotlinx.android.synthetic.main.activity_pdf_viewer.* import kotlinx.android.synthetic.main.pdf_view_tool_bar.* import java.io.File @@ -112,6 +113,10 @@ class PdfViewerActivity : AppCompatActivity() { init() } + override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(Languages.setLocale(newBase)) + } + private fun init() { if (intent.extras!!.containsKey(FILE_URL)) { fileUrl = intent.extras!!.getString(FILE_URL) @@ -175,10 +180,10 @@ class PdfViewerActivity : AppCompatActivity() { supportActionBar?.apply { setDisplayHomeAsUpEnabled(true) setDisplayShowHomeEnabled(true) - if(tvAppBarTitle!=null) { + if (tvAppBarTitle != null) { tvAppBarTitle?.text = toolbarTitle setDisplayShowTitleEnabled(false) - }else{ + } else { setDisplayShowTitleEnabled(true) title = toolbarTitle } diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt new file mode 100644 index 0000000..1746487 --- /dev/null +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt @@ -0,0 +1,32 @@ +package com.rajat.pdfviewer.util + +import android.content.Context +import android.content.res.Configuration +import android.preference.PreferenceManager +import java.util.* + +object Languages { + + fun setLocale(context: Context): Context? { + return updateResources(context, getLanguage(context)) + } + + private fun getLanguage(c: Context): String? { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c) + return sharedPreferences.getString( + "list_preference_fragment_more_language", Locale.getDefault().language + ) + } + + private fun updateResources(context: Context?, language: String?): Context? { + val result: Context? + val locale = Locale(language ?: Locale.getDefault().language) + Locale.setDefault(locale) + + val res = context?.resources + val config = Configuration(res?.configuration) + config.setLocale(locale) + result = context?.createConfigurationContext(config) + return result + } +} From 9cbfc42dd47045d7034042a539473ad22486d59d Mon Sep 17 00:00:00 2001 From: bharb Date: Tue, 2 Aug 2022 15:33:41 +0300 Subject: [PATCH 2/7] initial commit --- app/build.gradle | 2 +- pdfViewer/build.gradle | 1 + .../src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 19e35c1..1d6fc79 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,7 +7,7 @@ android { defaultConfig { applicationId "com.rajat.pdfviewer" - minSdkVersion 16 + minSdkVersion 23 targetSdkVersion 32 versionCode 2 versionName "1.1" diff --git a/pdfViewer/build.gradle b/pdfViewer/build.gradle index a6abc66..9e60728 100644 --- a/pdfViewer/build.gradle +++ b/pdfViewer/build.gradle @@ -40,6 +40,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.6.1' + implementation "androidx.preference:preference-ktx:1.2.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt index 1746487..b53eaf4 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt @@ -2,7 +2,7 @@ package com.rajat.pdfviewer.util import android.content.Context import android.content.res.Configuration -import android.preference.PreferenceManager +import androidx.preference.PreferenceManager import java.util.* object Languages { From 290b26aeee73c9972ff75686972255080385920b Mon Sep 17 00:00:00 2001 From: bharb Date: Tue, 2 Aug 2022 15:56:31 +0300 Subject: [PATCH 3/7] initial commit --- app/src/main/java/com/rajat/pdfviewer/MainActivity.kt | 8 +++++--- .../main/java/com/rajat/pdfviewer/PdfViewerActivity.kt | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt b/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt index 8bb4263..c53fe2c 100644 --- a/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt +++ b/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt @@ -8,6 +8,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import com.rajat.pdfviewer.databinding.ActivityMainBinding +import com.rajat.pdfviewer.util.Languages class MainActivity : AppCompatActivity() { @@ -18,7 +19,8 @@ class MainActivity : AppCompatActivity() { Manifest.permission.READ_EXTERNAL_STORAGE ) - private var download_file_url = "https://github.com/afreakyelf/afreakyelf/raw/main/Log4_Shell_Mid_Term_final.pdf" + private var download_file_url = + "https://github.com/afreakyelf/afreakyelf/raw/main/Log4_Shell_Mid_Term_final.pdf" var per = 0f private val PERMISSION_CODE = 4040 @@ -37,8 +39,8 @@ class MainActivity : AppCompatActivity() { private fun launchPdf() { startActivity( PdfViewerActivity.launchPdfFromUrl( - this, download_file_url, - "Title", "dir",true + this, Languages.setLocale(this), download_file_url, + "Title", "dir", true ) ) } diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt index d5ab752..0a5050c 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt @@ -23,7 +23,6 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat -import com.rajat.pdfviewer.util.Languages import kotlinx.android.synthetic.main.activity_pdf_viewer.* import kotlinx.android.synthetic.main.pdf_view_tool_bar.* import java.io.File @@ -38,6 +37,7 @@ class PdfViewerActivity : AppCompatActivity() { private var menuItem: MenuItem? = null private var fileUrl: String? = null + companion object { const val FILE_URL = "pdf_file_url" const val FILE_DIRECTORY = "pdf_file_directory" @@ -49,15 +49,17 @@ class PdfViewerActivity : AppCompatActivity() { var isPDFFromPath = false var isFromAssets = false var PERMISSION_CODE = 4040 - + var contextLanguage: Context? = null fun launchPdfFromUrl( context: Context?, + contextLanguage: Context?, pdfUrl: String?, pdfTitle: String?, directoryName: String?, enableDownload: Boolean = true ): Intent { + this.contextLanguage = contextLanguage val intent = Intent(context, PdfViewerActivity::class.java) intent.putExtra(FILE_URL, pdfUrl) intent.putExtra(FILE_TITLE, pdfTitle) @@ -114,7 +116,7 @@ class PdfViewerActivity : AppCompatActivity() { } override fun attachBaseContext(newBase: Context) { - super.attachBaseContext(Languages.setLocale(newBase)) + super.attachBaseContext(contextLanguage) } private fun init() { From 8f8990c170549ed9ceaa58b66464565889168c26 Mon Sep 17 00:00:00 2001 From: bharb Date: Tue, 2 Aug 2022 16:07:47 +0300 Subject: [PATCH 4/7] initial commit --- .../java/com/rajat/pdfviewer/MainActivity.kt | 3 +- .../com/rajat/pdfviewer/PdfViewerActivity.kt | 18 ++++++++++- .../rajat/pdfviewer/util/MyContextWrapper.kt | 32 ------------------- 3 files changed, 18 insertions(+), 35 deletions(-) delete mode 100644 pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt diff --git a/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt b/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt index c53fe2c..3be97df 100644 --- a/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt +++ b/app/src/main/java/com/rajat/pdfviewer/MainActivity.kt @@ -8,7 +8,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import com.rajat.pdfviewer.databinding.ActivityMainBinding -import com.rajat.pdfviewer.util.Languages class MainActivity : AppCompatActivity() { @@ -39,7 +38,7 @@ class MainActivity : AppCompatActivity() { private fun launchPdf() { startActivity( PdfViewerActivity.launchPdfFromUrl( - this, Languages.setLocale(this), download_file_url, + this, download_file_url, "Title", "dir", true ) ) diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt index 0a5050c..102baf7 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt @@ -52,6 +52,22 @@ class PdfViewerActivity : AppCompatActivity() { var contextLanguage: Context? = null fun launchPdfFromUrl( + context: Context?, + pdfUrl: String?, + pdfTitle: String?, + directoryName: String?, + enableDownload: Boolean = true + ): Intent { + val intent = Intent(context, PdfViewerActivity::class.java) + intent.putExtra(FILE_URL, pdfUrl) + intent.putExtra(FILE_TITLE, pdfTitle) + intent.putExtra(FILE_DIRECTORY, directoryName) + intent.putExtra(ENABLE_FILE_DOWNLOAD, enableDownload) + isPDFFromPath = false + return intent + } + + fun launchPdfFromUrlLanguage( context: Context?, contextLanguage: Context?, pdfUrl: String?, @@ -116,7 +132,7 @@ class PdfViewerActivity : AppCompatActivity() { } override fun attachBaseContext(newBase: Context) { - super.attachBaseContext(contextLanguage) + super.attachBaseContext(contextLanguage ?: newBase) } private fun init() { diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt deleted file mode 100644 index b53eaf4..0000000 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.rajat.pdfviewer.util - -import android.content.Context -import android.content.res.Configuration -import androidx.preference.PreferenceManager -import java.util.* - -object Languages { - - fun setLocale(context: Context): Context? { - return updateResources(context, getLanguage(context)) - } - - private fun getLanguage(c: Context): String? { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c) - return sharedPreferences.getString( - "list_preference_fragment_more_language", Locale.getDefault().language - ) - } - - private fun updateResources(context: Context?, language: String?): Context? { - val result: Context? - val locale = Locale(language ?: Locale.getDefault().language) - Locale.setDefault(locale) - - val res = context?.resources - val config = Configuration(res?.configuration) - config.setLocale(locale) - result = context?.createConfigurationContext(config) - return result - } -} From 2a9fb31302b92c128e5e3d005b872a7592514a3c Mon Sep 17 00:00:00 2001 From: baderharb <52153544+baderharb@users.noreply.github.com> Date: Tue, 2 Aug 2022 16:32:04 +0300 Subject: [PATCH 5/7] Update README.md --- README.md | 121 ------------------------------------------------------ 1 file changed, 121 deletions(-) diff --git a/README.md b/README.md index 3216438..8b13789 100644 --- a/README.md +++ b/README.md @@ -1,122 +1 @@ -

Pdf Viewer For Android

-

-A Simple PDF Viewer library which only occupies around 125kb while most of the Pdf viewer occupies upto 16MB space. -
-
- -

- -[![](https://jitpack.io/v/afreakyelf/Pdf-Viewer.svg)](https://jitpack.io/#afreakyelf/Pdf-Viewer) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/Apache-2.0) ![](https://img.shields.io/github/forks/afreakyelf/Pdf-Viewer?label=Forks) -![](https://img.shields.io/github/stars/afreakyelf/Pdf-Viewer?label=Stars&color=9cf) ![](https://visitor-badge.glitch.me/badge?page_id=afreakyelf.Pdf-Viewer)[![](https://jitci.com/gh/afreakyelf/Pdf-Viewer/svg)](https://jitci.com/gh/afreakyelf/Pdf-Viewer) - - - -## How to integrate into your app? -Integrating the project is simple, All you need to do is follow the below steps - -Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories: - -```java -allprojects { - repositories { - ... - maven { url "https://jitpack.io" } - } -} -``` - -Step 2. Add the dependency -```java -dependencies { - implementation 'com.github.afreakyelf:Pdf-Viewer:{Tag}' -} -``` -NOTE: Replace the tag with current release version, e.g - -```java -implementation 'com.github.afreakyelf:Pdf-Viewer:v1.0.7' -``` - -## How to use the library? -Now you have integrated the library in your project but **how do you use it**? Well its really easy just launch the intent with in following way: - -### Kotlin -```kotlin -open_pdf.setOnClickListener { - startActivity( - - // Use 'launchPdfFromPath' if you want to use assets file (enable "fromAssets" flag) / internal directory - - PdfViewerActivity.launchPdfFromUrl( //PdfViewerActivity.Companion.launchPdfFromUrl(.. :: incase of JAVA - context, - "pdf_url", // PDF URL in String format - "Pdf title/name ", // PDF Name/Title in String format - "pdf directory to save", // If nothing specific, Put "" it will save to Downloads - enableDownload = false // This param is true by defualt. - ) - ) - } -``` - -### Java - -```java - open_pdf.setOnClickListener(view -> { - startActivity( - - // Opening pdf from assets folder - - PdfViewerActivity.Companion.launchPdfFromPath( - this, - "file_name.pdf", - "Pdf title/name", - "assets", - false, - true - ) - ); - }); - -``` - -That's pretty much it and you're all wrapped up. - -### Ui Customizations -You need to add the custom theme to styles.xml/themes.xml file and override the required attribute values. -Parent theme can be either **Theme.PdfView.Light** or **Theme.PdfView.Dark** or the one with no actionbar from the application. -Note: If parent is not one of the themes from this library, all of the pdfView attributes should be added to that theme. - - - -#### Supported attributes - -| Attribute Name | Type | Expected changes | -|--|--|--| -|pdfView_backIcon|drawable|Navigation icon| -|pdfView_downloadIcon|drawable|Download icon| -|pdfView_downloadIconTint|color|Download icon tint| -|pdfView_actionBarTint|color|Actionbar background color| -|pdfView_titleTextStyle|style|Actionbar title text appearance| -|pdfView_progressBar|style|Progress bar style| - -## Contributing - -Any contributions you make are **greatly appreciated**. - -1. Fork the Project -2. Create your Feature Branch (`git checkout -b feature/NewFeature`) -3. Commit your Changes (`git commit -m 'Add some NewFeature'`) -4. Push to the Branch (`git push origin feature/NewFeature`) -5. Open a Pull Request - -## Donations -If this project help you reduce time to develop, you can give me a cup of coffee :) - -[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/paypalme/afreakyelf) - -## Author -Maintained by [Rajat Mittal](https://www.github.com/afreakyelf) From c1ab9afc5035384356b4524f5c16ea329dbf1371 Mon Sep 17 00:00:00 2001 From: bharb Date: Tue, 2 Aug 2022 16:47:01 +0300 Subject: [PATCH 6/7] Fix UI direction issue when change language to Arabic --- .../com/rajat/pdfviewer/PdfViewerActivity.kt | 21 ++---------- .../rajat/pdfviewer/util/MyContextWrapper.kt | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt index 102baf7..c729172 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt @@ -23,6 +23,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.Languages import kotlinx.android.synthetic.main.activity_pdf_viewer.* import kotlinx.android.synthetic.main.pdf_view_tool_bar.* import java.io.File @@ -67,24 +68,6 @@ class PdfViewerActivity : AppCompatActivity() { return intent } - fun launchPdfFromUrlLanguage( - context: Context?, - contextLanguage: Context?, - pdfUrl: String?, - pdfTitle: String?, - directoryName: String?, - enableDownload: Boolean = true - ): Intent { - this.contextLanguage = contextLanguage - val intent = Intent(context, PdfViewerActivity::class.java) - intent.putExtra(FILE_URL, pdfUrl) - intent.putExtra(FILE_TITLE, pdfTitle) - intent.putExtra(FILE_DIRECTORY, directoryName) - intent.putExtra(ENABLE_FILE_DOWNLOAD, enableDownload) - isPDFFromPath = false - return intent - } - fun launchPdfFromPath( context: Context?, path: String?, @@ -132,7 +115,7 @@ class PdfViewerActivity : AppCompatActivity() { } override fun attachBaseContext(newBase: Context) { - super.attachBaseContext(contextLanguage ?: newBase) + super.attachBaseContext(Languages.setLocale(newBase)) } private fun init() { diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt new file mode 100644 index 0000000..b53eaf4 --- /dev/null +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt @@ -0,0 +1,32 @@ +package com.rajat.pdfviewer.util + +import android.content.Context +import android.content.res.Configuration +import androidx.preference.PreferenceManager +import java.util.* + +object Languages { + + fun setLocale(context: Context): Context? { + return updateResources(context, getLanguage(context)) + } + + private fun getLanguage(c: Context): String? { + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c) + return sharedPreferences.getString( + "list_preference_fragment_more_language", Locale.getDefault().language + ) + } + + private fun updateResources(context: Context?, language: String?): Context? { + val result: Context? + val locale = Locale(language ?: Locale.getDefault().language) + Locale.setDefault(locale) + + val res = context?.resources + val config = Configuration(res?.configuration) + config.setLocale(locale) + result = context?.createConfigurationContext(config) + return result + } +} From 0542bebf1b5231e77b706cb1d5e0355aa1a27530 Mon Sep 17 00:00:00 2001 From: bharb Date: Mon, 23 Jan 2023 13:38:13 +0300 Subject: [PATCH 7/7] fix android 13 --- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 14 +++- .../rajat/sample/pdfviewer/MainActivity.kt | 79 ++++++++----------- .../com/rajat/sample/pdfviewer/Permissions.kt | 37 +++++++++ app/src/main/res/layout/activity_main.xml | 2 +- gradle.properties | 32 +++----- pdfViewer/build.gradle | 5 +- pdfViewer/src/main/AndroidManifest.xml | 3 + .../com/rajat/pdfviewer/PdfViewerActivity.kt | 76 +++++++++--------- .../rajat/pdfviewer/util/MyContextWrapper.kt | 2 +- .../com/rajat/pdfviewer/util/Permissions.kt | 37 +++++++++ 11 files changed, 182 insertions(+), 110 deletions(-) create mode 100644 app/src/main/java/com/rajat/sample/pdfviewer/Permissions.kt create mode 100644 pdfViewer/src/main/java/com/rajat/pdfviewer/util/Permissions.kt diff --git a/app/build.gradle b/app/build.gradle index 2b65014..5d7b29f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,7 +7,7 @@ android { defaultConfig { applicationId "com.rajat.sample.pdfviewer" - minSdkVersion 16 + minSdkVersion 23 targetSdkVersion 33 versionCode 2 versionName "1.1" @@ -47,4 +47,7 @@ dependencies { androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'com.android.support:multidex:2.0.0' implementation project(":pdfViewer") + // easy permissions + implementation 'com.vmadalin:easypermissions-ktx:1.0.0' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0e63de6..14063aa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,25 +1,31 @@ + package="com.rajat.pdfviewer"> - + + + + - diff --git a/app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt b/app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt index 65a41e4..7fbfb69 100644 --- a/app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt +++ b/app/src/main/java/com/rajat/sample/pdfviewer/MainActivity.kt @@ -1,28 +1,20 @@ package com.rajat.sample.pdfviewer -import android.Manifest -import android.content.pm.PackageManager +import android.Manifest.permission.READ_EXTERNAL_STORAGE +import android.Manifest.permission.READ_MEDIA_IMAGES +import android.os.Build import android.os.Bundle -import android.widget.Toast import androidx.appcompat.app.AppCompatActivity -import androidx.core.app.ActivityCompat -import androidx.core.content.ContextCompat import com.rajat.pdfviewer.PdfViewerActivity -import com.rajat.sample.pdfviewer.databinding.ActivityMainBinding +import com.rajat.pdfviewer.databinding.ActivityMainBinding +import com.vmadalin.easypermissions.EasyPermissions - -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks { private lateinit var binding: ActivityMainBinding - private val requiredPermissionList = arrayOf( - Manifest.permission.WRITE_EXTERNAL_STORAGE, - Manifest.permission.READ_EXTERNAL_STORAGE - ) - - private var download_file_url = "https://github.com/afreakyelf/afreakyelf/raw/main/Log4_Shell_Mid_Term_final.pdf" - var per = 0f - private val PERMISSION_CODE = 4040 + private var download_file_url = + "https://github.com/afreakyelf/afreakyelf/raw/main/Log4_Shell_Mid_Term_final.pdf" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -31,8 +23,7 @@ class MainActivity : AppCompatActivity() { setContentView(view) binding.openPdf.setOnClickListener { - if (checkAndRequestPermission()) - launchPdf() + requestFilesPermission() } } @@ -40,31 +31,34 @@ class MainActivity : AppCompatActivity() { startActivity( PdfViewerActivity.launchPdfFromUrl( context = this, pdfUrl = download_file_url, - pdfTitle = "Title", directoryName = "dir", enableDownload = true) + pdfTitle = "Title", directoryName = "dir", enableDownload = true + ) ) } - private fun checkAndRequestPermission(): Boolean { - val permissionsNeeded = ArrayList() - - for (permission in requiredPermissionList) { - if (ContextCompat.checkSelfPermission(this, permission) != - PackageManager.PERMISSION_GRANTED - ) { - permissionsNeeded.add(permission) + private fun requestFilesPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (hasPermission(READ_MEDIA_IMAGES)) { + launchPdf() + } else if (enableRequestPermission(READ_MEDIA_IMAGES)) { + requestPermission(READ_MEDIA_IMAGES, 1, "") + } + } else { + if (this.hasPermission(READ_EXTERNAL_STORAGE)) { + launchPdf() + } else if (enableRequestPermission(READ_EXTERNAL_STORAGE)) { + requestPermission(READ_EXTERNAL_STORAGE, 1, "") } } + } - if (permissionsNeeded.isNotEmpty()) { - ActivityCompat.requestPermissions( - this, - permissionsNeeded.toTypedArray(), - PERMISSION_CODE - ) - return false - } + override fun onPermissionsDenied(requestCode: Int, perms: List) { + } - return true + override fun onPermissionsGranted(requestCode: Int, perms: List) { + if (requestCode == 1) { + launchPdf() + } } override fun onRequestPermissionsResult( @@ -73,17 +67,8 @@ class MainActivity : AppCompatActivity() { grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) - when (requestCode) { - PERMISSION_CODE -> if (grantResults.isNotEmpty()) { - val readPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED - val writePermission = grantResults[1] == PackageManager.PERMISSION_GRANTED - if (readPermission && writePermission) - launchPdf() - else { - Toast.makeText(this, " Permission Denied", Toast.LENGTH_SHORT).show() - } - } + if (requestCode == 1) { + launchPdf() } } - } diff --git a/app/src/main/java/com/rajat/sample/pdfviewer/Permissions.kt b/app/src/main/java/com/rajat/sample/pdfviewer/Permissions.kt new file mode 100644 index 0000000..9c17304 --- /dev/null +++ b/app/src/main/java/com/rajat/sample/pdfviewer/Permissions.kt @@ -0,0 +1,37 @@ +package com.rajat.sample.pdfviewer + +import android.annotation.SuppressLint +import android.app.Activity +import android.content.Context +import android.content.pm.PackageManager +import android.view.View +import androidx.core.content.ContextCompat +import com.vmadalin.easypermissions.EasyPermissions + +fun Context.isPermissionGranted(permission: String): Boolean { + return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED +} + +@SuppressLint("MissingPermission") +fun View.isPermissionGranted(permission: String): Boolean { + return context.isPermissionGranted(permission) +} + + +fun Activity.hasPermission(permission: String) = EasyPermissions.hasPermissions( + this, + permission +) + +fun Activity.requestPermission(permission: String, requestCode: Int, rationalText: String) { + EasyPermissions.requestPermissions( + this, + rationalText, + requestCode, + permission + ) +} + +fun Activity.enableRequestPermission(permission: String): Boolean { + return EasyPermissions.somePermissionPermanentlyDenied(this, listOf(permission)) +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8b71c29..b785601 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,9 +10,9 @@ android:id="@+id/open_pdf" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:backgroundTint="@color/colorPrimary" android:text="Open pdf" android:textAllCaps="false" - android:backgroundTint="@color/colorPrimary" android:textColor="#ffffff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" diff --git a/gradle.properties b/gradle.properties index 9bb1cb2..5760a4d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,21 +1,15 @@ -# Project-wide Gradle settings. -# IDE (e.g. Android Studio) users: -# Gradle settings configured through the IDE *will override* -# any settings specified in this file. -# For more details on how to configure your build environment visit -# http://www.gradle.org/docs/current/userguide/build_environment.html -# Specifies the JVM arguments used for the daemon process. -# The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx2048m -# When configured, Gradle will run in incubating parallel mode. -# This option should only be used with decoupled projects. More details, visit -# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true -# AndroidX package structure to make it clearer which packages are bundled with the -# Android operating system, and which are packaged with your app"s APK -# https://developer.android.com/topic/libraries/support-library/androidx-rn +# Gradle +org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +org.gradle.parallel=true +org.gradle.configureondemand=true +org.gradle.daemon=true +# +# Kotlin +kotlin.code.style=official +# +# Android android.useAndroidX=true -# Automatically convert third-party libraries to use AndroidX android.enableJetifier=true -# Kotlin code style for this project: "official" or "obsolete": -kotlin.code.style=official +android.databinding.incremental=true +android.viewbinding.incremental=true +android.lifecycleProcessor.incremental=true diff --git a/pdfViewer/build.gradle b/pdfViewer/build.gradle index ead7530..81d72c2 100644 --- a/pdfViewer/build.gradle +++ b/pdfViewer/build.gradle @@ -6,7 +6,7 @@ android { compileSdkVersion 33 defaultConfig { - minSdkVersion 16 + minSdkVersion 23 targetSdkVersion 33 multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -48,5 +48,8 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' + // easy permissions + implementation 'com.vmadalin:easypermissions-ktx:1.0.0' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0' } \ No newline at end of file diff --git a/pdfViewer/src/main/AndroidManifest.xml b/pdfViewer/src/main/AndroidManifest.xml index 92dabfd..d3d96f7 100644 --- a/pdfViewer/src/main/AndroidManifest.xml +++ b/pdfViewer/src/main/AndroidManifest.xml @@ -8,6 +8,9 @@ + + + = Build.VERSION_CODES.TIRAMISU) { + if (hasPermission(permission.READ_MEDIA_IMAGES)) { + permissionGranted = true + downloadPdf() + } else if (enableRequestPermission(permission.READ_MEDIA_IMAGES)) { + requestPermission(permission.READ_MEDIA_IMAGES, 1, "") + } } else { + if (this.hasPermission(permission.READ_EXTERNAL_STORAGE)) { + permissionGranted = true + downloadPdf() + } else if (enableRequestPermission(permission.READ_EXTERNAL_STORAGE)) { + requestPermission(permission.READ_EXTERNAL_STORAGE, 1, "") + } + } + } + + override fun onPermissionsDenied(requestCode: Int, perms: List) { + permissionGranted = false + } + + override fun onPermissionsGranted(requestCode: Int, perms: List) { + if (requestCode == 1) { permissionGranted = true downloadPdf() + } else { + permissionGranted = false } } @@ -402,18 +412,12 @@ class PdfViewerActivity : AppCompatActivity() { grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) - if (requestCode == PERMISSION_CODE && - grantResults.isNotEmpty() && - grantResults[0] == PackageManager.PERMISSION_GRANTED - ) { + if (requestCode == 1) { permissionGranted = true downloadPdf() + } else { + permissionGranted = false } } - override fun onDestroy() { - super.onDestroy() - pdfView.closePdfRender() - } - } \ No newline at end of file diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt index b53eaf4..1746487 100644 --- a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/MyContextWrapper.kt @@ -2,7 +2,7 @@ package com.rajat.pdfviewer.util import android.content.Context import android.content.res.Configuration -import androidx.preference.PreferenceManager +import android.preference.PreferenceManager import java.util.* object Languages { diff --git a/pdfViewer/src/main/java/com/rajat/pdfviewer/util/Permissions.kt b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/Permissions.kt new file mode 100644 index 0000000..336ea67 --- /dev/null +++ b/pdfViewer/src/main/java/com/rajat/pdfviewer/util/Permissions.kt @@ -0,0 +1,37 @@ +package com.rajat.pdfviewer.util + +import android.annotation.SuppressLint +import android.app.Activity +import android.content.Context +import android.content.pm.PackageManager +import android.view.View +import androidx.core.content.ContextCompat +import com.vmadalin.easypermissions.EasyPermissions + +fun Context.isPermissionGranted(permission: String): Boolean { + return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED +} + +@SuppressLint("MissingPermission") +fun View.isPermissionGranted(permission: String): Boolean { + return context.isPermissionGranted(permission) +} + + +fun Activity.hasPermission(permission: String) = EasyPermissions.hasPermissions( + this, + permission +) + +fun Activity.requestPermission(permission: String, requestCode: Int, rationalText: String) { + EasyPermissions.requestPermissions( + this, + rationalText, + requestCode, + permission + ) +} + +fun Activity.enableRequestPermission(permission: String): Boolean { + return EasyPermissions.somePermissionPermanentlyDenied(this, listOf(permission)) +} \ No newline at end of file