blob: bed4b4def6078ef88a4b8567644372f029691c26 [file] [log] [blame]
Scott Main50f86c62010-10-08 16:06:40 -07001page.title=Device Administration
Joe Fernandez33baa5a2013-11-14 11:41:19 -08002page.tags=devicepolicymanager,policy,security
Katie McCormick262b4312010-09-30 16:27:25 -07003@jd:body
Scott Main50f86c62010-10-08 16:06:40 -07004
Katie McCormick262b4312010-09-30 16:27:25 -07005<div id="qv-wrapper">
6<div id="qv">
7 <h2>In this document</h2>
8 <ol>
9<li><a href="#overview">Device Administration API Overview</a>
10 <ol>
Scott Main50f86c62010-10-08 16:06:40 -070011 <li><a href="#how">How does it work?</a></li>
Katie McCormick262b4312010-09-30 16:27:25 -070012 <li><a href="#policies">Policies</a></li>
13 </ol>
14 </li>
Katie McCormick262b4312010-09-30 16:27:25 -070015 <li><a href="#sample">Sample Application</a></li>
16 <li><a href="#developing">Developing a Device Administration Application</a>
17 <ol>
Scott Main50f86c62010-10-08 16:06:40 -070018 <li><a href="#manifest">Creating the manifest</a></li>
19 <li><a href="#code">Implementing the code</a></li>
Katie McCormick262b4312010-09-30 16:27:25 -070020 </ol>
Scott Main50f86c62010-10-08 16:06:40 -070021 </li>
22
23 </ol>
24
25 <h2>Key classes</h2>
26 <ol>
27 <li>{@link android.app.admin.DeviceAdminReceiver}</li>
28 <li>{@link android.app.admin.DevicePolicyManager}</li>
29 <li>{@link android.app.admin.DeviceAdminInfo}</li>
Katie McCormick262b4312010-09-30 16:27:25 -070030 </ol>
Scott Main50f86c62010-10-08 16:06:40 -070031</div>
Katie McCormick262b4312010-09-30 16:27:25 -070032</div>
33
34<p>Android 2.2 introduces support for enterprise applications by offering the
35Android Device Administration API. The Device Administration API provides device
36administration features at the system level. These APIs allow you to create
37security-aware applications that are useful in enterprise settings, in which IT
38professionals require rich control over employee devices. For example, the
39built-in Android Email application has leveraged the new APIs to improve
40Exchange support. Through the Email application, Exchange administrators can
41enforce password policies &mdash; including alphanumeric passwords or numeric
42PINs &mdash; across devices. Administrators can also remotely wipe (that is,
43restore factory defaults on) lost or stolen handsets. Exchange users can sync
44their email and calendar data.</p>
45
46<p>This document is intended for developers who want to develop enterprise
47solutions for Android-powered devices. It discusses the various features
48provided by the Device Administration API to provide stronger security for
49employee devices that are powered by Android.</p>
Scott Main50f86c62010-10-08 16:06:40 -070050
51
Katie McCormick262b4312010-09-30 16:27:25 -070052<h2 id="overview">Device Administration API Overview</h2>
Scott Main50f86c62010-10-08 16:06:40 -070053
Katie McCormick262b4312010-09-30 16:27:25 -070054<p>Here are examples of the types of applications that might use the Device Administration API:</p>
55<ul>
56 <li>Email clients.</li>
57 <li>Security applications that do remote wipe.</li>
58 <li>Device management services and applications.</li>
59</ul>
60
Scott Main50f86c62010-10-08 16:06:40 -070061<h3 id="how">How does it work?</h3>
Katie McCormick262b4312010-09-30 16:27:25 -070062<p>You use the Device Administration API to write device admin applications that users
63install on their devices. The device admin application enforces the desired
64policies. Here's how it works:</p> <ul>
65 <li>A system administrator writes a device admin application that enforces
66remote/local device security policies. These policies could be hard-coded into
67the app, or the application could dynamically fetch policies from a third-party
Scott Main50f86c62010-10-08 16:06:40 -070068server. </li>
Katie McCormick262b4312010-09-30 16:27:25 -070069<li>The application is installed on users' devices. Android does
70not currently have an automated provisioning solution. Some of the ways a sysadmin might
71distribute the application to users are as follows:
72<ul>
Dirk Dougherty4d7bc6552012-01-27 17:56:49 -080073<li>Google Play.</li>
74<li>Enabling installation from another store.</li>
Katie McCormick262b4312010-09-30 16:27:25 -070075<li>Distributing the application through other means, such as email or websites.</li>
76
77</ul>
78
79
80</li>
81 <li>The system prompts the user to enable the device admin application. How
82and when this happens depends on how the application is implemented.</li>
83<li>Once users enable the device admin application, they are subject to
Scott Main50f86c62010-10-08 16:06:40 -070084its policies. Complying with those policies typically confers benefits, such as
Katie McCormick262b4312010-09-30 16:27:25 -070085access to sensitive systems and data.</li>
86</ul>
87<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>
88<p>If a user fails to comply with the policies (for example, if a user sets a
89password that violates the guidelines), it is up to the application to decide
90how to handle this. However, typically this will result in the user not being
91able to sync data.</p>
92<p>If a device attempts to connect to a server that requires policies not
93supported in the Device Administration API, the connection will not
94be allowed. The Device Administration API does not currently allow partial
95provisioning. In other words, if a device (for example, a legacy device) does
96not support all of the stated policies, there is no way to allow the
97device to connect.</p>
98<p>If a device contains multiple enabled admin applications, the strictest policy is
99enforced. There is no way to target a particular admin
100application.</p>
101<p>To uninstall an existing device admin application, users need to
102first unregister the application as an administrator. </p>
103
Scott Main50f86c62010-10-08 16:06:40 -0700104
105<h3 id="policies">Policies</h3>
106
Katie McCormick262b4312010-09-30 16:27:25 -0700107<p>In an enterprise setting, it's often the case that employee devices must
108adhere to a strict set of policies that govern the use of the device. The
109Device Administration API supports the policies listed in Table 1.
110Note that the Device Administration API currently only supports passwords for screen
Scott Main50f86c62010-10-08 16:06:40 -0700111lock:</p>
Katie McCormick262b4312010-09-30 16:27:25 -0700112<p class="table-caption"><strong>Table 1.</strong> Policies supported by the Device Administration API.</p>
113<table border="1">
114 <tr>
115 <th>Policy</th>
116 <th>Description</th>
117 </tr>
118 <tr>
119 <td>Password enabled</td>
120 <td>Requires that devices ask for PIN or passwords.</td>
121 </tr>
122 <tr>
123 <td>Minimum password length</td>
124 <td>Set the required number of characters for the password. For example, you
125can require PIN or passwords to have at least six characters. </td> </tr>
126 <tr>
Scott Main50f86c62010-10-08 16:06:40 -0700127 <td>Alphanumeric password required</td>
Katie McCormick262b4312010-09-30 16:27:25 -0700128 <td>Requires that passwords have a
129combination of letters and numbers. They may include symbolic characters.
130 </td>
131 </tr>
Katie McCormick62922c32011-04-28 17:05:17 -0700132
133 <tr>
134 <td>Complex password required</td>
135 <td>Requires that passwords must contain at least a letter, a numerical digit, and a special symbol. Introduced in Android 3.0.
136 </td>
137 </tr>
138
139<tr>
140 <td>Minimum letters required in password</td> <td>The minimum number of
141letters required in the password for all admins or a particular one. Introduced in Android 3.0.</td>
142</tr>
143
144
145 <tr>
146 <td>Minimum lowercase letters required in password</td>
147 <td>The minimum number of lowercase
148letters required in the password for all admins or a particular one. Introduced in Android 3.0.</td>
149</tr>
150
151 <tr>
152 <td>Minimum non-letter characters required in password</td>
153 <td>The minimum number of
154non-letter characters required in the password for all admins or a particular one. Introduced in Android 3.0.</td>
155</tr>
156
157<tr>
158 <td>Minimum numerical digits required in password</td>
159 <td>The minimum number of numerical digits required in the password for all admins or a particular one. Introduced in Android 3.0.</td>
160</tr>
161
162<tr>
163 <td>Minimum symbols required in password</td>
164 <td>The minimum number of symbols required in the password for all admins or a particular one. Introduced in Android 3.0.</td>
165</tr>
166
167<tr>
168 <td>Minimum uppercase letters required in password</td>
169 <td>The minimum number of uppercase letters required in the password for all admins or a particular one. Introduced in Android 3.0.</td>
170</tr>
171
172<tr>
173 <td>Password expiration timeout</td>
174 <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>
175</tr>
176
177<tr>
178 <td>Password history restriction</td>
179 <td>This policy prevents users from reusing the last <em>n</em> unique passwords.
180 This policy is typically used in conjunction with
181{@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()}, which forces
182users to update their passwords after a specified amount of time has elapsed.
183Introduced in Android 3.0.</td>
184</tr>
185
Katie McCormick262b4312010-09-30 16:27:25 -0700186 <tr>
187 <td>Maximum failed password attempts </td>
188 <td>Specifies how many times a user can enter the wrong password before the
189device wipes its data. The Device Administration API also allows administrators to
190remotely reset the device to factory defaults. This secures data in case the
191device is lost or stolen.</td>
192 </tr>
193 <tr>
194 <td>Maximum inactivity time lock</td>
195 <td>Sets the length of time since the user last touched the screen or
196pressed a button before the device locks the screen. When this happens, users
197need to enter their PIN or passwords again before they can use their devices and
198access data. The value can be between 1 and 60 minutes.</td> </tr>
Katie McCormick62922c32011-04-28 17:05:17 -0700199
200<tr>
201<td>Require storage encryption</td>
202<td>Specifies that the storage area should be encrypted, if the device supports it.
203Introduced in Android 3.0.</td> </tr>
204
Katie McCormick6c5951b2011-10-28 14:15:41 -0700205<tr>
206 <td>Disable camera</td>
207
208 <td>Specifies that the camera should be disabled. Note that this doesn't have
209to be a permanent disabling. The camera can be enabled/disabled dynamically
210based on context, time, and so on. Introduced in Android 4.0.</td>
211
212</tr>
213
214
Katie McCormick262b4312010-09-30 16:27:25 -0700215</table>
Scott Main50f86c62010-10-08 16:06:40 -0700216
217<h4>Other features</h4>
218
Katie McCormick262b4312010-09-30 16:27:25 -0700219<p>In addition to supporting the policies listed in the above table, the Device
220Administration API lets you do the following:</p> <ul>
221 <li>Prompt user to set a new password.</li>
222 <li>Lock device immediately.</li>
223 <li>Wipe the device's data (that is, restore the device to its factory defaults).</li>
224</ul>
225
226
227<h2 id="sample">Sample Application</h2>
Scott Main50f86c62010-10-08 16:06:40 -0700228
Scott Rowebdb05bd2014-08-07 15:38:56 -0700229<p>The examples used in this document are based on the Device Administration API
230sample, which is included in the SDK samples (available through the
231Android SDK Manager) and located on your system as
232<code>&lt;sdk_root&gt;/ApiDemos/app/src/main/java/com/example/android/apis/app/DeviceAdminSample.java</code>.</p>
233
234<p>The sample application offers a demo of device admin features. It presents users
Katie McCormick262b4312010-09-30 16:27:25 -0700235with a user interface that lets them enable the device admin application. Once
236they've enabled the application, they can use the buttons in the user interface
237to do the following:</p>
238<ul>
239 <li>Set password quality.</li>
Katie McCormick62922c32011-04-28 17:05:17 -0700240 <li>Specify requirements for the user's password, such as minimum length, the minimum number of
241 numeric characters it must contain, and so on.</li>
Katie McCormick262b4312010-09-30 16:27:25 -0700242 <li>Set the password. If the password does not conform to the specified
243policies, the system returns an error.</li>
244 <li>Set how many failed password attempts can occur before the device is wiped
245(that is, restored to factory settings).</li>
Katie McCormick62922c32011-04-28 17:05:17 -0700246<li>Set how long from now the password will expire.</li>
247<li>Set the password history length (<em>length</em> refers to number of old passwords stored in the history).
248This prevents users from reusing
249one of the last <em>n</em> passwords they previously used.</li>
250<li>Specify that the storage area should be encrypted, if the device supports it.</li>
Katie McCormick262b4312010-09-30 16:27:25 -0700251 <li>Set the maximum amount of inactive time that can elapse before the device
252locks.</li>
253 <li>Make the device lock immediately.</li>
254 <li>Wipe the device's data (that is, restore factory settings).</li>
Katie McCormick6c5951b2011-10-28 14:15:41 -0700255 <li>Disable the camera.</li>
Katie McCormick62922c32011-04-28 17:05:17 -0700256
Katie McCormick262b4312010-09-30 16:27:25 -0700257</ul>
258
Katie McCormick62922c32011-04-28 17:05:17 -0700259
260
Katie McCormick262b4312010-09-30 16:27:25 -0700261<img src="{@docRoot}images/admin/device-admin-app.png"/>
Katie McCormick62922c32011-04-28 17:05:17 -0700262
Katie McCormick262b4312010-09-30 16:27:25 -0700263<p class="img-caption"><strong>Figure 1.</strong> Screenshot of the Sample Application</p>
264
Scott Main50f86c62010-10-08 16:06:40 -0700265
266
Katie McCormick262b4312010-09-30 16:27:25 -0700267<h2 id="developing">Developing a Device Administration Application</h2>
268
269<p>System administrators can use the Device Administration API to write an application
270that enforces remote/local device security policy enforcement. This section
271summarizes the steps involved in creating a device administration
272application.</p>
Scott Main50f86c62010-10-08 16:06:40 -0700273
274<h3 id="manifest">Creating the manifest</h3>
275
Katie McCormick262b4312010-09-30 16:27:25 -0700276<p>To use the Device Administration API, the application's
277manifest must include the following:</p>
278<ul>
279 <li>A subclass of {@link android.app.admin.DeviceAdminReceiver} that includes the following:
280 <ul>
281 <li>The {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission.</li>
282 <li>The ability to respond to the {@link android.app.admin.DeviceAdminReceiver#ACTION_DEVICE_ADMIN_ENABLED}
283intent, expressed in the manifest as an intent filter.</li>
284 </ul>
285 </li>
286 <li>A declaration of security policies used in metadata.</li>
287</ul>
288<p>Here is an excerpt from the Device Administration sample manifest:</p>
Katie McCormick6c5951b2011-10-28 14:15:41 -0700289<pre>&lt;activity android:name=&quot;.app.DeviceAdminSample&quot;
290 android:label=&quot;&#64;string/activity_sample_device_admin&quot;&gt;
291 &lt;intent-filter&gt;
292 &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
293 &lt;category android:name=&quot;android.intent.category.SAMPLE_CODE&quot; /&gt;
294 &lt;/intent-filter&gt;
Katie McCormick262b4312010-09-30 16:27:25 -0700295&lt;/activity&gt;
Katie McCormick6c5951b2011-10-28 14:15:41 -0700296&lt;receiver android:name=&quot;.app.DeviceAdminSample$DeviceAdminSampleReceiver&quot;
297 android:label=&quot;&#64;string/sample_device_admin&quot;
298 android:description=&quot;&#64;string/sample_device_admin_description&quot;
299 android:permission=&quot;android.permission.BIND_DEVICE_ADMIN&quot;&gt;
300 &lt;meta-data android:name=&quot;android.app.device_admin&quot;
301 android:resource=&quot;&#64;xml/device_admin_sample&quot; /&gt;
302 &lt;intent-filter&gt;
303 &lt;action android:name=&quot;android.app.action.DEVICE_ADMIN_ENABLED&quot; /&gt;
304 &lt;/intent-filter&gt;
Katie McCormick262b4312010-09-30 16:27:25 -0700305&lt;/receiver&gt;</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700306
Katie McCormick262b4312010-09-30 16:27:25 -0700307 <p>Note that:</p>
308<ul>
Katie McCormick262b4312010-09-30 16:27:25 -0700309<li>The following attributes refer to string resources that for the sample application reside in
Scott Main50f86c62010-10-08 16:06:40 -0700310<code>ApiDemos/res/values/strings.xml</code>. For more information about resources, see
Katie McCormick262b4312010-09-30 16:27:25 -0700311<a
312href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.
313<ul>
Katie McCormick6c5951b2011-10-28 14:15:41 -0700314<li><code>android:label=&quot;&#64;string/activity_sample_device_admin&quot;</code> refers to the
Katie McCormick262b4312010-09-30 16:27:25 -0700315user-readable label for the activity.</li>
316
Katie McCormick6c5951b2011-10-28 14:15:41 -0700317<li><code>android:label=&quot;&#64;string/sample_device_admin&quot;</code> refers to the
Katie McCormick262b4312010-09-30 16:27:25 -0700318user-readable label for the permission.</li>
319
Katie McCormick6c5951b2011-10-28 14:15:41 -0700320<li><code>android:description=&quot;&#64;string/sample_device_admin_description&quot;</code> refers to
Katie McCormick262b4312010-09-30 16:27:25 -0700321the user-readable description of the permission. A descripton is typically longer and more
322informative than
323a label.</li>
324</ul>
325
Scott Main50f86c62010-10-08 16:06:40 -0700326
327<li><code>android:permission=&quot;android.permission.BIND_DEVICE_ADMIN&quot;
Katie McCormick262b4312010-09-30 16:27:25 -0700328</code> is a permission that a {@link android.app.admin.DeviceAdminReceiver} subclass must
329have, to ensure that only the system can interact with the receiver (no application can be granted this permission). This
330prevents other applications from abusing your device admin app.</li>
kmccormick76dfc022013-04-03 12:41:12 -0700331<li><code>android.app.action.DEVICE_ADMIN_ENABLED</code> is the primary
Katie McCormick262b4312010-09-30 16:27:25 -0700332action that a {@link android.app.admin.DeviceAdminReceiver} subclass must handle to be
333allowed to manage a device. This is set to the receiver when the user enables
334the device admin app. Your code typically handles this in
335{@link android.app.admin.DeviceAdminReceiver#onEnabled onEnabled()}. To be supported, the receiver must also
336require the {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission so that other applications
337cannot abuse it. </li>
338<li>When a user enables the device admin application, that gives the receiver
339permission to perform actions in response to the broadcast of particular system
340events. When suitable event arises, the application can impose a policy. For
341example, if the user attempts to set a new password that doesn't meet the policy
342requirements, the application can prompt the user to pick a different password
343that does meet the requirements.</li>
344
345 <li><code>android:resource=&quot;&#64;xml/device_admin_sample&quot;</code>
346declares the security policies used in metadata. The metadata provides additional
347information specific to the device administrator, as parsed by the {@link
348android.app.admin.DeviceAdminInfo} class. Here are the contents of
349<code>device_admin_sample.xml</code>:</li>
350</ul>
351<pre>&lt;device-admin xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
352 &lt;uses-policies&gt;
353 &lt;limit-password /&gt;
354 &lt;watch-login /&gt;
355 &lt;reset-password /&gt;
356 &lt;force-lock /&gt;
357 &lt;wipe-data /&gt;
Katie McCormick6c5951b2011-10-28 14:15:41 -0700358 &lt;expire-password /&gt;
359 &lt;encrypted-storage /&gt;
360 &lt;disable-camera /&gt;
Katie McCormick262b4312010-09-30 16:27:25 -0700361 &lt;/uses-policies&gt;
362&lt;/device-admin&gt;
363</pre>
364<p> In designing your device administration application, you don't need to
365include all of the policies, just the ones that are relevant for your app.
366</p>
367For more discussion of the manifest file, see the <a
Scott Main50f86c62010-10-08 16:06:40 -0700368href="{@docRoot}guide/topics/manifest/manifest-intro.html">Android Developers Guide</a>.
369
370
371
372<h3 id="code">Implementing the code</h3>
373
Katie McCormick262b4312010-09-30 16:27:25 -0700374<p>The Device Administration API includes the following classes:</p>
375<dl>
Scott Main50f86c62010-10-08 16:06:40 -0700376 <dt>{@link android.app.admin.DeviceAdminReceiver}</dt>
Katie McCormick262b4312010-09-30 16:27:25 -0700377 <dd>Base class for implementing a device administration component. This class provides
378a convenience for interpreting the raw intent actions that are sent by the
379system. Your Device Administration application must include a
380{@link android.app.admin.DeviceAdminReceiver} subclass.</dd>
381 <dt>{@link android.app.admin.DevicePolicyManager}</dt>
382<dd>A class for managing policies enforced on a device. Most clients of
383this class must have published a {@link android.app.admin.DeviceAdminReceiver} that the user
384has currently enabled. The {@link android.app.admin.DevicePolicyManager} manages policies for
385one or more {@link android.app.admin.DeviceAdminReceiver} instances</dd>
Scott Main50f86c62010-10-08 16:06:40 -0700386 <dt>{@link android.app.admin.DeviceAdminInfo}</dt>
Katie McCormick262b4312010-09-30 16:27:25 -0700387<dd>This class is used to specify metadata
388for a device administrator component.</dd>
389</dl>
390<p>These classes provide the foundation for a fully functional device administration application.
391The rest of this section describes how you use the {@link
392android.app.admin.DeviceAdminReceiver} and
393{@link android.app.admin.DevicePolicyManager} APIs to write a device admin application.</p>
Scott Main50f86c62010-10-08 16:06:40 -0700394
Katie McCormick262b4312010-09-30 16:27:25 -0700395<h4 id="receiver">Subclassing DeviceAdminReceiver</h4>
396<p>To create a device admin application, you must subclass
397{@link android.app.admin.DeviceAdminReceiver}. The {@link android.app.admin.DeviceAdminReceiver} class
398consists of a series of callbacks that are triggered when particular events
399occur.</p>
400<p>In its {@link android.app.admin.DeviceAdminReceiver} subclass, the sample application
401simply displays a {@link android.widget.Toast} notification in response to particular
402events. For example:</p>
403<pre>public class DeviceAdminSample extends DeviceAdminReceiver {
404
Katie McCormick6c5951b2011-10-28 14:15:41 -0700405 void showToast(Context context, String msg) {
406 String status = context.getString(R.string.admin_receiver_status, msg);
407 Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
408 }
409
Katie McCormick262b4312010-09-30 16:27:25 -0700410 &#64;Override
Katie McCormick6c5951b2011-10-28 14:15:41 -0700411 public void onEnabled(Context context, Intent intent) {
412 showToast(context, context.getString(R.string.admin_receiver_status_enabled));
413 }
Katie McCormick262b4312010-09-30 16:27:25 -0700414
Katie McCormick6c5951b2011-10-28 14:15:41 -0700415 &#64;Override
416 public CharSequence onDisableRequested(Context context, Intent intent) {
417 return context.getString(R.string.admin_receiver_status_disable_warning);
418 }
Katie McCormick262b4312010-09-30 16:27:25 -0700419
Katie McCormick6c5951b2011-10-28 14:15:41 -0700420 &#64;Override
421 public void onDisabled(Context context, Intent intent) {
422 showToast(context, context.getString(R.string.admin_receiver_status_disabled));
423 }
Katie McCormick262b4312010-09-30 16:27:25 -0700424
Katie McCormick6c5951b2011-10-28 14:15:41 -0700425 &#64;Override
426 public void onPasswordChanged(Context context, Intent intent) {
427 showToast(context, context.getString(R.string.admin_receiver_status_pw_changed));
Katie McCormick262b4312010-09-30 16:27:25 -0700428 }
429...
430}</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700431
Katie McCormick6c5951b2011-10-28 14:15:41 -0700432
Scott Main50f86c62010-10-08 16:06:40 -0700433<h4 id="enabling">Enabling the application</h4>
Katie McCormick262b4312010-09-30 16:27:25 -0700434<p>One of the major events a device admin application has to handle is the user
435enabling the application. The user must explicitly enable the application for
436the policies to be enforced. If the user chooses not to enable the application
437it will still be present on the device, but its policies will not be enforced, and the user will not
438get any of the application's benefits.</p>
439<p>The process of enabling the application begins when the user performs an
Scott Main50f86c62010-10-08 16:06:40 -0700440action that triggers the {@link android.app.admin.DevicePolicyManager#ACTION_ADD_DEVICE_ADMIN}
Katie McCormick262b4312010-09-30 16:27:25 -0700441intent. In the
442sample application, this happens when the user clicks the <strong>Enable
Katie McCormick6c5951b2011-10-28 14:15:41 -0700443Admin</strong> checkbox. </p>
444<p>When the user clicks the <strong>Enable Admin</strong> checkbox, the display
445changes to prompt the user to activate the device admin application, as shown in figure
Scott Main50f86c62010-10-08 16:06:40 -07004462.</p>
Katie McCormick262b4312010-09-30 16:27:25 -0700447
448<img src="{@docRoot}images/admin/device-admin-activate-prompt.png"/>
449<p class="img-caption"><strong>Figure 2.</strong> Sample Application: Activating the Application</p>
Katie McCormick262b4312010-09-30 16:27:25 -0700450
Katie McCormick6c5951b2011-10-28 14:15:41 -0700451<p>Below is the code that gets executed when the user clicks the <strong>Enable Admin</strong> checkbox. This has the effect of triggering the
452{@link android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(android.preference.Preference, java.lang.Object) onPreferenceChange()}
453callback. This callback is invoked when the value of this {@link android.preference.Preference} has been changed by the user and is about to be set and/or persisted. If the user is enabling the application, the display
454changes to prompt the user to activate the device admin application, as shown in figure
4552. Otherwise, the device admin application is disabled. </p>
Katie McCormick262b4312010-09-30 16:27:25 -0700456
Katie McCormick6c5951b2011-10-28 14:15:41 -0700457<pre>&#64;Override
458 public boolean onPreferenceChange(Preference preference, Object newValue) {
459 if (super.onPreferenceChange(preference, newValue)) {
460 return true;
461 }
462 boolean value = (Boolean) newValue;
463 if (preference == mEnableCheckbox) {
464 if (value != mAdminActive) {
465 if (value) {
466 // Launch the activity to have the user enable our admin.
467 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
468 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
469 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
470 mActivity.getString(R.string.add_admin_extra_app_text));
471 startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
472 // return false - don't update checkbox until we're really active
473 return false;
474 } else {
475 mDPM.removeActiveAdmin(mDeviceAdminSample);
476 enableDeviceCapabilitiesArea(false);
477 mAdminActive = false;
478 }
479 }
480 } else if (preference == mDisableCameraCheckbox) {
481 mDPM.setCameraDisabled(mDeviceAdminSample, value);
482 ...
483 }
484 return true;
485 }</pre>
486
Katie McCormick262b4312010-09-30 16:27:25 -0700487
488<p>The line
Scott Main50f86c62010-10-08 16:06:40 -0700489<code>intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
Katie McCormick262b4312010-09-30 16:27:25 -0700490mDeviceAdminSample)</code> states that <code>mDeviceAdminSample</code> (which is
491a {@link android.app.admin.DeviceAdminReceiver} component) is the target policy.
Scott Main50f86c62010-10-08 16:06:40 -0700492This line invokes the user interface shown in figure 2, which guides users through
Katie McCormick262b4312010-09-30 16:27:25 -0700493adding the device administrator to the system (or allows them to reject it).</p>
494
495<p>When the application needs to perform an operation that is contingent on the
496device admin application being enabled, it confirms that the application is
497active. To do this it uses the {@link android.app.admin.DevicePolicyManager} method
498{@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()}. Notice that the {@link android.app.admin.DevicePolicyManager}
499method {@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()} takes a {@link android.app.admin.DeviceAdminReceiver}
500component as its argument:</p>
Katie McCormick6c5951b2011-10-28 14:15:41 -0700501
Katie McCormick262b4312010-09-30 16:27:25 -0700502<pre>
503DevicePolicyManager mDPM;
504...
Katie McCormick6c5951b2011-10-28 14:15:41 -0700505private boolean isActiveAdmin() {
506 return mDPM.isAdminActive(mDeviceAdminSample);
Katie McCormick262b4312010-09-30 16:27:25 -0700507}
508</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700509
Katie McCormick6c5951b2011-10-28 14:15:41 -0700510
511
Scott Main50f86c62010-10-08 16:06:40 -0700512<h3 id="admin_ops">Managing policies</h3>
Katie McCormick262b4312010-09-30 16:27:25 -0700513<p>{@link android.app.admin.DevicePolicyManager} is a public class for managing policies
514enforced on a device. {@link android.app.admin.DevicePolicyManager} manages policies for one
515or more {@link android.app.admin.DeviceAdminReceiver} instances. </p>
516<p>You get a handle to the {@link android.app.admin.DevicePolicyManager} as follows: </p>
Scott Main50f86c62010-10-08 16:06:40 -0700517<pre>
518DevicePolicyManager mDPM =
519 (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
520</pre>
Katie McCormick262b4312010-09-30 16:27:25 -0700521<p>This section describes how to use {@link android.app.admin.DevicePolicyManager} to perform
522 administrative tasks:</p>
523<ul>
524 <li><a href="#pwd">Set password policies</a></li>
525 <li><a href="#lock">Set device lock</a></li>
526 <li><a href="#wipe">Perform data wipe</a></li>
527</ul>
Scott Main50f86c62010-10-08 16:06:40 -0700528
Katie McCormick262b4312010-09-30 16:27:25 -0700529<h4 id="pwd">Set password policies</h4>
530<p>{@link android.app.admin.DevicePolicyManager} includes APIs for setting and enforcing the
531device password policy. In the Device Administration API, the password only applies to
532screen lock. This section describes common password-related tasks.</p>
Scott Main50f86c62010-10-08 16:06:40 -0700533
Katie McCormick262b4312010-09-30 16:27:25 -0700534<h5>Set a password for the device</h5>
535<p>This code displays a user interface prompting the user to set a password:</p>
536<pre>Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
537startActivity(intent);
538</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700539
Katie McCormick262b4312010-09-30 16:27:25 -0700540<h5>Set the password quality</h5>
541<p>The password quality can be one of the following {@link android.app.admin.DevicePolicyManager} constants: </p>
542<dl>
Scott Main50f86c62010-10-08 16:06:40 -0700543 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_ALPHABETIC}</dt><dd>The user must enter a
Katie McCormick262b4312010-09-30 16:27:25 -0700544password containing at least alphabetic (or other symbol) characters.</dd>
Scott Main50f86c62010-10-08 16:06:40 -0700545 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_ALPHANUMERIC}</dt><dd>The user must enter a
Katie McCormick262b4312010-09-30 16:27:25 -0700546password containing at least <em>both</em> numeric <em>and</em> alphabetic (or
547other symbol) characters.</dd>
548 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_NUMERIC}</dt><dd>The user must enter a password
549containing at least numeric characters.</dd>
Katie McCormick62922c32011-04-28 17:05:17 -0700550<dt>{@link
551android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_COMPLEX}</dt><dd>The user
552must have entered a password containing at least a letter, a numerical digit and
553a special symbol.</dd>
554<dt>{@link
555android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_SOMETHING}</dt><dd>The
556policy requires some kind
Katie McCormick262b4312010-09-30 16:27:25 -0700557of password, but doesn't care what it is.</dd>
558 <dt>{@link android.app.admin.DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}</dt><dd>
559 The policy has no requirements for the password. </dd>
560</dl>
561<p>For example, this is how you would set the password policy to require an alphanumeric password:</p>
562<pre>
563DevicePolicyManager mDPM;
564ComponentName mDeviceAdminSample;
565...
566mDPM.setPasswordQuality(mDeviceAdminSample, DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
567</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700568
Katie McCormick62922c32011-04-28 17:05:17 -0700569<h5>Set password content requirements</h5>
570
571<p>Beginning with Android 3.0, the {@link android.app.admin.DevicePolicyManager} class
572includes methods that let you fine-tune the contents of the password. For
573example, you could set a policy that states that passwords must contain at least
574<em>n</em> uppercase letters. Here are the methods for fine-tuning a password's
575contents:</p>
576<ul>
577
578<li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumLetters(android.content.ComponentName,int) setPasswordMinimumLetters()}</li>
579
580<li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumLowerCase(android.content.ComponentName,int) setPasswordMinimumLowerCase()}</li>
581
582<li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumUpperCase(android.content.ComponentName,int) setPasswordMinimumUpperCase()}</li>
583
584<li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumNonLetter(android.content.ComponentName,int) setPasswordMinimumNonLetter()}</li>
585
586<li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumNumeric(android.content.ComponentName,int) setPasswordMinimumNumeric()}</li>
587
588<li>{@link android.app.admin.DevicePolicyManager#setPasswordMinimumSymbols(android.content.ComponentName,int) setPasswordMinimumSymbols()}</li>
589</ul>
590<p>For example, this snippet states that the password must have at least 2 uppercase letters:</p>
591<pre>
592DevicePolicyManager mDPM;
593ComponentName mDeviceAdminSample;
594int pwMinUppercase = 2;
595...
596mDPM.setPasswordMinimumUpperCase(mDeviceAdminSample, pwMinUppercase);</pre>
597
598
Katie McCormick262b4312010-09-30 16:27:25 -0700599<h5>Set the minimum password length</h5>
600<p>You can specify that a password must be at least the specified minimum
601length. For example:</p>
602<pre>DevicePolicyManager mDPM;
603ComponentName mDeviceAdminSample;
604int pwLength;
605...
606mDPM.setPasswordMinimumLength(mDeviceAdminSample, pwLength);
607</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700608
Katie McCormick262b4312010-09-30 16:27:25 -0700609<h5>Set maximum failed password attempts</h5>
610<p>You can set the maximum number of allowed failed password attempts before the
611device is wiped (that is, reset to factory settings). For example:</p>
Scott Main50f86c62010-10-08 16:06:40 -0700612<pre>DevicePolicyManager mDPM;
613ComponentName mDeviceAdminSample;
Katie McCormick262b4312010-09-30 16:27:25 -0700614int maxFailedPw;
615 ...
616mDPM.setMaximumFailedPasswordsForWipe(mDeviceAdminSample, maxFailedPw);</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700617
Katie McCormick62922c32011-04-28 17:05:17 -0700618<h5 id="expiration">Set password expiration timeout</h5>
619<p>Beginning with Android 3.0, you can use the
620{@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()}
621method 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>
622
623<pre>DevicePolicyManager mDPM;
624ComponentName mDeviceAdminSample;
625long pwExpiration;
626...
627mDPM.setPasswordExpirationTimeout(mDeviceAdminSample, pwExpiration);
628</pre>
Katie McCormick62922c32011-04-28 17:05:17 -0700629
630<h5 id="history">Restrict password based on history</h5>
631
632<p>Beginning with Android 3.0, you can use the
633{@link android.app.admin.DevicePolicyManager#setPasswordHistoryLength(android.content.ComponentName,int) setPasswordHistoryLength()}
634method to limit users'
635ability to reuse old passwords. This method takes a <em>length</em>
636parameter, which specifies how many old
637passwords are stored. When this policy is active, users cannot enter a new
638password that matches the last <em>n</em> passwords. This prevents
639users from using the same password over and over. This policy is typically used
640in conjunction with
641{@link android.app.admin.DevicePolicyManager#setPasswordExpirationTimeout(android.content.ComponentName,long) setPasswordExpirationTimeout()},
642which forces users
643to update their passwords after a specified amount of time has elapsed. </p>
644
645<p>For example, this snippet prohibits users from reusing any of their last 5 passwords:</p>
646
647<pre>DevicePolicyManager mDPM;
648ComponentName mDeviceAdminSample;
649int pwHistoryLength = 5;
650...
651mDPM.setPasswordHistoryLength(mDeviceAdminSample, pwHistoryLength);
652</pre>
653
654<h4 id="lock">Set device lock</h4>
Katie McCormick262b4312010-09-30 16:27:25 -0700655<p>You can set the maximum period of user inactivity that can occur before the
656device locks. For example:</p>
657<pre>
Scott Main50f86c62010-10-08 16:06:40 -0700658DevicePolicyManager mDPM;
Katie McCormick262b4312010-09-30 16:27:25 -0700659ComponentName mDeviceAdminSample;
660...
661long timeMs = 1000L*Long.parseLong(mTimeout.getText().toString());
662mDPM.setMaximumTimeToLock(mDeviceAdminSample, timeMs);
663</pre>
664<p>You can also programmatically tell the device to lock immediately:</p>
665<pre>
Scott Main50f86c62010-10-08 16:06:40 -0700666DevicePolicyManager mDPM;
Scott Rowebdb05bd2014-08-07 15:38:56 -0700667mDPM.lockNow();
668</pre>
Scott Main50f86c62010-10-08 16:06:40 -0700669
Katie McCormick62922c32011-04-28 17:05:17 -0700670
671
Katie McCormick262b4312010-09-30 16:27:25 -0700672<h4 id="wipe">Perform data wipe</h4>
Scott Main50f86c62010-10-08 16:06:40 -0700673
Katie McCormick262b4312010-09-30 16:27:25 -0700674<p>You can use the {@link android.app.admin.DevicePolicyManager} method
675{@link android.app.admin.DevicePolicyManager#wipeData wipeData()} to reset the device to factory settings. This is useful
676if the device is lost or stolen. Often the decision to wipe the device is the
677result of certain conditions being met. For example, you can use
678{@link android.app.admin.DevicePolicyManager#setMaximumFailedPasswordsForWipe setMaximumFailedPasswordsForWipe()} to state that a device should be
679wiped after a specific number of failed password attempts.</p>
680<p>You wipe data as follows:</p>
681<pre>
Scott Main50f86c62010-10-08 16:06:40 -0700682DevicePolicyManager mDPM;
Katie McCormick262b4312010-09-30 16:27:25 -0700683mDPM.wipeData(0);</pre>
Scott Rowebdb05bd2014-08-07 15:38:56 -0700684<p>The {@link android.app.admin.DevicePolicyManager#wipeData wipeData()} method takes as its
685 parameter a bit mask of additional options. Currently the value must be 0. </p>
Katie McCormick62922c32011-04-28 17:05:17 -0700686
Katie McCormick6c5951b2011-10-28 14:15:41 -0700687<h4>Disable camera</h4>
688<p>Beginning with Android 4.0, you can disable the camera. Note that this doesn't have to be a permanent disabling. The camera can be enabled/disabled dynamically based on context, time, and so on. </p>
Scott Rowebdb05bd2014-08-07 15:38:56 -0700689<p>You control whether the camera is disabled by using the
Katie McCormick6c5951b2011-10-28 14:15:41 -0700690{@link android.app.admin.DevicePolicyManager#setCameraDisabled(android.content.ComponentName, boolean) setCameraDisabled()} method. For example, this snippet sets the camera to be enabled or disabled based on a checkbox setting:</p>
691
692<pre>private CheckBoxPreference mDisableCameraCheckbox;
693DevicePolicyManager mDPM;
694ComponentName mDeviceAdminSample;
695...
696mDPM.setCameraDisabled(mDeviceAdminSample, mDisableCameraCheckbox.isChecked());<br />
697</pre>
698
699
Scott Rowebdb05bd2014-08-07 15:38:56 -0700700<h4 id="storage">Storage encryption</h4>
701<p>Beginning with Android 3.0, you can use the
Katie McCormick62922c32011-04-28 17:05:17 -0700702{@link android.app.admin.DevicePolicyManager#setStorageEncryption(android.content.ComponentName,boolean) setStorageEncryption()}
703method to set a policy requiring encryption of the storage area, where supported.</p>
704
705<p>For example:</p>
706
707<pre>
708DevicePolicyManager mDPM;
709ComponentName mDeviceAdminSample;
710...
711mDPM.setStorageEncryption(mDeviceAdminSample, true);
712</pre>
713<p>
Scott Rowebdb05bd2014-08-07 15:38:56 -0700714See the Device Administration API sample for a complete example of how to enable storage encryption.
715</p>