Skip to content

Commit 89218a9

Browse files
author
Irina Dumitrescu
committed
Add DPC support for disabling unknown sources globally
Test: manual test, the option is available in the TestDPC app. Bug: 111335021 Change-Id: I527f315c1136c030a31f18247eb11553aef74546
1 parent eb9f5e1 commit 89218a9

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
583583
mInstallNonMarketAppsPreference = (DpcSwitchPreference) findPreference(
584584
INSTALL_NONMARKET_APPS_KEY);
585585
mInstallNonMarketAppsPreference.setCustomConstraint(
586-
() -> mUserManager.hasUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES)
586+
() -> (mUserManager.hasUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES) ||
587+
mUserManager.hasUserRestriction(
588+
UserRestriction.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY))
587589
? R.string.user_restricted
588590
: NO_CUSTOM_CONSTRIANT);
589591
mInstallNonMarketAppsPreference.setOnPreferenceChangeListener(this);
@@ -1557,8 +1559,8 @@ private void updateStayOnWhilePluggedInPreference() {
15571559
* Update the preference switch for {@link Settings.Secure#INSTALL_NON_MARKET_APPS} setting.
15581560
*
15591561
* <p>
1560-
* If the user restriction {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is set, then
1561-
* we disable this preference.
1562+
* If one of the user restrictions {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} or
1563+
* {@link DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY} is set, then we disable this preference.
15621564
* </p>
15631565
*/
15641566
public void updateInstallNonMarketAppsPreference() {

app/src/main/java/com/afwsamples/testdpc/policy/UserRestriction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class UserRestriction {
5353
public static final String DISALLOW_UNIFIED_PASSWORD = "no_unified_password";
5454
public static final String DISALLOW_CONFIG_LOCATION = "no_config_location";
5555
public static final String DISALLOW_PRINTING= "no_printing";
56+
public static final String DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY =
57+
"no_install_unknown_sources_globally";
5658

5759
public String key;
5860
public int titleResId;
@@ -127,6 +129,8 @@ public UserRestriction(String key, int titleResId) {
127129
R.string.disallow_install_apps),
128130
new UserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES,
129131
R.string.disallow_install_unknown_sources),
132+
new UserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
133+
R.string.disallow_install_unknown_sources_globally),
130134
new UserRestriction(DISALLOW_MODIFY_ACCOUNTS, R
131135
.string.disallow_modify_accounts),
132136
new UserRestriction(DISALLOW_MOUNT_PHYSICAL_MEDIA,
@@ -298,4 +302,8 @@ public UserRestriction(String key, int titleResId) {
298302
DISALLOW_SHARE_INTO_MANAGED_PROFILE,
299303
DISALLOW_PRINTING,
300304
};
305+
306+
public static String[] QT_PLUS_RESTRICTIONS = {
307+
DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
308+
};
301309
}

app/src/main/java/com/afwsamples/testdpc/policy/UserRestrictionsDisplayFragment.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void onCreatePreferences(Bundle bundle, String rootkey) {
7676
}
7777

7878
updateAllUserRestrictions();
79-
constrainPerferences();
79+
constrainPreferences();
8080
}
8181

8282
@Override
@@ -98,7 +98,9 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
9898
mDevicePolicyManager.addUserRestriction(mAdminComponentName, restriction);
9999
} else {
100100
mDevicePolicyManager.clearUserRestriction(mAdminComponentName, restriction);
101-
if (DISALLOW_INSTALL_UNKNOWN_SOURCES.equals(restriction)) {
101+
if (DISALLOW_INSTALL_UNKNOWN_SOURCES.equals(restriction) ||
102+
UserRestriction.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY.equals(
103+
restriction)) {
102104
new AlertDialog.Builder(getActivity())
103105
.setMessage(R.string.check_setting_disallow_install_unknown_sources)
104106
.setPositiveButton(R.string.check_setting_ok, null)
@@ -130,7 +132,7 @@ private void updateUserRestriction(String userRestriction) {
130132
preference.setChecked(disallowed);
131133
}
132134

133-
private void constrainPerferences() {
135+
private void constrainPreferences() {
134136
for (String restriction : UserRestriction.MNC_PLUS_RESTRICTIONS) {
135137
DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction);
136138
pref.setMinSdkVersion(Build.VERSION_CODES.M);
@@ -147,6 +149,11 @@ private void constrainPerferences() {
147149
DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction);
148150
pref.setMinSdkVersion(Build.VERSION_CODES.P);
149151
}
152+
for (String restriction: UserRestriction.QT_PLUS_RESTRICTIONS) {
153+
DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction);
154+
// Replace this with Q when available
155+
pref.setMinSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT);
156+
}
150157
for (String restriction : UserRestriction.PRIMARY_USER_ONLY_RESTRICTIONS) {
151158
DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction);
152159
pref.setUserConstraint(DpcPreferenceHelper.USER_PRIMARY_USER);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@
437437
<string name="check_setting_disallow_install_unknown_sources">Ensure that
438438
\"Allow installs from unknown sources\" under Settings management is correctly checked.
439439
</string>
440+
<string name="disallow_install_unknown_sources_globally">Disallow installs from unknown sources globally</string>
440441
<string name="check_setting_ok">Ok</string>
441442
<string name="disallow_modify_accounts">Disallow modify accounts</string>
442443
<string name="disallow_mount_physical_media">Disallow mount physical media</string>

0 commit comments

Comments
 (0)