Skip to content

Commit

Permalink
Add new API-18 sample 'AppRestrictions'
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcollins committed Aug 7, 2013
1 parent 01d1b89 commit 52465e3
Show file tree
Hide file tree
Showing 20 changed files with 926 additions and 0 deletions.
77 changes: 77 additions & 0 deletions AppRestrictions/AppRestrictions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6762E9D8-DDEC-4D81-BF13-6549BA8C588D}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>AppRestrictions</RootNamespace>
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<AssemblyName>AppRestrictions</AssemblyName>
<TargetFrameworkVersion>v4.3</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<ConsolePause>false</ConsolePause>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Mono.Android" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CustomRestrictionsActivity.cs" />
<Compile Include="CustomRestrictionsFragment.cs" />
<Compile Include="GetRestrictionsReceiver.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\Main.axml" />
<AndroidResource Include="Resources\values\Strings.xml" />
<AndroidResource Include="Resources\drawable-hdpi\icon.png" />
<AndroidResource Include="Resources\drawable-mdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xhdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\icon.png" />
<AndroidResource Include="Resources\xml\custom_prefs.xml" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
<Folder Include="Resources\drawable-mdpi\" />
<Folder Include="Resources\drawable-xhdpi\" />
<Folder Include="Resources\drawable-xxhdpi\" />
<Folder Include="Resources\xml\" />
<Folder Include="Resources\drawable-hdpi\" />
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions AppRestrictions/AppRestrictions.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppRestrictions", "AppRestrictions.csproj", "{6762E9D8-DDEC-4D81-BF13-6549BA8C588D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6762E9D8-DDEC-4D81-BF13-6549BA8C588D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6762E9D8-DDEC-4D81-BF13-6549BA8C588D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6762E9D8-DDEC-4D81-BF13-6549BA8C588D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6762E9D8-DDEC-4D81-BF13-6549BA8C588D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = AppRestrictions.csproj
EndGlobalSection
EndGlobal
55 changes: 55 additions & 0 deletions AppRestrictions/CustomRestrictionsActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace AppRestrictions
{
/**
* 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.
*/
[Activity (Label = "CustomRestrictionsActivity")]
public class CustomRestrictionsActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

if (bundle == null) {
FragmentManager.BeginTransaction ()
.Replace (Android.Resource.Id.Content, new CustomRestrictionsFragment ()).Commit ();
}
}
}
}

197 changes: 197 additions & 0 deletions AppRestrictions/CustomRestrictionsFragment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* 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.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Preferences;

namespace AppRestrictions
{
/**
* 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 : PreferenceFragment
{
// Shared preference key for the boolean restriction.
private static readonly String KEY_BOOLEAN_PREF = "pref_boolean";
// Shared preference key for the single-select restriction.
private static readonly String KEY_CHOICE_PREF = "pref_choice";
// Shared preference key for the multi-select restriction.
private static readonly String KEY_MULTI_PREF = "pref_multi";

private List<IParcelable> 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;

public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);

AddPreferencesFromResource (Resource.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 = FindPreference (KEY_BOOLEAN_PREF) as CheckBoxPreference;
mChoicePref = FindPreference (KEY_CHOICE_PREF) as ListPreference;
mMultiPref = FindPreference (KEY_MULTI_PREF) as MultiSelectListPreference;

mBooleanPref.PreferenceChange += OnPreferenceChange;
mChoicePref.PreferenceChange += OnPreferenceChange;
mMultiPref.PreferenceChange += OnPreferenceChange;

RetainInstance = true;
}

public override void OnActivityCreated (Bundle savedInstanceState)
{
base.OnActivityCreated (savedInstanceState);
Activity act = Activity;

/* BEGIN_INCLUDE (GET_CURRENT_RESTRICTIONS) */
// Existing app restriction settings, if exist, can be retrieved from the Bundle.
mRestrictionsBundle = act.Intent.GetBundleExtra (Intent.ExtraRestrictionsBundle);

if (mRestrictionsBundle == null) {
mRestrictionsBundle = ((UserManager) act.GetSystemService (Context.UserService))
.GetApplicationRestrictions (Activity.PackageName);
}

if (mRestrictionsBundle == null) {
mRestrictionsBundle = new Bundle ();
}

mRestrictions = (List<IParcelable>) act.Intent.GetParcelableArrayListExtra (Intent.ExtraRestrictionsList);
/* END_INCLUDE (GET_CURRENT_RESTRICTIONS) */

// Transfers the saved values into the preference hierarchy.
if (mRestrictions != null) {
foreach (RestrictionEntry entry in mRestrictions) {
if (entry.Key.Equals (GetRestrictionsReceiver.KEY_BOOLEAN)) {
mBooleanPref.Checked = entry.SelectedState;
mBooleanEntry = entry;

} else if (entry.Key.Equals (GetRestrictionsReceiver.KEY_CHOICE)) {
mChoicePref.Value = entry.SelectedString;
mChoiceEntry = entry;

} else if (entry.Key.Equals (GetRestrictionsReceiver.KEY_MULTI_SELECT)) {
List <String> list = new List <String> ();
foreach (String value in entry.GetAllSelectedStrings ()) {
list.Add (value);
}
mMultiPref.Values = list;
mMultiEntry = entry;
}
}
} else {
mRestrictions = new List<IParcelable> ();

// 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.Type = RestrictionEntryType.Boolean;
mBooleanPref.Checked = mBooleanEntry.SelectedState;

// 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.Type = RestrictionEntryType.Choice;
mChoicePref.Value = mChoiceEntry.SelectedString;

// 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.Type = RestrictionEntryType.MultiSelect;

if (mMultiEntry.GetAllSelectedStrings() != null) {
List <String> hset = new List <String> ();
String[] values = mRestrictionsBundle.GetStringArray (GetRestrictionsReceiver.KEY_MULTI_SELECT);

if (values != null) {
foreach (String value in values) {
hset.Add (value);
}
}
mMultiPref.Values = hset;
}
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 (Activity.Intent);
intent.PutParcelableArrayListExtra (Intent.ExtraRestrictionsList, new List <IParcelable> (mRestrictions));
Activity.SetResult (Result.Ok, intent);
}

public void OnPreferenceChange (object sender, Preference.PreferenceChangeEventArgs e)
{
if (e.Preference == mBooleanPref) {
mBooleanEntry.SelectedState = (bool) e.NewValue;

} else if (e.Preference == mChoicePref) {
mChoiceEntry.SelectedString = (string) e.NewValue;

} else if (e.Preference == mMultiPref) {
String[] selectedStrings = new String [((IList <string>) e.NewValue).Count];
int i = 0;
foreach (String value in (IList<string>) e.NewValue) {
selectedStrings[i++] = value;
}
mMultiEntry.SetAllSelectedStrings (selectedStrings);
}

// Saves all the app restriction configuration changes from the custom activity.
Intent intent = new Intent (Activity.Intent);
intent.PutParcelableArrayListExtra (Intent.ExtraRestrictionsList, new List<IParcelable> (mRestrictions));
Activity.SetResult (Result.Ok, intent);
}
}
}

Loading

0 comments on commit 52465e3

Please sign in to comment.