diff options
Diffstat (limited to 'samples/AppRestrictions/src')
4 files changed, 0 insertions, 539 deletions
diff --git a/samples/AppRestrictions/src/com/example/android/apprestrictions/CustomRestrictionsActivity.java b/samples/AppRestrictions/src/com/example/android/apprestrictions/CustomRestrictionsActivity.java deleted file mode 100644 index 213b31357..000000000 --- a/samples/AppRestrictions/src/com/example/android/apprestrictions/CustomRestrictionsActivity.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.os.Bundle; - -/** - * This activity 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. When - * the activity is invoked (from Settings > Users), the stored settings 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, which are retrievable under a restricted - * profile. - */ -public class CustomRestrictionsActivity extends Activity { - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - if (savedInstanceState == null) { - getFragmentManager().beginTransaction().replace(android.R.id.content, - new CustomRestrictionsFragment()).commit(); - } - } -} diff --git a/samples/AppRestrictions/src/com/example/android/apprestrictions/CustomRestrictionsFragment.java b/samples/AppRestrictions/src/com/example/android/apprestrictions/CustomRestrictionsFragment.java deleted file mode 100644 index b04dfd1f7..000000000 --- a/samples/AppRestrictions/src/com/example/android/apprestrictions/CustomRestrictionsFragment.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * 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; - } -} diff --git a/samples/AppRestrictions/src/com/example/android/apprestrictions/GetRestrictionsReceiver.java b/samples/AppRestrictions/src/com/example/android/apprestrictions/GetRestrictionsReceiver.java deleted file mode 100644 index bb5a28391..000000000 --- a/samples/AppRestrictions/src/com/example/android/apprestrictions/GetRestrictionsReceiver.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.RestrictionEntry; -import android.content.res.Resources; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.util.Log; - -import java.util.ArrayList; - -public class GetRestrictionsReceiver extends BroadcastReceiver { - private static final String TAG = GetRestrictionsReceiver.class.getSimpleName(); - - // Keys for referencing app restriction settings from the platform. - public static final String KEY_BOOLEAN = "boolean_key"; - public static final String KEY_CHOICE = "choice_key"; - public static final String KEY_MULTI_SELECT = "multi_key"; - - @Override - public void onReceive(final Context context, Intent intent) { - final PendingResult result = goAsync(); - - // If app restriction settings are already created, they will be included in the Bundle - // as key/value pairs. - final Bundle existingRestrictions = - intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE); - Log.i(TAG, "existingRestrictions = " + existingRestrictions); - - new Thread() { - public void run() { - createRestrictions(context, result, existingRestrictions); - } - }.start(); - } - - // Initializes a boolean type restriction entry. - public static void populateBooleanEntry(Resources res, RestrictionEntry entry) { - entry.setType(RestrictionEntry.TYPE_BOOLEAN); - entry.setTitle(res.getString(R.string.boolean_entry_title)); - } - - // Initializes a single choice type restriction entry. - public static void populateChoiceEntry(Resources res, RestrictionEntry reSingleChoice) { - String[] choiceEntries = res.getStringArray(R.array.choice_entry_entries); - String[] choiceValues = res.getStringArray(R.array.choice_entry_values); - if (reSingleChoice.getSelectedString() == null) { - reSingleChoice.setSelectedString(choiceValues[0]); - } - reSingleChoice.setTitle(res.getString(R.string.choice_entry_title)); - reSingleChoice.setChoiceEntries(choiceEntries); - reSingleChoice.setChoiceValues(choiceValues); - reSingleChoice.setType(RestrictionEntry.TYPE_CHOICE); - } - - // Initializes a multi-select type restriction entry. - public static void populateMultiEntry(Resources res, RestrictionEntry reMultiSelect) { - String[] multiEntries = res.getStringArray(R.array.multi_entry_entries); - String[] multiValues = res.getStringArray(R.array.multi_entry_values); - if (reMultiSelect.getAllSelectedStrings() == null) { - reMultiSelect.setAllSelectedStrings(new String[0]); - } - reMultiSelect.setTitle(res.getString(R.string.multi_entry_title)); - reMultiSelect.setChoiceEntries(multiEntries); - reMultiSelect.setChoiceValues(multiValues); - reMultiSelect.setType(RestrictionEntry.TYPE_MULTI_SELECT); - } - - // Demonstrates the creation of standard app restriction types: boolean, single choice, and - // multi-select. - private ArrayList<RestrictionEntry> initRestrictions(Context context) { - ArrayList<RestrictionEntry> newRestrictions = new ArrayList<RestrictionEntry>(); - Resources res = context.getResources(); - - RestrictionEntry reBoolean = new RestrictionEntry(KEY_BOOLEAN, false); - populateBooleanEntry(res, reBoolean); - newRestrictions.add(reBoolean); - - RestrictionEntry reSingleChoice = new RestrictionEntry(KEY_CHOICE, (String) null); - populateChoiceEntry(res, reSingleChoice); - newRestrictions.add(reSingleChoice); - - RestrictionEntry reMultiSelect = new RestrictionEntry(KEY_MULTI_SELECT, (String[]) null); - populateMultiEntry(res, reMultiSelect); - newRestrictions.add(reMultiSelect); - - return newRestrictions; - } - - private void createRestrictions(Context context, PendingResult result, - Bundle existingRestrictions) { - // The incoming restrictions bundle contains key/value pairs representing existing app - // restrictions for this package. In order to retain existing app restrictions, you need to - // construct new restriction entries and then copy in any existing values for the new keys. - ArrayList<RestrictionEntry> newEntries = initRestrictions(context); - - // If app restrictions were not previously configured for the package, create the default - // restrictions entries and return them. - if (existingRestrictions == null) { - Bundle extras = new Bundle(); - extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries); - result.setResult(Activity.RESULT_OK, null, extras); - result.finish(); - return; - } - - // Retains current restriction settings by transferring existing restriction entries to - // new ones. - for (RestrictionEntry entry : newEntries) { - final String key = entry.getKey(); - if (KEY_BOOLEAN.equals(key)) { - entry.setSelectedState(existingRestrictions.getBoolean(KEY_BOOLEAN)); - } else if (KEY_CHOICE.equals(key)) { - if (existingRestrictions.containsKey(KEY_CHOICE)) { - entry.setSelectedString(existingRestrictions.getString(KEY_CHOICE)); - } - } else if (KEY_MULTI_SELECT.equals(key)) { - if (existingRestrictions.containsKey(KEY_MULTI_SELECT)) { - entry.setAllSelectedStrings(existingRestrictions.getStringArray(key)); - } - } - } - - final Bundle extras = new Bundle(); - - // This path demonstrates the use of a custom app restriction activity instead of standard - // types. When a custom activity is set, the standard types will not be available under - // app restriction settings. - // - // If your app has an existing activity for app restriction configuration, you can set it - // up with the intent here. - if (PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean(MainActivity.CUSTOM_CONFIG_KEY, false)) { - final Intent customIntent = new Intent(); - customIntent.setClass(context, CustomRestrictionsActivity.class); - extras.putParcelable(Intent.EXTRA_RESTRICTIONS_INTENT, customIntent); - } - - extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries); - result.setResult(Activity.RESULT_OK, null, extras); - result.finish(); - } -} diff --git a/samples/AppRestrictions/src/com/example/android/apprestrictions/MainActivity.java b/samples/AppRestrictions/src/com/example/android/apprestrictions/MainActivity.java deleted file mode 100644 index 57c443906..000000000 --- a/samples/AppRestrictions/src/com/example/android/apprestrictions/MainActivity.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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.SharedPreferences; -import android.os.Bundle; -import android.os.UserManager; -import android.preference.PreferenceManager; -import android.view.View; -import android.widget.CheckBox; -import android.widget.TextView; - -/** - * This is the main user interface of the App Restrictions sample app. It demonstrates the use - * of the App Restriction feature, which is available on Android 4.3 and above tablet devices - * with the multiuser feature. - * - * When launched under the primary User account, you can toggle between standard app restriction - * types and custom. When launched under a restricted profile, this activity displays app - * restriction settings, if available. - * - * Follow these steps to exercise the feature: - * 1. If this is the primary user, go to Settings > Users. - * 2. Create a restricted profile, if one doesn't exist already. - * 3. Open the profile settings, locate the sample app, and tap the app restriction settings - * icon. Configure app restrictions for the app. - * 4. In the lock screen, switch to the user's restricted profile, launch this sample app, - * and see the configured app restrictions displayed. - */ -public class MainActivity extends Activity { - private Bundle mRestrictionsBundle; - - // Checkbox to indicate whether custom or standard app restriction types are selected. - private CheckBox mCustomConfig; - - public static final String CUSTOM_CONFIG_KEY = "custom_config"; - - private TextView mMultiEntryValue; - private TextView mChoiceEntryValue; - private TextView mBooleanEntryValue; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Sets up user interface elements. - setContentView(R.layout.main); - - mCustomConfig = (CheckBox) findViewById(R.id.custom_app_limits); - final boolean customChecked = - PreferenceManager.getDefaultSharedPreferences(this).getBoolean( - CUSTOM_CONFIG_KEY, false); - if (customChecked) mCustomConfig.setChecked(true); - - mMultiEntryValue = (TextView) findViewById(R.id.multi_entry_id); - mChoiceEntryValue = (TextView) findViewById(R.id.choice_entry_id); - mBooleanEntryValue = (TextView) findViewById(R.id.boolean_entry_id); - } - - @Override - protected void onResume() { - super.onResume(); - - // If app restrictions are set for this package, when launched from a restricted profile, - // the settings are available in the returned Bundle as key/value pairs. - mRestrictionsBundle = - ((UserManager) getSystemService(Context.USER_SERVICE)) - .getApplicationRestrictions(getPackageName()); - if (mRestrictionsBundle == null) { - mRestrictionsBundle = new Bundle(); - } - - // Reads and displays values from a boolean type restriction entry, if available. - // An app can utilize these settings to restrict its content under a restricted profile. - final String booleanRestrictionValue = - mRestrictionsBundle.containsKey(GetRestrictionsReceiver.KEY_BOOLEAN) ? - mRestrictionsBundle.getBoolean(GetRestrictionsReceiver.KEY_BOOLEAN) + "": - getString(R.string.na); - mBooleanEntryValue.setText(booleanRestrictionValue); - - // Reads and displays values from a single choice restriction entry, if available. - final String singleChoiceRestrictionValue = - mRestrictionsBundle.containsKey(GetRestrictionsReceiver.KEY_CHOICE) ? - mRestrictionsBundle.getString(GetRestrictionsReceiver.KEY_CHOICE) : - getString(R.string.na); - mChoiceEntryValue.setText(singleChoiceRestrictionValue); - - // Reads and displays values from a multi-select restriction entry, if available. - final String[] multiSelectValues = - mRestrictionsBundle.getStringArray(GetRestrictionsReceiver.KEY_MULTI_SELECT); - if (multiSelectValues == null || multiSelectValues.length == 0) { - mMultiEntryValue.setText(getString(R.string.na)); - } else { - String tempValue = ""; - for (String value : multiSelectValues) { - tempValue = tempValue + value + " "; - } - mMultiEntryValue.setText(tempValue); - } - } - - /** - * Saves custom app restriction to the shared preference. - * - * This flag is used by {@code GetRestrictionsReceiver} to determine if a custom app - * restriction activity should be used. - * - * @param view - */ - public void onCustomClicked(View view) { - final SharedPreferences.Editor editor = - PreferenceManager.getDefaultSharedPreferences(this).edit(); - editor.putBoolean(CUSTOM_CONFIG_KEY, mCustomConfig.isChecked()).commit(); - } -} |