Skip to content

Commit

Permalink
Inform user about missing permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
bailuk committed Jul 15, 2024
1 parent 01ed65a commit e7520f9
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import ch.bailu.aat.util.ui.theme.UiTheme
import ch.bailu.aat.views.bar.MainControlBar
import ch.bailu.aat.views.layout.ContentView
import ch.bailu.aat.views.layout.LabelTextView
import ch.bailu.aat.views.msg.permission.DataDirectoryPermissionInfoView
import ch.bailu.aat.views.msg.permission.LocationPermissionInfoView
import ch.bailu.aat.views.preferences.SolidIndexListView
import ch.bailu.aat.views.preferences.VerticalScrollView
import ch.bailu.aat_lib.dispatcher.AppBroadcaster
Expand Down Expand Up @@ -44,10 +46,19 @@ class MainActivity : ActivityContext() {
private fun createViews() {
val contentView = ContentView(this, theme)
contentView.add(createButtonBar())
contentView.add(permissionInfo())
contentView.addW(createActionList())
setContentView(contentView)
}

private fun permissionInfo(): View {
return LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
addView(LocationPermissionInfoView(this@MainActivity, theme))
addView(DataDirectoryPermissionInfoView(this@MainActivity, appContext.dataDirectory, theme))
}
}

private fun createActionList(): View {
val list = VerticalScrollView(this)
list.add(SolidIndexListView(this, SolidPreset(appContext.storage), theme))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ object AppPermission {
return Build.VERSION.SDK_INT < 23 || checkLocationSdk23(context)
}

fun checkBackgroundLocation(context: Context): Boolean {
return Build.VERSION.SDK_INT < 29 || checkBackgroundLocationSdk29(context)
}

@TargetApi(29)
private fun checkBackgroundLocationSdk29(context: Context): Boolean {
return checkSdk23(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
}

@TargetApi(23)
private fun checkLocationSdk23(context: Context): Boolean {
return checkSdk23(context, Manifest.permission.ACCESS_FINE_LOCATION) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.bailu.aat.views.msg.overlay

import android.content.Context
import ch.bailu.aat.util.ui.tooltip.ToolTip.themeify
import ch.bailu.aat.util.ui.tooltip.ToolTip
import ch.bailu.aat.views.msg.AbsMsgView

class TipMsgView(context: Context) : AbsMsgView(context) {
init {
themeify(this)
ToolTip.themeify(this)
}

override fun attach() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch.bailu.aat.views.msg.permission

import android.content.Context
import ch.bailu.aat.util.ui.theme.UiTheme
import ch.bailu.aat_lib.preferences.OnPreferencesChanged
import ch.bailu.aat_lib.preferences.SolidFile
import ch.bailu.aat_lib.preferences.StorageInterface
import ch.bailu.aat_lib.resources.ToDo
import ch.bailu.foc.Foc

class DataDirectoryPermissionInfoView(context: Context, private val solid: SolidFile, theme: UiTheme) : PermissionInfoView(context, theme),
OnPreferencesChanged {

public override fun onAttachedToWindow() {
super.onAttachedToWindow()
solid.register(this)
updateText()
}

override fun onPreferencesChanged(storage: StorageInterface, key: String) {
if (solid.hasKey(key)) {
updateText()
}
}

public override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
solid.unregister(this)
}

private fun updateText() {
updateText(getPermissionText(solid.getValueAsFile()))
}

companion object {
private fun getPermissionText(file: Foc): String {
var result = ""
if (!file.exists()) {
if (file.hasParent()) {
result = getPermissionText(file.parent())
} else {
result = missingPermissionText
}
} else if (!file.canWrite() || !file.canRead()) {
result = missingPermissionText
}
return result
}

private val missingPermissionText = ToDo.translate(
"Data directory is missing permissions.\nChoose another directory or acquire permission with `Pick (SAF)…`\nPreferences -> General/System -> Data directory"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.bailu.aat.views.msg.permission

import android.content.Context
import ch.bailu.aat.util.AppPermission
import ch.bailu.aat.util.ui.theme.UiTheme
import ch.bailu.aat_lib.resources.ToDo

class LocationPermissionInfoView(context: Context, theme: UiTheme) : PermissionInfoView(context, theme) {

override fun onAttachedToWindow() {
super.onAttachedToWindow()
updateText(getPermissionText())
}

private fun getPermissionText(): String {
var result = ""

if (AppPermission.checkLocation(context)) {
if (!AppPermission.checkBackgroundLocation(context)) {
result = noBackgroundPermissionText
}
} else {
result = noPermissionText
}
return result
}

companion object {
private val noBackgroundPermissionText = ToDo.translate(
"No permission to access location from background\nUse Android app-specific settings to enable this permission"
)
private val noPermissionText = ToDo.translate(
"No permission to access location"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ch.bailu.aat.views.msg.permission

import android.content.Context
import android.widget.TextView
import ch.bailu.aat.util.ui.theme.AppTheme
import ch.bailu.aat.util.ui.theme.UiTheme

open class PermissionInfoView(context: Context, theme: UiTheme) : TextView(context) {
init {
theme.background(this)
theme.toolTip(this)
AppTheme.padding(this, 5)
}

fun updateText(text: String) {
visibility = if (text.isEmpty()) {
GONE
} else {
VISIBLE
}
this.text = text
}
}

0 comments on commit e7520f9

Please sign in to comment.