Merge "Initial changes for new clings. (Bug 11142616)" into jb-ub-now-indigo-rose
diff --git a/res/values/styles.xml b/res/values/styles.xml
index c74d508..a1d2c5c 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -31,6 +31,8 @@
     </style>
 
     <style name="Theme" parent="@android:style/Theme.Holo.Wallpaper.NoTitleBar">
+        <item name="android:windowTranslucentStatus">true</item>
+        <item name="android:windowTranslucentNavigation">true</item>
     </style>
 
     <style name="ClingButton">
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 61b46a7..278b13c 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -328,8 +328,6 @@
 
     private BubbleTextView mWaitingForResume;
 
-    protected TranslucentDecor mTransparentDecor;
-
     private HideFromAccessibilityHelper mHideFromAccessibilityHelper
         = new HideFromAccessibilityHelper();
 
@@ -428,9 +426,6 @@
         checkForLocaleChange();
         setContentView(R.layout.launcher);
 
-        mTransparentDecor = new TranslucentDecor(findViewById(R.id.launcher));
-        mTransparentDecor.requestTranslucentDecor(true);
-
         setupViews();
         grid.layout(this);
 
@@ -838,7 +833,7 @@
         if (mOnResumeState == State.WORKSPACE) {
             showWorkspace(false);
         } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
-            showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
+            showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
         }
         mOnResumeState = State.NONE;
 
@@ -893,10 +888,7 @@
             // Resets the previous all apps icon press state
             mAppsCustomizeContent.resetDrawableState();
         }
-        // Reset AllApps to its initial state
-        if (mAppsCustomizeTabHost != null) {
-            mAppsCustomizeTabHost.reset();
-        }
+
         // It is possible that widgets can receive updates while launcher is not in the foreground.
         // Consequently, the widgets will be inflated in the orientation of the foreground activity
         // (framework issue). On resuming, we ensure that any widgets are inflated for the current
@@ -1090,8 +1082,9 @@
             mOnResumeState = State.APPS_CUSTOMIZE;
         }
 
-        int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
-        if (currentScreen > -1) {
+        int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
+                PagedView.INVALID_RESTORE_PAGE);
+        if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
             mWorkspace.setRestorePage(currentScreen);
         }
 
@@ -1161,7 +1154,7 @@
         widgetButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View arg0) {
-                showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
+                showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
             }
         });
         widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
@@ -1711,7 +1704,9 @@
 
     @Override
     protected void onSaveInstanceState(Bundle outState) {
-        outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
+        if (mWorkspace.getChildCount() > 0) {
+            outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
+        }
         super.onSaveInstanceState(outState);
 
         outState.putInt(RUNTIME_STATE, mState.ordinal());
@@ -2174,7 +2169,7 @@
                 final String shortcutClass = intent.getComponent().getClassName();
 
                 if (shortcutClass.equals(WidgetAdder.class.getName())) {
-                    showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
+                    showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
                     return;
                 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
                     MemoryDumpActivity.startDump(this);
@@ -2264,7 +2259,7 @@
      * @param v The view that was clicked.
      */
     public void onClickAllAppsButton(View v) {
-        showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
+        showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
     }
 
     public void onTouchDownAllAppsButton(View v) {
@@ -3075,10 +3070,13 @@
     public void onWorkspaceShown(boolean animated) {
     }
 
-    void showAllApps(boolean animated,
-                     AppsCustomizePagedView.ContentType contentType) {
+    void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
+                     boolean resetPageToZero) {
         if (mState != State.WORKSPACE) return;
 
+        if (resetPageToZero) {
+            mAppsCustomizeTabHost.reset();
+        }
         showAppsCustomizeHelper(animated, false, contentType);
         mAppsCustomizeTabHost.requestFocus();
 
@@ -4194,6 +4192,57 @@
         return false;
     }
 
+    public void updateCustomContentHintVisibility() {
+        Cling cling = (Cling) findViewById(R.id.first_run_cling);
+        String ccHintStr = getFirstRunCustomContentHint();
+
+        if (mWorkspace.hasCustomContent()) {
+            // Show the custom content hint if ccHintStr is not empty
+            if (cling != null) {
+                setCustomContentHintVisibility(cling, ccHintStr, true, true);
+            }
+        } else {
+            // Hide the custom content hint
+            if (cling != null) {
+                setCustomContentHintVisibility(cling, ccHintStr, false, true);
+            }
+        }
+    }
+
+    private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
+                                                boolean animate) {
+        final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
+        if (ccHint != null) {
+            if (visible && !ccHintStr.isEmpty()) {
+                ccHint.setText(ccHintStr);
+                ccHint.setVisibility(View.VISIBLE);
+                if (animate) {
+                    ccHint.setAlpha(0f);
+                    ccHint.animate().alpha(1f)
+                                    .setDuration(SHOW_CLING_DURATION)
+                                    .start();
+                } else {
+                    ccHint.setAlpha(1f);
+                }
+            } else {
+                if (animate) {
+                    ccHint.animate().alpha(0f)
+                                    .setDuration(SHOW_CLING_DURATION)
+                                    .setListener(new AnimatorListenerAdapter() {
+                                        @Override
+                                        public void onAnimationEnd(Animator animation) {
+                                            ccHint.setVisibility(View.GONE);
+                                        }
+                                    })
+                                    .start();
+                } else {
+                    ccHint.setAlpha(0f);
+                    ccHint.setVisibility(View.GONE);
+                }
+            }
+        }
+    }
+
     public void showFirstRunCling() {
         if (isClingsEnabled() &&
                 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
@@ -4223,11 +4272,7 @@
                     sbHint.setText(sbHintStr);
                     sbHint.setVisibility(View.VISIBLE);
                 }
-                if (!ccHintStr.isEmpty()) {
-                    TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
-                    ccHint.setText(ccHintStr);
-                    ccHint.setVisibility(View.VISIBLE);
-                }
+                setCustomContentHintVisibility(cling, ccHintStr, true, false);
             }
             initCling(R.id.first_run_cling, 0, false, true);
         } else {
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index 2a339c0..e982985 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -97,6 +97,8 @@
     private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
     private static final boolean DISABLE_FLING_TO_DELETE = true;
 
+    public static final int INVALID_RESTORE_PAGE = -1001;
+
     private boolean mFreeScroll = false;
     private int mFreeScrollMinScrollX = -1;
     private int mFreeScrollMaxScrollX = -1;
@@ -115,7 +117,7 @@
     private int mNormalChildHeight;
 
     protected int mCurrentPage;
-    protected int mRestorePage = -1;
+    protected int mRestorePage = INVALID_RESTORE_PAGE;
     protected int mChildCountOnLastLayout;
 
     protected int mNextPage = INVALID_PAGE;
@@ -546,7 +548,6 @@
         if (getChildCount() == 0) {
             return;
         }
-
         mForceScreenScrolled = true;
         mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
         updateCurrentPageScroll();
@@ -982,9 +983,9 @@
 
         if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
                 !mDeferringForDelete) {
-            if (mRestorePage > -1) {
+            if (mRestorePage != INVALID_RESTORE_PAGE) {
                 setCurrentPage(mRestorePage);
-                mRestorePage = -1;
+                mRestorePage = INVALID_RESTORE_PAGE;
             } else {
                 setCurrentPage(getNextPage());
             }
diff --git a/src/com/android/launcher3/TranslucentDecor.java b/src/com/android/launcher3/TranslucentDecor.java
deleted file mode 100644
index b50c022..0000000
--- a/src/com/android/launcher3/TranslucentDecor.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (C) 2013 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.launcher3;
-
-import android.app.Activity;
-import android.view.View;
-import android.view.Window;
-import android.view.WindowManager;
-
-public class TranslucentDecor {
-    private static final int SYSTEM_UI_FLAG_TRANSPARENT_STATUS = 0x00001000;
-    private static final int SYSTEM_UI_FLAG_TRANSPARENT_NAVIGATION = 0x00002000;
-
-    // Replace with SDK constants when available.
-    public static final int FLAG_TRANSLUCENT_STATUS = 0x04000000;
-    public static final int FLAG_TRANSLUCENT_NAVIGATION = 0x08000000;
-
-    // Behave properly on early K builds.
-    public static final boolean SYSUI_SUPPORTED = !hasSystemUiFlag("ALLOW_TRANSIENT") &&
-            hasSystemUiFlag("TRANSPARENT_STATUS") &&
-            hasSystemUiFlag("TRANSPARENT_NAVIGATION");
-
-    public static final boolean WM_SUPPORTED =
-            hasWindowManagerFlag("TRANSLUCENT_STATUS") &&
-            hasWindowManagerFlag("TRANSLUCENT_NAVIGATION");
-
-    private final View mTarget;
-
-    public TranslucentDecor(View target) {
-        mTarget = target;
-    }
-
-    public void requestTranslucentDecor(boolean translucent) {
-        int sysui = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
-                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
-                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
-        if (WM_SUPPORTED && mTarget.getContext() instanceof Activity) {
-            Window w = ((Activity) mTarget.getContext()).getWindow();
-            int wmFlags = FLAG_TRANSLUCENT_STATUS | FLAG_TRANSLUCENT_NAVIGATION;
-            if (translucent) {
-                w.addFlags(wmFlags);
-            } else {
-               w.clearFlags(wmFlags);
-            }
-        } else if (SYSUI_SUPPORTED) {  // Remove when droidfood platform is updated
-            if (translucent) {
-                sysui |= SYSTEM_UI_FLAG_TRANSPARENT_STATUS | SYSTEM_UI_FLAG_TRANSPARENT_NAVIGATION;
-            }
-        }
-        mTarget.setSystemUiVisibility(sysui);
-    }
-
-    private static boolean hasWindowManagerFlag(String name) {
-        try {
-            return WindowManager.LayoutParams.class.getField("FLAG_" + name) != null;
-        } catch (NoSuchFieldException e) {
-            return false;
-        }
-    }
-
-    private static boolean hasSystemUiFlag(String name) {
-        try {
-            return View.class.getField("SYSTEM_UI_FLAG_" + name) != null;
-        } catch (NoSuchFieldException e) {
-            return false;
-        }
-    }
-}
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 4515b03..bbe0946 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -538,7 +538,14 @@
 
         // Ensure that the current page and default page are maintained.
         mDefaultPage = mOriginalDefaultPage + 1;
-        setCurrentPage(getCurrentPage() + 1);
+
+        // Update the custom content hint
+        mLauncher.updateCustomContentHintVisibility();
+        if (mRestorePage != INVALID_RESTORE_PAGE) {
+            mRestorePage = mRestorePage + 1;
+        } else {
+            setCurrentPage(getCurrentPage() + 1);
+        }
     }
 
     public void removeCustomContentPage() {
@@ -550,11 +557,24 @@
         mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID);
         mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID);
         removeView(customScreen);
+
+        if (mCustomContentCallbacks != null) {
+            mCustomContentCallbacks.onScrollProgressChanged(0);
+            mCustomContentCallbacks.onHide();
+        }
+
         mCustomContentCallbacks = null;
 
         // Ensure that the current page and default page are maintained.
         mDefaultPage = mOriginalDefaultPage - 1;
-        setCurrentPage(getCurrentPage() - 1);
+
+        // Update the custom content hint
+        mLauncher.updateCustomContentHintVisibility();
+        if (mRestorePage != INVALID_RESTORE_PAGE) {
+            mRestorePage = mRestorePage - 1;
+        } else {
+            setCurrentPage(getCurrentPage() - 1);
+        }
     }
 
     public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks,
@@ -3716,6 +3736,10 @@
         return mDragInfo;
     }
 
+    public int getRestorePage() {
+        return getNextPage() - numCustomPages();
+    }
+
     /**
      * Calculate the nearest cell where the given object would be dropped.
      *