1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apprestrictions;
import android.app.Activity;
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;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* This fragment is included in {@code CustomRestrictionsActivity}. It demonstrates how an app
* can integrate its own custom app restriction settings with the restricted profile feature.
*
* This sample app maintains custom app restriction settings in shared preferences. Your app
* can use other methods to maintain the settings. When this activity is invoked
* (from Settings > Users > Restricted Profile), the shared preferences are used to initialize
* the custom configuration on the user interface.
*
* Three sample input types are shown: checkbox, single-choice, and multi-choice. When the
* settings are modified by the user, the corresponding restriction entries are saved in the
* platform. The saved restriction entries are retrievable when the app is launched under a
* restricted profile.
*/
public class CustomRestrictionsFragment extends PreferenceFragment
implements Preference.OnPreferenceChangeListener {
// Shared preference key for the boolean restriction.
private static final String KEY_BOOLEAN_PREF = "pref_boolean";
// Shared preference key for the single-select restriction.
private static final String KEY_CHOICE_PREF = "pref_choice";
// Shared preference key for the multi-select restriction.
private static final String KEY_MULTI_PREF = "pref_multi";
private List<RestrictionEntry> mRestrictions;
private Bundle mRestrictionsBundle;
// Shared preferences for each of the sample input types.
private CheckBoxPreference mBooleanPref;
private ListPreference mChoicePref;
private MultiSelectListPreference mMultiPref;
// Restriction entries for each of the sample input types.
private RestrictionEntry mBooleanEntry;
private RestrictionEntry mChoiceEntry;
private RestrictionEntry mMultiEntry;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.custom_prefs);
// This sample app uses shared preferences to maintain app restriction settings. Your app
// can use other methods to maintain the settings.
mBooleanPref = (CheckBoxPreference) findPreference(KEY_BOOLEAN_PREF);
mChoicePref = (ListPreference) findPreference(KEY_CHOICE_PREF);
mMultiPref = (MultiSelectListPreference) findPreference(KEY_MULTI_PREF);
mBooleanPref.setOnPreferenceChangeListener(this);
mChoicePref.setOnPreferenceChangeListener(this);
mMultiPref.setOnPreferenceChangeListener(this);
setRetainInstance(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Activity activity = getActivity();
// BEGIN_INCLUDE (GET_CURRENT_RESTRICTIONS)
// Existing app restriction settings, if exist, can be retrieved from the Bundle.
mRestrictionsBundle =
activity.getIntent().getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
if (mRestrictionsBundle == null) {
mRestrictionsBundle =
((UserManager) activity.getSystemService(Context.USER_SERVICE))
.getApplicationRestrictions(activity.getPackageName());
}
if (mRestrictionsBundle == null) {
mRestrictionsBundle = new Bundle();
}
mRestrictions = activity.getIntent().getParcelableArrayListExtra(
Intent.EXTRA_RESTRICTIONS_LIST);
// END_INCLUDE (GET_CURRENT_RESTRICTIONS)
// Transfers the saved values into the preference hierarchy.
if (mRestrictions != null) {
for (RestrictionEntry entry : mRestrictions) {
if (entry.getKey().equals(GetRestrictionsReceiver.KEY_BOOLEAN)) {
mBooleanPref.setChecked(entry.getSelectedState());
mBooleanEntry = entry;
} else if (entry.getKey().equals(GetRestrictionsReceiver.KEY_CHOICE)) {
mChoicePref.setValue(entry.getSelectedString());
mChoiceEntry = entry;
} else if (entry.getKey().equals(GetRestrictionsReceiver.KEY_MULTI_SELECT)) {
HashSet<String> set = new HashSet<String>();
for (String value : entry.getAllSelectedStrings()) {
set.add(value);
}
mMultiPref.setValues(set);
mMultiEntry = entry;
}
}
} else {
mRestrictions = new ArrayList<RestrictionEntry>();
// Initializes the boolean restriction entry and updates its corresponding shared
// preference value.
mBooleanEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_BOOLEAN,
mRestrictionsBundle.getBoolean(GetRestrictionsReceiver.KEY_BOOLEAN, false));
mBooleanEntry.setType(RestrictionEntry.TYPE_BOOLEAN);
mBooleanPref.setChecked(mBooleanEntry.getSelectedState());
// Initializes the single choice restriction entry and updates its corresponding
// shared preference value.
mChoiceEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_CHOICE,
mRestrictionsBundle.getString(GetRestrictionsReceiver.KEY_CHOICE));
mChoiceEntry.setType(RestrictionEntry.TYPE_CHOICE);
mChoicePref.setValue(mChoiceEntry.getSelectedString());
// Initializes the multi-select restriction entry and updates its corresponding
// shared preference value.
mMultiEntry = new RestrictionEntry(GetRestrictionsReceiver.KEY_MULTI_SELECT,
mRestrictionsBundle.getStringArray(
GetRestrictionsReceiver.KEY_MULTI_SELECT));
mMultiEntry.setType(RestrictionEntry.TYPE_MULTI_SELECT);
if (mMultiEntry.getAllSelectedStrings() != null) {
HashSet<String> set = new HashSet<String>();
final String[] values = mRestrictionsBundle.getStringArray(
GetRestrictionsReceiver.KEY_MULTI_SELECT);
if (values != null) {
for (String value : values) {
set.add(value);
}
}
mMultiPref.setValues(set);
}
mRestrictions.add(mBooleanEntry);
mRestrictions.add(mChoiceEntry);
mRestrictions.add(mMultiEntry);
}
// Prepares result to be passed back to the Settings app when the custom restrictions
// activity finishes.
Intent intent = new Intent(getActivity().getIntent());
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST,
new ArrayList<RestrictionEntry>(mRestrictions));
getActivity().setResult(Activity.RESULT_OK, intent);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mBooleanPref) {
mBooleanEntry.setSelectedState((Boolean) newValue);
} else if (preference == mChoicePref) {
mChoiceEntry.setSelectedString((String) newValue);
} else if (preference == mMultiPref) {
String[] selectedStrings = new String[((Set<String>)newValue).size()];
int i = 0;
for (String value : (Set<String>) newValue) {
selectedStrings[i++] = value;
}
mMultiEntry.setAllSelectedStrings(selectedStrings);
}
// Saves all the app restriction configuration changes from the custom activity.
Intent intent = new Intent(getActivity().getIntent());
intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST,
new ArrayList<RestrictionEntry>(mRestrictions));
getActivity().setResult(Activity.RESULT_OK, intent);
return true;
}
}
|