Search in sources :

Example 41 with RestrictionEntry

use of android.content.RestrictionEntry in project Android-Developers-Samples by johnjohndoe.

the class GetRestrictionsReceiver method createRestrictions.

private void createRestrictions(Context context, PendingResult result, Bundle existingRestrictions) {
    // The incoming restrictions bundle contains key/value pairs representing existing app
    // restrictions for this package. In order to retain existing app restrictions, you need to
    // construct new restriction entries and then copy in any existing values for the new keys.
    ArrayList<RestrictionEntry> newEntries = initRestrictions(context);
    // restrictions entries and return them.
    if (existingRestrictions == null) {
        Bundle extras = new Bundle();
        extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);
        result.setResult(Activity.RESULT_OK, null, extras);
        result.finish();
        return;
    }
    // new ones.
    for (RestrictionEntry entry : newEntries) {
        final String key = entry.getKey();
        if (KEY_BOOLEAN.equals(key)) {
            entry.setSelectedState(existingRestrictions.getBoolean(KEY_BOOLEAN));
        } else if (KEY_CHOICE.equals(key)) {
            if (existingRestrictions.containsKey(KEY_CHOICE)) {
                entry.setSelectedString(existingRestrictions.getString(KEY_CHOICE));
            }
        } else if (KEY_MULTI_SELECT.equals(key)) {
            if (existingRestrictions.containsKey(KEY_MULTI_SELECT)) {
                entry.setAllSelectedStrings(existingRestrictions.getStringArray(key));
            }
        }
    }
    final Bundle extras = new Bundle();
    // up with the intent here.
    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(MainActivity.CUSTOM_CONFIG_KEY, false)) {
        final Intent customIntent = new Intent();
        customIntent.setClass(context, CustomRestrictionsActivity.class);
        extras.putParcelable(Intent.EXTRA_RESTRICTIONS_INTENT, customIntent);
    }
    extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);
    result.setResult(Activity.RESULT_OK, null, extras);
    result.finish();
}
Also used : RestrictionEntry(android.content.RestrictionEntry) Bundle(android.os.Bundle) Intent(android.content.Intent)

Example 42 with RestrictionEntry

use of android.content.RestrictionEntry in project android_packages_apps_Settings by omnirom.

the class AppRestrictionsFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    AppRestrictionsPreference pref = mCustomRequestMap.get(requestCode);
    if (pref == null) {
        Log.w(TAG, "Unknown requestCode " + requestCode);
        return;
    }
    if (resultCode == Activity.RESULT_OK) {
        String packageName = pref.getKey().substring(PKG_PREFIX.length());
        ArrayList<RestrictionEntry> list = data.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST);
        Bundle bundle = data.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
        if (list != null) {
            // If there's a valid result, persist it to the user manager.
            pref.setRestrictions(list);
            mUserManager.setApplicationRestrictions(packageName, RestrictionsManager.convertRestrictionsToBundle(list), mUser);
        } else if (bundle != null) {
            // If there's a valid result, persist it to the user manager.
            mUserManager.setApplicationRestrictions(packageName, bundle, mUser);
        }
    }
    // Remove request from the map
    mCustomRequestMap.remove(requestCode);
}
Also used : RestrictionEntry(android.content.RestrictionEntry) Bundle(android.os.Bundle)

Example 43 with RestrictionEntry

use of android.content.RestrictionEntry in project android_packages_apps_Settings by omnirom.

the class AppRestrictionsFragment method onRestrictionsReceived.

private void onRestrictionsReceived(AppRestrictionsPreference preference, ArrayList<RestrictionEntry> restrictions) {
    // Remove any earlier restrictions
    removeRestrictionsForApp(preference);
    // Non-custom-activity case - expand the restrictions in-place
    int count = 1;
    for (RestrictionEntry entry : restrictions) {
        Preference p = null;
        switch(entry.getType()) {
            case RestrictionEntry.TYPE_BOOLEAN:
                p = new SwitchPreference(getPrefContext());
                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(getPrefContext());
                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(getPrefContext());
                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(preference.getKey().substring(PKG_PREFIX.length()) + DELIMITER + entry.getKey());
            mAppList.addPreference(p);
            p.setOnPreferenceChangeListener(AppRestrictionsFragment.this);
            p.setIcon(R.drawable.empty_icon);
            preference.mChildren.add(p);
            count++;
        }
    }
    preference.setRestrictions(restrictions);
    if (// No visible restrictions
    count == 1 && preference.isImmutable() && preference.isChecked()) {
        // Special case of required app with no visible restrictions. Remove it
        mAppList.removePreference(preference);
    }
}
Also used : SwitchPreference(androidx.preference.SwitchPreference) Preference(androidx.preference.Preference) MultiSelectListPreference(androidx.preference.MultiSelectListPreference) ListPreference(androidx.preference.ListPreference) SwitchPreference(androidx.preference.SwitchPreference) RestrictionEntry(android.content.RestrictionEntry) MultiSelectListPreference(androidx.preference.MultiSelectListPreference) MultiSelectListPreference(androidx.preference.MultiSelectListPreference) ListPreference(androidx.preference.ListPreference) HashSet(java.util.HashSet)

Example 44 with RestrictionEntry

use of android.content.RestrictionEntry in project android_packages_apps_Settings by omnirom.

the class RestrictionUtils method getRestrictions.

/**
 * Returns the current user restrictions in the form of application
 * restriction entries.
 * @return list of RestrictionEntry objects with user-visible text.
 */
public static ArrayList<RestrictionEntry> getRestrictions(Context context, UserHandle user) {
    Resources res = context.getResources();
    ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
    UserManager um = UserManager.get(context);
    Bundle userRestrictions = um.getUserRestrictions(user);
    for (int i = 0; i < sRestrictionKeys.length; i++) {
        RestrictionEntry entry = new RestrictionEntry(sRestrictionKeys[i], !userRestrictions.getBoolean(sRestrictionKeys[i], false));
        entry.setTitle(res.getString(sRestrictionTitles[i]));
        entry.setDescription(res.getString(sRestrictionDescriptions[i]));
        entry.setType(RestrictionEntry.TYPE_BOOLEAN);
        entries.add(entry);
    }
    return entries;
}
Also used : RestrictionEntry(android.content.RestrictionEntry) UserManager(android.os.UserManager) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Resources(android.content.res.Resources)

Aggregations

RestrictionEntry (android.content.RestrictionEntry)44 Bundle (android.os.Bundle)17 HashSet (java.util.HashSet)16 MultiSelectListPreference (android.support.v14.preference.MultiSelectListPreference)10 ListPreference (android.support.v7.preference.ListPreference)10 ArrayList (java.util.ArrayList)9 Resources (android.content.res.Resources)8 Set (java.util.Set)8 UserManager (android.os.UserManager)7 StringTokenizer (java.util.StringTokenizer)7 SwitchPreference (android.support.v14.preference.SwitchPreference)5 Preference (android.support.v7.preference.Preference)5 ListPreference (androidx.preference.ListPreference)4 MultiSelectListPreference (androidx.preference.MultiSelectListPreference)4 Intent (android.content.Intent)3 Preference (androidx.preference.Preference)2 SwitchPreference (androidx.preference.SwitchPreference)2 Activity (android.app.Activity)1 Test (org.junit.Test)1