1 page.title=Device Administration 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>In this document</h2> 7 <ol> 8 <li><a href="#overview">Device Administration API Overview</a> 9 <ol> 10 <li><a href="#how">How does it work?</a></li> 11 <li><a href="#policies">Policies</a></li> 12 </ol> 13 </li> 14 <li><a href="#sample">Sample Application</a></li> 15 <li><a href="#developing">Developing a Device Administration Application</a> 16 <ol> 17 <li><a href="#manifest">Creating the manifest</a></li> 18 <li><a href="#code">Implementing the code</a></li> 19 </ol> 20 </li> 21 22 </ol> 23 24 <h2>Key classes</h2> 25 <ol> 26 <li>{@link android.app.admin.DeviceAdminReceiver}</li> 27 <li>{@link android.app.admin.DevicePolicyManager}</li> 28 <li>{@link android.app.admin.DeviceAdminInfo}</li> 29 </ol> 30 </div> 31 </div> 32 33 <p>Android 2.2 introduces support for enterprise applications by offering the 34 Android Device Administration API. The Device Administration API provides device 35 administration features at the system level. These APIs allow you to create 36 security-aware applications that are useful in enterprise settings, in which IT 37 professionals require rich control over employee devices. For example, the 38 built-in Android Email application has leveraged the new APIs to improve 39 Exchange support. Through the Email application, Exchange administrators can 40 enforce password policies — including alphanumeric passwords or numeric 41 PINs — across devices. Administrators can also remotely wipe (that is, 42 restore factory defaults on) lost or stolen handsets. Exchange users can sync 43 their email and calendar data.</p> 44 45 <p>This document is intended for developers who want to develop enterprise 46 solutions for Android-powered devices. It discusses the various features 47 provided by the Device Administration API to provide stronger security for 48 employee devices that are powered by Android.</p> 49 50 51 <h2 id="overview">Device Administration API Overview</h2> 52 53 <p>Here are examples of the types of applications that might use the Device Administration API:</p> 54 <ul> 55 <li>Email clients.</li> 56 <li>Security applications that do remote wipe.</li> 57 <li>Device management services and applications.</li> 58 </ul> 59 60 <h3 id="how">How does it work?</h3> 61 <p>You use the Device Administration API to write device admin applications that users 62 install on their devices. The device admin application enforces the desired 63 policies. Here's how it works:</p> <ul> 64 <li>A system administrator writes a device admin application that enforces 65 remote/local device security policies. These policies could be hard-coded into 66 the app, or the application could dynamically fetch policies from a third-party 67 server. </li> 68 <li>The application is installed on users' devices. Android does 69 not currently have an automated provisioning solution. Some of the ways a sysadmin might 70 distribute the application to users are as follows: 71 <ul> 72 <li>Android Market.</li> 73 <li>Enabling non-market installation.</li> 74 <li>Distributing the application through other means, such as email or websites.</li> 75 76 </ul> 77 78 79 </li> 80 <li>The system prompts the user to enable the device admin application. How 81 and when this happens depends on how the application is implemented.</li> 82 <li>Once users enable the device admin application, they are subject to 83 its policies. Complying with those policies typically confers benefits, such as 84 access to sensitive systems and data.</li> 85 </ul> 86 <p>If users do not enable the device admin app, it remains on the device, but in an inactive state. Users will not be subject to its policies, and they will conversely not get any of the application's benefits—for example, they may not be able to sync data.</p> 87 <p>If a user fails to comply with the policies (for example, if a user sets a 88 password that violates the guidelines), it is up to the application to decide 89 how to handle this. However, typically this will result in the user not being 90 able to sync data.</p> 91 <p>If a device attempts to connect to a server that requires policies not 92 supported in the Device Administration API, the connection will not 93 be allowed. The Device Administration API does not currently allow partial 94 provisioning. In other words, if a device (for example, a legacy device) does 95 not support all of the stated policies, there is no way to allow the 96 device to connect.</p> 97 <p>If a device contains multiple enabled admin applications, the strictest policy is 98 enforced. There is no way to target a particular admin 99 application.</p> 100 <p>To uninstall an existing device admin application, users need to 101 first unregister the application as an administrator. </p> 102 103 104 <h3 id="policies">Policies</h3> 105 106 <p>In an enterprise setting, it's often the case that employee devices must 107 adhere to a strict set of policies that govern the use of the device. The 108 Device Administration API supports the policies listed in Table 1. 109 Note that the Device Administration API currently only supports passwords for screen 110 lock:</p> 111 <p class="table-caption"><strong>Table 1.</strong> Policies supported by the Device Administration API.</p> 112 <table border="1"> 113 <tr> 114 <th>Policy</th> 115 <th>Description</th> 116 </tr> 117 <tr> 118 <td>Password enabled</td> 119 <td>Requires that devices ask for PIN or passwords.</td> 120 </tr> 121 <tr> 122 <td>Minimum password length</td> 123 <td>Set the required number of characters for the password. For example, you 124 can require PIN or passwords to have at least six characters. </td> </tr> 125 <tr> 126 <td>Alphanumeric password required</td> 127 <td>Requires that passwords have a 128 combination of letters and numbers. They may include symbolic characters. 129 </td> 130 </tr> 131 132 <tr> 133 <td>Complex password required</td> 134 <td>Requires that passwords must contain at least a letter, a numerical digit, and a special symbol. Introduced in Android 3.0. 135 </td> 136 </tr> 137 138 <tr> 139 <td>Minimum letters required in password</td> <td>The minimum number of 140 letters required in the password for all admins or a particular one. Introduced in Android 3.0.</td> 141 </tr> 142 143 144 <tr> 145 <td>Minimum lowercase letters required in password</td> 146 <td>The minimum number of lowercase 147 letters required in the password for all admins or a particular one. Introduced in Android 3.0.</td> 148 </tr> 149 150 <tr> 151 <td>Minimum non-letter characters required in password</td> 152 <td>The minimum number of 153 non-letter characters required in the password for all admins or a particular one. Introduced in Android 3.0.</td> 154 </tr> 155 156 <tr> 157 <td>Minimum numerical digits required in password</td> 158 <td>The minimum number of numerical digits required in the password for all admins or a particular one. Introduced in Android 3.0.</td> 159 </tr> 160 161 <tr> 162 <td>Minimum symbols required in password</td> 163 <td>The minimum number of symbols required in the password for all admins or a particular one. Introduced in Android 3.0.</td> 164 </tr> 165 166 <tr> 167 <td>Minimum uppercase letters required in password</td> 168 <td>The minimum number of uppercase letters required in the password for all admins or a particular one. Introduced in Android 3.0.</td> 169 </tr> 170 171 <tr> 172 <td>Password expiration timeout</td> 173 <td>When the password will expire, expressed as a delta in milliseconds from when a device admin sets the expiration timeout. Introduced in Android 3.0.</td> 174 </tr> 175 176 <tr> 177 <td>Password history restriction</td> 178 <td>This policy prevents users from reusing the last <em>n</em> unique passwords. 179 This policy is typically used in conjunction with 180 {@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()}, which forces 181 users to update their passwords after a specified amount of time has elapsed. 182 Introduced in Android 3.0.</td> 183 </tr> 184 185 <tr> 186 <td>Maximum failed password attempts </td> 187 <td>Specifies how many times a user can enter the wrong password before the 188 device wipes its data. The Device Administration API also allows administrators to 189 remotely reset the device to factory defaults. This secures data in case the 190 device is lost or stolen.</td> 191 </tr> 192 <tr> 193 <td>Maximum inactivity time lock</td> 194 <td>Sets the length of time since the user last touched the screen or 195 pressed a button before the device locks the screen. When this happens, users 196 need to enter their PIN or passwords again before they can use their devices and 197 access data. The value can be between 1 and 60 minutes.</td> </tr> 198 199 <tr> 200 <td>Require storage encryption</td> 201 <td>Specifies that the storage area should be encrypted, if the device supports it. 202 Introduced in Android 3.0.</td> </tr> 203 204 </table> 205 206 <h4>Other features</h4> 207 208 <p>In addition to supporting the policies listed in the above table, the Device 209 Administration API lets you do the following:</p> <ul> 210 <li>Prompt user to set a new password.</li> 211 <li>Lock device immediately.</li> 212 <li>Wipe the device's data (that is, restore the device to its factory defaults).</li> 213 </ul> 214 215 216 <h2 id="sample">Sample Application</h2> 217 218 <p>The examples used in this document are based on the <a 219 href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html"> 220 Device Administration API 221 sample</a>, which is included in the SDK samples. For information on downloading and 222 installing the SDK samples, see <a 223 href="{@docRoot}resources/samples/get.html"> 224 Getting the Samples</a>. Here is the <a 225 href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html"> 226 complete code</a> for 227 the sample. </p> 228 <p>The 229 sample application offers a demo of device admin features. It presents users 230 with a user interface that lets them enable the device admin application. Once 231 they've enabled the application, they can use the buttons in the user interface 232 to do the following:</p> 233 <ul> 234 <li>Set password quality.</li> 235 <li>Specify requirements for the user's password, such as minimum length, the minimum number of 236 numeric characters it must contain, and so on.</li> 237 <li>Set the password. If the password does not conform to the specified 238 policies, the system returns an error.</li> 239 <li>Set how many failed password attempts can occur before the device is wiped 240 (that is, restored to factory settings).</li> 241 <li>Set how long from now the password will expire.</li> 242 <li>Set the password history length (<em>length</em> refers to number of old passwords stored in the history). 243 This prevents users from reusing 244 one of the last <em>n</em> passwords they previously used.</li> 245 <li>Specify that the storage area should be encrypted, if the device supports it.</li> 246 <li>Set the maximum amount of inactive time that can elapse before the device 247 locks.</li> 248 <li>Make the device lock immediately.</li> 249 <li>Wipe the device's data (that is, restore factory settings).</li> 250 251 </ul> 252 253 254 255 <img src="{@docRoot}images/admin/device-admin-app.png"/> 256 257 <p class="img-caption"><strong>Figure 1.</strong> Screenshot of the Sample Application</p> 258 259 260 261 <h2 id="developing">Developing a Device Administration Application</h2> 262 263 <p>System administrators can use the Device Administration API to write an application 264 that enforces remote/local device security policy enforcement. This section 265 summarizes the steps involved in creating a device administration 266 application.</p> 267 268 <h3 id="manifest">Creating the manifest</h3> 269 270 <p>To use the Device Administration API, the application's 271 manifest must include the following:</p> 272 <ul> 273 <li>A subclass of {@link android.app.admin.DeviceAdminReceiver} that includes the following: 274 <ul> 275 <li>The {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission.</li> 276 <li>The ability to respond to the {@link android.app.admin.DeviceAdminReceiver#ACTION_DEVICE_ADMIN_ENABLED} 277 intent, expressed in the manifest as an intent filter.</li> 278 </ul> 279 </li> 280 <li>A declaration of security policies used in metadata.</li> 281 </ul> 282 <p>Here is an excerpt from the Device Administration sample manifest:</p> 283 <pre><activity android:name=".app.DeviceAdminSample$Controller" 284 android:label="@string/activity_sample_device_admin"> 285 <intent-filter> 286 <action android:name="android.intent.action.MAIN" /> 287 <category android:name="android.intent.category.SAMPLE_CODE" /> 288 </intent-filter> 289 </activity> 290 291 <receiver android:name=".app.DeviceAdminSample" 292 android:label="@string/sample_device_admin" 293 android:description="@string/sample_device_admin_description" 294 android:permission="android.permission.BIND_DEVICE_ADMIN"> 295 <meta-data android:name="android.app.device_admin" 296 android:resource="@xml/device_admin_sample" /> 297 <intent-filter> 298 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 299 </intent-filter> 300 </receiver></pre> 301 302 <p>Note that:</p> 303 <ul> 304 <li>The activity in the sample application is an {@link android.app.Activity} 305 subclass called <code>Controller</code>. The syntax 306 <code>".app.DeviceAdminSample$Controller"</code> indicates that 307 <code>Controller</code> is an inner class that is nested inside the 308 <code>DeviceAdminSample</code> class. Note that an Activity does not need to be 309 an inner class; it just is in this example.</li> 310 311 <li>The following attributes refer to string resources that for the sample application reside in 312 <code>ApiDemos/res/values/strings.xml</code>. For more information about resources, see 313 <a 314 href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>. 315 <ul> 316 <li><code>android:label="@string/activity_sample_device_admin"</code> refers to the 317 user-readable label for the activity.</li> 318 319 <li><code>android:label="@string/sample_device_admin"</code> refers to the 320 user-readable label for the permission.</li> 321 322 <li><code>android:description="@string/sample_device_admin_description"</code> refers to 323 the user-readable description of the permission. A descripton is typically longer and more 324 informative than 325 a label.</li> 326 </ul> 327 328 329 <li><code>android:permission="android.permission.BIND_DEVICE_ADMIN" 330 </code> is a permission that a {@link android.app.admin.DeviceAdminReceiver} subclass must 331 have, to ensure that only the system can interact with the receiver (no application can be granted this permission). This 332 prevents other applications from abusing your device admin app.</li> 333 <li><code>android.app.action.DEVICE_ADMIN_ENABLED</code> is the the primary 334 action that a {@link android.app.admin.DeviceAdminReceiver} subclass must handle to be 335 allowed to manage a device. This is set to the receiver when the user enables 336 the device admin app. Your code typically handles this in 337 {@link android.app.admin.DeviceAdminReceiver#onEnabled onEnabled()}. To be supported, the receiver must also 338 require the {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission so that other applications 339 cannot abuse it. </li> 340 <li>When a user enables the device admin application, that gives the receiver 341 permission to perform actions in response to the broadcast of particular system 342 events. When suitable event arises, the application can impose a policy. For 343 example, if the user attempts to set a new password that doesn't meet the policy 344 requirements, the application can prompt the user to pick a different password 345 that does meet the requirements.</li> 346 347 <li><code>android:resource="@xml/device_admin_sample"</code> 348 declares the security policies used in metadata. The metadata provides additional 349 information specific to the device administrator, as parsed by the {@link 350 android.app.admin.DeviceAdminInfo} class. Here are the contents of 351 <code>device_admin_sample.xml</code>:</li> 352 </ul> 353 <pre><device-admin xmlns:android="http://schemas.android.com/apk/res/android"> 354 <uses-policies> 355 <limit-password /> 356 <watch-login /> 357 <reset-password /> 358 <force-lock /> 359 <wipe-data /> 360 </uses-policies> 361 </device-admin> 362 </pre> 363 <p> In designing your device administration application, you don't need to 364 include all of the policies, just the ones that are relevant for your app. 365 </p> 366 For more discussion of the manifest file, see the <a 367 href="{@docRoot}guide/topics/manifest/manifest-intro.html">Android Developers Guide</a>. 368 369 370 371 <h3 id="code">Implementing the code</h3> 372 373 <p>The Device Administration API includes the following classes:</p> 374 <dl> 375 <dt>{@link android.app.admin.DeviceAdminReceiver}</dt> 376 <dd>Base class for implementing a device administration component. This class provides 377 a convenience for interpreting the raw intent actions that are sent by the 378 system. Your Device Administration application must include a 379 {@link android.app.admin.DeviceAdminReceiver} subclass.</dd> 380 <dt>{@link android.app.admin.DevicePolicyManager}</dt> 381 <dd>A class for managing policies enforced on a device. Most clients of 382 this class must have published a {@link android.app.admin.DeviceAdminReceiver} that the user 383 has currently enabled. The {@link android.app.admin.DevicePolicyManager} manages policies for 384 one or more {@link android.app.admin.DeviceAdminReceiver} instances</dd> 385 <dt>{@link android.app.admin.DeviceAdminInfo}</dt> 386 <dd>This class is used to specify metadata 387 for a device administrator component.</dd> 388 </dl> 389 <p>These classes provide the foundation for a fully functional device administration application. 390 The rest of this section describes how you use the {@link 391 android.app.admin.DeviceAdminReceiver} and 392 {@link android.app.admin.DevicePolicyManager} APIs to write a device admin application.</p> 393 394 <h4 id="receiver">Subclassing DeviceAdminReceiver</h4> 395 <p>To create a device admin application, you must subclass 396 {@link android.app.admin.DeviceAdminReceiver}. The {@link android.app.admin.DeviceAdminReceiver} class 397 consists of a series of callbacks that are triggered when particular events 398 occur.</p> 399 <p>In its {@link android.app.admin.DeviceAdminReceiver} subclass, the sample application 400 simply displays a {@link android.widget.Toast} notification in response to particular 401 events. For example:</p> 402 <pre>public class DeviceAdminSample extends DeviceAdminReceiver { 403 404 ... 405 @Override 406 public void onEnabled(Context context, Intent intent) { 407 showToast(context, "Sample Device Admin: enabled"); 408 } 409 410 @Override 411 public CharSequence onDisableRequested(Context context, Intent intent) { 412 return "This is an optional message to warn the user about disabling."; 413 } 414 415 @Override 416 public void onDisabled(Context context, Intent intent) { 417 showToast(context, "Sample Device Admin: disabled"); 418 } 419 420 @Override 421 public void onPasswordChanged(Context context, Intent intent) { 422 showToast(context, "Sample Device Admin: pw changed"); 423 } 424 425 void showToast(Context context, CharSequence msg) { 426 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); 427 } 428 ... 429 }</pre> 430 431 <h4 id="enabling">Enabling the application</h4> 432 <p>One of the major events a device admin application has to handle is the user 433 enabling the application. The user must explicitly enable the application for 434 the policies to be enforced. If the user chooses not to enable the application 435 it will still be present on the device, but its policies will not be enforced, and the user will not 436 get any of the application's benefits.</p> 437 <p>The process of enabling the application begins when the user performs an 438 action that triggers the {@link android.app.admin.DevicePolicyManager#ACTION_ADD_DEVICE_ADMIN} 439 intent. In the 440 sample application, this happens when the user clicks the <strong>Enable 441 Admin</strong> button. </p> 442 <p>When the user clicks the <strong>Enable Admin</strong> button, the display 443 changes to prompt the user to enable the device admin application, as shown in figure 444 2.</p> 445 446 <img src="{@docRoot}images/admin/device-admin-activate-prompt.png"/> 447 <p class="img-caption"><strong>Figure 2.</strong> Sample Application: Activating the Application</p> 448 <p>Below is the code that gets executed when the user clicks the <strong>Enable 449 Admin</strong> button shown in figure 1. </p> 450 451 <pre> private OnClickListener mEnableListener = new OnClickListener() { 452 public void onClick(View v) { 453 // Launch the activity to have the user enable our admin. 454 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 455 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, 456 mDeviceAdminSample); 457 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, 458 "Additional text explaining why this needs to be added."); 459 startActivityForResult(intent, RESULT_ENABLE); 460 } 461 }; 462 463 ... 464 // This code checks whether the device admin app was successfully enabled. 465 @Override 466 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 467 switch (requestCode) { 468 case RESULT_ENABLE: 469 if (resultCode == Activity.RESULT_OK) { 470 Log.i("DeviceAdminSample", "Administration enabled!"); 471 } else { 472 Log.i("DeviceAdminSample", "Administration enable FAILED!"); 473 } 474 return; 475 } 476 super.onActivityResult(requestCode, resultCode, data); 477 }</pre> 478 479 <p>The line 480 <code>intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, 481 mDeviceAdminSample)</code> states that <code>mDeviceAdminSample</code> (which is 482 a {@link android.app.admin.DeviceAdminReceiver} component) is the target policy. 483 This line invokes the user interface shown in figure 2, which guides users through 484 adding the device administrator to the system (or allows them to reject it).</p> 485 486 <p>When the application needs to perform an operation that is contingent on the 487 device admin application being enabled, it confirms that the application is 488 active. To do this it uses the {@link android.app.admin.DevicePolicyManager} method 489 {@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()}. Notice that the {@link android.app.admin.DevicePolicyManager} 490 method {@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()} takes a {@link android.app.admin.DeviceAdminReceiver} 491 component as its argument:</p> 492 <pre> 493 DevicePolicyManager mDPM; 494 ... 495 boolean active = mDPM.isAdminActive(mDeviceAdminSample); 496 if (active) { 497 // Admin app is active, so do some admin stuff 498 ... 499 } else { 500 // do something else 501 } 502 </pre> 503 504 <h3 id="admin_ops">Managing policies</h3> 505 <p>{@link android.app.admin.DevicePolicyManager} is a public class for managing policies 506 enforced on a device. {@link android.app.admin.DevicePolicyManager} manages policies for one 507 or more {@link android.app.admin.DeviceAdminReceiver} instances. </p> 508 <p>You get a handle to the {@link android.app.admin.DevicePolicyManager} as follows: </p> 509 <pre> 510 DevicePolicyManager mDPM = 511 (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); 512 </pre> 513 <p>This section describes how to use {@link android.app.admin.DevicePolicyManager} to perform 514 administrative tasks:</p> 515 <ul> 516 <li><a href="#pwd">Set password policies</a></li> 517 <li><a href="#lock">Set device lock</a></li> 518 <li><a href="#wipe">Perform data wipe</a></li> 519 </ul> 520 521 <h4 id="pwd">Set password policies</h4> 522 <p>{@link android.app.admin.DevicePolicyManager} includes APIs for setting and enforcing the 523 device password policy. In the Device Administration API, the password only applies to 524 screen lock. This section describes common password-related tasks.</p> 525 526 <h5>Set a password for the device</h5> 527 <p>This code displays a user interface prompting the user to set a password:</p> 528 <pre>Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); 529 startActivity(intent); 530 </pre> 531 532 <h5>Set the password quality</h5> 533 <p>The password quality can be one of the following {@link android.app.admin.DevicePolicyManager} constants: </p> 534 <dl> 535 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_ALPHABETIC}</dt><dd>The user must enter a 536 password containing at least alphabetic (or other symbol) characters.</dd> 537 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_ALPHANUMERIC}</dt><dd>The user must enter a 538 password containing at least <em>both</em> numeric <em>and</em> alphabetic (or 539 other symbol) characters.</dd> 540 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_NUMERIC}</dt><dd>The user must enter a password 541 containing at least numeric characters.</dd> 542 <dt>{@link 543 android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_COMPLEX}</dt><dd>The user 544 must have entered a password containing at least a letter, a numerical digit and 545 a special symbol.</dd> 546 <dt>{@link 547 android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_SOMETHING}</dt><dd>The 548 policy requires some kind 549 of password, but doesn't care what it is.</dd> 550 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}</dt><dd> 551 The policy has no requirements for the password. </dd> 552 </dl> 553 <p>For example, this is how you would set the password policy to require an alphanumeric password:</p> 554 <pre> 555 DevicePolicyManager mDPM; 556 ComponentName mDeviceAdminSample; 557 ... 558 mDPM.setPasswordQuality(mDeviceAdminSample, DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC); 559 </pre> 560 561 <h5>Set password content requirements</h5> 562 563 <p>Beginning with Android 3.0, the {@link android.app.admin.DevicePolicyManager} class 564 includes methods that let you fine-tune the contents of the password. For 565 example, you could set a policy that states that passwords must contain at least 566 <em>n</em> uppercase letters. Here are the methods for fine-tuning a password's 567 contents:</p> 568 <ul> 569 570 <li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumLetters(android.content.ComponentName,int) setPasswordMinimumLetters()}</li> 571 572 <li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumLowerCase(android.content.ComponentName,int) setPasswordMinimumLowerCase()}</li> 573 574 <li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumUpperCase(android.content.ComponentName,int) setPasswordMinimumUpperCase()}</li> 575 576 <li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumNonLetter(android.content.ComponentName,int) setPasswordMinimumNonLetter()}</li> 577 578 <li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumNumeric(android.content.ComponentName,int) setPasswordMinimumNumeric()}</li> 579 580 <li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumSymbols(android.content.ComponentName,int) setPasswordMinimumSymbols()}</li> 581 </ul> 582 <p>For example, this snippet states that the password must have at least 2 uppercase letters:</p> 583 <pre> 584 DevicePolicyManager mDPM; 585 ComponentName mDeviceAdminSample; 586 int pwMinUppercase = 2; 587 ... 588 mDPM.setPasswordMinimumUpperCase(mDeviceAdminSample, pwMinUppercase);</pre> 589 590 591 <h5>Set the minimum password length</h5> 592 <p>You can specify that a password must be at least the specified minimum 593 length. For example:</p> 594 <pre>DevicePolicyManager mDPM; 595 ComponentName mDeviceAdminSample; 596 int pwLength; 597 ... 598 mDPM.setPasswordMinimumLength(mDeviceAdminSample, pwLength); 599 </pre> 600 601 <h5>Set maximum failed password attempts</h5> 602 <p>You can set the maximum number of allowed failed password attempts before the 603 device is wiped (that is, reset to factory settings). For example:</p> 604 <pre>DevicePolicyManager mDPM; 605 ComponentName mDeviceAdminSample; 606 int maxFailedPw; 607 ... 608 mDPM.setMaximumFailedPasswordsForWipe(mDeviceAdminSample, maxFailedPw);</pre> 609 610 <h5 id="expiration">Set password expiration timeout</h5> 611 <p>Beginning with Android 3.0, you can use the 612 {@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()} 613 method to set when a password will expire, expressed as a delta in milliseconds from when a device admin sets the expiration timeout. For example:</p> 614 615 <pre>DevicePolicyManager mDPM; 616 ComponentName mDeviceAdminSample; 617 long pwExpiration; 618 ... 619 mDPM.setPasswordExpirationTimeout(mDeviceAdminSample, pwExpiration); 620 </pre> 621 622 <p>From the <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html" 623 >Device Administration API sample</a>, here is the code 624 that updates the password expiration status:</p> 625 626 <pre> 627 DevicePolicyManager mDPM; 628 ComponentName mDeviceAdminSample; 629 private TextView mPasswordExpirationStatus; 630 ... 631 void updatePasswordExpirationStatus() { 632 boolean active = mDPM.isAdminActive(mDeviceAdminSample); 633 String statusText; 634 if (active) { 635 long now = System.currentTimeMillis(); 636 // Query the DevicePolicyManager twice - first for the expiration values 637 // set by the sample app, and later, for the system values (which may be different 638 // if there is another administrator active.) 639 long expirationDate = mDPM.getPasswordExpiration(mDeviceAdminSample); 640 long mSecUntilExpiration = expirationDate - now; 641 if (mSecUntilExpiration >= 0) { 642 statusText = "Expiration in " + countdownString(mSecUntilExpiration); 643 } else { 644 statusText = "Expired " + countdownString(-mSecUntilExpiration) + " ago"; 645 } 646 647 // expirationTimeout is the cycle time between required password refresh 648 long expirationTimeout = mDPM.getPasswordExpirationTimeout(mDeviceAdminSample); 649 statusText += " / timeout period " + countdownString(expirationTimeout); 650 651 // Now report the aggregate (global) expiration time 652 statusText += " / Aggregate "; 653 expirationDate = mDPM.getPasswordExpiration(null); 654 mSecUntilExpiration = expirationDate - now; 655 if (mSecUntilExpiration >= 0) { 656 statusText += "expiration in " + countdownString(mSecUntilExpiration); 657 } else { 658 statusText += "expired " + countdownString(-mSecUntilExpiration) + " ago"; 659 } 660 } else { 661 statusText = "<inactive>"; 662 } 663 mPasswordExpirationStatus.setText(statusText);</pre> 664 665 <h5 id="history">Restrict password based on history</h5> 666 667 <p>Beginning with Android 3.0, you can use the 668 {@link android.app.admin.DevicePolicyManager#setPasswordHistoryLength(android.content.ComponentName,int) setPasswordHistoryLength()} 669 method to limit users' 670 ability to reuse old passwords. This method takes a <em>length</em> 671 parameter, which specifies how many old 672 passwords are stored. When this policy is active, users cannot enter a new 673 password that matches the last <em>n</em> passwords. This prevents 674 users from using the same password over and over. This policy is typically used 675 in conjunction with 676 {@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()}, 677 which forces users 678 to update their passwords after a specified amount of time has elapsed. </p> 679 680 <p>For example, this snippet prohibits users from reusing any of their last 5 passwords:</p> 681 682 <pre>DevicePolicyManager mDPM; 683 ComponentName mDeviceAdminSample; 684 int pwHistoryLength = 5; 685 ... 686 mDPM.setPasswordHistoryLength(mDeviceAdminSample, pwHistoryLength); 687 </pre> 688 689 <h4 id="lock">Set device lock</h4> 690 <p>You can set the maximum period of user inactivity that can occur before the 691 device locks. For example:</p> 692 <pre> 693 DevicePolicyManager mDPM; 694 ComponentName mDeviceAdminSample; 695 ... 696 long timeMs = 1000L*Long.parseLong(mTimeout.getText().toString()); 697 mDPM.setMaximumTimeToLock(mDeviceAdminSample, timeMs); 698 </pre> 699 <p>You can also programmatically tell the device to lock immediately:</p> 700 <pre> 701 DevicePolicyManager mDPM; 702 mDPM.lockNow();</pre> 703 704 705 706 <h4 id="wipe">Perform data wipe</h4> 707 708 <p>You can use the {@link android.app.admin.DevicePolicyManager} method 709 {@link android.app.admin.DevicePolicyManager#wipeData wipeData()} to reset the device to factory settings. This is useful 710 if the device is lost or stolen. Often the decision to wipe the device is the 711 result of certain conditions being met. For example, you can use 712 {@link android.app.admin.DevicePolicyManager#setMaximumFailedPasswordsForWipe setMaximumFailedPasswordsForWipe()} to state that a device should be 713 wiped after a specific number of failed password attempts.</p> 714 <p>You wipe data as follows:</p> 715 <pre> 716 DevicePolicyManager mDPM; 717 mDPM.wipeData(0);</pre> 718 <p>The {@link android.app.admin.DevicePolicyManager#wipeData wipeData()} method takes as its parameter a bit mask of 719 additional options. Currently the value must be 0. </p> 720 721 <h4 id=storage">Storage encryption</h4> 722 <p>Beginning with Android 3.0, you can use the 723 {@link android.app.admin.DevicePolicyManager#setStorageEncryption(android.content.ComponentName,boolean) setStorageEncryption()} 724 method to set a policy requiring encryption of the storage area, where supported.</p> 725 726 <p>For example:</p> 727 728 <pre> 729 DevicePolicyManager mDPM; 730 ComponentName mDeviceAdminSample; 731 ... 732 mDPM.setStorageEncryption(mDeviceAdminSample, true); 733 </pre> 734 <p> 735 See the <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html"> Device Administration API sample</a> for a complete 736 example of how to enable storage encryption.</p> 737