Merge "docs: Fix code sample in /work: Set up Single-Purpose Devices am: f4973c28a5 am: 90ee70e519" into nyc-dev am: c0b602c441

am: ce13039f1d

* commit 'ce13039f1d4336d713700455ad0af388cc896d0a':
  docs: Fix code sample in /work: Set up Single-Purpose Devices

Change-Id: Ic8bd13319057fe94d98aa2c3a9a64e46728c2345
This commit is contained in:
Billy Lamberta
2016-06-03 23:21:56 +00:00
committed by android-build-merger

View File

@@ -366,6 +366,17 @@ and implements the relevant COSU device management APIs:
<pre> <pre>
public class CosuActivity extends Activity { public class CosuActivity extends Activity {
private ComponentName mAdminComponentName;
private DevicePolicyManager mDevicePolicyManager;
private PackageManager mPackageManager;
private static final String Battery_PLUGGED_ANY = Integer.toString(
BatteryManager.BATTERY_PLUGGED_AC |
BatteryManager.BATTERY_PLUGGED_USB |
BatteryManager.BATTERY_PLUGGED_WIRELESS);
private static final String DONT_STAY_ON = "0";
&#64;Override &#64;Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -383,27 +394,27 @@ public class CosuActivity extends Activity {
// start lock task mode if it's not already active // start lock task mode if it's not already active
ActivityManager am = (ActivityManager) getSystemService( ActivityManager am = (ActivityManager) getSystemService(
Context.ACTIVITY_SERVICE); Context.ACTIVITY_SERVICE);
// ActivityManager.getLockTaskModeState api is not available in pre-M. // ActivityManager.getLockTaskModeState api is not available in pre-M.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (!am.isInLockTaskMode()) { if (!am.isInLockTaskMode()) {
startLockTask(); startLockTask();
} }
} else { } else {
if (am.getLockTaskModeState() == if (am.getLockTaskModeState() ==
ActivityManager.LOCK_TASK_MODE_NONE) { ActivityManager.LOCK_TASK_MODE_NONE) {
startLockTask(); startLockTask();
} }
} }
} }
private void setDefaultCosuPolicies(boolean active) { private void setDefaultCosuPolicies(boolean active) {
// set managed configurations // set user restrictions
setUserRestriction(DISALLOW_SAFE_BOOT, active); setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
setUserRestriction(DISALLOW_FACTORY_RESET, active); setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
setUserRestriction(DISALLOW_ADD_USER, active); setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
setUserRestriction(DISALLOW_MOUNT_PHYSICAL_MEDIA, active); setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
setUserRestriction(DISALLOW_ADJUST_VOLUME, active); setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);
// disable keyguard and status bar // disable keyguard and status bar
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active); mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
@@ -415,16 +426,16 @@ public class CosuActivity extends Activity {
// set System Update policy // set System Update policy
if (active){ if (active){
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName, mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
SystemUpdatePolicy.createWindowedInstallPolicy(60,120)); SystemUpdatePolicy.createWindowedInstallPolicy(60,120));
} else {
DevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName, null);
} }
else
// set this Activity as a lock task package // set this Activity as a lock task package
mDevicePolicyManager.setLockTaskPackages(mAdminComponentName, mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,
active ? new String[]{getPackageName()} : new String[]{}); active ? new String[]{getPackageName()} : new String[]{});
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
intentFilter.addCategory(Intent.CATEGORY_HOME); intentFilter.addCategory(Intent.CATEGORY_HOME);
@@ -434,38 +445,35 @@ public class CosuActivity extends Activity {
// set Cosu activity as home intent receiver so that it is started // set Cosu activity as home intent receiver so that it is started
// on reboot // on reboot
mDevicePolicyManager.addPersistentPreferredActivity( mDevicePolicyManager.addPersistentPreferredActivity(
mAdminComponentName, intentFilter, new ComponentName( mAdminComponentName, intentFilter, new ComponentName(
getPackageName(), CosuModeActivity.class.getName())) getPackageName(), CosuActivity.class.getName()));
} else { } else {
mDevicePolicyManager.clearPackagePersistentPreferredActivities( mDevicePolicyManager.clearPackagePersistentPreferredActivities(
mAdminComponentName, getPackageName()); mAdminComponentName, getPackageName());
} }
} }
private void setUserRestriction(String restriction, boolean disallow) { private void setUserRestriction(String restriction, boolean disallow) {
if (disallow) { if (disallow) {
mDevicePolicyManager.addUserRestriction(mAdminComponentName, mDevicePolicyManager.addUserRestriction(mAdminComponentName,
restriction); restriction);
} else { } else {
mDevicePolicyManager.clearUserRestriction(mAdminComponentName, mDevicePolicyManager.clearUserRestriction(mAdminComponentName,
restriction); restriction);
} }
} }
private void enableStayOnWhilePluggedIn(boolean enabled) { private void enableStayOnWhilePluggedIn(boolean enabled) {
if (enabled) { if (enabled) {
mDevicePolicyManager.setGlobalSetting( mDevicePolicyManager.setGlobalSetting(
mAdminComponentName, mAdminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
BatteryManager.BATTERY_PLUGGED_AC Battery_PLUGGED_ANY);
| BatteryManager.BATTERY_PLUGGED_USB } else {
| BatteryManager.BATTERY_PLUGGED_WIRELESS); mDevicePolicyManager.setGlobalSetting(
} else { mAdminComponentName,
mDevicePolicyManager.setGlobalSetting( Settings.Global.STAY_ON_WHILE_PLUGGED_IN, DONT_STAY_ON);
mAdminComponentName, }
Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);
}
} }
// TODO: Implement the rest of the Activity // TODO: Implement the rest of the Activity