private ArrayList<RestrictionEntry> initRestrictions(Context context) { ArrayList<RestrictionEntry> newRestrictions = new ArrayList<>(); Resources res = context.getResources(); RestrictionEntry reBoolean = new RestrictionEntry(KEY_BOOLEAN, false); populateBooleanEntry(res, reBoolean); newRestrictions.add(reBoolean); RestrictionEntry reSingleChoice = new RestrictionEntry(KEY_CHOICE, (String) null); populateChoiceEntry(res, reSingleChoice); newRestrictions.add(reSingleChoice); RestrictionEntry reMultiSelect = new RestrictionEntry(KEY_MULTI_SELECT, (String[]) null); populateMultiEntry(res, reMultiSelect); newRestrictions.add(reMultiSelect); return newRestrictions; }
@Override public void onReceive(final Context context, final Intent intent) { if (AppConstants.Versions.preJBMR2) { // This broadcast does not make any sense prior to Jelly Bean MR2. return; } final PendingResult result = goAsync(); new Thread() { @Override public void run() { final Bundle oldRestrictions = intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE); final Bundle extras = new Bundle(); ArrayList<RestrictionEntry> entries = initRestrictions(context, oldRestrictions); extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, entries); result.setResult(Activity.RESULT_OK, null, extras); result.finish(); } }.start(); }
protected RestrictionEntry[] convertBundleToRestrictions(Bundle restrictionBundle) { List<RestrictionEntry> restrictionEntries = new ArrayList<>(); Set<String> keys = restrictionBundle.keySet(); for (String key : keys) { Object value = restrictionBundle.get(key); if (value instanceof Boolean) { restrictionEntries.add(new RestrictionEntry(key, (boolean) value)); } else if (value instanceof Integer) { restrictionEntries.add(new RestrictionEntry(key, (int) value)); } else if (value instanceof String) { RestrictionEntry entry = new RestrictionEntry(RestrictionEntry.TYPE_STRING, key); entry.setSelectedString((String) value); restrictionEntries.add(entry); } else if (value instanceof String[]) { restrictionEntries.add(new RestrictionEntry(key, (String[]) value)); } else if (value instanceof Bundle) { addBundleEntryToRestrictions(restrictionEntries, key, (Bundle) value); } else if (value instanceof Parcelable[]) { addBundleArrayToRestrictions(restrictionEntries, key, (Parcelable[]) value); } } return restrictionEntries.toArray(new RestrictionEntry[0]); }
private int getTypeIndexFromRestrictionType(int restrictionType) { switch (restrictionType) { case RestrictionEntry.TYPE_BOOLEAN: return KeyValuePairDialogFragment.DialogType.BOOL_TYPE; case RestrictionEntry.TYPE_INTEGER: return KeyValuePairDialogFragment.DialogType.INT_TYPE; case RestrictionEntry.TYPE_STRING: return KeyValuePairDialogFragment.DialogType.STRING_TYPE; case RestrictionEntry.TYPE_MULTI_SELECT: return KeyValuePairDialogFragment.DialogType.STRING_ARRAY_TYPE; case RestrictionEntry.TYPE_BUNDLE: return KeyValuePairDialogFragment.DialogType.BUNDLE_TYPE; case RestrictionEntry.TYPE_BUNDLE_ARRAY: return KeyValuePairDialogFragment.DialogType.BUNDLE_ARRAY_TYPE; default: throw new AssertionError("Unknown restriction type"); } }
private int getRestrictionTypeFromDialogType(int typeIndex) { switch (typeIndex) { case KeyValuePairDialogFragment.DialogType.BOOL_TYPE: return RestrictionEntry.TYPE_BOOLEAN; case KeyValuePairDialogFragment.DialogType.INT_TYPE: return RestrictionEntry.TYPE_INTEGER; case KeyValuePairDialogFragment.DialogType.STRING_TYPE: return RestrictionEntry.TYPE_STRING; case KeyValuePairDialogFragment.DialogType.STRING_ARRAY_TYPE: return RestrictionEntry.TYPE_MULTI_SELECT; case KeyValuePairDialogFragment.DialogType.BUNDLE_TYPE: return RestrictionEntry.TYPE_BUNDLE; case KeyValuePairDialogFragment.DialogType.BUNDLE_ARRAY_TYPE: return RestrictionEntry.TYPE_BUNDLE_ARRAY; default: throw new AssertionError("Unknown type index"); } }
@TargetApi(Build.VERSION_CODES.M) private static void addBundleArrayRestrictionToBundle(Bundle bundle, RestrictionEntry entry) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { RestrictionEntry[] bundleRestrictionArray = entry.getRestrictions(); Bundle[] bundleArray = new Bundle[bundleRestrictionArray.length]; for (int i = 0; i < bundleRestrictionArray.length; i++) { RestrictionEntry[] bundleRestrictions = bundleRestrictionArray[i].getRestrictions(); if (bundleRestrictions == null) { // Non-bundle entry found in bundle array. Log.w(TAG, "addRestrictionToBundle: " + "Non-bundle entry found in bundle array"); bundleArray[i] = new Bundle(); } else { bundleArray[i] = convertRestrictionsToBundle(Arrays.asList( bundleRestrictions)); } } bundle.putParcelableArray(entry.getKey(), bundleArray); } else { Log.w(TAG, "addBundleArrayRestrictionToBundle is called in pre-M"); } }
@Override public void onReceive(final Context context, Intent intent) { final PendingResult result = goAsync(); new Thread() { @Override public void run() { final Bundle extras = new Bundle(); ArrayList<RestrictionEntry> restrictionEntries = initRestrictions(context); extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, restrictionEntries); result.setResult(Activity.RESULT_OK,null,extras); result.finish(); } }.run(); }
@Override public void onReceive(final Context context, Intent intent) { final PendingResult result = goAsync(); new Thread() { public void run() { Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND); final Bundle extras = new Bundle(); // Create the restriction final RestrictionEntry hideExplicit = new RestrictionEntry( BLOCK_EXPLICIT_RESTRICTION_KEY, true); hideExplicit.setTitle(context.getString(R.string.podcast_block_explicit)); // Put everything together and send it back final ArrayList<RestrictionEntry> list = new ArrayList<>(); list.add(hideExplicit); extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, list); result.setResult(Activity.RESULT_OK, null, extras); result.finish(); } }.start(); }
public static void populateChoiceEntry(Resources res, RestrictionEntry reSingleChoice) { String[] choiceEntries = res.getStringArray(R.array.choice_entry_entries); String[] choiceValues = res.getStringArray(R.array.choice_entry_values); if (reSingleChoice.getSelectedString() == null) { reSingleChoice.setSelectedString(choiceValues[0]); } reSingleChoice.setTitle(res.getString(R.string.choice_entry_title)); reSingleChoice.setChoiceEntries(choiceEntries); reSingleChoice.setChoiceValues(choiceValues); reSingleChoice.setType(RestrictionEntry.TYPE_CHOICE); }
public static void populateMultiEntry(Resources res, RestrictionEntry reMultiSelect) { String[] multiEntries = res.getStringArray(R.array.multi_entry_entries); String[] multiValues = res.getStringArray(R.array.multi_entry_values); if (reMultiSelect.getAllSelectedStrings() == null) { reMultiSelect.setAllSelectedStrings(new String[0]); } reMultiSelect.setTitle(res.getString(R.string.multi_entry_title)); reMultiSelect.setChoiceEntries(multiEntries); reMultiSelect.setChoiceValues(multiValues); reMultiSelect.setType(RestrictionEntry.TYPE_MULTI_SELECT); }
private ArrayList<RestrictionEntry> initRestrictions(Context context, Bundle oldRestrictions) { ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>(); for (Restriction restriction : RestrictedProfileConfiguration.DEFAULT_RESTRICTIONS) { RestrictionEntry entry = createRestrictionEntryWithDefaultValue(context, restriction, oldRestrictions.getBoolean(restriction.name, true)); entries.add(entry); } return entries; }
@TargetApi(Build.VERSION_CODES.M) private void addBundleArrayToRestrictions(List<RestrictionEntry> restrictionEntries, String key, Parcelable[] value) { int length = value.length; RestrictionEntry[] entriesArray = new RestrictionEntry[length]; for (int i = 0; i < entriesArray.length; ++i) { entriesArray[i] = RestrictionEntry.createBundleEntry(key, convertBundleToRestrictions((Bundle) value[i])); } restrictionEntries.add(RestrictionEntry.createBundleArrayEntry(key, entriesArray)); }
@TargetApi(Build.VERSION_CODES.M) private void updateRestrictionEntryFromResultIntent(RestrictionEntry restrictionEntry, Intent intent) { switch (restrictionEntry.getType()) { case RestrictionEntry.TYPE_BOOLEAN: restrictionEntry.setSelectedState(intent.getBooleanExtra(RESULT_VALUE, false)); break; case RestrictionEntry.TYPE_INTEGER: restrictionEntry.setIntValue(intent.getIntExtra(RESULT_VALUE, 0)); break; case RestrictionEntry.TYPE_STRING: restrictionEntry.setSelectedString(intent.getStringExtra(RESULT_VALUE)); break; case RestrictionEntry.TYPE_MULTI_SELECT: restrictionEntry.setAllSelectedStrings(intent.getStringArrayExtra(RESULT_VALUE)); break; case RestrictionEntry.TYPE_BUNDLE: { Bundle bundle = intent.getBundleExtra(RESULT_VALUE); restrictionEntry.setRestrictions(convertBundleToRestrictions(bundle)); break; } case RestrictionEntry.TYPE_BUNDLE_ARRAY: { Parcelable[] bundleArray = intent.getParcelableArrayExtra(RESULT_VALUE); RestrictionEntry[] restrictionEntryArray = new RestrictionEntry[bundleArray.length]; for (int i = 0; i< bundleArray.length; i++) { restrictionEntryArray[i] = RestrictionEntry.createBundleEntry(String.valueOf(i), convertBundleToRestrictions((Bundle) bundleArray[i])); } restrictionEntry.setRestrictions(restrictionEntryArray); break; } } }
private void loadManifestAppRestrictions(String pkgName) { if (!TextUtils.isEmpty(pkgName)) { List<RestrictionEntry> manifestRestrictions = null; try { manifestRestrictions = mRestrictionsManager.getManifestRestrictions(pkgName); convertTypeChoiceAndNullToString(manifestRestrictions); } catch (NullPointerException e) { // This means no default restrictions. } if (manifestRestrictions != null) { loadAppRestrictionsList(manifestRestrictions.toArray(new RestrictionEntry[0])); } } }
/** * TODO (b/23378519): Remove this method and add support for type choice and null. */ private void convertTypeChoiceAndNullToString(List<RestrictionEntry> restrictionEntries) { for (RestrictionEntry entry : restrictionEntries) { if (entry.getType() == RestrictionEntry.TYPE_CHOICE || entry.getType() == RestrictionEntry.TYPE_NULL) { entry.setType(RestrictionEntry.TYPE_STRING); } } }
private static Bundle addRestrictionToBundle(Bundle bundle, RestrictionEntry entry) { switch (entry.getType()) { case RestrictionEntry.TYPE_BOOLEAN: bundle.putBoolean(entry.getKey(), entry.getSelectedState()); break; case RestrictionEntry.TYPE_CHOICE: case RestrictionEntry.TYPE_MULTI_SELECT: bundle.putStringArray(entry.getKey(), entry.getAllSelectedStrings()); break; case RestrictionEntry.TYPE_INTEGER: bundle.putInt(entry.getKey(), entry.getIntValue()); break; case RestrictionEntry.TYPE_STRING: case RestrictionEntry.TYPE_NULL: bundle.putString(entry.getKey(), entry.getSelectedString()); break; case RestrictionEntry.TYPE_BUNDLE: addBundleRestrictionToBundle(bundle, entry); break; case RestrictionEntry.TYPE_BUNDLE_ARRAY: addBundleArrayRestrictionToBundle(bundle, entry); break; default: throw new IllegalArgumentException( "Unsupported restrictionEntry type: " + entry.getType()); } return bundle; }
@TargetApi(Build.VERSION_CODES.M) private static void addBundleRestrictionToBundle(Bundle bundle, RestrictionEntry entry) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { RestrictionEntry[] restrictions = entry.getRestrictions(); Bundle childBundle = convertRestrictionsToBundle(Arrays.asList(restrictions)); bundle.putBundle(entry.getKey(), childBundle); } else { Log.w(TAG, "addBundleRestrictionToBundle is called in pre-M"); } }
private ArrayList<RestrictionEntry> initRestrictions(Context context) { ArrayList<RestrictionEntry> restrictions = new ArrayList<RestrictionEntry>(); RestrictionEntry allowChanges = new RestrictionEntry("allow_changes",false); allowChanges.setTitle(context.getString(R.string.allow_vpn_changes)); restrictions.add(allowChanges); return restrictions; }
public static void populateBooleanEntry(Resources res, RestrictionEntry entry) { entry.setType(RestrictionEntry.TYPE_BOOLEAN); entry.setTitle(res.getString(R.string.boolean_entry_title)); }
protected void loadAppRestrictionsList(RestrictionEntry[] restrictionEntries) { if (restrictionEntries != null) { mAppRestrictionsArrayAdapter.clear(); mAppRestrictionsArrayAdapter.addAll(Arrays.asList(restrictionEntries)); } }
@TargetApi(Build.VERSION_CODES.M) private void addBundleEntryToRestrictions(List<RestrictionEntry> restrictionEntries, String key, Bundle value) { restrictionEntries.add(RestrictionEntry.createBundleEntry( key, convertBundleToRestrictions(value))); }
@Override public void onEditButtonClick(RestrictionEntry restrictionEntry) { showEditDialog(restrictionEntry); }
private void showEditDialog(final RestrictionEntry restrictionEntry) { mEditingRestrictionEntry = restrictionEntry; int type = KeyValuePairDialogFragment.DialogType.BOOL_TYPE; Object value = null; String key = ""; if (restrictionEntry != null) { key = restrictionEntry.getKey(); type = getTypeIndexFromRestrictionType(restrictionEntry.getType()); switch (restrictionEntry.getType()) { case RestrictionEntry.TYPE_BOOLEAN: value = restrictionEntry.getSelectedState(); break; case RestrictionEntry.TYPE_INTEGER: value = restrictionEntry.getIntValue(); break; case RestrictionEntry.TYPE_STRING: value = restrictionEntry.getSelectedString(); break; case RestrictionEntry.TYPE_MULTI_SELECT: value = restrictionEntry.getAllSelectedStrings(); break; case RestrictionEntry.TYPE_BUNDLE: value = RestrictionManagerCompat.convertRestrictionsToBundle(Arrays.asList( getRestrictionEntries(restrictionEntry))); break; case RestrictionEntry.TYPE_BUNDLE_ARRAY: RestrictionEntry[] restrictionEntries = getRestrictionEntries(restrictionEntry); Bundle[] bundles = new Bundle[restrictionEntries.length]; for (int i = 0; i < restrictionEntries.length; i++) { bundles[i] = RestrictionManagerCompat.convertRestrictionsToBundle(Arrays.asList( getRestrictionEntries(restrictionEntries[i]))); } value = bundles; break; } } int[] supportType = (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) ? SUPPORTED_TYPES_PRE_M : SUPPORTED_TYPES; KeyValuePairDialogFragment dialogFragment = KeyValuePairDialogFragment.newInstance(type, true, key, value, supportType, getCurrentAppName()); dialogFragment.setTargetFragment(this, RESULT_CODE_EDIT_DIALOG); dialogFragment.show(getFragmentManager(), "dialog"); }
@TargetApi(Build.VERSION_CODES.M) private RestrictionEntry[] getRestrictionEntries(RestrictionEntry restrictionEntry) { return restrictionEntry.getRestrictions(); }
public RestrictionEntryEditDeleteArrayAdapter(Context context, List<RestrictionEntry> entries, OnEditButtonClickListener onEditButtonClickListener, OnDeleteButtonClickListener onDeleteButtonClickListener) { super(context, entries, onEditButtonClickListener, onDeleteButtonClickListener); }
@Override protected String getDisplayName(RestrictionEntry entry) { return entry.getKey(); }
private RestrictionEntry createRestrictionEntryWithDefaultValue(Context context, Restriction restriction, boolean defaultValue) { RestrictionEntry entry = new RestrictionEntry(restriction.name, defaultValue); entry.setTitle(restriction.getTitle(context)); return entry; }
/** * Converts a list of restrictions to the corresponding bundle, using the following mapping: * <table> * <tr><th>RestrictionEntry</th><th>Bundle</th></tr> * <tr><td>{@link RestrictionEntry#TYPE_BOOLEAN}</td><td>{@link Bundle#putBoolean}</td></tr> * <tr><td>{@link RestrictionEntry#TYPE_CHOICE}, * {@link RestrictionEntry#TYPE_MULTI_SELECT}</td> * <td>{@link Bundle#putStringArray}</td></tr> * <tr><td>{@link RestrictionEntry#TYPE_INTEGER}</td><td>{@link Bundle#putInt}</td></tr> * <tr><td>{@link RestrictionEntry#TYPE_STRING}</td><td>{@link Bundle#putString}</td></tr> * <tr><td>{@link RestrictionEntry#TYPE_BUNDLE}</td><td>{@link Bundle#putBundle}</td></tr> * <tr><td>{@link RestrictionEntry#TYPE_BUNDLE_ARRAY}</td> * <td>{@link Bundle#putParcelableArray}</td></tr> * </table> * TYPE_BUNDLE and TYPE_BUNDLE_ARRAY are supported from api level 23 onwards. * @param entries list of restrictions */ public static Bundle convertRestrictionsToBundle(List<RestrictionEntry> entries) { final Bundle bundle = new Bundle(); for (RestrictionEntry entry : entries) { addRestrictionToBundle(bundle, entry); } return bundle; }