Skip to content

Commit 55cadb0

Browse files
author
Amith Yamasani
committed
Changes to handle restrictions API change
Framework passes in a Bundle, application returns ArrayList<RestrictionEntry>. Bug: 8633967 Change-Id: Ib9b1c9fe555a0a87a77b7278d1fe3ef41ca07b9b
1 parent fb6dc4f commit 55cadb0

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

samples/AppLimits/src/com/example/android/applimits/CustomRestrictionsActivity.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616

1717
package com.example.android.applimits;
1818

19+
import android.content.Context;
1920
import android.content.Intent;
2021
import android.content.RestrictionEntry;
2122
import android.os.Bundle;
23+
import android.os.UserManager;
2224
import android.preference.CheckBoxPreference;
2325
import android.preference.ListPreference;
2426
import android.preference.MultiSelectListPreference;
2527
import android.preference.Preference;
2628
import android.preference.Preference.OnPreferenceChangeListener;
2729
import android.preference.PreferenceActivity;
2830

31+
import com.example.android.applimits.GetRestrictionsReceiver;
32+
2933
import java.util.ArrayList;
3034
import java.util.HashSet;
3135
import java.util.List;
@@ -39,6 +43,7 @@ public class CustomRestrictionsActivity extends PreferenceActivity
3943
private static final String KEY_MULTI_PREF = "multi";
4044

4145
List<RestrictionEntry> mRestrictions;
46+
private Bundle mRestrictionsBundle;
4247

4348
CheckBoxPreference mCustomPref;
4449
ListPreference mChoicePref;
@@ -52,16 +57,20 @@ public class CustomRestrictionsActivity extends PreferenceActivity
5257
public void onCreate(Bundle savedInstanceState) {
5358
super.onCreate(savedInstanceState);
5459

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();
6168
}
6269

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);
6574
}
6675

6776
this.addPreferencesFromResource(R.xml.custom_prefs);
@@ -89,10 +98,25 @@ public void onCreate(Bundle savedInstanceState) {
8998
}
9099
} else {
91100
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());
94109
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+
}
96120
mRestrictions.add(mCustomEntry);
97121
mRestrictions.add(mChoiceEntry);
98122
mRestrictions.add(mMultiEntry);
@@ -101,14 +125,14 @@ public void onCreate(Bundle savedInstanceState) {
101125
mChoicePref.setOnPreferenceChangeListener(this);
102126
mMultiPref.setOnPreferenceChangeListener(this);
103127
Intent intent = new Intent(getIntent());
104-
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS,
128+
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST,
105129
new ArrayList<RestrictionEntry>(mRestrictions));
106130
setResult(RESULT_OK, intent);
107131
}
108132

109133
public void onSaveInstanceState(Bundle outState) {
110134
super.onSaveInstanceState(outState);
111-
outState.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS,
135+
outState.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST,
112136
new ArrayList<RestrictionEntry>(mRestrictions));
113137
}
114138

@@ -127,7 +151,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
127151
mMultiEntry.setAllSelectedStrings(selectedStrings);
128152
}
129153
Intent intent = new Intent(getIntent());
130-
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS,
154+
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST,
131155
new ArrayList<RestrictionEntry>(mRestrictions));
132156
setResult(RESULT_OK, intent);
133157
return true;

samples/AppLimits/src/com/example/android/applimits/GetRestrictionsReceiver.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class GetRestrictionsReceiver extends BroadcastReceiver {
3939
@Override
4040
public void onReceive(final Context context, Intent intent) {
4141
final PendingResult result = goAsync();
42-
final ArrayList<RestrictionEntry> oldRestrictions =
43-
intent.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS);
42+
final Bundle oldRestrictions =
43+
intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
4444
Log.i(TAG, "oldRestrictions = " + oldRestrictions);
4545
new Thread() {
4646
public void run() {
@@ -97,34 +97,32 @@ private ArrayList<RestrictionEntry> initRestrictions(Context context) {
9797
return newRestrictions;
9898
}
9999

100-
private void createRestrictions(Context context,
101-
PendingResult result, ArrayList<RestrictionEntry> old) {
100+
private void createRestrictions(Context context, PendingResult result, Bundle old) {
102101
Resources res = context.getResources();
103102

104103
ArrayList<RestrictionEntry> newEntries = initRestrictions(context);
105104
// If this is the first time, create the default restrictions entries and return them.
106105
if (old == null) {
107106
Bundle extras = new Bundle();
108-
extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS, newEntries);
107+
extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);
109108
result.setResult(Activity.RESULT_OK, null, extras);
110109
result.finish();
111110
return;
112111
}
113112

114-
boolean custom = false;
115-
for (RestrictionEntry entry : old) {
116-
if (entry.getKey().equals(KEY_CUSTOM)) {
117-
if (entry.getSelectedState()) {
118-
custom = true;
113+
boolean custom = old.getBoolean(KEY_CUSTOM, false);
114+
for (RestrictionEntry entry : newEntries) {
115+
final String key = entry.getKey();
116+
if (KEY_CUSTOM.equals(key)) {
117+
entry.setSelectedState(custom);
118+
} else if (KEY_CHOICE.equals(key)) {
119+
if (old.containsKey(KEY_CHOICE)) {
120+
entry.setSelectedString(old.getString(KEY_CHOICE));
121+
}
122+
} else if (KEY_MULTI_SELECT.equals(key)) {
123+
if (old.containsKey(KEY_MULTI_SELECT)) {
124+
entry.setAllSelectedStrings(old.getStringArray(key));
119125
}
120-
RestrictionEntry newEntry = find(newEntries, KEY_CUSTOM);
121-
newEntry.setSelectedState(entry.getSelectedState());
122-
} else if (entry.getKey().equals(KEY_CHOICE)) {
123-
RestrictionEntry newEntry = find(newEntries, KEY_CHOICE);
124-
newEntry.setSelectedString(entry.getSelectedString());
125-
} else if (entry.getKey().equals(KEY_MULTI_SELECT)) {
126-
RestrictionEntry newEntry = find(newEntries, KEY_MULTI_SELECT);
127-
newEntry.setAllSelectedStrings(entry.getAllSelectedStrings());
128126
}
129127
}
130128

@@ -134,17 +132,8 @@ private void createRestrictions(Context context,
134132
customIntent.setClass(context, CustomRestrictionsActivity.class);
135133
extras.putParcelable(Intent.EXTRA_RESTRICTIONS_INTENT, customIntent);
136134
}
137-
extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS, newEntries);
135+
extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);
138136
result.setResult(Activity.RESULT_OK, null, extras);
139137
result.finish();
140138
}
141-
142-
private RestrictionEntry find(ArrayList<RestrictionEntry> entries, String key) {
143-
for (RestrictionEntry entry : entries) {
144-
if (entry.getKey().equals(key)) {
145-
return entry;
146-
}
147-
}
148-
return null;
149-
}
150139
}

0 commit comments

Comments
 (0)