Merge "Add ApiDemos for secure surfaces." into jb-mr1-dev
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index 6d13ce6..b7f6819 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -262,6 +262,30 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".app.SecureWindowActivity"
+                android:label="@string/activity_secure_window">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
+        <activity android:name=".app.SecureDialogActivity"
+                android:label="@string/activity_secure_dialog">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
+        <activity android:name=".app.SecureSurfaceViewActivity"
+                android:label="@string/activity_secure_surface_view">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <!-- Fragment Samples -->
 
         <activity android:name=".app.FragmentAlertDialog"
diff --git a/samples/ApiDemos/res/layout/secure_dialog_activity.xml b/samples/ApiDemos/res/layout/secure_dialog_activity.xml
new file mode 100644
index 0000000..00457a4
--- /dev/null
+++ b/samples/ApiDemos/res/layout/secure_dialog_activity.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- See corresponding Java code SecureDialogActivity.java. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical">
+
+    <!-- Message to show to use. -->
+    <TextView android:id="@+id/text"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="0"
+        android:gravity="center_vertical|center_horizontal"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="@string/secure_dialog_activity_text"/>
+
+    <!-- Button to show the dialog. -->
+    <Button android:id="@+id/show"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_horizontal"
+        android:text="@string/secure_dialog_show_button"/>
+</LinearLayout>
diff --git a/samples/ApiDemos/res/layout/secure_surface_view_activity.xml b/samples/ApiDemos/res/layout/secure_surface_view_activity.xml
new file mode 100644
index 0000000..b7657b4
--- /dev/null
+++ b/samples/ApiDemos/res/layout/secure_surface_view_activity.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- See corresponding Java code SecureSurfaceViewActivity.java. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical">
+
+    <!-- Message to show to use. -->
+    <TextView android:id="@+id/text"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="0"
+        android:gravity="center_vertical|center_horizontal"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="@string/secure_surface_view_activity_text"/>
+
+    <android.opengl.GLSurfaceView android:id="@+id/surface_view"
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" />
+</LinearLayout>
diff --git a/samples/ApiDemos/res/layout/secure_window_activity.xml b/samples/ApiDemos/res/layout/secure_window_activity.xml
new file mode 100644
index 0000000..6d086da
--- /dev/null
+++ b/samples/ApiDemos/res/layout/secure_window_activity.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- See corresponding Java code SecureWindowActivity.java. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical">
+
+    <!-- Message to show to use. -->
+    <TextView android:id="@+id/text"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_weight="0"
+        android:gravity="center_vertical|center_horizontal"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="@string/secure_window_activity_text"/>
+</LinearLayout>
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index e1e8adb..9d8616d 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -122,6 +122,32 @@
     <string name="presentation_alert_info_text">Display %1$d Info</string>
     <string name="presentation_alert_dismiss_text">OK</string>
 
+    <string name="activity_secure_window">App/Activity/Secure Surfaces/Secure Window</string>
+    <string name="secure_window_activity_text">This activity demonstrates how to make an activity
+            use a secure surface so that its contents will only be visible on secure displays.
+            The activity\'s window has been marked with FLAG_SECURE to make it use a secure surface.
+            Consequently, the contents of the activity will not appear in screenshots and will not
+            be mirrored to non-secure displays.\n
+            \n
+            I am a secure activity!</string>
+
+    <string name="activity_secure_dialog">App/Activity/Secure Surfaces/Secure Dialog</string>
+    <string name="secure_dialog_activity_text">This activity demonstrates how to make a dialog use
+            a secure surface so that its contents will only be visible on secure displays.
+            The dialog\'s window has been marked with FLAG_SECURE to make it use a secure surface.
+            Consequently, the contents of the dialog will not appear in screenshots and will not
+            be mirrored to non-secure displays.</string>
+    <string name="secure_dialog_show_button">Show secure dialog</string>
+    <string name="secure_dialog_dialog_text">I am a secure dialog!</string>
+
+    <string name="activity_secure_surface_view">App/Activity/Secure Surfaces/Secure Surface View</string>
+    <string name="secure_surface_view_activity_text">This activity demonstrates how to make a
+            SurfaceView use a secure surface so that its contents will only be visible on
+            secure displays.
+            The surface view\'s window has been made secure using setSecure(true) to make it use
+            a secure surface.  Consequently, the contents of the surface view will not appear in
+            screenshots and will not be mirrored to non-secure displays.</string>
+
     <string name="fragment_alert_dialog">App/Fragment/Alert Dialog</string>
 
     <string name="fragment_arguments">App/Fragment/Arguments</string>
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PresentationActivity.java b/samples/ApiDemos/src/com/example/android/apis/app/PresentationActivity.java
index 93647bf..3e561aa 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/PresentationActivity.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/PresentationActivity.java
@@ -64,9 +64,10 @@
 
     // The content that we want to show on the presentation.
     private static final int[] CHANNELS = new int[] {
-        R.drawable.sample_4, R.drawable.frantic, R.drawable.beach,
+        R.drawable.frantic,
         R.drawable.photo1, R.drawable.photo2, R.drawable.photo3,
         R.drawable.photo4, R.drawable.photo5, R.drawable.photo6,
+        R.drawable.sample_4,
     };
 
     private DisplayManager mDisplayManager;
@@ -182,8 +183,9 @@
     }
 
     private int getNextChannel() {
+        final int channel = mNextChannelNumber;
         mNextChannelNumber = (mNextChannelNumber + 1) % CHANNELS.length;
-        return mNextChannelNumber;
+        return channel;
     }
 
     @Override
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/SecureDialogActivity.java b/samples/ApiDemos/src/com/example/android/apis/app/SecureDialogActivity.java
new file mode 100644
index 0000000..fe871aa
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/SecureDialogActivity.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.os.Bundle;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.Button;
+
+/**
+ * <h3>Secure Dialog Activity</h3>
+ *
+ * <p>
+ * This activity demonstrates how to create a dialog whose window is backed by
+ * a secure surface using {@link WindowManager.LayoutParams#FLAG_SECURE}.
+ * Because the surface is secure, its contents cannot be captured in screenshots
+ * and will not be visible on non-secure displays even when mirrored.
+ * </p><p>
+ * Here are a few things you can do to experiment with secure surfaces and
+ * observe their behavior.
+ * <ul>
+ * <li>Try taking a screenshot.  Either the system will prevent you from taking
+ * a screenshot altogether or the screenshot should not contain the contents
+ * of the secure surface.
+ * <li>Try mirroring the secure surface onto a non-secure display such as an
+ * "Overlay Display" created using the "Simulate secondary displays" option in
+ * the "Developer options" section of the Settings application.  The non-secure
+ * secondary display should not show the contents of the secure surface.
+ * <li>Try mirroring the secure surface onto a secure display such as an
+ * HDMI display with HDCP enabled.  The contents of the secure surface should appear
+ * on the display.
+ * </ul>
+ * </p>
+ */
+public class SecureDialogActivity extends Activity
+        implements View.OnClickListener {
+    /**
+     * Initialization of the Activity after it is first created.  Must at least
+     * call {@link android.app.Activity#setContentView setContentView()} to
+     * describe what is to be displayed in the screen.
+     */
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        // Be sure to call the super class.
+        super.onCreate(savedInstanceState);
+
+        // See assets/res/any/layout/secure_dialog_activity.xml for this
+        // view layout definition, which is being set here as
+        // the content of our screen.
+        setContentView(R.layout.secure_dialog_activity);
+
+        // Handle click events on the button to show the dialog.
+        Button button = (Button)findViewById(R.id.show);
+        button.setOnClickListener(this);
+    }
+
+    /**
+     * Called when the button to show the dialog is clicked.
+     */
+    @Override
+    public void onClick(View v) {
+        // Create a dialog.
+        AlertDialog dialog = new AlertDialog.Builder(this)
+                .setPositiveButton(android.R.string.ok, null)
+                .setMessage(R.string.secure_dialog_dialog_text)
+                .create();
+
+        // Make the dialog secure.  This must be done at the time the dialog is
+        // created.  It cannot be changed after the dialog has been shown.
+        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
+                WindowManager.LayoutParams.FLAG_SECURE);
+
+        // Show the dialog.
+        dialog.show();
+    }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/SecureSurfaceViewActivity.java b/samples/ApiDemos/src/com/example/android/apis/app/SecureSurfaceViewActivity.java
new file mode 100644
index 0000000..2c3abed
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/SecureSurfaceViewActivity.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+import com.example.android.apis.graphics.CubeRenderer;
+
+import android.app.Activity;
+import android.opengl.GLSurfaceView;
+import android.os.Bundle;
+import android.view.SurfaceView;
+import android.view.WindowManager;
+
+/**
+ * <h3>Secure Window Activity</h3>
+ *
+ * <p>
+ * This activity demonstrates how to create a {@link SurfaceView} backed by
+ * a secure surface using {@link SurfaceView#setSecure}.
+ * Because the surface is secure, its contents cannot be captured in screenshots
+ * and will not be visible on non-secure displays even when mirrored.
+ * </p><p>
+ * Here are a few things you can do to experiment with secure surfaces and
+ * observe their behavior.
+ * <ul>
+ * <li>Try taking a screenshot.  Either the system will prevent you from taking
+ * a screenshot altogether or the screenshot should not contain the contents
+ * of the secure surface.
+ * <li>Try mirroring the secure surface onto a non-secure display such as an
+ * "Overlay Display" created using the "Simulate secondary displays" option in
+ * the "Developer options" section of the Settings application.  The non-secure
+ * secondary display should not show the contents of the secure surface.
+ * <li>Try mirroring the secure surface onto a secure display such as an
+ * HDMI display with HDCP enabled.  The contents of the secure surface should appear
+ * on the display.
+ * </ul>
+ * </p>
+ */
+public class SecureSurfaceViewActivity extends Activity {
+    /**
+     * Initialization of the Activity after it is first created.  Must at least
+     * call {@link android.app.Activity#setContentView setContentView()} to
+     * describe what is to be displayed in the screen.
+     */
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        // Be sure to call the super class.
+        super.onCreate(savedInstanceState);
+
+        // See assets/res/any/layout/secure_surface_view_activity.xml for this
+        // view layout definition, which is being set here as
+        // the content of our screen.
+        setContentView(R.layout.secure_surface_view_activity);
+
+        // Set up the surface view.
+        // We use a GLSurfaceView in this demonstration but ordinary
+        // SurfaceViews also support the same secure surface functionality.
+        GLSurfaceView surfaceView = (GLSurfaceView)findViewById(R.id.surface_view);
+        surfaceView.setRenderer(new CubeRenderer(false));
+
+        // Make the surface view secure.  This must be done at the time the surface view
+        // is created before the surface view's containing window is attached to
+        // the window manager which happens after onCreate returns.
+        // It cannot be changed later.
+        surfaceView.setSecure(true);
+    }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/SecureWindowActivity.java b/samples/ApiDemos/src/com/example/android/apis/app/SecureWindowActivity.java
new file mode 100644
index 0000000..4b5e8b6
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/SecureWindowActivity.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.WindowManager;
+
+/**
+ * <h3>Secure Window Activity</h3>
+ *
+ * <p>
+ * This activity demonstrates how to create an activity whose window is backed by
+ * a secure surface using {@link WindowManager.LayoutParams#FLAG_SECURE}.
+ * Because the surface is secure, its contents cannot be captured in screenshots
+ * and will not be visible on non-secure displays even when mirrored.
+ * </p><p>
+ * Here are a few things you can do to experiment with secure surfaces and
+ * observe their behavior.
+ * <ul>
+ * <li>Try taking a screenshot.  Either the system will prevent you from taking
+ * a screenshot altogether or the screenshot should not contain the contents
+ * of the secure surface.
+ * <li>Try mirroring the secure surface onto a non-secure display such as an
+ * "Overlay Display" created using the "Simulate secondary displays" option in
+ * the "Developer options" section of the Settings application.  The non-secure
+ * secondary display should not show the contents of the secure surface.
+ * <li>Try mirroring the secure surface onto a secure display such as an
+ * HDMI display with HDCP enabled.  The contents of the secure surface should appear
+ * on the display.
+ * </ul>
+ * </p>
+ */
+public class SecureWindowActivity extends Activity {
+    /**
+     * Initialization of the Activity after it is first created.  Must at least
+     * call {@link android.app.Activity#setContentView setContentView()} to
+     * describe what is to be displayed in the screen.
+     */
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        // Be sure to call the super class.
+        super.onCreate(savedInstanceState);
+
+        // See assets/res/any/layout/secure_window_activity.xml for this
+        // view layout definition, which is being set here as
+        // the content of our screen.
+        setContentView(R.layout.secure_window_activity);
+
+        // Make the window secure.  This must be done at the time the activity
+        // is created.  It cannot be changed later.
+        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
+                WindowManager.LayoutParams.FLAG_SECURE);
+    }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/graphics/CubeRenderer.java b/samples/ApiDemos/src/com/example/android/apis/graphics/CubeRenderer.java
index ac0ae27..b30808c 100644
--- a/samples/ApiDemos/src/com/example/android/apis/graphics/CubeRenderer.java
+++ b/samples/ApiDemos/src/com/example/android/apis/graphics/CubeRenderer.java
@@ -25,7 +25,7 @@
  * Render a pair of tumbling cubes.
  */
 
-class CubeRenderer implements GLSurfaceView.Renderer {
+public class CubeRenderer implements GLSurfaceView.Renderer {
     public CubeRenderer(boolean useTranslucentBackground) {
         mTranslucentBackground = useTranslucentBackground;
         mCube = new Cube();