28
28
29
29
import java .util .ArrayList ;
30
30
import java .util .HashSet ;
31
+ import java .util .List ;
31
32
import java .util .Set ;
32
33
33
34
public class CustomRestrictionsActivity extends PreferenceActivity
@@ -37,7 +38,7 @@ public class CustomRestrictionsActivity extends PreferenceActivity
37
38
private static final String KEY_CHOICE_PREF = "choice" ;
38
39
private static final String KEY_MULTI_PREF = "multi" ;
39
40
40
- ArrayList <RestrictionEntry > mRestrictions ;
41
+ List <RestrictionEntry > mRestrictions ;
41
42
42
43
CheckBoxPreference mCustomPref ;
43
44
ListPreference mChoicePref ;
@@ -59,40 +60,56 @@ public void onCreate(Bundle savedInstanceState) {
59
60
mRestrictions = savedInstanceState .getParcelableArrayList (Intent .EXTRA_RESTRICTIONS );
60
61
}
61
62
63
+ if (mRestrictions == null ) {
64
+ mRestrictions = new ArrayList <RestrictionEntry >(getApplicationRestrictions ());
65
+ }
66
+
62
67
this .addPreferencesFromResource (R .xml .custom_prefs );
63
68
mCustomPref = (CheckBoxPreference ) findPreference (KEY_CUSTOM_PREF );
64
69
mChoicePref = (ListPreference ) findPreference (KEY_CHOICE_PREF );
65
70
mMultiPref = (MultiSelectListPreference ) findPreference (KEY_MULTI_PREF );
66
71
67
72
// Transfer the saved values into the preference hierarchy
68
- for (RestrictionEntry entry : mRestrictions ) {
69
- if (entry .getKey ().equals (GetRestrictionsReceiver .KEY_CUSTOM )) {
70
- mCustomPref .setChecked (entry .getSelectedState ());
71
- mCustomEntry = entry ;
72
- } else if (entry .getKey ().equals (GetRestrictionsReceiver .KEY_CHOICE )) {
73
- mChoicePref .setValue (entry .getSelectedString ());
74
- mChoiceEntry = entry ;
75
- } else if (entry .getKey ().equals (GetRestrictionsReceiver .KEY_MULTI_SELECT )) {
76
- HashSet <String > set = new HashSet <String >();
77
- for (String value : entry .getAllSelectedStrings ()) {
78
- set .add (value );
73
+ if (mRestrictions != null ) {
74
+ for (RestrictionEntry entry : mRestrictions ) {
75
+ if (entry .getKey ().equals (GetRestrictionsReceiver .KEY_CUSTOM )) {
76
+ mCustomPref .setChecked (entry .getSelectedState ());
77
+ mCustomEntry = entry ;
78
+ } else if (entry .getKey ().equals (GetRestrictionsReceiver .KEY_CHOICE )) {
79
+ mChoicePref .setValue (entry .getSelectedString ());
80
+ mChoiceEntry = entry ;
81
+ } else if (entry .getKey ().equals (GetRestrictionsReceiver .KEY_MULTI_SELECT )) {
82
+ HashSet <String > set = new HashSet <String >();
83
+ for (String value : entry .getAllSelectedStrings ()) {
84
+ set .add (value );
85
+ }
86
+ mMultiPref .setValues (set );
87
+ mMultiEntry = entry ;
79
88
}
80
- mMultiPref .setValues (set );
81
- mMultiEntry = entry ;
82
89
}
90
+ } else {
91
+ mRestrictions = new ArrayList <RestrictionEntry >();
92
+ mCustomEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_CUSTOM , false );
93
+ mChoiceEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_CHOICE , (String ) null );
94
+ mMultiEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_MULTI_SELECT ,
95
+ new String [0 ]);
96
+ mRestrictions .add (mCustomEntry );
97
+ mRestrictions .add (mChoiceEntry );
98
+ mRestrictions .add (mMultiEntry );
83
99
}
84
100
mCustomPref .setOnPreferenceChangeListener (this );
85
101
mChoicePref .setOnPreferenceChangeListener (this );
86
102
mMultiPref .setOnPreferenceChangeListener (this );
87
103
Intent intent = new Intent (getIntent ());
88
104
intent .putParcelableArrayListExtra (Intent .EXTRA_RESTRICTIONS ,
89
- mRestrictions );
105
+ new ArrayList < RestrictionEntry >( mRestrictions ) );
90
106
setResult (RESULT_OK , intent );
91
107
}
92
108
93
109
public void onSaveInstanceState (Bundle outState ) {
94
110
super .onSaveInstanceState (outState );
95
- outState .putParcelableArrayList (Intent .EXTRA_RESTRICTIONS , mRestrictions );
111
+ outState .putParcelableArrayList (Intent .EXTRA_RESTRICTIONS ,
112
+ new ArrayList <RestrictionEntry >(mRestrictions ));
96
113
}
97
114
98
115
@ Override
@@ -111,7 +128,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
111
128
}
112
129
Intent intent = new Intent (getIntent ());
113
130
intent .putParcelableArrayListExtra (Intent .EXTRA_RESTRICTIONS ,
114
- mRestrictions );
131
+ new ArrayList < RestrictionEntry >( mRestrictions ) );
115
132
setResult (RESULT_OK , intent );
116
133
return true ;
117
134
}
0 commit comments