Changes to handle restrictions API change

Framework passes in a Bundle, application returns ArrayList<RestrictionEntry>.

Bug: 8633967
Change-Id: Ib9b1c9fe555a0a87a77b7278d1fe3ef41ca07b9b
This commit is contained in:
Amith Yamasani
2013-04-17 10:48:37 -07:00
parent fb6dc4fedf
commit 55cadb05de
2 changed files with 55 additions and 42 deletions

View File

@@ -16,9 +16,11 @@
package com.example.android.applimits; package com.example.android.applimits;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.RestrictionEntry; import android.content.RestrictionEntry;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserManager;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.MultiSelectListPreference; import android.preference.MultiSelectListPreference;
@@ -26,6 +28,8 @@ import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import com.example.android.applimits.GetRestrictionsReceiver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@@ -39,6 +43,7 @@ public class CustomRestrictionsActivity extends PreferenceActivity
private static final String KEY_MULTI_PREF = "multi"; private static final String KEY_MULTI_PREF = "multi";
List<RestrictionEntry> mRestrictions; List<RestrictionEntry> mRestrictions;
private Bundle mRestrictionsBundle;
CheckBoxPreference mCustomPref; CheckBoxPreference mCustomPref;
ListPreference mChoicePref; ListPreference mChoicePref;
@@ -52,16 +57,20 @@ public class CustomRestrictionsActivity extends PreferenceActivity
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mRestrictions = getIntent().getParcelableArrayListExtra( mRestrictionsBundle = getIntent().getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
Intent.EXTRA_RESTRICTIONS); if (mRestrictionsBundle == null) {
mRestrictionsBundle =
if (savedInstanceState != null ((UserManager) getSystemService(Context.USER_SERVICE))
&& savedInstanceState.containsKey(Intent.EXTRA_RESTRICTIONS)) { .getApplicationRestrictions(getPackageName());
mRestrictions = savedInstanceState.getParcelableArrayList(Intent.EXTRA_RESTRICTIONS); }
if (mRestrictionsBundle == null) {
mRestrictionsBundle = new Bundle();
} }
if (mRestrictions == null) { if (savedInstanceState != null
mRestrictions = new ArrayList<RestrictionEntry>(getApplicationRestrictions()); && savedInstanceState.containsKey(Intent.EXTRA_RESTRICTIONS_LIST)) {
mRestrictions = savedInstanceState.getParcelableArrayList(
Intent.EXTRA_RESTRICTIONS_LIST);
} }
this.addPreferencesFromResource(R.xml.custom_prefs); this.addPreferencesFromResource(R.xml.custom_prefs);
@@ -89,10 +98,25 @@ public class CustomRestrictionsActivity extends PreferenceActivity
} }
} else { } else {
mRestrictions = new ArrayList<RestrictionEntry>(); mRestrictions = new ArrayList<RestrictionEntry>();
mCustomEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_CUSTOM, false); mCustomEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_CUSTOM,
mChoiceEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_CHOICE, (String) null); mRestrictionsBundle.getBoolean(GetRestrictionsReceiver.KEY_CUSTOM, false));
mCustomEntry.setType(RestrictionEntry.TYPE_BOOLEAN);
mCustomPref.setChecked(mCustomEntry.getSelectedState());
mChoiceEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_CHOICE,
mRestrictionsBundle.getString(GetRestrictionsReceiver.KEY_CHOICE));
mChoiceEntry.setType(RestrictionEntry.TYPE_CHOICE);
mChoicePref.setValue(mChoiceEntry.getSelectedString());
mMultiEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_MULTI_SELECT, mMultiEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_MULTI_SELECT,
new String[0]); mRestrictionsBundle.getStringArray(GetRestrictionsReceiver.KEY_MULTI_SELECT));
mMultiEntry.setType(RestrictionEntry.TYPE_MULTI_SELECT);
if (mMultiEntry.getAllSelectedStrings() != null) {
HashSet<String> set = new HashSet<String>();
for (String value : mRestrictionsBundle.getStringArray(
GetRestrictionsReceiver.KEY_MULTI_SELECT)) {
set.add(value);
}
mMultiPref.setValues(set);
}
mRestrictions.add(mCustomEntry); mRestrictions.add(mCustomEntry);
mRestrictions.add(mChoiceEntry); mRestrictions.add(mChoiceEntry);
mRestrictions.add(mMultiEntry); mRestrictions.add(mMultiEntry);
@@ -101,14 +125,14 @@ public class CustomRestrictionsActivity extends PreferenceActivity
mChoicePref.setOnPreferenceChangeListener(this); mChoicePref.setOnPreferenceChangeListener(this);
mMultiPref.setOnPreferenceChangeListener(this); mMultiPref.setOnPreferenceChangeListener(this);
Intent intent = new Intent(getIntent()); Intent intent = new Intent(getIntent());
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST,
new ArrayList<RestrictionEntry>(mRestrictions)); new ArrayList<RestrictionEntry>(mRestrictions));
setResult(RESULT_OK, intent); setResult(RESULT_OK, intent);
} }
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS, outState.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST,
new ArrayList<RestrictionEntry>(mRestrictions)); new ArrayList<RestrictionEntry>(mRestrictions));
} }
@@ -127,7 +151,7 @@ public class CustomRestrictionsActivity extends PreferenceActivity
mMultiEntry.setAllSelectedStrings(selectedStrings); mMultiEntry.setAllSelectedStrings(selectedStrings);
} }
Intent intent = new Intent(getIntent()); Intent intent = new Intent(getIntent());
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST,
new ArrayList<RestrictionEntry>(mRestrictions)); new ArrayList<RestrictionEntry>(mRestrictions));
setResult(RESULT_OK, intent); setResult(RESULT_OK, intent);
return true; return true;

View File

@@ -39,8 +39,8 @@ public class GetRestrictionsReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(final Context context, Intent intent) { public void onReceive(final Context context, Intent intent) {
final PendingResult result = goAsync(); final PendingResult result = goAsync();
final ArrayList<RestrictionEntry> oldRestrictions = final Bundle oldRestrictions =
intent.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS); intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
Log.i(TAG, "oldRestrictions = " + oldRestrictions); Log.i(TAG, "oldRestrictions = " + oldRestrictions);
new Thread() { new Thread() {
public void run() { public void run() {
@@ -97,34 +97,32 @@ public class GetRestrictionsReceiver extends BroadcastReceiver {
return newRestrictions; return newRestrictions;
} }
private void createRestrictions(Context context, private void createRestrictions(Context context, PendingResult result, Bundle old) {
PendingResult result, ArrayList<RestrictionEntry> old) {
Resources res = context.getResources(); Resources res = context.getResources();
ArrayList<RestrictionEntry> newEntries = initRestrictions(context); ArrayList<RestrictionEntry> newEntries = initRestrictions(context);
// If this is the first time, create the default restrictions entries and return them. // If this is the first time, create the default restrictions entries and return them.
if (old == null) { if (old == null) {
Bundle extras = new Bundle(); Bundle extras = new Bundle();
extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS, newEntries); extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);
result.setResult(Activity.RESULT_OK, null, extras); result.setResult(Activity.RESULT_OK, null, extras);
result.finish(); result.finish();
return; return;
} }
boolean custom = false; boolean custom = old.getBoolean(KEY_CUSTOM, false);
for (RestrictionEntry entry : old) { for (RestrictionEntry entry : newEntries) {
if (entry.getKey().equals(KEY_CUSTOM)) { final String key = entry.getKey();
if (entry.getSelectedState()) { if (KEY_CUSTOM.equals(key)) {
custom = true; entry.setSelectedState(custom);
} else if (KEY_CHOICE.equals(key)) {
if (old.containsKey(KEY_CHOICE)) {
entry.setSelectedString(old.getString(KEY_CHOICE));
}
} else if (KEY_MULTI_SELECT.equals(key)) {
if (old.containsKey(KEY_MULTI_SELECT)) {
entry.setAllSelectedStrings(old.getStringArray(key));
} }
RestrictionEntry newEntry = find(newEntries, KEY_CUSTOM);
newEntry.setSelectedState(entry.getSelectedState());
} else if (entry.getKey().equals(KEY_CHOICE)) {
RestrictionEntry newEntry = find(newEntries, KEY_CHOICE);
newEntry.setSelectedString(entry.getSelectedString());
} else if (entry.getKey().equals(KEY_MULTI_SELECT)) {
RestrictionEntry newEntry = find(newEntries, KEY_MULTI_SELECT);
newEntry.setAllSelectedStrings(entry.getAllSelectedStrings());
} }
} }
@@ -134,17 +132,8 @@ public class GetRestrictionsReceiver extends BroadcastReceiver {
customIntent.setClass(context, CustomRestrictionsActivity.class); customIntent.setClass(context, CustomRestrictionsActivity.class);
extras.putParcelable(Intent.EXTRA_RESTRICTIONS_INTENT, customIntent); extras.putParcelable(Intent.EXTRA_RESTRICTIONS_INTENT, customIntent);
} }
extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS, newEntries); extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);
result.setResult(Activity.RESULT_OK, null, extras); result.setResult(Activity.RESULT_OK, null, extras);
result.finish(); result.finish();
} }
private RestrictionEntry find(ArrayList<RestrictionEntry> entries, String key) {
for (RestrictionEntry entry : entries) {
if (entry.getKey().equals(key)) {
return entry;
}
}
return null;
}
} }