Skip to content

Commit

Permalink
Merge pull request #133 from TeamAmaze/feature
Browse files Browse the repository at this point in the history
Add notification permission; bump trashbin version; bump sdk version
  • Loading branch information
VishalNehra authored Oct 31, 2023
2 parents f3be684 + f4c5484 commit 3191b2e
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ dependencies {
implementation 'org.zwobble.mammoth:mammoth:1.4.2'
implementation 'me.zhanghai.android.fastscroll:library:1.1.8'
implementation "ch.acra:acra-core:5.7.0"
implementation 'com.github.TeamAmaze:AmazeTrashBin:1.0.7'
implementation 'com.github.TeamAmaze:AmazeTrashBin:1.0.9'

// https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor
implementation group: 'com.drewnoakes', name: 'metadata-extractor', version: '2.16.0'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-feature
android:name="android.software.leanback"
Expand Down
103 changes: 84 additions & 19 deletions app/src/main/java/com/amaze/fileutilities/PermissionsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import android.location.LocationManager
import android.net.Uri
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
Expand Down Expand Up @@ -62,28 +64,36 @@ open class PermissionsActivity :
if (VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestAllFilesAccess(onPermissionGranted)
}

if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && !checkNotificationPermission()) {
requestNotificationPermission(onPermissionGranted, true)
}
}
}

@RequiresApi(VERSION_CODES.TIRAMISU)
open fun checkNotificationPermission(): Boolean {
return (
ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED
)
}

fun haveStoragePermissions(): Boolean {
if (VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (VERSION.SDK_INT < Build.VERSION_CODES.R && !checkStoragePermission()) {
return false
}
if (VERSION.SDK_INT >= Build.VERSION_CODES.R &&
!Environment.isExternalStorageManager()
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!checkStoragePermission()) {
return false
}
}
return true
}

companion object {
private const val PERMISSION_LENGTH = 3
private const val PERMISSION_LENGTH = 5
private const val STORAGE_PERMISSION = 0
private const val ALL_FILES_PERMISSION = 1
private const val LOCATION_PERMISSION = 2
const val NOTIFICATION_PERMISSION = 3
}

private val permissionCallbacks = arrayOfNulls<OnPermissionGranted>(PERMISSION_LENGTH)
Expand All @@ -92,7 +102,10 @@ open class PermissionsActivity :
override fun onPermissionGranted(isGranted: Boolean) {
if (isGranted) {
// Utils.enableScreenRotation(this@PermissionsActivity)
val action = Intent(this@PermissionsActivity, MainActivity::class.java)
val action = Intent(
this@PermissionsActivity,
MainActivity::class.java
)
action.addCategory(Intent.CATEGORY_LAUNCHER)
startActivity(action)
finish()
Expand Down Expand Up @@ -120,11 +133,22 @@ open class PermissionsActivity :
permissionCallbacks[STORAGE_PERMISSION]?.onPermissionGranted(isGranted(grantResults))
} else if (requestCode == LOCATION_PERMISSION) {
if (isGranted(grantResults)) {
// Utils.enableScreenRotation(this)
permissionCallbacks[LOCATION_PERMISSION]!!.onPermissionGranted(true)
permissionCallbacks[LOCATION_PERMISSION] = null
} else if (requestCode == NOTIFICATION_PERMISSION &&
VERSION.SDK_INT >= VERSION_CODES.TIRAMISU
) {
if (!isGranted(grantResults)) {
Toast.makeText(
this, R.string.grantfailed,
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(this, R.string.grant_location_failed, Toast.LENGTH_SHORT).show()
Toast.makeText(
this, R.string.grant_location_failed,
Toast.LENGTH_SHORT
).show()
requestStoragePermission(
permissionCallbacks[LOCATION_PERMISSION]!!,
false
Expand All @@ -137,13 +161,30 @@ open class PermissionsActivity :

private fun checkStoragePermission(): Boolean {
// Verify that all required contact permissions have been granted.
return (
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
== PackageManager.PERMISSION_GRANTED
)

// Verify that all required contact permissions have been granted.
return if (VERSION.SDK_INT >= Build.VERSION_CODES.R) {
(
ActivityCompat.checkSelfPermission(
this, Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
)
== PackageManager.PERMISSION_GRANTED
) || (
ActivityCompat.checkSelfPermission(
this, Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
)

== PackageManager.PERMISSION_GRANTED
) || Environment.isExternalStorageManager()
} else {
(
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
== PackageManager.PERMISSION_GRANTED
)
}
}

fun isLocationEnabled(onPermissionGranted: OnPermissionGranted) {
Expand Down Expand Up @@ -237,6 +278,29 @@ open class PermissionsActivity :
)
}

@RequiresApi(VERSION_CODES.TIRAMISU)
private fun requestNotificationPermission(
onPermissionGranted: OnPermissionGranted,
isInitialStart: Boolean
) {
// Utils.disableScreenRotation(this)
val builder: AlertDialog.Builder = this.let {
AlertDialog.Builder(it, R.style.Custom_Dialog_Dark)
}
builder.setMessage(R.string.grant_notification_permission)
.setTitle(R.string.grant_permission)
.setNegativeButton(R.string.cancel) { dialog, _ ->
finish()
}.setCancelable(false)
requestPermission(
Manifest.permission.POST_NOTIFICATIONS,
NOTIFICATION_PERMISSION,
builder,
onPermissionGranted,
isInitialStart
)
}

/**
* Requests permission, overrides {@param rationale}'s POSITIVE button dialog action.
*
Expand Down Expand Up @@ -329,7 +393,8 @@ open class PermissionsActivity :
startActivity(intent)
} catch (e: Exception) {
log.error("Failed to initial activity to grant all files access", e)
applicationContext.showToastInCenter(getString(R.string.grantfailed))
applicationContext
.showToastInCenter(getString(R.string.grantfailed))
}
} catch (e: Exception) {
log.error("Failed to grant all files access", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@

package com.amaze.fileutilities.home_page

import android.Manifest
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.text.Html
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import com.amaze.fileutilities.R
import com.amaze.fileutilities.databinding.WelcomePermissionPrivacyLayoutBinding
Expand Down Expand Up @@ -61,7 +67,9 @@ class PermissionFragmentWelcome : Fragment() {
val activity = requireActivity() as WelcomeScreen
activity.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R && !checkStoragePermission()) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R &&
!checkStoragePermission()
) {
requestStoragePermission(onPermissionGranted, true)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Expand All @@ -70,6 +78,16 @@ class PermissionFragmentWelcome : Fragment() {
}
}
}
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && !checkNotificationPermission()) {
binding.grantNotificationButton.visibility = View.VISIBLE
binding.notificationSummaryText.visibility = View.VISIBLE
binding.grantNotificationButton.setOnClickListener {
val activity = requireActivity() as WelcomeScreen
activity.run {
requestNotificationPermission(onPermissionGranted, true)
}
}
}
binding.doneButton.setOnClickListener {
val activity = requireActivity() as WelcomeScreen
activity.run {
Expand Down Expand Up @@ -109,4 +127,15 @@ class PermissionFragmentWelcome : Fragment() {
super.onDestroyView()
_binding = null
}

@RequiresApi(VERSION_CODES.TIRAMISU)
fun checkNotificationPermission(): Boolean {
return (
ActivityCompat.checkSelfPermission(
requireContext(),
Manifest.permission.POST_NOTIFICATIONS
)
== PackageManager.PERMISSION_GRANTED
)
}
}
Loading

0 comments on commit 3191b2e

Please sign in to comment.