Merge "Updating launcher icon to fix alignment. Bug:  6511716" into jb-dev
diff --git a/res/layout/cropimage.xml b/res/layout/cropimage.xml
index aefebe8..c434fb6 100644
--- a/res/layout/cropimage.xml
+++ b/res/layout/cropimage.xml
@@ -17,9 +17,5 @@
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
-    <view class="com.android.gallery3d.ui.GLRootView"
-            android:id="@+id/gl_root_view"
-            android:background="@null"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent" />
+    <include layout="@layout/gl_root_group"/>
 </FrameLayout>
diff --git a/res/layout/dialog_picker.xml b/res/layout/dialog_picker.xml
index ba3f500..4a625a1 100644
--- a/res/layout/dialog_picker.xml
+++ b/res/layout/dialog_picker.xml
@@ -18,8 +18,7 @@
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
-    <com.android.gallery3d.ui.GLRootView
-            android:id="@+id/gl_root_view"
+    <include layout="@layout/gl_root_group"
             android:layout_weight="1"
             android:layout_height="0dp"
             android:layout_width="match_parent"/>
diff --git a/res/layout/gl_root_group.xml b/res/layout/gl_root_group.xml
new file mode 100644
index 0000000..76ff33b
--- /dev/null
+++ b/res/layout/gl_root_group.xml
@@ -0,0 +1,26 @@
+<?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.
+-->
+
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+    <com.android.gallery3d.ui.GLRootView
+            android:id="@+id/gl_root_view"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"/>
+    <View android:id="@+id/gl_root_cover"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@android:color/black"/>
+</merge>
diff --git a/res/layout/main.xml b/res/layout/main.xml
index 7dfe57a..d367301 100644
--- a/res/layout/main.xml
+++ b/res/layout/main.xml
@@ -4,14 +4,7 @@
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
-    <com.android.gallery3d.ui.GLRootView
-            android:id="@+id/gl_root_view"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:layout_alignParentBottom="true"
-            android:layout_alignParentLeft="true"
-            android:layout_alignParentRight="true"
-            android:layout_alignParentTop="true"/>
+    <include layout="@layout/gl_root_group"/>
     <FrameLayout android:id="@+id/footer"
             android:visibility="gone"
             android:layout_alignParentBottom="true"
diff --git a/src/com/android/gallery3d/app/Gallery.java b/src/com/android/gallery3d/app/Gallery.java
index dd30d7e..bdd4a0b 100644
--- a/src/com/android/gallery3d/app/Gallery.java
+++ b/src/com/android/gallery3d/app/Gallery.java
@@ -198,6 +198,9 @@
                     data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString());
                 }
                 data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString());
+                if (intent.getBooleanExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, false)) {
+                    data.putBoolean(PhotoPage.KEY_TREAT_BACK_AS_UP, true);
+                }
 
                 // Displays the filename as title, reading the filename from the interface:
                 // {@link android.provider.OpenableColumns#DISPLAY_NAME}.
diff --git a/src/com/android/gallery3d/app/MovieActivity.java b/src/com/android/gallery3d/app/MovieActivity.java
index 8dd69e4..b0e53c4 100644
--- a/src/com/android/gallery3d/app/MovieActivity.java
+++ b/src/com/android/gallery3d/app/MovieActivity.java
@@ -96,6 +96,10 @@
         winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
         winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
         win.setAttributes(winParams);
+
+        // We set the background in the theme to have the launching animation.
+        // But for the performance (and battery), we remove the background here.
+        win.setBackgroundDrawable(null);
     }
 
     private void initializeActionBar(Intent intent) {
diff --git a/src/com/android/gallery3d/app/MoviePlayer.java b/src/com/android/gallery3d/app/MoviePlayer.java
index 2c8f9eb..565d2b2 100644
--- a/src/com/android/gallery3d/app/MoviePlayer.java
+++ b/src/com/android/gallery3d/app/MoviePlayer.java
@@ -76,6 +76,7 @@
     private long mResumeableTime = Long.MAX_VALUE;
     private int mVideoPosition = 0;
     private boolean mHasPaused = false;
+    private int mLastSystemUiVis = 0;
 
     // If the time bar is being dragged.
     private boolean mDragging;
@@ -134,7 +135,10 @@
         mVideoView.setOnSystemUiVisibilityChangeListener(
                 new View.OnSystemUiVisibilityChangeListener() {
             public void onSystemUiVisibilityChange(int visibility) {
-                if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
+                int diff = mLastSystemUiVis ^ visibility;
+                mLastSystemUiVis = visibility;
+                if ((diff & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0
+                        && (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                     mAllowShowingSystemUI = true;
                     mController.show();
                 }
@@ -168,14 +172,14 @@
     }
 
     private void showSystemUi(boolean visible) {
-        int flag = visible ? 0 : View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
-                View.SYSTEM_UI_FLAG_LOW_PROFILE;
-        mVideoView.setSystemUiVisibility(flag);
-        if (visible) {
-            mActionBar.show();
-        } else {
-            mActionBar.hide();
+        int flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
+                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
+                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
+        if (!visible) {
+            flag |= View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_FULLSCREEN
+                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
         }
+        mVideoView.setSystemUiVisibility(flag);
     }
 
     public void onSaveInstanceState(Bundle outState) {
diff --git a/src/com/android/gallery3d/app/PhotoPage.java b/src/com/android/gallery3d/app/PhotoPage.java
index 2132d06..36ccc67 100644
--- a/src/com/android/gallery3d/app/PhotoPage.java
+++ b/src/com/android/gallery3d/app/PhotoPage.java
@@ -91,6 +91,7 @@
     public static final String KEY_INDEX_HINT = "index-hint";
     public static final String KEY_OPEN_ANIMATION_RECT = "open-animation-rect";
     public static final String KEY_APP_BRIDGE = "app-bridge";
+    public static final String KEY_TREAT_BACK_AS_UP = "treat-back-as-up";
 
     public static final String KEY_RETURN_INDEX_HINT = "return-index-hint";
 
@@ -127,6 +128,7 @@
     private SnailAlbum mScreenNailSet;
     private OrientationManager mOrientationManager;
     private boolean mHasActivityResult;
+    private boolean mTreatBackAsUp;
 
     private NfcAdapter mNfcAdapter;
 
@@ -180,6 +182,7 @@
         mOriginalSetPathString = mSetPathString;
         mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity.getAndroidContext());
         Path itemPath = Path.fromString(data.getString(KEY_MEDIA_ITEM_PATH));
+        mTreatBackAsUp = data.getBoolean(KEY_TREAT_BACK_AS_UP, false);
 
         if (mSetPathString != null) {
             mAppBridge = (AppBridge) data.getParcelable(KEY_APP_BRIDGE);
@@ -439,7 +442,11 @@
         } else if (mAppBridge == null || !switchWithCaptureAnimation(-1)) {
             // We are leaving this page. Set the result now.
             setResult();
-            super.onBackPressed();
+            if (mTreatBackAsUp) {
+                onUpPressed();
+            } else {
+                super.onBackPressed();
+            }
         }
     }
 
diff --git a/src/com/android/gallery3d/gadget/WidgetClickHandler.java b/src/com/android/gallery3d/gadget/WidgetClickHandler.java
index 36575e4..35f9bec 100644
--- a/src/com/android/gallery3d/gadget/WidgetClickHandler.java
+++ b/src/com/android/gallery3d/gadget/WidgetClickHandler.java
@@ -26,6 +26,7 @@
 
 import com.android.gallery3d.R;
 import com.android.gallery3d.app.Gallery;
+import com.android.gallery3d.app.PhotoPage;
 
 public class WidgetClickHandler extends Activity {
     private static final String TAG = "PhotoAppWidgetClickHandler";
@@ -46,14 +47,19 @@
     @Override
     protected void onCreate(Bundle savedState) {
         super.onCreate(savedState);
-        Intent intent = getIntent();
-        if (isValidDataUri(intent.getData())) {
-            startActivity(new Intent(Intent.ACTION_VIEW, intent.getData()));
+        Uri uri = getIntent().getData();
+        Intent intent;
+        if (isValidDataUri(uri)) {
+            intent = new Intent(Intent.ACTION_VIEW, uri);
+            intent.putExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, true);
         } else {
             Toast.makeText(this,
                     R.string.no_such_item, Toast.LENGTH_LONG).show();
-            startActivity(new Intent(this, Gallery.class));
+            intent = new Intent(this, Gallery.class);
         }
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
+                Intent.FLAG_ACTIVITY_TASK_ON_HOME);
+        startActivity(intent);
         finish();
     }
 }
diff --git a/src/com/android/gallery3d/ui/GLRootView.java b/src/com/android/gallery3d/ui/GLRootView.java
index f78e6e6..99ed8cb 100644
--- a/src/com/android/gallery3d/ui/GLRootView.java
+++ b/src/com/android/gallery3d/ui/GLRootView.java
@@ -25,7 +25,9 @@
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.SurfaceHolder;
+import android.view.View;
 
+import com.android.gallery3d.R;
 import com.android.gallery3d.anim.CanvasAnimation;
 import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.util.GalleryUtils;
@@ -104,6 +106,7 @@
 
     private long mLastDrawFinishTime;
     private boolean mInDownState = false;
+    private boolean mFirstDraw = true;
 
     public GLRootView(Context context) {
         this(context, null);
@@ -322,6 +325,20 @@
             mRenderLock.unlock();
         }
 
+        // We put a black cover View in front of the SurfaceView and hide it
+        // after the first draw. This prevents the SurfaceView being transparent
+        // before the first draw.
+        if (mFirstDraw) {
+            mFirstDraw = false;
+            post(new Runnable() {
+                    public void run() {
+                        View root = getRootView();
+                        View cover = root.findViewById(R.id.gl_root_cover);
+                        cover.setVisibility(GONE);
+                    }
+                });
+        }
+
         if (DEBUG_PROFILE_SLOW_ONLY) {
             long t = System.nanoTime();
             long durationInMs = (t - mLastDrawFinishTime) / 1000000;