API sample for using new disallow camera policy.

Change-Id: Ic83b2079312860a3c4b9348ca28dc7d3c0a2b9e6
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 54be31d..5f9e990 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -619,6 +619,10 @@
     <string name="header_encryption">Encryption</string>
 
     <string name="enable_admin">Enable admin</string>
+    <string name="device_capabilities_category">Device capabilities</string>
+    <string name="disable_camera">Disable all device cameras</string>
+    <string name="camera_disabled">Device cameras disabled</string>
+    <string name="camera_enabled">Device cameras enabled</string>
     <string name="password_controls_category">Password controls</string>
     <string name="set_password_user">Set password (user)</string>
     <string name="set_password_api">Set password (via API)</string>
diff --git a/samples/ApiDemos/res/xml/device_admin_general.xml b/samples/ApiDemos/res/xml/device_admin_general.xml
index ef46238..61ccd32 100644
--- a/samples/ApiDemos/res/xml/device_admin_general.xml
+++ b/samples/ApiDemos/res/xml/device_admin_general.xml
@@ -28,4 +28,13 @@
 
     </PreferenceCategory>
 
+    <PreferenceCategory
+        android:title="@string/device_capabilities_category" >
+
+        <CheckBoxPreference
+            android:key="key_disable_camera"
+            android:title="@string/disable_camera" />
+
+    </PreferenceCategory>
+
 </PreferenceScreen>
diff --git a/samples/ApiDemos/res/xml/device_admin_sample.xml b/samples/ApiDemos/res/xml/device_admin_sample.xml
index 10edb7e..2468919 100644
--- a/samples/ApiDemos/res/xml/device_admin_sample.xml
+++ b/samples/ApiDemos/res/xml/device_admin_sample.xml
@@ -24,6 +24,7 @@
         <wipe-data />
         <expire-password />
         <encrypted-storage />
+        <disable-camera />
     </uses-policies>
 </device-admin>
 <!-- END_INCLUDE(meta_data) -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java b/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
index a9af983..324b8ce 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
@@ -67,6 +67,7 @@
 
     // The following keys are used to find each preference item
     private static final String KEY_ENABLE_ADMIN = "key_enable_admin";
+    private static final String KEY_DISABLE_CAMERA = "key_disable_camera";
 
     private static final String KEY_CATEGORY_QUALITY = "key_category_quality";
     private static final String KEY_SET_PASSWORD = "key_set_password";
@@ -243,6 +244,7 @@
             implements OnPreferenceChangeListener {
         // UI elements
         private CheckBoxPreference mEnableCheckbox;
+        private CheckBoxPreference mDisableCameraCheckbox;
 
         @Override
         public void onCreate(Bundle savedInstanceState) {
@@ -250,6 +252,8 @@
             addPreferencesFromResource(R.xml.device_admin_general);
             mEnableCheckbox = (CheckBoxPreference) findPreference(KEY_ENABLE_ADMIN);
             mEnableCheckbox.setOnPreferenceChangeListener(this);
+            mDisableCameraCheckbox = (CheckBoxPreference) findPreference(KEY_DISABLE_CAMERA);
+            mDisableCameraCheckbox.setOnPreferenceChangeListener(this);
         }
 
         // At onResume time, reload UI with current values as required
@@ -257,6 +261,12 @@
         public void onResume() {
             super.onResume();
             mEnableCheckbox.setChecked(mAdminActive);
+            enableDeviceCapabilitiesArea(mAdminActive);
+
+            if (mAdminActive) {
+                mDPM.setCameraDisabled(mDeviceAdminSample, mDisableCameraCheckbox.isChecked());
+                reloadSummaries();
+            }
         }
 
         @Override
@@ -264,10 +274,10 @@
             if (super.onPreferenceChange(preference, newValue)) {
                 return true;
             }
+            boolean value = (Boolean) newValue;
             if (preference == mEnableCheckbox) {
-                boolean newActive = (Boolean) newValue;
-                if (newActive != mAdminActive) {
-                    if (newActive) {
+                if (value != mAdminActive) {
+                    if (value) {
                         // Launch the activity to have the user enable our admin.
                         Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
@@ -278,12 +288,29 @@
                         return false;
                     } else {
                         mDPM.removeActiveAdmin(mDeviceAdminSample);
+                        enableDeviceCapabilitiesArea(false);
                         mAdminActive = false;
                     }
                 }
+            } else if (preference == mDisableCameraCheckbox) {
+                mDPM.setCameraDisabled(mDeviceAdminSample, value);
+                reloadSummaries();
             }
             return true;
         }
+
+        @Override
+        protected void reloadSummaries() {
+            super.reloadSummaries();
+            String cameraSummary = getString(mDPM.getCameraDisabled(mDeviceAdminSample)
+                    ? R.string.camera_disabled : R.string.camera_enabled);
+            mDisableCameraCheckbox.setSummary(cameraSummary);
+        }
+
+        /** Updates the device capabilities area (dis/enabling) as the admin is (de)activated */
+        private void enableDeviceCapabilitiesArea(boolean enabled) {
+            mDisableCameraCheckbox.setEnabled(enabled);
+        }
     }
 
     /**