Add usage of hideable BottomSheetBehavior

Change-Id: I32ef3bea0594a803af4c551dd0c81b64d75c60cc
diff --git a/samples/SupportDesignDemos/AndroidManifest.xml b/samples/SupportDesignDemos/AndroidManifest.xml
index 3a4dbdf..3cae8ad 100644
--- a/samples/SupportDesignDemos/AndroidManifest.xml
+++ b/samples/SupportDesignDemos/AndroidManifest.xml
@@ -199,8 +199,17 @@
             </intent-filter>
         </activity>
 
-        <activity android:name=".widget.BottomSheetUsage"
-                  android:label="@string/design_bottomsheet"
+        <activity android:name=".widget.BottomSheetPersistent"
+                  android:label="@string/design_bottomsheet_persistent"
+                  android:theme="@style/Theme.Design">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.example.android.support.design.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
+        <activity android:name=".widget.BottomSheetHideable"
+                  android:label="@string/design_bottomsheet_hideable"
                   android:theme="@style/Theme.Design">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
diff --git a/samples/SupportDesignDemos/res/layout/design_bottom_sheet.xml b/samples/SupportDesignDemos/res/layout/design_bottom_sheet.xml
deleted file mode 100644
index 56197ea..0000000
--- a/samples/SupportDesignDemos/res/layout/design_bottom_sheet.xml
+++ /dev/null
@@ -1,72 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2015 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.
--->
-<android.support.design.widget.CoordinatorLayout
-        android:id="@+id/coordinator"
-        xmlns:android="http://schemas.android.com/apk/res/android"
-        xmlns:app="http://schemas.android.com/apk/res/com.example.android.support.design"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent">
-
-    <!--
-        This is the bottom sheet. You can use any View as a bottom sheet by marking it with
-        app:layout_behavior as BottomSheetBehavior.
-    -->
-    <LinearLayout
-            android:id="@+id/bottom_sheet"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:layout_marginLeft="@dimen/bottom_sheet_horizontal_margin"
-            android:layout_marginRight="@dimen/bottom_sheet_horizontal_margin"
-            android:background="?android:attr/windowBackground"
-            android:elevation="16dp"
-            android:minHeight="128dp"
-            android:orientation="vertical"
-            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
-            app:behavior_peekHeight="128dp">
-
-        <TextView
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:text="@string/bottom_sheet"
-                android:paddingTop="8dp"
-                android:paddingLeft="16dp"
-                android:paddingRight="16dp"
-                android:paddingBottom="8dp"
-                style="@style/TextAppearance.AppCompat.Title"/>
-
-        <View
-                android:layout_width="match_parent"
-                android:layout_height="1dp"
-                android:background="@android:color/darker_gray"/>
-
-        <android.support.v4.widget.NestedScrollView
-                android:id="@+id/scroll"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent">
-
-            <TextView
-                    android:id="@+id/dialogue"
-                    android:layout_width="match_parent"
-                    android:layout_height="wrap_content"
-                    android:padding="16dp"
-                    style="@style/TextAppearance.AppCompat.Body1"/>
-
-        </android.support.v4.widget.NestedScrollView>
-
-    </LinearLayout>
-
-</android.support.design.widget.CoordinatorLayout>
\ No newline at end of file
diff --git a/samples/SupportDesignDemos/res/layout/design_bottom_sheet_hideable.xml b/samples/SupportDesignDemos/res/layout/design_bottom_sheet_hideable.xml
new file mode 100644
index 0000000..18a543e
--- /dev/null
+++ b/samples/SupportDesignDemos/res/layout/design_bottom_sheet_hideable.xml
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2015 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.
+-->
+<android.support.design.widget.CoordinatorLayout
+        android:id="@+id/coordinator"
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        xmlns:app="http://schemas.android.com/apk/res/com.example.android.support.design"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+    <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:orientation="vertical"
+            android:padding="16dp">
+
+        <TextView
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/bottomsheet_hideable"
+                style="@style/TextAppearance.AppCompat.Headline"/>
+
+        <Button
+                android:id="@+id/toggle"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
+                android:freezesText="true"
+                android:text="@string/bottomsheet_hide"/>
+
+    </LinearLayout>
+
+    <!--
+        This is the bottom sheet. You can use any View as a bottom sheet by marking it with
+        app:layout_behavior as BottomSheetBehavior.
+    -->
+    <LinearLayout
+            android:id="@+id/bottom_sheet"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:layout_marginLeft="@dimen/bottom_sheet_horizontal_margin"
+            android:layout_marginRight="@dimen/bottom_sheet_horizontal_margin"
+            android:background="?android:attr/windowBackground"
+            android:elevation="@dimen/bottom_sheet_elevation"
+            android:minHeight="@dimen/bottom_sheet_peek_height"
+            android:orientation="vertical"
+            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
+            app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"
+            app:behavior_hideable="true">
+
+        <include layout="@layout/include_bottom_sheet"/>
+
+    </LinearLayout>
+
+    <TextView
+            android:id="@+id/slide_offset"
+            android:layout_width="128dp"
+            android:layout_height="wrap_content"
+            android:layout_gravity="top|right"
+            android:padding="8dp"
+            android:elevation="24dp"
+            android:clickable="false"
+            style="@style/TextAppearance.AppCompat.Body1"/>
+
+</android.support.design.widget.CoordinatorLayout>
diff --git a/samples/SupportDesignDemos/res/layout/design_bottom_sheet_persistent.xml b/samples/SupportDesignDemos/res/layout/design_bottom_sheet_persistent.xml
new file mode 100644
index 0000000..5a8139cd
--- /dev/null
+++ b/samples/SupportDesignDemos/res/layout/design_bottom_sheet_persistent.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2015 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.
+-->
+<android.support.design.widget.CoordinatorLayout
+        android:id="@+id/coordinator"
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        xmlns:app="http://schemas.android.com/apk/res/com.example.android.support.design"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+    <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:text="@string/bottomsheet_persistent"
+            android:padding="16dp"
+            style="@style/TextAppearance.AppCompat.Headline"/>
+
+    <!--
+        This is the bottom sheet. You can use any View as a bottom sheet by marking it with
+        app:layout_behavior as BottomSheetBehavior.
+    -->
+    <LinearLayout
+            android:id="@+id/bottom_sheet"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:layout_marginLeft="@dimen/bottom_sheet_horizontal_margin"
+            android:layout_marginRight="@dimen/bottom_sheet_horizontal_margin"
+            android:background="?android:attr/windowBackground"
+            android:elevation="@dimen/bottom_sheet_elevation"
+            android:minHeight="@dimen/bottom_sheet_peek_height"
+            android:orientation="vertical"
+            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
+            app:behavior_peekHeight="@dimen/bottom_sheet_peek_height">
+
+        <include layout="@layout/include_bottom_sheet"/>
+
+    </LinearLayout>
+
+</android.support.design.widget.CoordinatorLayout>
diff --git a/samples/SupportDesignDemos/res/layout/include_bottom_sheet.xml b/samples/SupportDesignDemos/res/layout/include_bottom_sheet.xml
new file mode 100644
index 0000000..ab00ece
--- /dev/null
+++ b/samples/SupportDesignDemos/res/layout/include_bottom_sheet.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2015 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:tools="http://schemas.android.com/tools"
+       xmlns:android="http://schemas.android.com/apk/res/android"
+       tools:showIn="@layout/design_bottom_sheet">
+
+    <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:text="@string/bottom_sheet"
+            android:paddingTop="8dp"
+            android:paddingLeft="16dp"
+            android:paddingRight="16dp"
+            android:paddingBottom="8dp"
+            style="@style/TextAppearance.AppCompat.Title"/>
+
+    <View
+            android:layout_width="match_parent"
+            android:layout_height="1dp"
+            android:background="@android:color/darker_gray"/>
+
+    <android.support.v4.widget.NestedScrollView
+            android:id="@+id/scroll"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent">
+
+        <TextView
+                android:id="@+id/dialogue"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:padding="16dp"
+                style="@style/TextAppearance.AppCompat.Body1"/>
+
+    </android.support.v4.widget.NestedScrollView>
+
+</merge>
diff --git a/samples/SupportDesignDemos/res/values/dimens.xml b/samples/SupportDesignDemos/res/values/dimens.xml
index ac8777c..dc3a5f6 100644
--- a/samples/SupportDesignDemos/res/values/dimens.xml
+++ b/samples/SupportDesignDemos/res/values/dimens.xml
@@ -16,4 +16,6 @@
 -->
 <resources>
     <dimen name="bottom_sheet_horizontal_margin">0dp</dimen>
+    <dimen name="bottom_sheet_elevation">16dp</dimen>
+    <dimen name="bottom_sheet_peek_height">128dp</dimen>
 </resources>
diff --git a/samples/SupportDesignDemos/res/values/strings.xml b/samples/SupportDesignDemos/res/values/strings.xml
index c3a981a..4c660ba 100644
--- a/samples/SupportDesignDemos/res/values/strings.xml
+++ b/samples/SupportDesignDemos/res/values/strings.xml
@@ -21,7 +21,8 @@
     <string name="design_navigation">NavigationView/Usage</string>
     <string name="design_tabs_basic">TabLayout/Usage</string>
     <string name="design_text_input">Text Input</string>
-    <string name="design_bottomsheet">BottomSheet/Usage</string>
+    <string name="design_bottomsheet_persistent">BottomSheet/Persistent</string>
+    <string name="design_bottomsheet_hideable">BottomSheet/Hideable</string>
 
     <string name="fab_size_normal">Normal size</string>
     <string name="fab_size_mini">Mini size</string>
@@ -83,5 +84,9 @@
     <string name="menu_settings">Settings</string>
 
     <string name="bottom_sheet">Bottom sheet</string>
+    <string name="bottomsheet_persistent">Persistent</string>
+    <string name="bottomsheet_hideable">Hideable</string>
+    <string name="bottomsheet_hide">Hide</string>
+    <string name="bottomsheet_show">Show</string>
 
 </resources>
diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetHideable.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetHideable.java
new file mode 100644
index 0000000..c3d1960
--- /dev/null
+++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetHideable.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2015 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.support.design.widget;
+
+import com.example.android.support.design.R;
+
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.support.design.widget.BottomSheetBehavior;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+
+
+/**
+ * This demonstrates basic usage of hideable {@link BottomSheetBehavior}.
+ */
+public class BottomSheetHideable extends BottomSheetUsageBase {
+
+    private TextView mTextSlideOffset;
+
+    private Button mToggle;
+
+    @Override
+    protected int getLayoutId() {
+        return R.layout.design_bottom_sheet_hideable;
+    }
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        mTextSlideOffset = (TextView) findViewById(R.id.slide_offset);
+        mToggle = (Button) findViewById(R.id.toggle);
+        mToggle.setOnClickListener(mOnClickListener);
+        mBehavior.setBottomSheetListener(new BottomSheetBehavior.BottomSheetListener() {
+            @Override
+            public void onStateChanged(@BottomSheetBehavior.State int newState) {
+                switch (newState) {
+                    case BottomSheetBehavior.STATE_HIDDEN:
+                        mToggle.setText(R.string.bottomsheet_show);
+                        mToggle.setEnabled(true);
+                        break;
+                    case BottomSheetBehavior.STATE_EXPANDED:
+                    case BottomSheetBehavior.STATE_COLLAPSED:
+                        mToggle.setText(R.string.bottomsheet_hide);
+                        mToggle.setEnabled(true);
+                        break;
+                }
+            }
+
+            @Override
+            public void onSlide(float slideOffset) {
+                mTextSlideOffset.setText(String.valueOf(slideOffset));
+            }
+        });
+    }
+
+    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            if (v.getId() == R.id.toggle && mBehavior != null) {
+                mToggle.setEnabled(false);
+                if (mBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
+                    mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
+                } else {
+                    mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
+                }
+            }
+        }
+    };
+
+}
diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetPersistent.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetPersistent.java
new file mode 100644
index 0000000..82b6c7d
--- /dev/null
+++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetPersistent.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2015 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.support.design.widget;
+
+import com.example.android.support.design.R;
+
+import android.support.design.widget.BottomSheetBehavior;
+
+
+/**
+ * This demonstrates basic usage of persistent {@link BottomSheetBehavior}.
+ */
+public class BottomSheetPersistent extends BottomSheetUsageBase {
+
+    @Override
+    protected int getLayoutId() {
+        return R.layout.design_bottom_sheet_persistent;
+    }
+
+}
diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetUsage.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetUsageBase.java
similarity index 71%
rename from samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetUsage.java
rename to samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetUsageBase.java
index 21f6ffe..f13e78b 100644
--- a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetUsage.java
+++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/BottomSheetUsageBase.java
@@ -20,6 +20,7 @@
 import com.example.android.support.design.Shakespeare;
 
 import android.os.Bundle;
+import android.support.annotation.LayoutRes;
 import android.support.annotation.Nullable;
 import android.support.design.widget.BottomSheetBehavior;
 import android.support.v7.app.AppCompatActivity;
@@ -30,25 +31,31 @@
 /**
  * This demonstrates basic usage of {@link BottomSheetBehavior}.
  */
-public class BottomSheetUsage extends AppCompatActivity {
+abstract class BottomSheetUsageBase extends AppCompatActivity {
 
-    private BottomSheetBehavior<LinearLayout> mBehavior;
+    protected BottomSheetBehavior<LinearLayout> mBehavior;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        setContentView(R.layout.design_bottom_sheet);
+        setContentView(getLayoutId());
         ((TextView) findViewById(R.id.dialogue)).setText(TextUtils.concat(Shakespeare.DIALOGUE));
         mBehavior = BottomSheetBehavior.from((LinearLayout) findViewById(R.id.bottom_sheet));
     }
 
     @Override
     public void onBackPressed() {
-        if (mBehavior != null && mBehavior.getState() != BottomSheetBehavior.STATE_COLLAPSED) {
-            mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
-        } else {
-            super.onBackPressed();
+        if (mBehavior != null) {
+            int state = mBehavior.getState();
+            if (state != BottomSheetBehavior.STATE_COLLAPSED &&
+                    state != BottomSheetBehavior.STATE_HIDDEN) {
+                mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
+                return;
+            }
         }
+        super.onBackPressed();
     }
 
+    protected abstract int getLayoutId();
+
 }