Skip to content

Commit

Permalink
fix(PermissionsDialog): Granting permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
axel358 committed Jan 18, 2024
1 parent d5d0f6a commit b3fa06f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 38 deletions.
106 changes: 82 additions & 24 deletions app/src/main/java/cu/axel/smartdock/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import cu.axel.smartdock.fragments.PreferencesFragment
import cu.axel.smartdock.services.NotificationService
import cu.axel.smartdock.utils.ColorUtils
import cu.axel.smartdock.utils.DeviceUtils
import kotlin.reflect.KFunction0


class MainActivity : AppCompatActivity() {
Expand All @@ -43,12 +44,16 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
supportFragmentManager.beginTransaction().replace(R.id.settings_container, PreferencesFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.settings_container, PreferencesFragment())
.commit()
if (!DeviceUtils.hasStoragePermission(this)) {
DeviceUtils.requestStoragePermissions(this)
}
if (!DeviceUtils.canDrawOverOtherApps(this) || !DeviceUtils.isAccessibilityServiceEnabled(this))
if (!DeviceUtils.canDrawOverOtherApps(this) || !DeviceUtils.isAccessibilityServiceEnabled(
this
)
)
showPermissionsDialog()
if (sharedPreferences.getInt("dock_layout", -1) == -1)
showDockLayoutsDialog()
Expand Down Expand Up @@ -92,35 +97,72 @@ class MainActivity : AppCompatActivity() {
builder.setView(view)
permissionsDialog = builder.create()
overlayBtn.setOnClickListener {
showPermissionInfoDialog(R.string.display_over_other_apps, R.string.display_over_other_apps_desc,
(DeviceUtils::grantOverlayPermissions)(this), canDrawOverOtherApps)
showPermissionInfoDialog(
R.string.display_over_other_apps, R.string.display_over_other_apps_desc,
::grantOverlayPermissions, canDrawOverOtherApps
)
}
storageBtn.setOnClickListener {
showPermissionInfoDialog(R.string.storage, R.string.storage_desc,
DeviceUtils.requestStoragePermissions(this), hasStoragePermission)
showPermissionInfoDialog(
R.string.storage, R.string.storage_desc,
::requestStoragePermissions, hasStoragePermission
)
}
adminBtn.setOnClickListener {
showPermissionInfoDialog(R.string.device_administrator, R.string.device_administrator_desc,
DeviceUtils.requestDeviceAdminPermissions(this), isDeviceAdminEnabled)
showPermissionInfoDialog(
R.string.device_administrator, R.string.device_administrator_desc,
::requestDeviceAdminPermissions, isDeviceAdminEnabled
)
}
notificationsBtn.setOnClickListener { showNotificationsDialog() }
accessibilityBtn.setOnClickListener { showAccessibilityDialog() }
locationBtn.setOnClickListener {
showPermissionInfoDialog(R.string.location, R.string.location_desc,
DeviceUtils.requestLocationPermissions(this), hasLocationPermission)
showPermissionInfoDialog(
R.string.location, R.string.location_desc,
::requestLocationPermissions, hasLocationPermission
)
}
usageBtn.setOnClickListener {
showPermissionInfoDialog(R.string.usage_stats, R.string.usage_stats_desc,
startActivity(Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)),
DeviceUtils.hasUsageStatsPermission(this))
showPermissionInfoDialog(
R.string.usage_stats, R.string.usage_stats_desc,
::requestUsageStatsPermissions,
DeviceUtils.hasUsageStatsPermission(this)
)
}
secureBtn.setOnClickListener {
showPermissionInfoDialog(
R.string.write_secure,
R.string.write_secure_desc,
null,
true
)
}
secureBtn.setOnClickListener { showPermissionInfoDialog(R.string.write_secure, R.string.write_secure_desc, null, true) }
requiredBtn.setOnClickListener { viewSwitcher.showPrevious() }
optionalBtn.setOnClickListener { viewSwitcher.showNext() }
updatePermissionsStatus()
permissionsDialog.show()
}

private fun grantOverlayPermissions() {
DeviceUtils.grantOverlayPermissions(this)
}

private fun requestStoragePermissions() {
DeviceUtils.requestStoragePermissions(this)
}

private fun requestLocationPermissions() {
DeviceUtils.requestLocationPermissions(this)
}

private fun requestDeviceAdminPermissions() {
DeviceUtils.requestDeviceAdminPermissions(this)
}

private fun requestUsageStatsPermissions() {
startActivity(Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS))
}

private fun showDockLayoutsDialog() {
val editor = sharedPreferences.edit()
val dialog = MaterialAlertDialogBuilder(this)
Expand Down Expand Up @@ -156,18 +198,21 @@ class MainActivity : AppCompatActivity() {
}
if (DeviceUtils.isAccessibilityServiceEnabled(this)) {
accessibilityBtn.setIconResource(R.drawable.ic_settings)
accessibilityBtn.iconTint = ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[0])
accessibilityBtn.iconTint =
ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[0])
} else {
accessibilityBtn.setIconResource(R.drawable.ic_alert)
accessibilityBtn.iconTint = ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[2])
accessibilityBtn.iconTint =
ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[2])
}
if (DeviceUtils.hasUsageStatsPermission(this)) {
usageBtn.setIconResource(R.drawable.ic_granted)
usageBtn.iconTint = ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[0])
}
if (DeviceUtils.isServiceRunning(this, NotificationService::class.java)) {
notificationsBtn.setIconResource(R.drawable.ic_settings)
notificationsBtn.iconTint = ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[0])
notificationsBtn.iconTint =
ColorStateList.valueOf(ColorUtils.getThemeColors(this, false)[0])
}
isDeviceAdminEnabled = DeviceUtils.isDeviceAdminEnabled(this)
if (isDeviceAdminEnabled) {
Expand All @@ -191,12 +236,17 @@ class MainActivity : AppCompatActivity() {
}
}

private fun showPermissionInfoDialog(permission: Int, description: Int, grantMethod: Unit?, granted: Boolean) {
private fun showPermissionInfoDialog(
permission: Int,
description: Int,
grantMethod: KFunction0<Unit>?,
granted: Boolean
) {
val dialogBuilder = MaterialAlertDialogBuilder(this)
dialogBuilder.setTitle(permission)
dialogBuilder.setMessage(description)
if (!granted)
dialogBuilder.setPositiveButton(R.string.grant) { _, _ -> grantMethod!!.run {} }
dialogBuilder.setPositiveButton(R.string.grant) { _, _ -> grantMethod!!.invoke() }
else dialogBuilder.setPositiveButton(R.string.ok, null)
dialogBuilder.show()
}
Expand All @@ -221,8 +271,12 @@ class MainActivity : AppCompatActivity() {
}
}
dialogBuilder.setNeutralButton(R.string.help) { _, _ ->
startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse("https://github.com/axel358/smartdock#grant-restricted-permissions")))
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://github.com/axel358/smartdock#grant-restricted-permissions")
)
)
}
dialogBuilder.show()
}
Expand All @@ -236,8 +290,12 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this, R.string.enable_access_help, Toast.LENGTH_LONG).show()
}
dialogBuilder.setNeutralButton(R.string.help) { _, _ ->
startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse("https://github.com/axel358/smartdock#grant-restricted-permissions")))
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://github.com/axel358/smartdock#grant-restricted-permissions")
)
)
}
dialogBuilder.show()
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/cu/axel/smartdock/services/DockService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,8 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
if (iconUri != "default") {
val bitmap = Utils.getBitmapFromUri(context, Uri.parse(iconUri))
val icon = Utils.getCircularBitmap(bitmap)
if (icon != null) avatarIv.setImageBitmap(icon)
if (icon != null)
avatarIv.setImageBitmap(icon)
} else avatarIv.setImageResource(R.drawable.ic_user)
}
appMenu.alpha = 0f
Expand Down
59 changes: 46 additions & 13 deletions app/src/main/java/cu/axel/smartdock/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ object Utils {
//Copy the bitmap to avoid software rendering issues
val bitmapCopy = bitmap.copy(Bitmap.Config.ARGB_8888, false)
bitmap.recycle()
val result = Bitmap.createBitmap(bitmapCopy.width, bitmapCopy.height, Bitmap.Config.ARGB_8888)
val result =
Bitmap.createBitmap(bitmapCopy.width, bitmapCopy.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(result)
val color = -0xbdbdbe
val paint = Paint()
val rect = Rect(0, 0, bitmapCopy.width, bitmapCopy.height)
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
paint.color = color
canvas.drawCircle((bitmapCopy.width / 2).toFloat(), (bitmapCopy.height / 2).toFloat(), (bitmap.width / 2).toFloat(), paint)
canvas.drawCircle(
(bitmapCopy.width / 2).toFloat(),
(bitmapCopy.height / 2).toFloat(),
(bitmap.width / 2).toFloat(),
paint
)
paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))
canvas.drawBitmap(bitmapCopy, rect, rect, paint)
bitmapCopy.recycle()
Expand Down Expand Up @@ -110,30 +116,51 @@ object Utils {
fun saveLog(context: Context, name: String, log: String) {
try {
val fw = FileWriter(
File(context.getExternalFilesDir(null), name + "_" + currentDateString + ".log"))
File(context.getExternalFilesDir(null), name + "_" + currentDateString + ".log")
)
fw.write(log)
fw.close()
} catch (_: IOException) {
}
}

fun makeWindowParams(width: Int, height: Int, context: Context,
preferLastDisplay: Boolean): WindowManager.LayoutParams {
fun makeWindowParams(
width: Int, height: Int, context: Context,
preferLastDisplay: Boolean
): WindowManager.LayoutParams {
val displayWidth = DeviceUtils.getDisplayMetrics(context, preferLastDisplay).widthPixels
val displayHeight = DeviceUtils.getDisplayMetrics(context, preferLastDisplay).heightPixels
val layoutParams = WindowManager.LayoutParams()
layoutParams.format = PixelFormat.TRANSLUCENT
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
layoutParams.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY else WindowManager.LayoutParams.TYPE_PHONE
layoutParams.type =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
else
WindowManager.LayoutParams.TYPE_PHONE
layoutParams.width = displayWidth.coerceAtMost(width)
layoutParams.height = displayHeight.coerceAtMost(height)
return layoutParams
}

fun solve(expression: String): Double {
if (expression.contains("+")) return expression.split("\\+".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toDouble() + expression.split("\\+".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() else if (expression.contains("-")) return expression.split("\\-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toDouble() - expression.split("\\-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble()
if (expression.contains("/")) return expression.split("\\/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toDouble() / expression.split("\\/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble()
return if (expression.contains("*")) expression.split("\\*".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toDouble() * expression.split("\\*".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() else 0.0
if (expression.contains("+")) return expression.split("\\+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[0].toDouble() + expression.split("\\+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[1].toDouble() else if (expression.contains("-")) return expression.split(
"\\-".toRegex()
).dropLastWhile { it.isEmpty() }
.toTypedArray()[0].toDouble() - expression.split("\\-".toRegex())
.dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble()
if (expression.contains("/")) return expression.split("\\/".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[0].toDouble() / expression.split("\\/".toRegex())
.dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble()
return if (expression.contains("*")) expression.split("\\*".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[0].toDouble() * expression.split("\\*".toRegex())
.dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() else 0.0
}

fun backupPreferences(context: Context, backupUri: Uri) {
Expand All @@ -147,7 +174,7 @@ object Utils {
type = "integer"
}
stringBuilder.append(type).append(" ").append(key).append(" ")
.append(value.toString()).append("\n")
.append(value.toString()).append("\n")
}
val content = stringBuilder.toString().trim { it <= ' ' }
var os: OutputStream? = null
Expand All @@ -163,7 +190,6 @@ object Utils {
try {
os.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
Expand All @@ -178,12 +204,19 @@ object Utils {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = sharedPreferences.edit()
while (br.readLine().also { line = it } != null) {
val contents = line.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val contents =
line.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (contents.size > 2) {
val type = contents[0]
val key = contents[1]
val value = contents[2]
if (type == "boolean") editor.putBoolean(key, java.lang.Boolean.parseBoolean(value)) else if (type == "integer") editor.putInt(key, value.toInt()) else editor.putString(key, value)
if (type == "boolean") editor.putBoolean(
key,
java.lang.Boolean.parseBoolean(value)
) else if (type == "integer") editor.putInt(
key,
value.toInt()
) else editor.putString(key, value)
}
}
br.close()
Expand Down

0 comments on commit b3fa06f

Please sign in to comment.