Add RetailDemo project.

Contains the DemoPlayer component that is responsible for playing
demo video when the device is in retail demo mode.

Bug: 29359781
Change-Id: I16234eaf1e38f299c9a9bce7024b52dcadcb2429
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..e520275
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,34 @@
+# Copyright (C) 2016 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_CERTIFICATE := platform
+LOCAL_MODULE_TAGS := optional
+LOCAL_PACKAGE_NAME := RetailDemo
+LOCAL_PRIVILEGED_MODULE := true
+
+LOCAL_RESOURCE_DIR := \
+    $(LOCAL_PATH)/res
+
+LOCAL_SRC_FILES := \
+    $(call all-java-files-under, src)
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    android-support-annotations
+
+include $(BUILD_PACKAGE)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
new file mode 100644
index 0000000..166e6b4
--- /dev/null
+++ b/AndroidManifest.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+        package="com.android.retaildemo">
+
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.WAKE_LOCK" />
+    <!-- TODO: Remove this when we decide between demo user and SUW-based demo mode -->
+    <uses-permission android:name="android.permission.MANAGE_USERS" />
+
+    <application
+            android:allowBackup="false"
+            android:label="@string/retail_demo_title"
+            android:supportsRtl="true">
+
+        <activity android:name=".DemoPlayer"
+                android:enabled="false"
+                android:exported="true"
+                android:immersive="true"
+                android:screenOrientation="nosensor"
+                android:theme="@android:style/Theme.Material.NoActionBar.Fullscreen">
+            <!-- Higher priority than setup wizard -->
+            <intent-filter android:priority="6">
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.HOME" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
+    </application>
+
+</manifest>
\ No newline at end of file
diff --git a/res/layout/retail_video.xml b/res/layout/retail_video.xml
new file mode 100644
index 0000000..f51296c
--- /dev/null
+++ b/res/layout/retail_video.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <VideoView
+            android:id="@+id/video_content"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:layout_gravity="center"
+            android:keepScreenOn="true" />
+
+</merge>
diff --git a/res/values/strings.xml b/res/values/strings.xml
new file mode 100644
index 0000000..07e9174
--- /dev/null
+++ b/res/values/strings.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+
+    <!-- Title of the application. Used as a fallback for TalkBack when activity label is not specified (an accessibility service). [CHAR LIMIT=NONE] -->
+    <string name="retail_demo_title">Retail demo</string>
+
+</resources>
diff --git a/src/com/android/retaildemo/DemoPlayer.java b/src/com/android/retaildemo/DemoPlayer.java
new file mode 100644
index 0000000..fcced3e
--- /dev/null
+++ b/src/com/android/retaildemo/DemoPlayer.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2016 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.android.retaildemo;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.media.MediaPlayer;
+import android.os.Bundle;
+import android.os.Environment;
+import android.os.PowerManager;
+import android.os.UserManager;
+import android.util.Log;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.VideoView;
+
+import java.io.File;
+
+/**
+ * This is the prototyping activity for playing the retail demo video. This will also try to keep
+ * the screen on.
+ *
+ * Note: this activity checks for the file retail_video.mp4 in the path specified by the system
+ * property "ro.retaildemo.video_path". To run this, push your test video to that path.
+ */
+public class DemoPlayer extends Activity {
+
+    private static final String TAG = "DemoPlayer";
+    private static final String PRELOADED_VIDEO_FILE = Environment.getDataPreloadsDemoDirectory()
+            + File.separator + "retail_demo.mp4";
+
+    private VideoView mVideoView;
+    private PowerManager mPowerManager;
+    private int mVideoPosition;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        // Keep screen on
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
+                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
+        // Make view full screen
+        getWindow().getDecorView().setSystemUiVisibility(
+                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
+                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
+                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
+                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
+                        | View.SYSTEM_UI_FLAG_FULLSCREEN
+                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
+                        | View.STATUS_BAR_DISABLE_BACK
+        );
+        setContentView(R.layout.retail_video);
+
+        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
+
+        mVideoView = (VideoView) findViewById(R.id.video_content);
+
+        // Start playing the video when it is ready
+        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
+            @Override
+            public void onPrepared(MediaPlayer mediaPlayer) {
+                mediaPlayer.setLooping(true);
+                mVideoView.start();
+                // if we pause before screen off, try to resume
+                if (mVideoPosition > 0) {
+                    mVideoView.seekTo(mVideoPosition);
+                }
+            }
+        });
+
+        loadVideo();
+    }
+
+    private void loadVideo() {
+        // Load the video from resource
+        try {
+            mVideoView.setVideoPath(PRELOADED_VIDEO_FILE);
+        } catch (Exception e) {
+            Log.e(TAG, "Exception setting video uri! " + e.getMessage());
+            // If video cannot be load, reset retail mode
+            finish();
+        }
+    }
+
+    @Override
+    public boolean dispatchTouchEvent(MotionEvent ev) {
+        if (UserManager.get(this).isDemoUser()) {
+            disableSelf();
+        }
+        return true;
+    }
+
+    private void disableSelf() {
+        getPackageManager().setComponentEnabledSetting(getComponentName(),
+                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
+    }
+
+    @Override
+    public void onPause() {
+        // Save video position
+        if (mVideoView != null) {
+            mVideoPosition = mVideoView.getCurrentPosition();
+            mVideoView.pause();
+        }
+        // If power key is pressed to turn screen off, turn screen back on
+        if (!mPowerManager.isInteractive()) {
+            forceTurnOnScreen();
+        }
+        super.onPause();
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        // Resume video playing
+        if (mVideoView != null) {
+            mVideoView.resume();
+        }
+    }
+
+    @Override
+    protected void onStop() {
+        // Stop video
+        if (mVideoView != null) {
+            mVideoView.stopPlayback();
+        }
+        super.onStop();
+    }
+
+    private void forceTurnOnScreen() {
+        final PowerManager.WakeLock wakeLock = mPowerManager.newWakeLock(
+                PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
+        wakeLock.acquire();
+        // Device waken up, release the wake-lock
+        wakeLock.release();
+    }
+}