Follow changes to RestrictionEntry API

Implement custom restrictions activity.
Fixed some bugs.

Change-Id: I094a6ffcc41c2936f76a8731048d7cb712c1b857
This commit is contained in:
Amith Yamasani
2013-03-28 14:34:36 -07:00
parent a864530969
commit 6e4757f0a6
4 changed files with 152 additions and 53 deletions

View File

@@ -4414,6 +4414,27 @@
<string name="user_restrictions_title">Allow apps and content</string> <string name="user_restrictions_title">Allow apps and content</string>
<!-- User limits screen, user name rename text [CHAR LIMIT=15] --> <!-- User limits screen, user name rename text [CHAR LIMIT=15] -->
<string name="user_rename">RENAME</string> <string name="user_rename">RENAME</string>
<!-- Preference label for custom restrictions [CHAR LIMIT=35] -->
<string name="app_restrictions_custom_label">Set application limits</string>
<!-- Restrictions title for configuring wifi and mobile [CHAR LIMIT=35] -->
<string name="restriction_wifi_config_title">Wi\u2011Fi and Mobile</string>
<!-- Restrictions summary for configuring wifi and mobile [CHAR LIMIT=100] -->
<string name="restriction_wifi_config_summary">Allow modification of Wi\u2011Fi and Mobile settings</string>
<!-- Restrictions title for changing bluetooth configuration [CHAR LIMIT=35] -->
<string name="restriction_bluetooth_config_title">Bluetooth</string>
<!-- Restrictions summary for changing bluetooth configuration [CHAR LIMIT=100] -->
<string name="restriction_bluetooth_config_summary">Allow modification of Bluetooth pairings and settings</string>
<!-- Restrictions title for allowing NFC transfers [CHAR LIMIT=35] -->
<string name="restriction_nfc_enable_title">NFC</string>
<!-- Restrictions summary for allowing NFC transfers (tablet) [CHAR LIMIT=100] -->
<string name="restriction_nfc_enable_summary" product="tablet">Allow data exchange when the tablet touches another device</string>
<!-- Restrictions summary for allowing NFC transfers (phone) [CHAR LIMIT=100] -->
<string name="restriction_nfc_enable_summary" product="default">Allow data exchange when the phone touches another device</string>
<!-- Restrictions title for allowing location sharing [CHAR LIMIT=35] -->
<string name="restriction_location_enable_title">NFC</string>
<!-- Restrictions summary for allowing location sharing [CHAR LIMIT=100] -->
<string name="restriction_location_enable_summary" >Let apps use your location information</string>
<!-- Wizard back button label [CHAR LIMIT=25] --> <!-- Wizard back button label [CHAR LIMIT=25] -->
<string name="wizard_back">Back</string> <string name="wizard_back">Back</string>

View File

@@ -460,8 +460,8 @@ public class Settings extends PreferenceActivity
} else if (id == R.id.application_settings) { } else if (id == R.id.application_settings) {
if (mAppRestrictions != null) { if (mAppRestrictions != null) {
for (RestrictionEntry entry : mAppRestrictions) { for (RestrictionEntry entry : mAppRestrictions) {
if (entry.key.equals(RestrictionsReceiver.KEY_ENABLE_APPS) if (entry.getKey().equals(RestrictionsReceiver.KEY_ENABLE_APPS)
&& !entry.getBooleanValue()) { && !entry.getSelectedState()) {
target.remove(i); target.remove(i);
} }
} }

View File

@@ -31,6 +31,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
@@ -90,6 +91,10 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
private boolean mFirstTime = true; private boolean mFirstTime = true;
private boolean mNewUser; private boolean mNewUser;
private int mCustomRequestCode;
private HashMap<Integer, AppRestrictionsPreference> mCustomRequestMap =
new HashMap<Integer,AppRestrictionsPreference>();
public static class Activity extends PreferenceActivity { public static class Activity extends PreferenceActivity {
@Override @Override
public Intent getIntent() { public Intent getIntent() {
@@ -133,7 +138,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
RestrictionEntry getRestriction(String key) { RestrictionEntry getRestriction(String key) {
if (restrictions == null) return null; if (restrictions == null) return null;
for (RestrictionEntry entry : restrictions) { for (RestrictionEntry entry : restrictions) {
if (entry.key.equals(key)) { if (entry.getKey().equals(key)) {
return entry; return entry;
} }
} }
@@ -296,7 +301,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
if (v.getTag() instanceof AppRestrictionsPreference) { if (v.getTag() instanceof AppRestrictionsPreference) {
AppRestrictionsPreference pref = (AppRestrictionsPreference) v.getTag(); AppRestrictionsPreference pref = (AppRestrictionsPreference) v.getTag();
if (v.getId() == R.id.app_restrictions_settings) { if (v.getId() == R.id.app_restrictions_settings) {
handleSettingsClick(pref); toggleAppPanel(pref);
} else if (!pref.isRequired()) { } else if (!pref.isRequired()) {
pref.setChecked(!pref.isChecked()); pref.setChecked(!pref.isChecked());
mSelectedPackages.put(pref.getKey().substring(PKG_PREFIX.length()), mSelectedPackages.put(pref.getKey().substring(PKG_PREFIX.length()),
@@ -317,21 +322,18 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
ArrayList<RestrictionEntry> restrictions = appPref.getRestrictions(); ArrayList<RestrictionEntry> restrictions = appPref.getRestrictions();
if (restrictions != null) { if (restrictions != null) {
for (RestrictionEntry entry : restrictions) { for (RestrictionEntry entry : restrictions) {
if (entry.key.equals(restrictionKey)) { if (entry.getKey().equals(restrictionKey)) {
switch (entry.type) { switch (entry.getType()) {
case RestrictionEntry.TYPE_BOOLEAN: case RestrictionEntry.TYPE_BOOLEAN:
entry.setValue((Boolean) newValue); entry.setSelectedState((Boolean) newValue);
break; break;
case RestrictionEntry.TYPE_CHOICE: case RestrictionEntry.TYPE_CHOICE:
case RestrictionEntry.TYPE_CHOICE_LEVEL: case RestrictionEntry.TYPE_CHOICE_LEVEL:
ListPreference listPref = (ListPreference) preference; ListPreference listPref = (ListPreference) preference;
entry.setValue((String) newValue); entry.setSelectedString((String) newValue);
for (int i = 0; i < listPref.getEntryValues().length; i++) { String readable = findInArray(entry.getChoiceEntries(),
if (entry.values[i].equals(newValue)) { entry.getChoiceValues(), (String) newValue);
listPref.setSummary(entry.choices[i]); listPref.setSummary(readable);
break;
}
}
break; break;
case RestrictionEntry.TYPE_MULTI_SELECT: case RestrictionEntry.TYPE_MULTI_SELECT:
MultiSelectListPreference msListPref = MultiSelectListPreference msListPref =
@@ -339,7 +341,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
Set<String> set = (Set<String>) newValue; Set<String> set = (Set<String>) newValue;
String [] selectedValues = new String[set.size()]; String [] selectedValues = new String[set.size()];
set.toArray(selectedValues); set.toArray(selectedValues);
entry.setMultipleValues(selectedValues); entry.setAllSelectedStrings(selectedValues);
break; break;
default: default:
continue; continue;
@@ -360,7 +362,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
return true; return true;
} }
private void handleSettingsClick(AppRestrictionsPreference preference) { private void toggleAppPanel(AppRestrictionsPreference preference) {
if (preference.getKey().startsWith(PKG_PREFIX)) { if (preference.getKey().startsWith(PKG_PREFIX)) {
if (preference.panelOpen) { if (preference.panelOpen) {
for (Preference p : preference.childPreferences) { for (Preference p : preference.childPreferences) {
@@ -372,6 +374,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
List<RestrictionEntry> oldEntries = List<RestrictionEntry> oldEntries =
mUserManager.getApplicationRestrictions(packageName, mUser); mUserManager.getApplicationRestrictions(packageName, mUser);
Intent intent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES); Intent intent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
intent.setPackage(packageName);
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS,
new ArrayList<RestrictionEntry>(oldEntries)); new ArrayList<RestrictionEntry>(oldEntries));
getActivity().sendOrderedBroadcast(intent, null, getActivity().sendOrderedBroadcast(intent, null,
@@ -384,6 +387,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
class RestrictionsResultReceiver extends BroadcastReceiver { class RestrictionsResultReceiver extends BroadcastReceiver {
private static final String CUSTOM_RESTRICTIONS_INTENT = Intent.EXTRA_RESTRICTIONS_INTENT;
String packageName; String packageName;
AppRestrictionsPreference preference; AppRestrictionsPreference preference;
@@ -395,39 +399,43 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
ArrayList<RestrictionEntry> restrictions = getResultExtras(true).getParcelableArrayList( Bundle results = getResultExtras(true);
final ArrayList<RestrictionEntry> restrictions = results.getParcelableArrayList(
Intent.EXTRA_RESTRICTIONS); Intent.EXTRA_RESTRICTIONS);
if (restrictions != null) { Intent restrictionsIntent = (Intent) results.getParcelable(CUSTOM_RESTRICTIONS_INTENT);
if (restrictions != null && restrictionsIntent == null) {
// Non-custom-activity case - expand the restrictions in-place
int count = 1; int count = 1;
for (RestrictionEntry entry : restrictions) { for (RestrictionEntry entry : restrictions) {
Preference p = null; Preference p = null;
switch (entry.type) { switch (entry.getType()) {
case RestrictionEntry.TYPE_BOOLEAN: case RestrictionEntry.TYPE_BOOLEAN:
p = new CheckBoxPreference(context); p = new CheckBoxPreference(context);
p.setTitle(entry.title); p.setTitle(entry.getTitle());
p.setSummary(entry.description); p.setSummary(entry.getDescription());
((CheckBoxPreference)p).setChecked(entry.getBooleanValue()); ((CheckBoxPreference)p).setChecked(entry.getSelectedState());
break; break;
case RestrictionEntry.TYPE_CHOICE: case RestrictionEntry.TYPE_CHOICE:
case RestrictionEntry.TYPE_CHOICE_LEVEL: case RestrictionEntry.TYPE_CHOICE_LEVEL:
p = new ListPreference(context); p = new ListPreference(context);
p.setTitle(entry.title); p.setTitle(entry.getTitle());
String value = entry.getStringValue(); String value = entry.getSelectedString();
if (value == null) { if (value == null) {
value = entry.description; value = entry.getDescription();
} }
p.setSummary(value); p.setSummary(findInArray(entry.getChoiceEntries(), entry.getChoiceValues(),
((ListPreference)p).setEntryValues(entry.values); value));
((ListPreference)p).setEntries(entry.choices); ((ListPreference)p).setEntryValues(entry.getChoiceValues());
((ListPreference)p).setValue(entry.getStringValue()); ((ListPreference)p).setEntries(entry.getChoiceEntries());
((ListPreference)p).setValue(value);
break; break;
case RestrictionEntry.TYPE_MULTI_SELECT: case RestrictionEntry.TYPE_MULTI_SELECT:
p = new MultiSelectListPreference(context); p = new MultiSelectListPreference(context);
p.setTitle(entry.title); p.setTitle(entry.getTitle());
((MultiSelectListPreference)p).setEntryValues(entry.values); ((MultiSelectListPreference)p).setEntryValues(entry.getChoiceValues());
((MultiSelectListPreference)p).setEntries(entry.choices); ((MultiSelectListPreference)p).setEntries(entry.getChoiceEntries());
HashSet<String> set = new HashSet<String>(); HashSet<String> set = new HashSet<String>();
for (String s : entry.getMultipleValues()) { for (String s : entry.getAllSelectedStrings()) {
set.add(s); set.add(s);
} }
((MultiSelectListPreference)p).setValues(set); ((MultiSelectListPreference)p).setValues(set);
@@ -440,7 +448,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
p.setOrder(preference.getOrder() + count); p.setOrder(preference.getOrder() + count);
// Store the restrictions key string as a key for the preference // Store the restrictions key string as a key for the preference
p.setKey(preference.getKey().substring(PKG_PREFIX.length()) + DELIMITER p.setKey(preference.getKey().substring(PKG_PREFIX.length()) + DELIMITER
+ entry.key); + entry.getKey());
mAppList.addPreference(p); mAppList.addPreference(p);
p.setOnPreferenceChangeListener(AppRestrictionsFragment.this); p.setOnPreferenceChangeListener(AppRestrictionsFragment.this);
preference.childPreferences.add(p); preference.childPreferences.add(p);
@@ -448,11 +456,81 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
} }
} }
preference.setRestrictions(restrictions); preference.setRestrictions(restrictions);
mUserManager.setApplicationRestrictions(packageName, restrictions, mUser);
} else if (restrictionsIntent != null) {
final Intent customIntent = restrictionsIntent;
customIntent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, restrictions);
Preference p = new Preference(context);
p.setTitle(R.string.app_restrictions_custom_label);
p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
int requestCode = generateCustomActivityRequestCode(
RestrictionsResultReceiver.this.preference);
AppRestrictionsFragment.this.startActivityForResult(
customIntent, requestCode);
return false;
}
});
p.setPersistent(false);
p.setOrder(preference.getOrder() + 1);
preference.childPreferences.add(p);
mAppList.addPreference(p);
preference.setRestrictions(restrictions);
} }
mUserManager.setApplicationRestrictions(packageName, restrictions, mUser);
} }
} }
/**
* Generates a request code that is stored in a map to retrieve the associated
* AppRestrictionsPreference.
* @param preference
* @return
*/
private int generateCustomActivityRequestCode(AppRestrictionsPreference preference) {
mCustomRequestCode++;
mCustomRequestMap.put(mCustomRequestCode, preference);
return mCustomRequestCode;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Got activity resultCode=" + resultCode + ", requestCode="
+ requestCode + ", data=" + data);
AppRestrictionsPreference pref = mCustomRequestMap.get(requestCode);
if (pref == null) {
Log.w(TAG, "Unknown requestCode " + requestCode);
return;
}
if (resultCode == Activity.RESULT_OK) {
ArrayList<RestrictionEntry> list =
data.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS);
if (list != null) {
// If there's a valid result, persist it to the user manager.
String packageName = pref.getKey().substring(PKG_PREFIX.length());
pref.setRestrictions(list);
mUserManager.setApplicationRestrictions(packageName, list, mUser);
}
toggleAppPanel(pref);
}
// Remove request from the map
mCustomRequestMap.remove(requestCode);
}
private String findInArray(String[] choiceEntries, String[] choiceValues,
String selectedString) {
for (int i = 0; i < choiceValues.length; i++) {
if (choiceValues[i].equals(selectedString)) {
return choiceEntries[i];
}
}
return selectedString;
}
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().startsWith(PKG_PREFIX)) { if (preference.getKey().startsWith(PKG_PREFIX)) {

View File

@@ -77,12 +77,12 @@ public class RestrictionsReceiver extends BroadcastReceiver {
String[] oldEnabledSections = new String[0]; String[] oldEnabledSections = new String[0];
if (old != null) { if (old != null) {
for (RestrictionEntry r : old) { for (RestrictionEntry r : old) {
if (r.key.equals(KEY_ENABLE_APPS)) { if (r.getKey().equals(KEY_ENABLE_APPS)) {
oldEnableApps = r.getBooleanValue(); oldEnableApps = r.getSelectedState();
} else if (r.key.equals(KEY_CONTENT_RATING)) { } else if (r.getKey().equals(KEY_CONTENT_RATING)) {
oldContentRating = r.getStringValue(); oldContentRating = r.getSelectedString();
} else if (r.key.equals(KEY_SECTIONS_TO_SHOW)) { } else if (r.getKey().equals(KEY_SECTIONS_TO_SHOW)) {
oldEnabledSections = r.getMultipleValues(); oldEnabledSections = r.getAllSelectedStrings();
} }
} }
} }
@@ -92,17 +92,17 @@ public class RestrictionsReceiver extends BroadcastReceiver {
RestrictionEntry r1 = new RestrictionEntry(KEY_ENABLE_APPS, RestrictionEntry r1 = new RestrictionEntry(KEY_ENABLE_APPS,
Boolean.toString(oldEnableApps)); Boolean.toString(oldEnableApps));
r1.title = "Enable apps"; r1.setTitle("Enable apps");
r1.description = "Show the Apps section in Settings"; r1.setDescription("Show the Apps section in Settings");
r1.type = RestrictionEntry.TYPE_BOOLEAN; r1.setType(RestrictionEntry.TYPE_BOOLEAN);
newRestrictions.add(r1); newRestrictions.add(r1);
RestrictionEntry r2 = new RestrictionEntry(KEY_CONTENT_RATING, oldContentRating); RestrictionEntry r2 = new RestrictionEntry(KEY_CONTENT_RATING, oldContentRating);
r2.title = "Test: Content rating"; r2.setTitle("Test: Content rating");
r2.description = "Limit content to chosen rating and lower"; r2.setDescription("Limit content to chosen rating and lower");
r2.type = RestrictionEntry.TYPE_CHOICE_LEVEL; r2.setType(RestrictionEntry.TYPE_CHOICE_LEVEL);
r2.values = new String[] { "G", "PG", "PG13", "R", "NR" }; r2.setChoiceValues(new String[] { "G", "PG", "PG13", "R", "NR"});
r2.choices = new String[] { "G", "PG", "PG-13", "Restricted", "Not Rated" }; r2.setChoiceEntries(new String[] { "G", "PG", "PG-13", "Restricted", "Not Rated" });
newRestrictions.add(r2); newRestrictions.add(r2);
String [] values = new String[SECTION_IDS.length]; String [] values = new String[SECTION_IDS.length];
@@ -114,10 +114,10 @@ public class RestrictionsReceiver extends BroadcastReceiver {
i++; i++;
} }
RestrictionEntry r3 = new RestrictionEntry(KEY_SECTIONS_TO_SHOW, oldEnabledSections); RestrictionEntry r3 = new RestrictionEntry(KEY_SECTIONS_TO_SHOW, oldEnabledSections);
r3.type = RestrictionEntry.TYPE_MULTI_SELECT; r3.setType(RestrictionEntry.TYPE_MULTI_SELECT);
r3.choices = choices; r3.setChoiceEntries(choices);
r3.values = values; r3.setChoiceValues(values);
r3.title = "Test: Sections to show"; r3.setTitle("Test: Sections to show");
newRestrictions.add(r3); newRestrictions.add(r3);
Bundle extras = new Bundle(); Bundle extras = new Bundle();