android: PR suggestions for syspolicyHandler (#308)

Updates tailscale/corp#18202

Signed-off-by: Percy Wegmann <percy@tailscale.com>
pull/304/head
Percy Wegmann 2 months ago committed by GitHub
parent 6668265ea5
commit 7e3a4a2ee9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -15,7 +15,6 @@ import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.RestrictionsManager
import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
@ -38,10 +37,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.tailscale.ipn.mdm.AlwaysNeverUserDecidesMDMSetting
import com.tailscale.ipn.mdm.BooleanMDMSetting
import com.tailscale.ipn.mdm.ShowHideMDMSetting
import com.tailscale.ipn.mdm.StringMDMSetting
import com.tailscale.ipn.mdm.MDMSettings
import com.tailscale.ipn.ui.localapi.Client
import com.tailscale.ipn.ui.localapi.Request
import com.tailscale.ipn.ui.model.Ipn
@ -459,29 +455,15 @@ class App : Application(), libtailscale.AppContext {
@Throws(IOException::class, GeneralSecurityException::class)
override fun getSyspolicyBooleanValue(key: String): Boolean {
val manager = getSystemService(Context.RESTRICTIONS_SERVICE) as RestrictionsManager
return BooleanMDMSetting(key, key).getFrom(manager.applicationRestrictions, this)
return getSyspolicyStringValue(key) == "true"
}
@Throws(IOException::class, GeneralSecurityException::class)
override fun getSyspolicyStringValue(key: String): String {
val manager = getSystemService(Context.RESTRICTIONS_SERVICE) as RestrictionsManager
val alwaysNeverSetting = AlwaysNeverUserDecidesMDMSetting(key, key, false)
val showHideSetting = ShowHideMDMSetting(key, key, false)
val stringSetting = StringMDMSetting(key, key, false)
return when {
alwaysNeverSetting.keyExists(key) ->
alwaysNeverSetting.getFrom(manager.applicationRestrictions, this).value
showHideSetting.keyExists(key) ->
showHideSetting.getFrom(manager.applicationRestrictions, this).value
stringSetting.keyExists(key) ->
stringSetting.getFrom(manager.applicationRestrictions, this) ?: ""
else -> {
Log.d("MDM", "$key is not defined on Android. Returning empty.")
""
}
}
return MDMSettings.allSettingsByKey[key]?.flow?.value?.toString()
?: run {
Log.d("MDM", "$key is not defined on Android. Returning empty.")
""
}
}
}

@ -57,6 +57,8 @@ object MDMSettings {
.map { it.call(MDMSettings) as MDMSetting<*> }
}
val allSettingsByKey by lazy { allSettings.associateBy { it.key } }
fun update(app: App, restrictionsManager: RestrictionsManager?) {
val bundle = restrictionsManager?.applicationRestrictions
allSettings.forEach { it.setFrom(bundle, app) }

@ -4,32 +4,14 @@
package com.tailscale.ipn.mdm
import android.os.Bundle
import android.util.Log
import com.tailscale.ipn.App
import com.tailscale.ipn.ui.util.set
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
// Don't keep track of the key if shouldRegisterKey is false - shouldRegisterKey should be false when creating an MDMSetting to check whether a key has been registered with that setting.
abstract class MDMSetting<T>(defaultValue: T, val key: String, val localizedTitle: String, private val shouldRegisterKey: Boolean = true) {
private val keys = mutableSetOf<String>()
init {
if (shouldRegisterKey) { registerKey(key)
}
}
abstract class MDMSetting<T>(defaultValue: T, val key: String, val localizedTitle: String) {
val flow: StateFlow<T> = MutableStateFlow<T>(defaultValue)
private fun registerKey(key: String) {
if (!keyExists(key)) {
keys.add(key)
}
}
fun keyExists(key: String): Boolean {
return keys.contains(key)
}
fun setFrom(bundle: Bundle?, app: App) {
val v = getFrom(bundle, app)
flow.set(v)
@ -44,8 +26,8 @@ class BooleanMDMSetting(key: String, localizedTitle: String) :
bundle?.getBoolean(key) ?: app.getEncryptedPrefs().getBoolean(key, false)
}
class StringMDMSetting(key: String, localizedTitle: String, shouldRegisterKey: Boolean = true) :
MDMSetting<String?>(null, key, localizedTitle, shouldRegisterKey) {
class StringMDMSetting(key: String, localizedTitle: String) :
MDMSetting<String?>(null, key, localizedTitle) {
override fun getFrom(bundle: Bundle?, app: App) =
bundle?.getString(key) ?: app.getEncryptedPrefs().getString(key, null)
}
@ -57,8 +39,8 @@ class StringArrayListMDMSetting(key: String, localizedTitle: String) :
?: app.getEncryptedPrefs().getStringSet(key, HashSet<String>())?.toList()
}
class AlwaysNeverUserDecidesMDMSetting(key: String, localizedTitle: String, shouldRegisterKey: Boolean = true) :
MDMSetting<AlwaysNeverUserDecides>(AlwaysNeverUserDecides.UserDecides, key, localizedTitle, shouldRegisterKey) {
class AlwaysNeverUserDecidesMDMSetting(key: String, localizedTitle: String) :
MDMSetting<AlwaysNeverUserDecides>(AlwaysNeverUserDecides.UserDecides, key, localizedTitle) {
override fun getFrom(bundle: Bundle?, app: App): AlwaysNeverUserDecides {
val storedString =
bundle?.getString(key)
@ -78,8 +60,8 @@ class AlwaysNeverUserDecidesMDMSetting(key: String, localizedTitle: String, shou
}
}
class ShowHideMDMSetting(key: String, localizedTitle: String, shouldRegisterKey: Boolean = true) :
MDMSetting<ShowHide>(ShowHide.Show, key, localizedTitle, shouldRegisterKey) {
class ShowHideMDMSetting(key: String, localizedTitle: String) :
MDMSetting<ShowHide>(ShowHide.Show, key, localizedTitle) {
override fun getFrom(bundle: Bundle?, app: App): ShowHide {
val storedString =
bundle?.getString(key)

Loading…
Cancel
Save