Skip to content

Commit 3ca4548

Browse files
committed
Add App Restrictions
App Restrictions are a way to configure an App by a mobile device managment application. This lets admins set defaults without requiring a rooted phone.
1 parent ac3de41 commit 3ca4548

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
android:supportsRtl="true"
3131
android:theme="@style/AppTheme">
3232

33+
<meta-data
34+
android:name="android.content.APP_RESTRICTIONS"
35+
android:resource="@xml/app_config" />
36+
3337
<receiver android:name=".OnBootReceiver"
3438
android:exported="true">
3539
<intent-filter>

app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import androidx.preference.PreferenceManager
2929
import kotlinx.serialization.*
3030
import kotlinx.serialization.json.*
3131
import java.io.File
32+
import java.io.FileNotFoundException
3233
import java.util.UUID
3334

3435

@@ -109,6 +110,40 @@ class Defaults {
109110
*/
110111
this.scaling = 1.0f / Utils.getDisplayMetrics(context, Display.DEFAULT_DISPLAY).density.coerceAtLeast(1.0f)
111112

113+
/*
114+
Try read defaults from app restrictions
115+
*/
116+
117+
val restrictionsManager =
118+
context.getSystemService(Context.RESTRICTIONS_SERVICE) as android.content.RestrictionsManager
119+
val appConfig = restrictionsManager.applicationRestrictions
120+
if (appConfig != null && appConfig.size() > 0) {
121+
this.port = appConfig.getInt("port", this.port)
122+
this.portReverse = appConfig.getInt("portReverse", this.portReverse)
123+
this.portRepeater = appConfig.getInt("portRepeater", this.portRepeater)
124+
this.fileTransfer = appConfig.getBoolean("fileTransfer", this.fileTransfer)
125+
this.viewOnly = appConfig.getBoolean("viewOnly", this.viewOnly)
126+
this.showPointers = appConfig.getBoolean("showPointers", this.showPointers)
127+
this.password = appConfig.getString("password", this.password) ?: this.password
128+
this.startOnBoot = appConfig.getBoolean("startOnBoot", this.startOnBoot)
129+
this.startOnBootDelay = appConfig.getInt("startOnBootDelay", this.startOnBootDelay)
130+
131+
val scalingStr = appConfig.getString("scaling", "0.0")
132+
try {
133+
val scaling = scalingStr.toFloat()
134+
if (scaling > 0.0f)
135+
this.scaling = scaling
136+
} catch (e: NumberFormatException) {
137+
Log.w(TAG, "Invalid scaling value in app restrictions: $scalingStr")
138+
}
139+
140+
val accessKey = appConfig.getString("accessKey", "")
141+
if (accessKey != null && accessKey.isNotEmpty())
142+
this.accessKey = accessKey
143+
144+
return
145+
}
146+
112147
/*
113148
read provided defaults
114149
*/
@@ -121,7 +156,7 @@ class Defaults {
121156
this.portRepeater = readDefault.portRepeater
122157
this.fileTransfer = readDefault.fileTransfer
123158
// only set new scaling if there is one given; i.e. don't overwrite generated default
124-
if(readDefault.scaling > 0)
159+
if (readDefault.scaling > 0)
125160
this.scaling = readDefault.scaling
126161
this.viewOnly = readDefault.viewOnly
127162
this.showPointers = readDefault.showPointers
@@ -133,6 +168,8 @@ class Defaults {
133168
this.startOnBoot = readDefault.startOnBoot
134169
this.startOnBootDelay = readDefault.startOnBootDelay
135170
// add here!
171+
} catch (_: FileNotFoundException) {
172+
// ignore
136173
} catch (e: Exception) {
137174
Log.w(TAG, "${e.message}")
138175
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@
7070
<string name="share_activtiy_server_not_running">Not shared text, server is not running.</string>
7171
<string name="share_activtiy_no_clients_connected">Not shared text, no clients connected.</string>
7272
<string name="share_activtiy_shared_successfully">Shared text to clipboards of all connected clients.</string>
73+
<string name="settings_port_reverse" translatable="false">Reverse Port</string>
74+
<string name="settings_port_repeater" translatable="false">Repeater Port</string>
7375
</resources>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<restrictions xmlns:android="http://schemas.android.com/apk/res/android">
3+
<restriction
4+
android:defaultValue="5900"
5+
android:key="port"
6+
android:restrictionType="integer"
7+
android:title="@string/main_activity_settings_port" />
8+
9+
<restriction
10+
android:defaultValue="5500"
11+
android:key="portReverse"
12+
android:restrictionType="integer"
13+
android:title="@string/settings_port_reverse" />
14+
15+
<restriction
16+
android:defaultValue="5500"
17+
android:key="portRepeater"
18+
android:restrictionType="integer"
19+
android:title="@string/settings_port_repeater" />
20+
21+
<restriction
22+
android:defaultValue="0.0"
23+
android:key="scaling"
24+
android:restrictionType="string"
25+
android:title="@string/main_activity_settings_scaling" /> <!-- NOTE: float not supported, so we use string -->
26+
27+
<restriction
28+
android:defaultValue="false"
29+
android:key="viewOnly"
30+
android:restrictionType="bool"
31+
android:title="@string/main_activity_settings_view_only" />
32+
33+
<restriction
34+
android:defaultValue="false"
35+
android:key="showPointers"
36+
android:restrictionType="bool"
37+
android:title="@string/main_activity_settings_show_pointers" />
38+
39+
<restriction
40+
android:defaultValue="true"
41+
android:key="fileTransfer"
42+
android:restrictionType="bool"
43+
android:title="@string/main_activity_settings_file_transfer" />
44+
45+
<restriction
46+
android:defaultValue=""
47+
android:key="password"
48+
android:restrictionType="string"
49+
android:title="@string/main_activity_settings_password" />
50+
51+
<restriction
52+
android:defaultValue=""
53+
android:key="accessKey"
54+
android:restrictionType="string"
55+
android:title="@string/main_activity_settings_access_key" />
56+
57+
<restriction
58+
android:defaultValue="true"
59+
android:key="startOnBoot"
60+
android:restrictionType="bool"
61+
android:title="@string/main_activity_settings_start_on_boot" />
62+
63+
<restriction
64+
android:defaultValue="0"
65+
android:key="startOnBootDelay"
66+
android:restrictionType="integer"
67+
android:title="@string/main_activity_settings_start_on_boot_delay" />
68+
69+
</restrictions>

0 commit comments

Comments
 (0)