device-admin.jd revision 19aad293c0dba4ed9a47939c487fecdd5318ef08
1page.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
34Android Device Administration API. The Device Administration API provides device
35administration features at the system level.  These APIs allow you to create
36security-aware applications that are useful in enterprise settings, in which IT
37professionals require rich control over employee devices. For example, the
38built-in Android Email application has leveraged the new APIs to improve
39Exchange support. Through the Email application, Exchange administrators can
40enforce password policies &mdash;  including alphanumeric passwords or numeric
41PINs &mdash; across devices. Administrators can also remotely wipe (that is,
42restore factory defaults on) lost or stolen handsets. Exchange users can sync
43their email and calendar data.</p>
44
45<p>This document is intended for developers who want to develop enterprise
46solutions for Android-powered devices. It discusses the various features
47provided by the Device Administration API to provide stronger security for
48employee 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
62install on their devices. The device admin application enforces the desired
63policies. Here's how it works:</p> <ul>
64  <li>A system administrator writes a device admin application that enforces
65remote/local device security policies. These policies could be hard-coded into
66the app, or the application could dynamically fetch policies from a third-party
67server. </li>
68<li>The  application is installed on users' devices. Android does
69not currently have an automated provisioning solution. Some of the ways a sysadmin might
70distribute 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
81and when this happens depends on how the application is implemented.</li>
82<li>Once  users enable the device admin application, they are subject to
83its policies. Complying with those policies typically confers benefits, such as
84access 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&mdash;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
88password that violates the guidelines), it is up to the application to decide
89how to handle this. However, typically this will result in the user not being
90able to sync data.</p>
91<p>If a device attempts to connect to a server that requires policies not
92supported in the Device Administration API, the connection will not
93be allowed. The Device Administration API does not currently allow partial
94provisioning. In other words, if a device (for example, a legacy device) does
95not support all of the stated policies, there is no way to allow the
96device to connect.</p>
97<p>If a device contains multiple enabled admin applications, the strictest policy is
98enforced. There is no way to target a particular admin
99application.</p>
100<p>To uninstall an existing device admin application, users need to
101first 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
107adhere to a strict set of policies that govern the use of the device. The
108Device Administration API supports the  policies listed in Table 1.
109Note that the Device Administration API currently only supports passwords for screen
110lock:</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
124can 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
128combination 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
140letters 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 
147letters 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
153non-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
181users to update their passwords after a specified amount of time has elapsed.
182Introduced 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
188device wipes its data.  The Device Administration API also allows administrators to
189remotely reset the device to  factory defaults. This secures data in case the
190device 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
195pressed a button before the device locks the screen. When this happens, users
196need to enter their PIN or passwords again before they can use their devices and
197access 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. 
202Introduced 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
209Administration 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
219href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html">
220Device Administration API
221sample</a>, which is included in the SDK samples. For information on downloading and
222installing the SDK samples, see <a
223href="{@docRoot}resources/samples/get.html">
224Getting the Samples</a>. Here is the  <a
225href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html">
226complete code</a> for
227the sample. </p>
228<p>The
229sample application offers a demo of device admin features. It presents users
230with a user interface that lets them enable the device admin application. Once
231they've enabled the application, they can use the buttons in the user interface
232to 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
238policies, 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). 
243This prevents users from reusing 
244one 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
247locks.</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
264that enforces remote/local device security policy enforcement. This section
265summarizes the steps involved in creating a device administration
266application.</p>
267
268<h3 id="manifest">Creating the manifest</h3>
269
270<p>To use the Device Administration API, the application's
271manifest 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}
277intent, 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>&lt;activity android:name=&quot;.app.DeviceAdminSample$Controller&quot;
284          android:label=&quot;&#64;string/activity_sample_device_admin&quot;&gt;
285    &lt;intent-filter&gt;
286        &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
287        &lt;category android:name=&quot;android.intent.category.SAMPLE_CODE&quot; /&gt;
288    &lt;/intent-filter&gt;
289&lt;/activity&gt;
290
291&lt;receiver android:name=&quot;.app.DeviceAdminSample&quot;
292          android:label=&quot;&#64;string/sample_device_admin&quot;
293          android:description=&quot;&#64;string/sample_device_admin_description&quot;
294          android:permission=&quot;android.permission.BIND_DEVICE_ADMIN&quot;&gt;
295    &lt;meta-data android:name=&quot;android.app.device_admin&quot;
296               android:resource=&quot;&#64;xml/device_admin_sample&quot; /&gt;
297    &lt;intent-filter&gt;
298        &lt;action android:name=&quot;android.app.action.DEVICE_ADMIN_ENABLED&quot; /&gt;
299    &lt;/intent-filter&gt;
300&lt;/receiver&gt;</pre>
301
302 <p>Note that:</p>
303<ul>
304  <li>The activity in the sample application is an {@link android.app.Activity}
305subclass called <code>Controller</code>. The syntax
306<code>&quot;.app.DeviceAdminSample$Controller&quot;</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
309an 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
314href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.
315<ul>
316<li><code>android:label=&quot;@string/activity_sample_device_admin&quot;</code> refers to the
317user-readable label for the activity.</li>
318
319<li><code>android:label=&quot;@string/sample_device_admin&quot;</code> refers to the
320user-readable label for the permission.</li>
321
322<li><code>android:description=&quot;@string/sample_device_admin_description&quot;</code> refers to
323the user-readable description of the permission. A descripton is typically longer and more
324informative than
325a label.</li>
326</ul>
327
328
329<li><code>android:permission=&quot;android.permission.BIND_DEVICE_ADMIN&quot;
330</code> is a permission that a {@link android.app.admin.DeviceAdminReceiver} subclass must
331have, to ensure that only the system can interact with the receiver (no application can be granted this permission). This
332prevents other applications from abusing your device admin app.</li>
333<li><code>android.app.action.DEVICE_ADMIN_ENABLED</code> is the  the primary
334action that a {@link android.app.admin.DeviceAdminReceiver} subclass must handle to be
335allowed to manage a device. This is set to the receiver when the user enables
336the device admin app. Your code typically handles this in
337{@link android.app.admin.DeviceAdminReceiver#onEnabled onEnabled()}. To be supported, the receiver must also
338require the {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission so that other applications
339cannot abuse it.  </li>
340<li>When a user enables the device admin application, that gives the receiver
341permission to perform actions in response to the broadcast of particular system
342events. When suitable event arises, the application can impose a policy. For
343example, if the user attempts to set a new password that doesn't meet the policy
344requirements, the application can prompt the user to pick a different password
345that does meet the requirements.</li>
346
347  <li><code>android:resource=&quot;&#64;xml/device_admin_sample&quot;</code>
348declares the security policies used in metadata. The metadata provides additional
349information specific to the device administrator, as parsed by the {@link
350android.app.admin.DeviceAdminInfo} class. Here are the contents of
351<code>device_admin_sample.xml</code>:</li>
352</ul>
353<pre>&lt;device-admin xmlns:android=&quot;http://schemas.android.com/apk/res/android">;
354  &lt;uses-policies&gt;
355    &lt;limit-password /&gt;
356    &lt;watch-login /&gt;
357    &lt;reset-password /&gt;
358    &lt;force-lock /&gt;
359    &lt;wipe-data /&gt;
360  &lt;/uses-policies&gt;
361&lt;/device-admin&gt;
362</pre>
363<p> In designing your device administration application, you don't need to
364include all of the policies, just the ones that are relevant for your app.
365</p>
366For more discussion of the manifest file, see the <a
367href="{@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
377a convenience for interpreting the raw intent actions   that are sent by the
378system. 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
382this class must have published a {@link android.app.admin.DeviceAdminReceiver} that the user
383has currently enabled. The {@link android.app.admin.DevicePolicyManager} manages policies for
384one 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
387for a device administrator component.</dd>
388</dl>
389<p>These classes provide the foundation for a fully functional device administration application.
390The rest of this section describes how you use the {@link
391android.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
397consists of a series of callbacks that are triggered when particular events
398occur.</p>
399<p>In its {@link android.app.admin.DeviceAdminReceiver} subclass, the sample application
400simply displays a {@link android.widget.Toast} notification in response to particular
401events. For example:</p>
402<pre>public class DeviceAdminSample extends DeviceAdminReceiver {
403
404...
405    &#64;Override
406    public void onEnabled(Context context, Intent intent) {
407        showToast(context, &quot;Sample Device Admin: enabled&quot;);
408    }
409
410    &#64;Override
411    public CharSequence onDisableRequested(Context context, Intent intent) {
412        return &quot;This is an optional message to warn the user about disabling.&quot;;
413    }
414
415    &#64;Override
416    public void onDisabled(Context context, Intent intent) {
417        showToast(context, &quot;Sample Device Admin: disabled&quot;);
418    }
419
420    &#64;Override
421    public void onPasswordChanged(Context context, Intent intent) {
422        showToast(context, &quot;Sample Device Admin: pw changed&quot;);
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
433enabling the application. The user must explicitly enable the application for
434the policies to be enforced. If the user chooses not to enable the application
435it will still be present on the device, but its policies will not be enforced, and the user will not
436get any of the application's benefits.</p>
437<p>The process of enabling the application begins when the user performs an
438action that triggers the {@link android.app.admin.DevicePolicyManager#ACTION_ADD_DEVICE_ADMIN}
439intent. In the
440sample application, this happens when the user clicks the <strong>Enable
441Admin</strong> button. </p>
442<p>When the user clicks the <strong>Enable Admin</strong> button, the display
443changes to prompt the user to enable the device admin application, as shown in figure
4442.</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
449Admin</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               &quot;Additional text explaining why this needs to be added.&quot;);
459        startActivityForResult(intent, RESULT_ENABLE);
460    }
461};
462
463...
464// This code checks whether the device admin app was successfully enabled.
465&#64;Override
466protected void onActivityResult(int requestCode, int resultCode, Intent data) {
467    switch (requestCode) {
468        case RESULT_ENABLE:
469            if (resultCode == Activity.RESULT_OK) {
470                Log.i(&quot;DeviceAdminSample&quot;, &quot;Administration enabled!&quot;);
471            } else {
472                Log.i(&quot;DeviceAdminSample&quot;, &quot;Administration enable FAILED!&quot;);
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,
481mDeviceAdminSample)</code> states that <code>mDeviceAdminSample</code> (which is
482a {@link android.app.admin.DeviceAdminReceiver} component) is the target policy.
483This line invokes the user interface shown in figure 2, which guides users through
484adding 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
487device admin application being enabled, it confirms that the application is
488active. 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}
490method {@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()} takes a {@link android.app.admin.DeviceAdminReceiver}
491component as its argument:</p>
492<pre>
493DevicePolicyManager mDPM;
494...
495boolean active = mDPM.isAdminActive(mDeviceAdminSample);
496if (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
506enforced on a device. {@link android.app.admin.DevicePolicyManager} manages policies for one
507or 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>
510DevicePolicyManager 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
523device password policy. In the Device Administration API, the password only applies to
524screen 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);
529startActivity(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
536password 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
538password containing at least <em>both</em> numeric <em>and</em> alphabetic (or
539other symbol) characters.</dd>
540  <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_NUMERIC}</dt><dd>The user must enter a   password
541containing at least numeric characters.</dd>
542<dt>{@link
543android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_COMPLEX}</dt><dd>The user
544must have entered a password containing at least a letter, a numerical digit and
545a special symbol.</dd> 
546<dt>{@link
547android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_SOMETHING}</dt><dd>The
548policy requires some kind
549of 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>
555DevicePolicyManager mDPM;
556ComponentName mDeviceAdminSample;
557...
558mDPM.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
564includes methods that let you fine-tune the contents of the password. For
565example, 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
567contents:</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>
584DevicePolicyManager mDPM;
585ComponentName mDeviceAdminSample;
586int pwMinUppercase = 2;
587...
588mDPM.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
593length. For example:</p>
594<pre>DevicePolicyManager mDPM;
595ComponentName mDeviceAdminSample;
596int pwLength;
597...
598mDPM.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
603device is wiped (that is, reset to factory settings). For example:</p>
604<pre>DevicePolicyManager mDPM;
605ComponentName mDeviceAdminSample;
606int maxFailedPw;
607 ...
608mDPM.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()} 
613method 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;
616ComponentName mDeviceAdminSample;
617long pwExpiration;
618...
619mDPM.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
624that updates the password expiration status:</p>
625
626<pre>
627DevicePolicyManager mDPM;
628ComponentName mDeviceAdminSample;
629private TextView mPasswordExpirationStatus;
630...
631void 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 &gt;= 0) {
642            statusText = &quot;Expiration in &quot; + countdownString(mSecUntilExpiration);
643        } else {
644            statusText = &quot;Expired &quot; + countdownString(-mSecUntilExpiration) + &quot; ago&quot;;
645        }
646
647        // expirationTimeout is the cycle time between required password refresh
648        long expirationTimeout = mDPM.getPasswordExpirationTimeout(mDeviceAdminSample);
649        statusText += &quot; / timeout period &quot; + countdownString(expirationTimeout);
650
651        // Now report the aggregate (global) expiration time
652        statusText += &quot; / Aggregate &quot;;
653        expirationDate = mDPM.getPasswordExpiration(null);
654        mSecUntilExpiration = expirationDate - now;
655        if (mSecUntilExpiration &gt;= 0) {
656            statusText += &quot;expiration in &quot; + countdownString(mSecUntilExpiration);
657        } else {
658            statusText += &quot;expired &quot; + countdownString(-mSecUntilExpiration) + &quot; ago&quot;;
659        }
660    } else {
661        statusText = &quot;&lt;inactive&gt;&quot;;
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()} 
669method to limit users'
670ability to reuse old passwords. This method takes a <em>length</em>
671parameter, which specifies how many old
672passwords are stored. When this policy is active, users cannot enter a new
673password that matches the last <em>n</em> passwords. This prevents
674users from using the same password over and over. This policy is typically used
675in conjunction with 
676{@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()},
677which forces users
678to 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;
683ComponentName mDeviceAdminSample;
684int pwHistoryLength = 5;
685...
686mDPM.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
691device locks. For example:</p>
692<pre>
693DevicePolicyManager mDPM;
694ComponentName mDeviceAdminSample;
695...
696long timeMs = 1000L*Long.parseLong(mTimeout.getText().toString());
697mDPM.setMaximumTimeToLock(mDeviceAdminSample, timeMs);
698</pre>
699<p>You can also programmatically tell the device to lock immediately:</p>
700<pre>
701DevicePolicyManager mDPM;
702mDPM.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
710if the device is lost or stolen. Often the decision to wipe the device is the
711result 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
713wiped after a specific number of failed password attempts.</p>
714<p>You wipe data as follows:</p>
715<pre>
716DevicePolicyManager mDPM;
717mDPM.wipeData(0);</pre>
718<p>The {@link android.app.admin.DevicePolicyManager#wipeData wipeData()} method takes as its parameter a bit mask of
719additional 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()} 
724method to set a policy requiring encryption of the storage area, where supported.</p>
725
726<p>For example:</p>
727
728<pre>
729DevicePolicyManager mDPM;
730ComponentName mDeviceAdminSample;
731...
732mDPM.setStorageEncryption(mDeviceAdminSample, true);
733</pre>
734<p>
735See the <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html"> Device Administration API sample</a> for a complete
736example of how to enable storage encryption.</p>
737