Use current wallpaper as fallback view. am: c8de5e2b01
am: feeabb7cba

Change-Id: I521ccbabaef66da88d8ce9490f47347903ecf25d
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b1697a2..e1178c5 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -32,7 +32,7 @@
                 android:exported="true"
                 android:immersive="true"
                 android:screenOrientation="nosensor"
-                android:theme="@android:style/Theme.Material.Light.NoActionBar.Fullscreen">
+                android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen">
             <!-- Higher priority than setup wizard -->
             <intent-filter android:priority="6">
                 <action android:name="android.intent.action.MAIN" />
diff --git a/res/layout/retail_video.xml b/res/layout/retail_video.xml
index f51296c..3393604 100644
--- a/res/layout/retail_video.xml
+++ b/res/layout/retail_video.xml
@@ -16,6 +16,22 @@
 
 <merge xmlns:android="http://schemas.android.com/apk/res/android">
 
+    <FrameLayout android:id="@+id/fallback_layout"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:keepScreenOn="true"
+            android:visibility="gone"
+            android:background="@android:color/transparent">
+        <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="@string/touch_to_continue"
+                android:layout_gravity="bottom|center_horizontal"
+                android:textAppearance="@android:style/TextAppearance.Material.Medium"
+                android:textColor="@android:color/white"
+                android:layout_marginBottom="@dimen/fallback_view_text_margin_bottom" />
+    </FrameLayout>
+
     <VideoView
             android:id="@+id/video_content"
             android:layout_width="match_parent"
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
new file mode 100644
index 0000000..0b484d2
--- /dev/null
+++ b/res/values/dimens.xml
@@ -0,0 +1,18 @@
+<?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>
+    <dimen name="fallback_view_text_margin_bottom">32dp</dimen>
+</resources>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0a18496..432d101 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -24,7 +24,7 @@
     <!-- Message displayed to the user when downloading the video. [CHAR LIMIT=NONE] -->
     <string name="downloading_video_msg">Downloading video\u2026</string>
 
-    <!-- Message displayed to the user when the device has no network connectivity to download video. [CHAR LIMIT=NONE] -->
-    <string name="no_network_connectivity">No network connectivity to download video</string>
+    <!-- Message displayed to let the user know that the screen needs to be touched to start a new demo session. [CHAR LIMIT=NONE] -->
+    <string name="touch_to_continue">Touch the screen to continue</string>
 
 </resources>
diff --git a/src/com/android/retaildemo/DemoPlayer.java b/src/com/android/retaildemo/DemoPlayer.java
index 736e165..84852c8 100644
--- a/src/com/android/retaildemo/DemoPlayer.java
+++ b/src/com/android/retaildemo/DemoPlayer.java
@@ -25,6 +25,7 @@
 import android.os.PowerManager;
 import android.os.SystemClock;
 import android.os.UserManager;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
@@ -102,23 +103,57 @@
             }
         });
 
+        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
+            @Override
+            public boolean onError(MediaPlayer mp, int what, int extra) {
+                displayFallbackView();
+                return true;
+            }
+        });
+
         loadVideo();
     }
 
+    private void displayFallbackView() {
+        if (DEBUG) Log.d(TAG, "Showing the fallback view");
+        findViewById(R.id.fallback_layout).setVisibility(View.VISIBLE);
+        mVideoView.setVisibility(View.GONE);
+    }
+
+    private void displayVideoView() {
+        mVideoView.setVisibility(View.VISIBLE);
+        findViewById(R.id.fallback_layout).setVisibility(View.GONE);
+    }
+
     private void loadVideo() {
         // If the video is already downloaded, then use that and check for an update.
         // Otherwise check if the video is preloaded, if not download the video from the
         // specified url.
+        boolean isVideoSet = false;
         if (new File(mDownloadPath).exists()) {
             if (DEBUG) Log.d(TAG, "Using the already existing video at " + mDownloadPath);
             setVideoPath(mDownloadPath);
+            isVideoSet = true;
         } else if (new File(PRELOADED_VIDEO_FILE).exists()) {
             if (DEBUG) Log.d(TAG, "Using the preloaded video at " + PRELOADED_VIDEO_FILE);
             setVideoPath(PRELOADED_VIDEO_FILE);
+            isVideoSet = true;
+        }
+
+        final String downloadUrl = getString(R.string.retail_demo_video_download_url);
+        // If the download url is empty, then no need to start the download task.
+        if (TextUtils.isEmpty(downloadUrl)) {
+            if (!isVideoSet) {
+                displayFallbackView();
+            }
+            return;
         }
         if (!checkIfDownloadingAllowed()) {
             if (DEBUG) Log.d(TAG, "Downloading not allowed, neither starting download nor checking"
                     + " for an update.");
+            if (!isVideoSet) {
+                displayFallbackView();
+            }
             return;
         }
         new DownloadVideoTask(this, mDownloadPath, this).run();
@@ -138,15 +173,20 @@
 
     @Override
     public void onFileDownloaded(final String filePath) {
-        mVideoView.post(new Runnable() {
+        runOnUiThread(new Runnable() {
             @Override
             public void run() {
-                mVideoView.setVideoPath(filePath);
+                setVideoPath(filePath);
             }
         });
     }
 
     @Override
+    public void onError() {
+        displayFallbackView();
+    }
+
+    @Override
     public boolean dispatchTouchEvent(MotionEvent ev) {
         if (getSystemService(UserManager.class).isDemoUser()) {
             disableSelf();
@@ -195,10 +235,10 @@
         // Load the video from resource
         try {
             mVideoView.setVideoPath(videoPath);
+            displayVideoView();
         } catch (Exception e) {
             Log.e(TAG, "Exception setting video uri! " + e.getMessage());
-            // If video cannot be load, reset retail mode
-            finish();
+            displayFallbackView();
         }
     }
 
diff --git a/src/com/android/retaildemo/DownloadVideoTask.java b/src/com/android/retaildemo/DownloadVideoTask.java
index 5f2db8a..d635553 100644
--- a/src/com/android/retaildemo/DownloadVideoTask.java
+++ b/src/com/android/retaildemo/DownloadVideoTask.java
@@ -16,7 +16,6 @@
 
 package com.android.retaildemo;
 
-import android.app.AlertDialog;
 import android.app.DownloadManager;
 import android.app.ProgressDialog;
 import android.content.BroadcastReceiver;
@@ -60,7 +59,7 @@
     private Handler mHandler;
 
     private ProgressDialog mProgressDialog;
-    private AlertDialog mErrorMsgDialog;
+    private DownloadResultReceiver mDownloadReceiver;
     private NetworkChangeReceiver mNetworkChangeReceiver;
     private String mDownloadUrl;
     private long mVideoDownloadId;
@@ -77,6 +76,7 @@
     }
 
     public void run() {
+        mDownloadReceiver = new DownloadResultReceiver();
         mContext.registerReceiver(mDownloadReceiver,
                 new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
 
@@ -92,8 +92,7 @@
             mHandler.sendMessage(mHandler.obtainMessage(MSG_CHECK_FOR_UPDATE));
         } else {
             if (!isConnectedToNetwork()) {
-                mErrorMsgDialog = createErrorMsgDialog(R.string.no_network_connectivity);
-                mErrorMsgDialog.show();
+                mListener.onError();
                 mNetworkChangeReceiver = new NetworkChangeReceiver();
                 mContext.registerReceiver(mNetworkChangeReceiver,
                         new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
@@ -117,7 +116,7 @@
         return request;
     }
 
-    private final BroadcastReceiver mDownloadReceiver = new BroadcastReceiver() {
+    private class DownloadResultReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {
             if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
@@ -206,9 +205,13 @@
             if (cursor != null & cursor.moveToFirst()) {
                 final int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                 if (cursor.getInt(columnIndex) == DownloadManager.STATUS_SUCCESSFUL) {
-                    mContext.unregisterReceiver(mDownloadReceiver);
+                    if (mDownloadReceiver != null) {
+                        mContext.unregisterReceiver(mDownloadReceiver);
+                        mDownloadReceiver = null;
+                    }
                     if (mNetworkChangeReceiver != null) {
                         mContext.unregisterReceiver(mNetworkChangeReceiver);
+                        mNetworkChangeReceiver = null;
                     }
                     final String fileUri = cursor.getString(
                             cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
@@ -236,7 +239,6 @@
                 if (mDownloadFile.exists()) {
                     mHandler.sendMessage(mHandler.obtainMessage(MSG_CHECK_FOR_UPDATE));
                 } else {
-                    mErrorMsgDialog.dismiss();
                     startDownload();
                 }
             }
@@ -259,13 +261,6 @@
         return info != null && info.isConnected();
     }
 
-    private AlertDialog createErrorMsgDialog(int msgResId) {
-        return new AlertDialog.Builder(mContext)
-                .setMessage(msgResId)
-                .setCancelable(false)
-                .create();
-    }
-
     private String getFileBaseName(String fileName) {
         final int pos = fileName.lastIndexOf(".");
         return pos > 0 ? fileName.substring(0, pos) : fileName;
@@ -273,5 +268,6 @@
 
     interface ResultListener {
         void onFileDownloaded(String downloadedFilePath);
+        void onError();
     }
 }
\ No newline at end of file