TYPE_BUNDLE_ARRAY源码实例Demo

android.content.RestrictionEntry#TYPE_BUNDLE_ARRAY源码实例Demo

下面列出了android.content.RestrictionEntry#TYPE_BUNDLE_ARRAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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(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 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");
}
 
如果文章对您有帮助,请点击页面广告。以鼓励作者做出的努力。