Search in sources :

Example 36 with RestrictionEntry

use of android.content.RestrictionEntry in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class AppRestrictionsFragment method addLocationAppRestrictionsPreference.

private void addLocationAppRestrictionsPreference(AppRestrictionsHelper.SelectableAppInfo app, AppRestrictionsPreference p) {
    String packageName = app.packageName;
    p.setIcon(R.drawable.ic_preference_location);
    p.setKey(getKeyForPackage(packageName));
    ArrayList<RestrictionEntry> restrictions = RestrictionUtils.getRestrictions(getActivity(), mUser);
    RestrictionEntry locationRestriction = restrictions.get(0);
    p.setTitle(locationRestriction.getTitle());
    p.setRestrictions(restrictions);
    p.setSummary(locationRestriction.getDescription());
    p.setChecked(locationRestriction.getSelectedState());
    p.setPersistent(false);
    p.setOnPreferenceClickListener(this);
    p.setOrder(MAX_APP_RESTRICTIONS);
    mAppList.addPreference(p);
}
Also used : RestrictionEntry(android.content.RestrictionEntry)

Example 37 with RestrictionEntry

use of android.content.RestrictionEntry in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class AppRestrictionsFragment method onPreferenceChange.

@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(PKG_PREFIX + 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;
                    }
                    mUserManager.setApplicationRestrictions(packageName, RestrictionsManager.convertRestrictionsToBundle(restrictions), mUser);
                    break;
                }
            }
        }
        return true;
    }
    return false;
}
Also used : StringTokenizer(java.util.StringTokenizer) HashSet(java.util.HashSet) Set(java.util.Set) RestrictionEntry(android.content.RestrictionEntry) MultiSelectListPreference(androidx.preference.MultiSelectListPreference) ListPreference(androidx.preference.ListPreference)

Example 38 with RestrictionEntry

use of android.content.RestrictionEntry in project Resurrection_packages_apps_Settings by ResurrectionRemix.

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)

Example 39 with RestrictionEntry

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

the class CustomRestrictionsFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final Activity activity = getActivity();
    // BEGIN_INCLUDE (GET_CURRENT_RESTRICTIONS)
    // Existing app restriction settings, if exist, can be retrieved from the Bundle.
    mRestrictionsBundle = activity.getIntent().getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
    if (mRestrictionsBundle == null) {
        mRestrictionsBundle = ((UserManager) activity.getSystemService(Context.USER_SERVICE)).getApplicationRestrictions(activity.getPackageName());
    }
    if (mRestrictionsBundle == null) {
        mRestrictionsBundle = new Bundle();
    }
    mRestrictions = activity.getIntent().getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST);
    // Transfers the saved values into the preference hierarchy.
    if (mRestrictions != null) {
        for (RestrictionEntry entry : mRestrictions) {
            if (entry.getKey().equals(GetRestrictionsReceiver.KEY_BOOLEAN)) {
                mBooleanPref.setChecked(entry.getSelectedState());
                mBooleanEntry = entry;
            } else if (entry.getKey().equals(GetRestrictionsReceiver.KEY_CHOICE)) {
                mChoicePref.setValue(entry.getSelectedString());
                mChoiceEntry = entry;
            } else if (entry.getKey().equals(GetRestrictionsReceiver.KEY_MULTI_SELECT)) {
                HashSet<String> set = new HashSet<String>();
                for (String value : entry.getAllSelectedStrings()) {
                    set.add(value);
                }
                mMultiPref.setValues(set);
                mMultiEntry = entry;
            }
        }
    } else {
        mRestrictions = new ArrayList<RestrictionEntry>();
        // Initializes the boolean restriction entry and updates its corresponding shared
        // preference value.
        mBooleanEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_BOOLEAN, mRestrictionsBundle.getBoolean(GetRestrictionsReceiver.KEY_BOOLEAN, false));
        mBooleanEntry.setType(RestrictionEntry.TYPE_BOOLEAN);
        mBooleanPref.setChecked(mBooleanEntry.getSelectedState());
        // Initializes the single choice restriction entry and updates its corresponding
        // shared preference value.
        mChoiceEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_CHOICE, mRestrictionsBundle.getString(GetRestrictionsReceiver.KEY_CHOICE));
        mChoiceEntry.setType(RestrictionEntry.TYPE_CHOICE);
        mChoicePref.setValue(mChoiceEntry.getSelectedString());
        // Initializes the multi-select restriction entry and updates its corresponding
        // shared preference value.
        mMultiEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_MULTI_SELECT, mRestrictionsBundle.getStringArray(GetRestrictionsReceiver.KEY_MULTI_SELECT));
        mMultiEntry.setType(RestrictionEntry.TYPE_MULTI_SELECT);
        if (mMultiEntry.getAllSelectedStrings() != null) {
            HashSet<String> set = new HashSet<String>();
            final String[] values = mRestrictionsBundle.getStringArray(GetRestrictionsReceiver.KEY_MULTI_SELECT);
            if (values != null) {
                for (String value : values) {
                    set.add(value);
                }
            }
            mMultiPref.setValues(set);
        }
        mRestrictions.add(mBooleanEntry);
        mRestrictions.add(mChoiceEntry);
        mRestrictions.add(mMultiEntry);
    }
    // Prepares result to be passed back to the Settings app when the custom restrictions
    // activity finishes.
    Intent intent = new Intent(getActivity().getIntent());
    intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST, new ArrayList<RestrictionEntry>(mRestrictions));
    getActivity().setResult(Activity.RESULT_OK, intent);
}
Also used : Bundle(android.os.Bundle) RestrictionEntry(android.content.RestrictionEntry) Activity(android.app.Activity) Intent(android.content.Intent) HashSet(java.util.HashSet)

Example 40 with RestrictionEntry

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

the class GetRestrictionsReceiver method initRestrictions.

// Demonstrates the creation of standard app restriction types: boolean, single choice, and
// multi-select.
private ArrayList<RestrictionEntry> initRestrictions(Context context) {
    ArrayList<RestrictionEntry> newRestrictions = new ArrayList<RestrictionEntry>();
    Resources res = context.getResources();
    RestrictionEntry reBoolean = new RestrictionEntry(KEY_BOOLEAN, false);
    populateBooleanEntry(res, reBoolean);
    newRestrictions.add(reBoolean);
    RestrictionEntry reSingleChoice = new RestrictionEntry(KEY_CHOICE, (String) null);
    populateChoiceEntry(res, reSingleChoice);
    newRestrictions.add(reSingleChoice);
    RestrictionEntry reMultiSelect = new RestrictionEntry(KEY_MULTI_SELECT, (String[]) null);
    populateMultiEntry(res, reMultiSelect);
    newRestrictions.add(reMultiSelect);
    return newRestrictions;
}
Also used : RestrictionEntry(android.content.RestrictionEntry) 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