Skip to content

Commit

Permalink
show error when permissions are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Apr 13, 2024
1 parent 47d72fb commit f22a686
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class VolumeHandler(private var mContext: Context) {
}

fun applyVolume(){
if(!PermissionManager(mContext).grantedDoNotDisturb()){
if(!PermissionManager(mContext).grantedDoNotDisturbAndNotify()){
Log.d(TAG(), "VolumeHandler: VolumeSetting: Do not disturb not granted! Not changing Volume!")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package de.felixnuesse.timedsilence.ui.notifications
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import de.felixnuesse.timedsilence.R
import de.felixnuesse.timedsilence.extensions.TAG
Expand Down Expand Up @@ -67,4 +69,28 @@ class ErrorNotifications {
notification.build()
)
}

fun showErrorWithAction(context: Context, title: String, content: String, actiontitle: String, intent: Intent) {

val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(ERROR_CHANNEL_ID, ERROR_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)


val pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_IMMUTABLE)
val action = Notification.Action(R.drawable.icon_settings, actiontitle, pendingIntent)


val notification = Notification.Builder(context, ERROR_CHANNEL_ID)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.icon_error)
.setOnlyAlertOnce(true)
.setActions(action)

notificationManager.notify(
NOTIFICATION_ID,
notification.build()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package de.felixnuesse.timedsilence.util

import android.Manifest
import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE
import android.app.AlarmManager
import android.app.AlertDialog
import android.app.NotificationManager
Expand All @@ -13,10 +16,12 @@ import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import de.felixnuesse.timedsilence.Constants
import de.felixnuesse.timedsilence.R
import de.felixnuesse.timedsilence.ui.notifications.ErrorNotifications


class PermissionManager(private var mContext: Context) {
Expand Down Expand Up @@ -66,6 +71,30 @@ class PermissionManager(private var mContext: Context) {
return notificationManager.isNotificationPolicyAccessGranted
}

fun grantedDoNotDisturbAndNotify(): Boolean {
val granted = grantedDoNotDisturb()
if(!granted) {
val appinfo = ActivityManager.RunningAppProcessInfo()
ActivityManager.getMyMemoryState(appinfo)
if((appinfo.importance == IMPORTANCE_FOREGROUND || appinfo.importance == IMPORTANCE_VISIBLE)) {
Toast.makeText(mContext, mContext.getText(R.string.dnd_missing_permissions), Toast.LENGTH_LONG).show()
requestDoNotDisturb()
} else {
ErrorNotifications()
.showErrorWithAction(
mContext,
mContext.getString(R.string.dnd_missing_permissions_title),
mContext.getString(R.string.dnd_missing_permissions),
mContext.getString(R.string.dnd_missing_permission_action_title),
Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
)
Toast.makeText(mContext, mContext.getText(R.string.dnd_missing_permissions), Toast.LENGTH_LONG).show()
}

}
return granted
}

fun grantedAlarms(): Boolean {
val alarmManager = mContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@
<string name="bluetooth_add_new">Add new bluetooth setting</string>
<string name="bluetooth_dialog_title_title">Add a bluetooth device</string>
<string name="dialog_bluetooth_select_device">Select a paired device</string>
<string name="dnd_missing_permissions">You did not grant \"Do not Disturb\"-access. We can\'t change your volume!</string>
<string name="dnd_missing_permissions_title">Missing Permissions!</string>
<string name="dnd_missing_permission_action_title">Fix!</string>
</resources>

0 comments on commit f22a686

Please sign in to comment.