下面列出了android.content.RestrictionEntry#getType() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(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;
}
}
}
/**
* 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;
}
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 = (Util.SDK_INT < 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");
}