Skip to content

Commit 4913317

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

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package net.christianbeier.droidvnc_ng
2323

2424
import android.content.Context
25+
import android.content.RestrictionsManager
2526
import android.content.SharedPreferences
2627
import android.util.Log
2728
import android.view.Display
@@ -109,13 +110,46 @@ 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+
val appConfig = (context.getSystemService(Context.RESTRICTIONS_SERVICE) as RestrictionsManager).applicationRestrictions
117+
if (appConfig != null && appConfig.size() > 0) {
118+
Log.i(TAG, "Loading defaults from app restrictions")
119+
this.port = appConfig.getInt("port", this.port)
120+
this.portReverse = appConfig.getInt("portReverse", this.portReverse)
121+
this.portRepeater = appConfig.getInt("portRepeater", this.portRepeater)
122+
this.fileTransfer = appConfig.getBoolean("fileTransfer", this.fileTransfer)
123+
this.viewOnly = appConfig.getBoolean("viewOnly", this.viewOnly)
124+
this.showPointers = appConfig.getBoolean("showPointers", this.showPointers)
125+
this.password = appConfig.getString("password", this.password) ?: this.password
126+
this.startOnBoot = appConfig.getBoolean("startOnBoot", this.startOnBoot)
127+
this.startOnBootDelay = appConfig.getInt("startOnBootDelay", this.startOnBootDelay)
128+
129+
val scalingStr = appConfig.getString("scaling", "0.0")
130+
try {
131+
val scaling = scalingStr.toFloat()
132+
if (scaling > 0.0f)
133+
this.scaling = scaling
134+
} catch (e: NumberFormatException) {
135+
Log.w(TAG, "Invalid scaling value in app restrictions: $scalingStr")
136+
}
137+
138+
val accessKey = appConfig.getString("accessKey", "")
139+
if (accessKey != null && accessKey.isNotEmpty())
140+
this.accessKey = accessKey
141+
142+
return
143+
}
144+
112145
/*
113146
read provided defaults
114147
*/
115148
val jsonFile = File(context.getExternalFilesDir(null), "defaults.json")
116149
try {
117150
val jsonString = jsonFile.readText()
118151
val readDefault = Json.decodeFromString<Defaults>(jsonString)
152+
Log.i(TAG, "Loading defaults from json file")
119153
this.port = readDefault.port
120154
this.portReverse = readDefault.portReverse
121155
this.portRepeater = readDefault.portRepeater

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)