Skip to content

Commit 6c5951b

Browse files
committed
Doc change: Updating device-admin with camera info.
Change-Id: I5a5bf50b1362346f0a245ee40248a62a3c94a879
1 parent 9514291 commit 6c5951b

File tree

3 files changed

+118
-129
lines changed

3 files changed

+118
-129
lines changed

docs/html/guide/topics/admin/device-admin.jd

Lines changed: 118 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ page.title=Device Administration
2727
<li>{@link android.app.admin.DevicePolicyManager}</li>
2828
<li>{@link android.app.admin.DeviceAdminInfo}</li>
2929
</ol>
30+
<h2>Related samples</h2>
31+
<ol>
32+
<li><a
33+
href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html">
34+
DeviceAdminSample</a></li>
35+
</ol>
3036
</div>
3137
</div>
3238

@@ -201,6 +207,16 @@ access data. The value can be between 1 and 60 minutes.</td> </tr>
201207
<td>Specifies that the storage area should be encrypted, if the device supports it.
202208
Introduced in Android 3.0.</td> </tr>
203209

210+
<tr>
211+
<td>Disable camera</td>
212+
213+
<td>Specifies that the camera should be disabled. Note that this doesn't have
214+
to be a permanent disabling. The camera can be enabled/disabled dynamically
215+
based on context, time, and so on. Introduced in Android 4.0.</td>
216+
217+
</tr>
218+
219+
204220
</table>
205221

206222
<h4>Other features</h4>
@@ -247,6 +263,7 @@ one of the last <em>n</em> passwords they previously used.</li>
247263
locks.</li>
248264
<li>Make the device lock immediately.</li>
249265
<li>Wipe the device's data (that is, restore factory settings).</li>
266+
<li>Disable the camera.</li>
250267

251268
</ul>
252269

@@ -280,46 +297,38 @@ intent, expressed in the manifest as an intent filter.</li>
280297
<li>A declaration of security policies used in metadata.</li>
281298
</ul>
282299
<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;
300+
<pre>&lt;activity android:name=&quot;.app.DeviceAdminSample&quot;
301+
android:label=&quot;&#64;string/activity_sample_device_admin&quot;&gt;
302+
&lt;intent-filter&gt;
303+
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
304+
&lt;category android:name=&quot;android.intent.category.SAMPLE_CODE&quot; /&gt;
305+
&lt;/intent-filter&gt;
289306
&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;
307+
&lt;receiver android:name=&quot;.app.DeviceAdminSample$DeviceAdminSampleReceiver&quot;
308+
android:label=&quot;&#64;string/sample_device_admin&quot;
309+
android:description=&quot;&#64;string/sample_device_admin_description&quot;
310+
android:permission=&quot;android.permission.BIND_DEVICE_ADMIN&quot;&gt;
311+
&lt;meta-data android:name=&quot;android.app.device_admin&quot;
312+
android:resource=&quot;&#64;xml/device_admin_sample&quot; /&gt;
313+
&lt;intent-filter&gt;
314+
&lt;action android:name=&quot;android.app.action.DEVICE_ADMIN_ENABLED&quot; /&gt;
315+
&lt;/intent-filter&gt;
300316
&lt;/receiver&gt;</pre>
301317

302318
<p>Note that:</p>
303319
<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>&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
309-
an inner class; it just is in this example.</li>
310-
311320
<li>The following attributes refer to string resources that for the sample application reside in
312321
<code>ApiDemos/res/values/strings.xml</code>. For more information about resources, see
313322
<a
314323
href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.
315324
<ul>
316-
<li><code>android:label=&quot;@string/activity_sample_device_admin&quot;</code> refers to the
325+
<li><code>android:label=&quot;&#64;string/activity_sample_device_admin&quot;</code> refers to the
317326
user-readable label for the activity.</li>
318327

319-
<li><code>android:label=&quot;@string/sample_device_admin&quot;</code> refers to the
328+
<li><code>android:label=&quot;&#64;string/sample_device_admin&quot;</code> refers to the
320329
user-readable label for the permission.</li>
321330

322-
<li><code>android:description=&quot;@string/sample_device_admin_description&quot;</code> refers to
331+
<li><code>android:description=&quot;&#64;string/sample_device_admin_description&quot;</code> refers to
323332
the user-readable description of the permission. A descripton is typically longer and more
324333
informative than
325334
a label.</li>
@@ -357,6 +366,9 @@ android.app.admin.DeviceAdminInfo} class. Here are the contents of
357366
&lt;reset-password /&gt;
358367
&lt;force-lock /&gt;
359368
&lt;wipe-data /&gt;
369+
&lt;expire-password /&gt;
370+
&lt;encrypted-storage /&gt;
371+
&lt;disable-camera /&gt;
360372
&lt;/uses-policies&gt;
361373
&lt;/device-admin&gt;
362374
</pre>
@@ -401,33 +413,34 @@ simply displays a {@link android.widget.Toast} notification in response to parti
401413
events. For example:</p>
402414
<pre>public class DeviceAdminSample extends DeviceAdminReceiver {
403415

404-
...
416+
void showToast(Context context, String msg) {
417+
String status = context.getString(R.string.admin_receiver_status, msg);
418+
Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
419+
}
420+
421+
&#64;Override
422+
public void onEnabled(Context context, Intent intent) {
423+
showToast(context, context.getString(R.string.admin_receiver_status_enabled));
424+
}
425+
405426
&#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+
public CharSequence onDisableRequested(Context context, Intent intent) {
428+
return context.getString(R.string.admin_receiver_status_disable_warning);
429+
}
430+
431+
&#64;Override
432+
public void onDisabled(Context context, Intent intent) {
433+
showToast(context, context.getString(R.string.admin_receiver_status_disabled));
434+
}
435+
436+
&#64;Override
437+
public void onPasswordChanged(Context context, Intent intent) {
438+
showToast(context, context.getString(R.string.admin_receiver_status_pw_changed));
427439
}
428440
...
429441
}</pre>
430442

443+
431444
<h4 id="enabling">Enabling the application</h4>
432445
<p>One of the major events a device admin application has to handle is the user
433446
enabling the application. The user must explicitly enable the application for
@@ -438,43 +451,50 @@ get any of the application's benefits.</p>
438451
action that triggers the {@link android.app.admin.DevicePolicyManager#ACTION_ADD_DEVICE_ADMIN}
439452
intent. In the
440453
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
454+
Admin</strong> checkbox. </p>
455+
<p>When the user clicks the <strong>Enable Admin</strong> checkbox, the display
456+
changes to prompt the user to activate the device admin application, as shown in figure
444457
2.</p>
445458

446459
<img src="{@docRoot}images/admin/device-admin-activate-prompt.png"/>
447460
<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-
               &quot;Additional text explaining why this needs to be added.&quot;);
459-
        startActivityForResult(intent, RESULT_ENABLE);
460-
}
461-
};
462461

463-
...
464-
// This code checks whether the device admin app was successfully enabled.
465-
&#64;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(&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>
462+
<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
463+
{@link android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(android.preference.Preference, java.lang.Object) onPreferenceChange()}
464+
callback. 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
465+
changes to prompt the user to activate the device admin application, as shown in figure
466+
2. Otherwise, the device admin application is disabled. </p>
467+
468+
<pre>&#64;Override
469+
public boolean onPreferenceChange(Preference preference, Object newValue) {
470+
if (super.onPreferenceChange(preference, newValue)) {
471+
return true;
472+
}
473+
boolean value = (Boolean) newValue;
474+
if (preference == mEnableCheckbox) {
475+
if (value != mAdminActive) {
476+
if (value) {
477+
// Launch the activity to have the user enable our admin.
478+
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
479+
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
480+
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
481+
mActivity.getString(R.string.add_admin_extra_app_text));
482+
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
483+
// return false - don't update checkbox until we're really active
484+
return false;
485+
} else {
486+
mDPM.removeActiveAdmin(mDeviceAdminSample);
487+
enableDeviceCapabilitiesArea(false);
488+
mAdminActive = false;
489+
}
490+
}
491+
} else if (preference == mDisableCameraCheckbox) {
492+
mDPM.setCameraDisabled(mDeviceAdminSample, value);
493+
...
494+
}
495+
return true;
496+
}</pre>
497+
478498

479499
<p>The line
480500
<code>intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
@@ -489,18 +509,17 @@ active. To do this it uses the {@link android.app.admin.DevicePolicyManager} met
489509
{@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()}. Notice that the {@link android.app.admin.DevicePolicyManager}
490510
method {@link android.app.admin.DevicePolicyManager#isAdminActive(android.content.ComponentName) isAdminActive()} takes a {@link android.app.admin.DeviceAdminReceiver}
491511
component as its argument:</p>
512+
492513
<pre>
493514
DevicePolicyManager mDPM;
494515
...
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
516+
private boolean isActiveAdmin() {
517+
return mDPM.isAdminActive(mDeviceAdminSample);
501518
}
502519
</pre>
503520

521+
522+
504523
<h3 id="admin_ops">Managing policies</h3>
505524
<p>{@link android.app.admin.DevicePolicyManager} is a public class for managing policies
506525
enforced on a device. {@link android.app.admin.DevicePolicyManager} manages policies for one
@@ -618,49 +637,6 @@ long pwExpiration;
618637
...
619638
mDPM.setPasswordExpirationTimeout(mDeviceAdminSample, pwExpiration);
620639
</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 &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>
664640

665641
<h5 id="history">Restrict password based on history</h5>
666642

@@ -718,6 +694,19 @@ mDPM.wipeData(0);</pre>
718694
<p>The {@link android.app.admin.DevicePolicyManager#wipeData wipeData()} method takes as its parameter a bit mask of
719695
additional options. Currently the value must be 0. </p>
720696

697+
<h4>Disable camera</h4>
698+
<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>
699+
<p>You control whether the camera is disabled by using the
700+
{@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>
701+
702+
<pre>private CheckBoxPreference mDisableCameraCheckbox;
703+
DevicePolicyManager mDPM;
704+
ComponentName mDeviceAdminSample;
705+
...
706+
mDPM.setCameraDisabled(mDeviceAdminSample, mDisableCameraCheckbox.isChecked());<br />
707+
</pre>
708+
709+
721710
<h4 id=storage">Storage encryption</h4>
722711
<p>Beginning with Android 3.0, you can use the
723712
{@link android.app.admin.DevicePolicyManager#setStorageEncryption(android.content.ComponentName,boolean) setStorageEncryption()}
-522 Bytes
Loading
-261 KB
Loading

0 commit comments

Comments
 (0)