Example usage for android.content RestrictionEntry getChoiceEntries

List of usage examples for android.content RestrictionEntry getChoiceEntries

Introduction

In this page you can find the example usage for android.content RestrictionEntry getChoiceEntries.

Prototype

public String[] getChoiceEntries() 

Source Link

Document

Returns the list of strings, set earlier, that will be presented as choices to the user.

Usage

From source file:com.android.tv.settings.users.AppRestrictionsFragment.java

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    String key = preference.getKey();
    if (key != null && key.contains(DELIMITER)) {
        StringTokenizer st = new StringTokenizer(key, DELIMITER);
        final String packageName = st.nextToken();
        final String restrictionKey = st.nextToken();
        AppRestrictionsPreference appPref = (AppRestrictionsPreference) mAppList
                .findPreference(getKeyForPackage(packageName));
        ArrayList<RestrictionEntry> restrictions = appPref.getRestrictions();
        if (restrictions != null) {
            for (RestrictionEntry entry : restrictions) {
                if (entry.getKey().equals(restrictionKey)) {
                    switch (entry.getType()) {
                    case RestrictionEntry.TYPE_BOOLEAN:
                        entry.setSelectedState((Boolean) newValue);
                        break;
                    case RestrictionEntry.TYPE_CHOICE:
                    case RestrictionEntry.TYPE_CHOICE_LEVEL:
                        ListPreference listPref = (ListPreference) preference;
                        entry.setSelectedString((String) newValue);
                        String readable = findInArray(entry.getChoiceEntries(), entry.getChoiceValues(),
                                (String) newValue);
                        listPref.setSummary(readable);
                        break;
                    case RestrictionEntry.TYPE_MULTI_SELECT:
                        Set<String> set = (Set<String>) newValue;
                        String[] selectedValues = new String[set.size()];
                        set.toArray(selectedValues);
                        entry.setAllSelectedStrings(selectedValues);
                        break;
                    default:
                        continue;
                    }//from   w w w  . ja  va  2 s . c  o  m
                    mUserManager.setApplicationRestrictions(packageName,
                            RestrictionsManager.convertRestrictionsToBundle(restrictions), mUser);
                    break;
                }
            }
        }
    }
    return true;
}

From source file:com.android.tv.settings.users.AppRestrictionsFragment.java

private void onRestrictionsReceived(AppRestrictionsPreference preference,
        ArrayList<RestrictionEntry> restrictions) {
    // Remove any earlier restrictions
    preference.removeAll();/*from   w w w  .  j a  v  a  2s .  c om*/
    // Non-custom-activity case - expand the restrictions in-place
    int count = 1;
    final Context themedContext = getPreferenceManager().getContext();
    for (RestrictionEntry entry : restrictions) {
        Preference p = null;
        switch (entry.getType()) {
        case RestrictionEntry.TYPE_BOOLEAN:
            p = new SwitchPreference(themedContext);
            p.setTitle(entry.getTitle());
            p.setSummary(entry.getDescription());
            ((SwitchPreference) p).setChecked(entry.getSelectedState());
            break;
        case RestrictionEntry.TYPE_CHOICE:
        case RestrictionEntry.TYPE_CHOICE_LEVEL:
            p = new ListPreference(themedContext);
            p.setTitle(entry.getTitle());
            String value = entry.getSelectedString();
            if (value == null) {
                value = entry.getDescription();
            }
            p.setSummary(findInArray(entry.getChoiceEntries(), entry.getChoiceValues(), value));
            ((ListPreference) p).setEntryValues(entry.getChoiceValues());
            ((ListPreference) p).setEntries(entry.getChoiceEntries());
            ((ListPreference) p).setValue(value);
            ((ListPreference) p).setDialogTitle(entry.getTitle());
            break;
        case RestrictionEntry.TYPE_MULTI_SELECT:
            p = new MultiSelectListPreference(themedContext);
            p.setTitle(entry.getTitle());
            ((MultiSelectListPreference) p).setEntryValues(entry.getChoiceValues());
            ((MultiSelectListPreference) p).setEntries(entry.getChoiceEntries());
            HashSet<String> set = new HashSet<>();
            Collections.addAll(set, entry.getAllSelectedStrings());
            ((MultiSelectListPreference) p).setValues(set);
            ((MultiSelectListPreference) p).setDialogTitle(entry.getTitle());
            break;
        case RestrictionEntry.TYPE_NULL:
        default:
        }
        if (p != null) {
            p.setPersistent(false);
            p.setOrder(preference.getOrder() + count);
            // Store the restrictions key string as a key for the preference
            p.setKey(getPackageFromKey(preference.getKey()) + DELIMITER + entry.getKey());
            preference.addPreference(p);
            p.setOnPreferenceChangeListener(AppRestrictionsFragment.this);
            p.setIcon(BLANK_DRAWABLE);
            count++;
        }
    }
    preference.setRestrictions(restrictions);
    if (count == 1 // No visible restrictions
            && preference.isImmutable() && preference.isChecked()) {
        // Special case of required app with no visible restrictions. Remove it
        mAppList.removePreference(preference);
    }
}