Generate example of GuidedStepSupportFragment

Bug: 21504593

Change-Id: Ifafc4a956060def75bc01d45eeb568d3077a977e
diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml
index 9b29b02..d278b03 100644
--- a/samples/SupportLeanbackDemos/AndroidManifest.xml
+++ b/samples/SupportLeanbackDemos/AndroidManifest.xml
@@ -63,6 +63,11 @@
             android:windowSoftInputMode="adjustResize"
             android:exported="true" />
 
+        <activity android:name="GuidedStepSupportActivity"
+            android:theme="@style/Theme.Example.Leanback.GuidedStep"
+            android:windowSoftInputMode="adjustResize"
+            android:exported="true" />
+
         <activity android:name="DetailsPresenterSelectionActivity"
             android:theme="@style/Theme.Example.Leanback.GuidedStep"
             android:exported="true" />
diff --git a/samples/SupportLeanbackDemos/generatev4.py b/samples/SupportLeanbackDemos/generatev4.py
new file mode 100755
index 0000000..9c3de08
--- /dev/null
+++ b/samples/SupportLeanbackDemos/generatev4.py
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+
+# 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.
+
+import os
+import sys
+
+file = open('src/com/example/android/leanback/GuidedStepActivity.java', 'r')
+outfile = open('src/com/example/android/leanback/GuidedStepSupportActivity.java', 'w')
+
+outfile.write("/* This file is auto-generated from GuidedStepActivity.  DO NOT MODIFY. */\n\n")
+
+for line in file:
+    line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
+    line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
+    line = line.replace('GuidedStepFragment', 'GuidedStepSupportFragment')
+    line = line.replace('GuidedStepActivity', 'GuidedStepSupportActivity')
+    line = line.replace('extends Activity', 'extends FragmentActivity')
+    line = line.replace('Activity activity', 'FragmentActivity activity')
+    outfile.write(line)
+file.close()
+outfile.close()
diff --git a/samples/SupportLeanbackDemos/res/values/strings.xml b/samples/SupportLeanbackDemos/res/values/strings.xml
index 328e83f..45cb779 100644
--- a/samples/SupportLeanbackDemos/res/values/strings.xml
+++ b/samples/SupportLeanbackDemos/res/values/strings.xml
@@ -35,6 +35,8 @@
     <string name="vgrid_description">VerticalGridView test</string>
     <string name="guidedstep">Guided Step</string>
     <string name="guidedstep_description">GuidedStepFragment test</string>
+    <string name="guidedstepsupport">Guided Step(support version)</string>
+    <string name="guidedstepsupport_description">GuidedStepSupportFragment test</string>
     <string name="browseerror">Browse Error</string>
     <string name="browseerror_description">BrowseError test</string>
     <string name="detail_presenter_options">Choose Presenter for Details</string>
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java
new file mode 100644
index 0000000..a7f599d
--- /dev/null
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java
@@ -0,0 +1,271 @@
+/* This file is auto-generated from GuidedStepActivity.  DO NOT MODIFY. */
+
+/*
+ * Copyright (C) 2014 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.leanback;
+
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.support.v17.leanback.app.GuidedStepSupportFragment;
+import android.support.v17.leanback.widget.GuidedAction;
+import android.support.v17.leanback.widget.GuidanceStylist;
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.util.Log;
+import android.view.ViewGroup;
+import android.view.ViewTreeObserver.OnGlobalLayoutListener;
+
+import java.util.List;
+
+/**
+ * Activity that showcases different aspects of GuidedStepSupportFragments.
+ */
+public class GuidedStepSupportActivity extends FragmentActivity {
+
+    private static final int CONTINUE = 1;
+    private static final int BACK = 2;
+
+    private static final int FIRST_NAME = 1;
+    private static final int LAST_NAME = 2;
+
+    private static final int OPTION_CHECK_SET_ID = 10;
+    private static final int DEFAULT_OPTION = 0;
+    private static final String[] OPTION_NAMES = { "Option A", "Option B", "Option C" };
+    private static final String[] OPTION_DESCRIPTIONS = { "Here's one thing you can do",
+            "Here's another thing you can do", "Here's one more thing you can do" };
+    private static final int[] OPTION_DRAWABLES = { R.drawable.ic_guidedstep_option_a,
+            R.drawable.ic_guidedstep_option_b, R.drawable.ic_guidedstep_option_c };
+
+    private static final String TAG = GuidedStepSupportActivity.class.getSimpleName();
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        Log.v(TAG, "onCreate");
+        super.onCreate(savedInstanceState);
+        GuidedStepSupportFragment.addAsRoot(this, new FirstStepFragment(), android.R.id.content);
+    }
+
+    @Override
+    public void onConfigurationChanged(Configuration newConfig) {
+        Log.v(TAG, "onConfigurationChanged");
+        super.onConfigurationChanged(newConfig);
+    }
+
+    @Override
+    protected void onSaveInstanceState(Bundle outState) {
+        Log.v(TAG, "onSaveInstanceState");
+        super.onSaveInstanceState(outState);
+    }
+
+    @Override
+    protected void onRestoreInstanceState(Bundle savedInstanceState) {
+        Log.v(TAG, "onRestoreInstanceState");
+        super.onRestoreInstanceState(savedInstanceState);
+    }
+
+    private static void addAction(List<GuidedAction> actions, long id, String title, String desc) {
+        actions.add(new GuidedAction.Builder()
+                .id(id)
+                .title(title)
+                .description(desc)
+                .build());
+    }
+
+    private static void addEditableAction(List<GuidedAction> actions, long id, String title, String desc) {
+        actions.add(new GuidedAction.Builder()
+                .id(id)
+                .title(title)
+                .description(desc)
+                .editable(true)
+                .build());
+    }
+
+    private static void addCheckedAction(List<GuidedAction> actions, int iconResId, Context context,
+            String title, String desc) {
+        actions.add(new GuidedAction.Builder()
+                .title(title)
+                .description(desc)
+                .checkSetId(OPTION_CHECK_SET_ID)
+                .iconResourceId(iconResId, context)
+                .build());
+    }
+
+    /**
+     * The first fragment is instantiated via XML, so it must be public.
+     */
+    public static class FirstStepFragment extends GuidedStepSupportFragment {
+        @Override
+        public int onProvideTheme() {
+            return R.style.Theme_Example_Leanback_GuidedStep_First;
+        }
+
+        @Override
+        public Guidance onCreateGuidance(Bundle savedInstanceState) {
+            String title = getString(R.string.guidedstep_first_title);
+            String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
+            String description = getString(R.string.guidedstep_first_description);
+            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
+            return new Guidance(title, description, breadcrumb, icon);
+        }
+
+        @Override
+        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+            addAction(actions, CONTINUE, "Continue", "Let's do it");
+            addAction(actions, BACK, "Cancel", "Nevermind");
+        }
+
+        @Override
+        public void onGuidedActionClicked(GuidedAction action) {
+            FragmentManager fm = getFragmentManager();
+            if (action.getId() == CONTINUE) {
+                GuidedStepSupportFragment.add(fm, new SecondStepFragment(), android.R.id.content);
+            } else {
+                getActivity().finish();
+            }
+        }
+    }
+
+    public static class SecondStepFragment extends GuidedStepSupportFragment {
+
+        @Override
+        public Guidance onCreateGuidance(Bundle savedInstanceState) {
+            String title = getString(R.string.guidedstep_second_title);
+            String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
+            String description = getString(R.string.guidedstep_second_description);
+            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
+            return new Guidance(title, description, breadcrumb, icon);
+        }
+
+        @Override
+        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+            addEditableAction(actions, FIRST_NAME, "Pat", "Your first name");
+            addEditableAction(actions, LAST_NAME, "Smith", "Your last name");
+        }
+
+        @Override
+        public void onGuidedActionClicked(GuidedAction action) {
+            if (action.getId() == LAST_NAME) {
+                FragmentManager fm = getFragmentManager();
+                GuidedStepSupportFragment.add(fm, new ThirdStepFragment());
+            }
+        }
+
+    }
+
+    public static class ThirdStepFragment extends GuidedStepSupportFragment {
+
+        private int mSelectedOption = DEFAULT_OPTION;
+
+        @Override
+        public Guidance onCreateGuidance(Bundle savedInstanceState) {
+            String title = getString(R.string.guidedstep_third_title);
+            String breadcrumb = getString(R.string.guidedstep_third_breadcrumb);
+            String description = getString(R.string.guidedstep_third_description);
+            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
+            return new Guidance(title, description, breadcrumb, icon);
+        }
+
+        @Override
+        public GuidanceStylist onCreateGuidanceStylist() {
+            return new GuidanceStylist() {
+                @Override
+                public int onProvideLayoutId() {
+                    return R.layout.guidedstep_second_guidance;
+                }
+            };
+        }
+
+        @Override
+        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+            String desc = "The description can be quite long as well.  ";
+            desc += "Just be sure to set multilineDescription to true in the GuidedAction.";
+            actions.add(new GuidedAction.Builder()
+                    .title("Note that Guided Actions can have titles that are quite long.")
+                    .description(desc)
+                    .multilineDescription(true)
+                    .infoOnly(true)
+                    .enabled(false)
+                    .build());
+            for (int i = 0; i < OPTION_NAMES.length; i++) {
+                addCheckedAction(actions, OPTION_DRAWABLES[i], getActivity(), OPTION_NAMES[i],
+                        OPTION_DESCRIPTIONS[i]);
+                if (i == DEFAULT_OPTION) {
+                    actions.get(actions.size() -1).setChecked(true);
+                }
+            }
+            addAction(actions, CONTINUE, "Continue", "");
+        }
+
+        @Override
+        public void onGuidedActionClicked(GuidedAction action) {
+            if (action.getId() == CONTINUE) {
+                FragmentManager fm = getFragmentManager();
+                FourthStepFragment f = new FourthStepFragment();
+                Bundle arguments = new Bundle();
+                arguments.putInt(FourthStepFragment.EXTRA_OPTION, mSelectedOption);
+                f.setArguments(arguments);
+                GuidedStepSupportFragment.add(fm, f, android.R.id.content);
+            } else {
+                mSelectedOption = getSelectedActionPosition()-1;
+            }
+        }
+
+    }
+
+    public static class FourthStepFragment extends GuidedStepSupportFragment {
+        public static final String EXTRA_OPTION = "extra_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[getOption()];
+            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
+            return new Guidance(title, description, breadcrumb, icon);
+        }
+
+        @Override
+        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+            addAction(actions, CONTINUE, "Done", "All finished");
+            addAction(actions, BACK, "Back", "Forgot something...");
+        }
+
+        @Override
+        public void onGuidedActionClicked(GuidedAction action) {
+            if (action.getId() == CONTINUE) {
+                getActivity().finish();
+            } else {
+                getFragmentManager().popBackStack();
+            }
+        }
+
+    }
+
+}
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java
index 57723e2..244e44f 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java
@@ -73,6 +73,8 @@
                     R.string.vgrid_description);
             addAction(actions, GuidedStepActivity.class, R.string.guidedstep,
                     R.string.guidedstep_description);
+            addAction(actions, GuidedStepSupportActivity.class, R.string.guidedstepsupport,
+                    R.string.guidedstepsupport_description);
             addAction(actions, BrowseErrorActivity.class, R.string.browseerror,
                     R.string.browseerror_description);
             addAction(actions, DetailsPresenterSelectionActivity.class,