Add example of vertical grid transition and GuidedStepFragment

Added example of vertical grid transition

Added example of launching GuidedStepFragment from BrowseFragment.

Also fixed couple bug in GuidedStepActivity:
1. All fragment class should be public
2. Remove argument from constructor. Pass Arguments instead.

Bug: 23319507

Change-Id: Ia7b8d6669445be62f8f2a325cc460021ca392ded
diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml
index e382ed9..9b29b02 100644
--- a/samples/SupportLeanbackDemos/AndroidManifest.xml
+++ b/samples/SupportLeanbackDemos/AndroidManifest.xml
@@ -45,12 +45,14 @@
             android:exported="true" />
 
         <activity android:name="VerticalGridActivity"
+            android:theme="@style/Theme.Example.Leanback.VerticalGrid"
             android:exported="true" />
 
         <activity android:name="SearchActivity"
             android:exported="true" />
 
         <activity android:name="BrowseErrorActivity"
+            android:theme="@style/Theme.Example.Leanback.Browse"
             android:exported="true" />
 
         <activity android:name="HorizontalGridTestActivity"
diff --git a/samples/SupportLeanbackDemos/res/layout/browse.xml b/samples/SupportLeanbackDemos/res/layout/browse.xml
index 367337a..4d46233 100644
--- a/samples/SupportLeanbackDemos/res/layout/browse.xml
+++ b/samples/SupportLeanbackDemos/res/layout/browse.xml
@@ -27,4 +27,9 @@
 	    android:layout_height="match_parent"
 	/>
 
+  <!-- container for hosting GuidedStepFragment background -->
+  <FrameLayout android:id="@+id/lb_guidedstep_background"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent" />
+
 </FrameLayout>
diff --git a/samples/SupportLeanbackDemos/res/values/themes.xml b/samples/SupportLeanbackDemos/res/values/themes.xml
index 696d82c..1fdf46b 100644
--- a/samples/SupportLeanbackDemos/res/values/themes.xml
+++ b/samples/SupportLeanbackDemos/res/values/themes.xml
@@ -23,6 +23,8 @@
     </style>
     <style name="Theme.Example.Leanback.Details" parent="Theme.Leanback.Details">
     </style>
+    <style name="Theme.Example.Leanback.VerticalGrid" parent="Theme.Leanback.VerticalGrid">
+    </style>
     <style name="Theme.Example.Leanback.Rows" parent="Theme.Leanback">
         <item name="browseTitleTextStyle">@style/Widget.Example.Leanback.Title.Text</item>
         <item name="rowsVerticalGridStyle">@style/Widget.Example.Leanback.Rows.VerticalGridView</item>
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java
index 0a50951..f4c8044 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java
@@ -17,6 +17,7 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.support.v4.app.ActivityOptionsCompat;
+import android.support.v17.leanback.app.GuidedStepFragment;
 import android.support.v17.leanback.widget.ArrayObjectAdapter;
 import android.support.v17.leanback.widget.HeaderItem;
 import android.support.v17.leanback.widget.ImageCardView;
@@ -42,6 +43,10 @@
     private ArrayObjectAdapter mRowsAdapter;
     private BackgroundHelper mBackgroundHelper = new BackgroundHelper();
 
+    // For good performance, it's important to use a single instance of
+    // a card presenter for all rows using that presenter.
+    final static CardPresenter sCardPresenter = new CardPresenter();
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         Log.i(TAG, "onCreate");
@@ -77,17 +82,18 @@
             }
         });
         if (TEST_ENTRANCE_TRANSITION) {
-            // don't run entrance transition if Activity is restored.
+            // don't run entrance transition if fragment is restored.
             if (savedInstanceState == null) {
                 prepareEntranceTransition();
             }
-            // simulate delay loading data
-            new Handler().postDelayed(new Runnable() {
-                public void run() {
-                    startEntranceTransition();
-                }
-            }, 2000);
         }
+        // simulates in a real world use case  data being loaded two seconds later
+        new Handler().postDelayed(new Runnable() {
+            public void run() {
+                loadData();
+                startEntranceTransition();
+            }
+        }, 2000);
     }
 
     private void setupRows() {
@@ -100,25 +106,24 @@
 
         mRowsAdapter = new ArrayObjectAdapter(lrp);
 
-        // For good performance, it's important to use a single instance of
-        // a card presenter for all rows using that presenter.
-        final CardPresenter cardPresenter = new CardPresenter();
+        setAdapter(mRowsAdapter);
+    }
 
+    private void loadData() {
         for (int i = 0; i < NUM_ROWS; ++i) {
-            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
+            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(sCardPresenter);
             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
             listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_2));
             listRowAdapter.add(new PhotoItem("Android TV", "by Google", R.drawable.gallery_photo_3));
             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
-            listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_6));
+            listRowAdapter.add(new PhotoItem("This is a test", "Open GuidedStepFragment", R.drawable.gallery_photo_6));
             listRowAdapter.add(new PhotoItem("Android TV", "open RowsActivity", R.drawable.gallery_photo_7));
             listRowAdapter.add(new PhotoItem("Leanback", "open BrowseActivity", R.drawable.gallery_photo_8));
             HeaderItem header = new HeaderItem(i, "Row " + i);
             mRowsAdapter.add(new ListRow(header, listRowAdapter));
         }
 
-        setAdapter(mRowsAdapter);
     }
 
     private final class ItemViewClickedListener implements OnItemViewClickedListener {
@@ -128,7 +133,12 @@
 
             Intent intent;
             Bundle bundle;
-            if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) {
+            if (((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_6) {
+                GuidedStepFragment.add(getFragmentManager(),
+                        new GuidedStepActivity.FirstStepFragment(),
+                        android.R.id.content);
+                return;
+            } else if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_8) {
                 intent = new Intent(getActivity(), BrowseActivity.class);
                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
                         .toBundle();
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java
index ac0b671..b03c14c 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsPresenterSelectionActivity.java
@@ -49,7 +49,7 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        GuidedStepFragment.add(getFragmentManager(), new SetupFragment());
+        GuidedStepFragment.addAsRoot(this, new SetupFragment(), android.R.id.content);
     }
 
     private static void addAction(List<GuidedAction> actions, long id, String title, String desc) {
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java
index 1c19282..f189a05 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java
@@ -58,12 +58,7 @@
     protected void onCreate(Bundle savedInstanceState) {
         Log.v(TAG, "onCreate");
         super.onCreate(savedInstanceState);
-        GuidedStepFragment.add(getFragmentManager(), new FirstStepFragment());
-        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
-            @Override public void onGlobalLayout() {
-                //Log.v(TAG, "onGlobalLayout", new Exception());
-            }
-        });
+        GuidedStepFragment.addAsRoot(this, new FirstStepFragment(), android.R.id.content);
     }
 
     @Override
@@ -139,14 +134,14 @@
         public void onGuidedActionClicked(GuidedAction action) {
             FragmentManager fm = getFragmentManager();
             if (action.getId() == CONTINUE) {
-                GuidedStepFragment.add(fm, new SecondStepFragment());
+                GuidedStepFragment.add(fm, new SecondStepFragment(), android.R.id.content);
             } else {
                 getActivity().finish();
             }
         }
     }
 
-    private static class SecondStepFragment extends GuidedStepFragment {
+    public static class SecondStepFragment extends GuidedStepFragment {
 
         @Override
         public Guidance onCreateGuidance(Bundle savedInstanceState) {
@@ -173,7 +168,7 @@
 
     }
 
-    private static class ThirdStepFragment extends GuidedStepFragment {
+    public static class ThirdStepFragment extends GuidedStepFragment {
 
         private int mSelectedOption = DEFAULT_OPTION;
 
@@ -221,7 +216,11 @@
         public void onGuidedActionClicked(GuidedAction action) {
             if (action.getId() == CONTINUE) {
                 FragmentManager fm = getFragmentManager();
-                GuidedStepFragment.add(fm, new FourthStepFragment(mSelectedOption));
+                FourthStepFragment f = new FourthStepFragment();
+                Bundle arguments = new Bundle();
+                arguments.putInt(FourthStepFragment.EXTRA_OPTION, mSelectedOption);
+                f.setArguments(arguments);
+                GuidedStepFragment.add(fm, f, android.R.id.content);
             } else {
                 mSelectedOption = getSelectedActionPosition()-1;
             }
@@ -229,18 +228,23 @@
 
     }
 
-    private static class FourthStepFragment extends GuidedStepFragment {
-        private final int mOption;
+    public static class FourthStepFragment extends GuidedStepFragment {
+        public static final String EXTRA_OPTION = "extra_option";
 
-        public FourthStepFragment(int option) {
-            mOption = option;
+        public FourthStepFragment() {
+        }
+
+        public int getOption() {
+            Bundle b = getArguments();
+            if (b == null) return 0;
+            return b.getInt(EXTRA_OPTION, 0);
         }
 
         @Override
         public Guidance onCreateGuidance(Bundle savedInstanceState) {
             String title = getString(R.string.guidedstep_fourth_title);
             String breadcrumb = getString(R.string.guidedstep_fourth_breadcrumb);
-            String description = "You chose: " + OPTION_NAMES[mOption];
+            String description = "You chose: " + OPTION_NAMES[getOption()];
             Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
             return new Guidance(title, description, breadcrumb, icon);
         }
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java
index c1b60f4..57723e2 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java
@@ -20,6 +20,7 @@
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.support.v4.app.ActivityOptionsCompat;
 import android.support.v17.leanback.app.GuidedStepFragment;
 import android.support.v17.leanback.widget.GuidedAction;
 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
@@ -39,7 +40,8 @@
         super.onCreate(savedInstanceState);
 
         mGuidedStepFragment = new StepFragment();
-        GuidedStepFragment.add(getFragmentManager(), mGuidedStepFragment);
+        GuidedStepFragment.addAsRoot(this, mGuidedStepFragment, android.R.id.content);
+
     }
 
     public static class StepFragment extends GuidedStepFragment {
@@ -90,7 +92,9 @@
         public void onGuidedActionClicked(GuidedAction action) {
             Intent intent = action.getIntent();
             if (intent != null) {
-                startActivity(intent);
+                Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
+                        .toBundle();
+                startActivity(intent, bundle);
             }
         }
 
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java
index 9ffaf66..fe664dd 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java
@@ -15,6 +15,7 @@
 
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.Handler;
 import android.support.v17.leanback.widget.ArrayObjectAdapter;
 import android.support.v17.leanback.widget.Presenter;
 import android.support.v17.leanback.widget.RowPresenter;
@@ -34,6 +35,7 @@
     private static final int NUM_COLUMNS = 3;
     private static final int NUM_ITEMS = 50;
     private static final int HEIGHT = 200;
+    private static final boolean TEST_ENTRANCE_TRANSITION = true;
 
     private static class Adapter extends ArrayObjectAdapter {
         public Adapter(StringPresenter presenter) {
@@ -54,6 +56,25 @@
         setTitle("Leanback Vertical Grid Demo");
 
         setupFragment();
+        if (TEST_ENTRANCE_TRANSITION) {
+            // don't run entrance transition if fragment is restored.
+            if (savedInstanceState == null) {
+                prepareEntranceTransition();
+            }
+        }
+        // simulates in a real world use case  data being loaded two seconds later
+        new Handler().postDelayed(new Runnable() {
+            public void run() {
+                loadData();
+                startEntranceTransition();
+            }
+        }, 2000);
+    }
+
+    private void loadData() {
+        for (int i = 0; i < NUM_ITEMS; i++) {
+            mAdapter.add(Integer.toString(i));
+        }
     }
 
     private void setupFragment() {
@@ -62,9 +83,6 @@
         setGridPresenter(gridPresenter);
 
         mAdapter = new Adapter(new StringPresenter());
-        for (int i = 0; i < NUM_ITEMS; i++) {
-            mAdapter.add(Integer.toString(i));
-        }
         setAdapter(mAdapter);
 
         setOnItemViewSelectedListener(new OnItemViewSelectedListener() {