16
16
17
17
package com .example .android .applimits ;
18
18
19
+ import android .content .Context ;
19
20
import android .content .Intent ;
20
21
import android .content .RestrictionEntry ;
21
22
import android .os .Bundle ;
23
+ import android .os .UserManager ;
22
24
import android .preference .CheckBoxPreference ;
23
25
import android .preference .ListPreference ;
24
26
import android .preference .MultiSelectListPreference ;
25
27
import android .preference .Preference ;
26
28
import android .preference .Preference .OnPreferenceChangeListener ;
27
29
import android .preference .PreferenceActivity ;
28
30
31
+ import com .example .android .applimits .GetRestrictionsReceiver ;
32
+
29
33
import java .util .ArrayList ;
30
34
import java .util .HashSet ;
31
35
import java .util .List ;
@@ -39,6 +43,7 @@ public class CustomRestrictionsActivity extends PreferenceActivity
39
43
private static final String KEY_MULTI_PREF = "multi" ;
40
44
41
45
List <RestrictionEntry > mRestrictions ;
46
+ private Bundle mRestrictionsBundle ;
42
47
43
48
CheckBoxPreference mCustomPref ;
44
49
ListPreference mChoicePref ;
@@ -52,16 +57,20 @@ public class CustomRestrictionsActivity extends PreferenceActivity
52
57
public void onCreate (Bundle savedInstanceState ) {
53
58
super .onCreate (savedInstanceState );
54
59
55
- mRestrictions = getIntent ().getParcelableArrayListExtra (
56
- Intent .EXTRA_RESTRICTIONS );
57
-
58
- if (savedInstanceState != null
59
- && savedInstanceState .containsKey (Intent .EXTRA_RESTRICTIONS )) {
60
- mRestrictions = savedInstanceState .getParcelableArrayList (Intent .EXTRA_RESTRICTIONS );
60
+ mRestrictionsBundle = getIntent ().getBundleExtra (Intent .EXTRA_RESTRICTIONS_BUNDLE );
61
+ if (mRestrictionsBundle == null ) {
62
+ mRestrictionsBundle =
63
+ ((UserManager ) getSystemService (Context .USER_SERVICE ))
64
+ .getApplicationRestrictions (getPackageName ());
65
+ }
66
+ if (mRestrictionsBundle == null ) {
67
+ mRestrictionsBundle = new Bundle ();
61
68
}
62
69
63
- if (mRestrictions == null ) {
64
- mRestrictions = new ArrayList <RestrictionEntry >(getApplicationRestrictions ());
70
+ if (savedInstanceState != null
71
+ && savedInstanceState .containsKey (Intent .EXTRA_RESTRICTIONS_LIST )) {
72
+ mRestrictions = savedInstanceState .getParcelableArrayList (
73
+ Intent .EXTRA_RESTRICTIONS_LIST );
65
74
}
66
75
67
76
this .addPreferencesFromResource (R .xml .custom_prefs );
@@ -89,10 +98,25 @@ public void onCreate(Bundle savedInstanceState) {
89
98
}
90
99
} else {
91
100
mRestrictions = new ArrayList <RestrictionEntry >();
92
- mCustomEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_CUSTOM , false );
93
- mChoiceEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_CHOICE , (String ) null );
101
+ mCustomEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_CUSTOM ,
102
+ mRestrictionsBundle .getBoolean (GetRestrictionsReceiver .KEY_CUSTOM , false ));
103
+ mCustomEntry .setType (RestrictionEntry .TYPE_BOOLEAN );
104
+ mCustomPref .setChecked (mCustomEntry .getSelectedState ());
105
+ mChoiceEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_CHOICE ,
106
+ mRestrictionsBundle .getString (GetRestrictionsReceiver .KEY_CHOICE ));
107
+ mChoiceEntry .setType (RestrictionEntry .TYPE_CHOICE );
108
+ mChoicePref .setValue (mChoiceEntry .getSelectedString ());
94
109
mMultiEntry = new RestrictionEntry (GetRestrictionsReceiver .KEY_MULTI_SELECT ,
95
- new String [0 ]);
110
+ mRestrictionsBundle .getStringArray (GetRestrictionsReceiver .KEY_MULTI_SELECT ));
111
+ mMultiEntry .setType (RestrictionEntry .TYPE_MULTI_SELECT );
112
+ if (mMultiEntry .getAllSelectedStrings () != null ) {
113
+ HashSet <String > set = new HashSet <String >();
114
+ for (String value : mRestrictionsBundle .getStringArray (
115
+ GetRestrictionsReceiver .KEY_MULTI_SELECT )) {
116
+ set .add (value );
117
+ }
118
+ mMultiPref .setValues (set );
119
+ }
96
120
mRestrictions .add (mCustomEntry );
97
121
mRestrictions .add (mChoiceEntry );
98
122
mRestrictions .add (mMultiEntry );
@@ -101,14 +125,14 @@ public void onCreate(Bundle savedInstanceState) {
101
125
mChoicePref .setOnPreferenceChangeListener (this );
102
126
mMultiPref .setOnPreferenceChangeListener (this );
103
127
Intent intent = new Intent (getIntent ());
104
- intent .putParcelableArrayListExtra (Intent .EXTRA_RESTRICTIONS ,
128
+ intent .putParcelableArrayListExtra (Intent .EXTRA_RESTRICTIONS_LIST ,
105
129
new ArrayList <RestrictionEntry >(mRestrictions ));
106
130
setResult (RESULT_OK , intent );
107
131
}
108
132
109
133
public void onSaveInstanceState (Bundle outState ) {
110
134
super .onSaveInstanceState (outState );
111
- outState .putParcelableArrayList (Intent .EXTRA_RESTRICTIONS ,
135
+ outState .putParcelableArrayList (Intent .EXTRA_RESTRICTIONS_LIST ,
112
136
new ArrayList <RestrictionEntry >(mRestrictions ));
113
137
}
114
138
@@ -127,7 +151,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
127
151
mMultiEntry .setAllSelectedStrings (selectedStrings );
128
152
}
129
153
Intent intent = new Intent (getIntent ());
130
- intent .putParcelableArrayListExtra (Intent .EXTRA_RESTRICTIONS ,
154
+ intent .putParcelableArrayListExtra (Intent .EXTRA_RESTRICTIONS_LIST ,
131
155
new ArrayList <RestrictionEntry >(mRestrictions ));
132
156
setResult (RESULT_OK , intent );
133
157
return true ;
0 commit comments