Skip to content

Commit 48a96e9

Browse files
tony-makAndroid (Google) Code Review
authored andcommitted
Merge "Fix incorrect handling of bundle array restriction" into ub-testdpc-nyc
2 parents e64b2f3 + 623d002 commit 48a96e9

File tree

2 files changed

+126
-41
lines changed

2 files changed

+126
-41
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (C) 2016 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.afwsamples.testdpc.common;
18+
19+
import android.annotation.TargetApi;
20+
import android.content.RestrictionEntry;
21+
import android.os.Build;
22+
import android.os.Bundle;
23+
import android.util.Log;
24+
25+
import java.util.Arrays;
26+
import java.util.List;
27+
28+
/**
29+
* Ported from {@link android.content.RestrictionsManager}.
30+
*/
31+
public class RestrictionManagerCompat {
32+
private static final String TAG = "RestrictionManager";
33+
34+
/**
35+
* Converts a list of restrictions to the corresponding bundle, using the following mapping:
36+
* <table>
37+
* <tr><th>RestrictionEntry</th><th>Bundle</th></tr>
38+
* <tr><td>{@link RestrictionEntry#TYPE_BOOLEAN}</td><td>{@link Bundle#putBoolean}</td></tr>
39+
* <tr><td>{@link RestrictionEntry#TYPE_CHOICE},
40+
* {@link RestrictionEntry#TYPE_MULTI_SELECT}</td>
41+
* <td>{@link Bundle#putStringArray}</td></tr>
42+
* <tr><td>{@link RestrictionEntry#TYPE_INTEGER}</td><td>{@link Bundle#putInt}</td></tr>
43+
* <tr><td>{@link RestrictionEntry#TYPE_STRING}</td><td>{@link Bundle#putString}</td></tr>
44+
* <tr><td>{@link RestrictionEntry#TYPE_BUNDLE}</td><td>{@link Bundle#putBundle}</td></tr>
45+
* <tr><td>{@link RestrictionEntry#TYPE_BUNDLE_ARRAY}</td>
46+
* <td>{@link Bundle#putParcelableArray}</td></tr>
47+
* </table>
48+
* TYPE_BUNDLE and TYPE_BUNDLE_ARRAY are supported from api level 23 onwards.
49+
* @param entries list of restrictions
50+
*/
51+
public static Bundle convertRestrictionsToBundle(List<RestrictionEntry> entries) {
52+
final Bundle bundle = new Bundle();
53+
for (RestrictionEntry entry : entries) {
54+
addRestrictionToBundle(bundle, entry);
55+
}
56+
return bundle;
57+
}
58+
59+
private static Bundle addRestrictionToBundle(Bundle bundle, RestrictionEntry entry) {
60+
switch (entry.getType()) {
61+
case RestrictionEntry.TYPE_BOOLEAN:
62+
bundle.putBoolean(entry.getKey(), entry.getSelectedState());
63+
break;
64+
case RestrictionEntry.TYPE_CHOICE:
65+
case RestrictionEntry.TYPE_MULTI_SELECT:
66+
bundle.putStringArray(entry.getKey(), entry.getAllSelectedStrings());
67+
break;
68+
case RestrictionEntry.TYPE_INTEGER:
69+
bundle.putInt(entry.getKey(), entry.getIntValue());
70+
break;
71+
case RestrictionEntry.TYPE_STRING:
72+
case RestrictionEntry.TYPE_NULL:
73+
bundle.putString(entry.getKey(), entry.getSelectedString());
74+
break;
75+
case RestrictionEntry.TYPE_BUNDLE:
76+
addBundleRestrictionToBundle(bundle, entry);
77+
break;
78+
case RestrictionEntry.TYPE_BUNDLE_ARRAY:
79+
addBundleArrayRestrictionToBundle(bundle, entry);
80+
break;
81+
default:
82+
throw new IllegalArgumentException(
83+
"Unsupported restrictionEntry type: " + entry.getType());
84+
}
85+
return bundle;
86+
}
87+
88+
@TargetApi(Build.VERSION_CODES.M)
89+
private static void addBundleRestrictionToBundle(Bundle bundle, RestrictionEntry entry) {
90+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
91+
RestrictionEntry[] restrictions = entry.getRestrictions();
92+
Bundle childBundle = convertRestrictionsToBundle(Arrays.asList(restrictions));
93+
bundle.putBundle(entry.getKey(), childBundle);
94+
} else {
95+
Log.w(TAG, "addBundleRestrictionToBundle is called in pre-M");
96+
}
97+
}
98+
99+
@TargetApi(Build.VERSION_CODES.M)
100+
private static void addBundleArrayRestrictionToBundle(Bundle bundle, RestrictionEntry entry) {
101+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
102+
RestrictionEntry[] bundleRestrictionArray = entry.getRestrictions();
103+
Bundle[] bundleArray = new Bundle[bundleRestrictionArray.length];
104+
for (int i = 0; i < bundleRestrictionArray.length; i++) {
105+
RestrictionEntry[] bundleRestrictions =
106+
bundleRestrictionArray[i].getRestrictions();
107+
if (bundleRestrictions == null) {
108+
// Non-bundle entry found in bundle array.
109+
Log.w(TAG, "addRestrictionToBundle: " +
110+
"Non-bundle entry found in bundle array");
111+
bundleArray[i] = new Bundle();
112+
} else {
113+
bundleArray[i] = convertRestrictionsToBundle(Arrays.asList(
114+
bundleRestrictions));
115+
}
116+
}
117+
bundle.putParcelableArray(entry.getKey(), bundleArray);
118+
} else {
119+
Log.w(TAG, "addBundleArrayRestrictionToBundle is called in pre-M");
120+
}
121+
}
122+
}

app/src/main/java/com/afwsamples/testdpc/profilepolicy/apprestrictions/ManageAppRestrictionsFragment.java

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.afwsamples.testdpc.R;
3939
import com.afwsamples.testdpc.common.EditDeleteArrayAdapter;
4040
import com.afwsamples.testdpc.common.ManageAppFragment;
41+
import com.afwsamples.testdpc.common.RestrictionManagerCompat;
4142
import com.afwsamples.testdpc.common.keyvaluepair.KeyValuePairDialogFragment;
4243

4344
import java.util.ArrayList;
@@ -180,15 +181,15 @@ private void showEditDialog(final RestrictionEntry restrictionEntry) {
180181
value = restrictionEntry.getAllSelectedStrings();
181182
break;
182183
case RestrictionEntry.TYPE_BUNDLE:
183-
value = convertRestrictionsToBundle(Arrays.asList(
184+
value = RestrictionManagerCompat.convertRestrictionsToBundle(Arrays.asList(
184185
getRestrictionEntries(restrictionEntry)));
185186
break;
186187
case RestrictionEntry.TYPE_BUNDLE_ARRAY:
187188
RestrictionEntry[] restrictionEntries = getRestrictionEntries(restrictionEntry);
188189
Bundle[] bundles = new Bundle[restrictionEntries.length];
189190
for (int i = 0; i < restrictionEntries.length; i++) {
190191
bundles[i] =
191-
convertRestrictionsToBundle(Arrays.asList(
192+
RestrictionManagerCompat.convertRestrictionsToBundle(Arrays.asList(
192193
getRestrictionEntries(restrictionEntries[i])));
193194
}
194195
value = bundles;
@@ -374,7 +375,7 @@ protected void saveConfig() {
374375
((ApplicationInfo) mManagedAppsSpinner.getSelectedItem()).packageName;
375376
mDevicePolicyManager.setApplicationRestrictions(
376377
DeviceAdminReceiver.getComponentName(getActivity()), pkgName,
377-
convertRestrictionsToBundle(mRestrictionEntries));
378+
RestrictionManagerCompat.convertRestrictionsToBundle(mRestrictionEntries));
378379
mLastRestrictionEntries = new ArrayList<>(mRestrictionEntries);
379380
showToast(getString(R.string.set_app_restrictions_success, pkgName));
380381
}
@@ -391,44 +392,6 @@ protected void loadDefault() {
391392
((ApplicationInfo) mManagedAppsSpinner.getSelectedItem()).packageName);
392393
}
393394

394-
/**
395-
* Wrapper for RestrictionsManager.convertRestrictionsToBundle, with a fallback implementation
396-
* for versions before M where this was introduced.
397-
* The fallback recognises only the restriction types available on L.
398-
*/
399-
@TargetApi(Build.VERSION_CODES.M)
400-
private Bundle convertRestrictionsToBundle(List<RestrictionEntry> restrictionEntries) {
401-
if (restrictionEntries == null) {
402-
return null;
403-
}
404-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
405-
return RestrictionsManager.convertRestrictionsToBundle(restrictionEntries);
406-
}
407-
Bundle bundle = new Bundle();
408-
for (RestrictionEntry entry : restrictionEntries) {
409-
switch (entry.getType()) {
410-
case RestrictionEntry.TYPE_BOOLEAN:
411-
bundle.putBoolean(entry.getKey(), entry.getSelectedState());
412-
break;
413-
case RestrictionEntry.TYPE_INTEGER:
414-
bundle.putInt(entry.getKey(), entry.getIntValue());
415-
break;
416-
case RestrictionEntry.TYPE_STRING:
417-
case RestrictionEntry.TYPE_NULL:
418-
bundle.putString(entry.getKey(), entry.getSelectedString());
419-
break;
420-
case RestrictionEntry.TYPE_CHOICE:
421-
case RestrictionEntry.TYPE_MULTI_SELECT:
422-
bundle.putStringArray(entry.getKey(), entry.getAllSelectedStrings());
423-
break;
424-
default:
425-
throw new IllegalArgumentException(
426-
"Unsupported restrictionEntry type: " + entry.getType());
427-
}
428-
}
429-
return bundle;
430-
}
431-
432395
@TargetApi(Build.VERSION_CODES.M)
433396
private RestrictionEntry[] getRestrictionEntries(RestrictionEntry restrictionEntry) {
434397
return restrictionEntry.getRestrictions();

0 commit comments

Comments
 (0)