getKey()源码实例Demo

android.content.RestrictionEntry#getKey()源码实例Demo

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

private void resolveRestrictions() {
    RestrictionsManager manager =
            (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
    Bundle restrictions = manager.getApplicationRestrictions();
    List<RestrictionEntry> entries = manager.getManifestRestrictions(
            getActivity().getApplicationContext().getPackageName());
    for (RestrictionEntry entry : entries) {
        String key = entry.getKey();
        Log.d(TAG, "key: " + key);
        if (key.equals(KEY_CAN_SAY_HELLO)) {
            updateCanSayHello(entry, restrictions);
        } else if (key.equals(KEY_MESSAGE)) {
            updateMessage(entry, restrictions);
        } else if (key.equals(KEY_NUMBER)) {
            updateNumber(entry, restrictions);
        } else if (key.equals(KEY_RANK)) {
            updateRank(entry, restrictions);
        } else if (key.equals(KEY_APPROVALS)) {
            updateApprovals(entry, restrictions);
        } else if (key.equals(KEY_ITEMS)) {
            updateItems(restrictions);
        }
    }
}
 
private void resolveRestrictions() {
    RestrictionsManager manager =
            (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
    Bundle restrictions = manager.getApplicationRestrictions();
    List<RestrictionEntry> entries = manager.getManifestRestrictions(
            getActivity().getApplicationContext().getPackageName());
    for (RestrictionEntry entry : entries) {
        String key = entry.getKey();
        Log.d(TAG, "key: " + key);
        if (key.equals(KEY_CAN_SAY_HELLO)) {
            updateCanSayHello(entry, restrictions);
        } else if (key.equals(KEY_MESSAGE)) {
            updateMessage(entry, restrictions);
        } else if (key.equals(KEY_NUMBER)) {
            updateNumber(entry, restrictions);
        } else if (key.equals(KEY_RANK)) {
            updateRank(entry, restrictions);
        } else if (key.equals(KEY_APPROVALS)) {
            updateApprovals(entry, restrictions);
        } else if (key.equals(KEY_ITEMS)) {
            updateItems(restrictions);
        }
    }
}
 
/**
 * Loads the restrictions for the AppRestrictionSchema sample.
 *
 * @param activity The activity
 */
private void loadRestrictions(Activity activity) {
    RestrictionsManager manager =
            (RestrictionsManager) activity.getSystemService(Context.RESTRICTIONS_SERVICE);
    List<RestrictionEntry> restrictions =
            manager.getManifestRestrictions(Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA);
    SharedPreferences prefs = activity.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
    for (RestrictionEntry restriction : restrictions) {
        String key = restriction.getKey();
        if (RESTRICTION_KEY_SAY_HELLO.equals(key)) {
            updateCanSayHello(prefs.getBoolean(RESTRICTION_KEY_SAY_HELLO,
                    restriction.getSelectedState()));
        } else if (RESTRICTION_KEY_MESSAGE.equals(key)) {
            updateMessage(prefs.getString(RESTRICTION_KEY_MESSAGE,
                    restriction.getSelectedString()));
        } else if (RESTRICTION_KEY_NUMBER.equals(key)) {
            updateNumber(prefs.getInt(RESTRICTION_KEY_NUMBER,
                    restriction.getIntValue()));
        } else if (RESTRICTION_KEY_RANK.equals(key)) {
            updateRank(activity, restriction.getChoiceValues(),
                    prefs.getString(RESTRICTION_KEY_RANK, restriction.getSelectedString()));
        } else if (RESTRICTION_KEY_APPROVALS.equals(key)) {
            updateApprovals(activity, restriction.getChoiceValues(),
                    TextUtils.split(prefs.getString(RESTRICTION_KEY_APPROVALS,
                                    TextUtils.join(DELIMETER,
                                            restriction.getAllSelectedStrings())),
                            DELIMETER));
        } else if (BUNDLE_SUPPORTED && RESTRICTION_KEY_ITEMS.equals(key)) {
            String itemsString = prefs.getString(RESTRICTION_KEY_ITEMS, "");
            HashMap<String, String> items = new HashMap<>();
            for (String itemString : TextUtils.split(itemsString, DELIMETER)) {
                String[] strings = itemString.split(SEPARATOR, 2);
                items.put(strings[0], strings[1]);
            }
            updateItems(activity, items);
        }
    }
}
 
/**
 * Loads the restrictions for the AppRestrictionSchema sample.
 *
 * @param activity The activity
 */
private void loadRestrictions(Activity activity) {
    RestrictionsManager manager =
            (RestrictionsManager) activity.getSystemService(Context.RESTRICTIONS_SERVICE);
    List<RestrictionEntry> restrictions =
            manager.getManifestRestrictions(Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA);
    SharedPreferences prefs = activity.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
    for (RestrictionEntry restriction : restrictions) {
        String key = restriction.getKey();
        if (RESTRICTION_KEY_SAY_HELLO.equals(key)) {
            updateCanSayHello(prefs.getBoolean(RESTRICTION_KEY_SAY_HELLO,
                    restriction.getSelectedState()));
        } else if (RESTRICTION_KEY_MESSAGE.equals(key)) {
            updateMessage(prefs.getString(RESTRICTION_KEY_MESSAGE,
                    restriction.getSelectedString()));
        } else if (RESTRICTION_KEY_NUMBER.equals(key)) {
            updateNumber(prefs.getInt(RESTRICTION_KEY_NUMBER,
                    restriction.getIntValue()));
        } else if (RESTRICTION_KEY_RANK.equals(key)) {
            updateRank(activity, restriction.getChoiceValues(),
                    prefs.getString(RESTRICTION_KEY_RANK, restriction.getSelectedString()));
        } else if (RESTRICTION_KEY_APPROVALS.equals(key)) {
            updateApprovals(activity, restriction.getChoiceValues(),
                    TextUtils.split(prefs.getString(RESTRICTION_KEY_APPROVALS,
                                    TextUtils.join(DELIMETER,
                                            restriction.getAllSelectedStrings())),
                            DELIMETER));
        } else if (BUNDLE_SUPPORTED && RESTRICTION_KEY_ITEMS.equals(key)) {
            String itemsString = prefs.getString(RESTRICTION_KEY_ITEMS, "");
            HashMap<String, String> items = new HashMap<>();
            for (String itemString : TextUtils.split(itemsString, DELIMETER)) {
                String[] strings = itemString.split(SEPARATOR, 2);
                items.put(strings[0], strings[1]);
            }
            updateItems(activity, items);
        }
    }
}
 
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");
}
 
@Override
protected String getDisplayName(RestrictionEntry entry) {
    return entry.getKey();
}