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

View File

@@ -39,8 +39,8 @@ public class GetRestrictionsReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
final PendingResult result = goAsync();
final ArrayList<RestrictionEntry> oldRestrictions =
intent.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS);
final Bundle oldRestrictions =
intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
Log.i(TAG, "oldRestrictions = " + oldRestrictions);
new Thread() {
public void run() {
@@ -97,34 +97,32 @@ public class GetRestrictionsReceiver extends BroadcastReceiver {
return newRestrictions;
}
private void createRestrictions(Context context,
PendingResult result, ArrayList<RestrictionEntry> old) {
private void createRestrictions(Context context, PendingResult result, Bundle old) {
Resources res = context.getResources();
ArrayList<RestrictionEntry> newEntries = initRestrictions(context);
// If this is the first time, create the default restrictions entries and return them.
if (old == null) {
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.finish();
return;
}
boolean custom = false;
for (RestrictionEntry entry : old) {
if (entry.getKey().equals(KEY_CUSTOM)) {
if (entry.getSelectedState()) {
custom = true;
boolean custom = old.getBoolean(KEY_CUSTOM, false);
for (RestrictionEntry entry : newEntries) {
final String key = entry.getKey();
if (KEY_CUSTOM.equals(key)) {
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);
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.finish();
}
private RestrictionEntry find(ArrayList<RestrictionEntry> entries, String key) {
for (RestrictionEntry entry : entries) {
if (entry.getKey().equals(key)) {
return entry;
}
}
return null;
}
}