private void createRestrictions()

in AppRestrictions/Application/src/main/java/com/example/android/apprestrictions/GetRestrictionsReceiver.java [111:163]


    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);

        // If app restrictions were not previously configured for the package, create the default
        // 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;
        }

        // Retains current restriction settings by transferring existing restriction entries to
        // 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();

        // This path demonstrates the use of a custom app restriction activity instead of standard
        // types.  When a custom activity is set, the standard types will not be available under
        // app restriction settings.
        //
        // If your app has an existing activity for app restriction configuration, you can set it
        // up with the intent here.
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        if (prefs.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();
    }