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/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
}
}
@@ -396,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
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
+ }
+}
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