|
| 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 | +} |
0 commit comments