Search in sources :

Example 6 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 7 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 8 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)

Example 9 with RestrictionEntry

use of android.content.RestrictionEntry in project cw-omnibus by commonsguy.

the class RestrictionEntriesReceiver method buildBooleanRestriction.

private RestrictionEntry buildBooleanRestriction(Context ctxt, Bundle current) {
    RestrictionEntry entry = new RestrictionEntry(RESTRICTION_BOOLEAN, current.getBoolean(RESTRICTION_BOOLEAN, false));
    entry.setTitle(ctxt.getString(R.string.boolean_restriction_title));
    entry.setDescription(ctxt.getString(R.string.boolean_restriction_desc));
    return (entry);
}
Also used : RestrictionEntry(android.content.RestrictionEntry)

Example 10 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_settings_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)

Aggregations

RestrictionEntry (android.content.RestrictionEntry)13 Bundle (android.os.Bundle)5 HashSet (java.util.HashSet)4 Intent (android.content.Intent)3 ArrayList (java.util.ArrayList)3 Resources (android.content.res.Resources)2 MultiSelectListPreference (android.support.v14.preference.MultiSelectListPreference)2 ListPreference (android.support.v7.preference.ListPreference)2 Set (java.util.Set)2 Activity (android.app.Activity)1 UserManager (android.os.UserManager)1 SwitchPreference (android.support.v14.preference.SwitchPreference)1 Preference (android.support.v7.preference.Preference)1 StringTokenizer (java.util.StringTokenizer)1