GuidedStepSupportActivity.java revision ab1d3dce4807789e76bb7c4c7b6693d5aa993b1b
1/* This file is auto-generated from GuidedStepActivity.  DO NOT MODIFY. */
2
3/*
4 * Copyright (C) 2014 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package com.example.android.leanback;
20
21import android.support.v4.app.FragmentActivity;
22import android.support.v4.app.FragmentManager;
23import android.content.Context;
24import android.content.Intent;
25import android.content.res.Configuration;
26import android.graphics.drawable.Drawable;
27import android.os.Bundle;
28import android.support.v17.leanback.app.GuidedStepSupportFragment;
29import android.support.v17.leanback.widget.GuidedAction;
30import android.support.v17.leanback.widget.GuidanceStylist;
31import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
32import android.util.Log;
33import android.view.ViewGroup;
34import android.view.ViewTreeObserver.OnGlobalLayoutListener;
35
36import java.util.List;
37
38/**
39 * Activity that showcases different aspects of GuidedStepSupportFragments.
40 */
41public class GuidedStepSupportActivity extends FragmentActivity {
42
43    private static final int CONTINUE = 1;
44    private static final int BACK = 2;
45
46    private static final int FIRST_NAME = 1;
47    private static final int LAST_NAME = 2;
48
49    private static final int OPTION_CHECK_SET_ID = 10;
50    private static final int DEFAULT_OPTION = 0;
51    private static final String[] OPTION_NAMES = { "Option A", "Option B", "Option C" };
52    private static final String[] OPTION_DESCRIPTIONS = { "Here's one thing you can do",
53            "Here's another thing you can do", "Here's one more thing you can do" };
54    private static final int[] OPTION_DRAWABLES = { R.drawable.ic_guidedstep_option_a,
55            R.drawable.ic_guidedstep_option_b, R.drawable.ic_guidedstep_option_c };
56
57    private static final String TAG = GuidedStepSupportActivity.class.getSimpleName();
58
59    @Override
60    protected void onCreate(Bundle savedInstanceState) {
61        Log.v(TAG, "onCreate");
62        super.onCreate(savedInstanceState);
63        GuidedStepSupportFragment.addAsRoot(this, new FirstStepFragment(), android.R.id.content);
64    }
65
66    @Override
67    public void onConfigurationChanged(Configuration newConfig) {
68        Log.v(TAG, "onConfigurationChanged");
69        super.onConfigurationChanged(newConfig);
70    }
71
72    @Override
73    protected void onSaveInstanceState(Bundle outState) {
74        Log.v(TAG, "onSaveInstanceState");
75        super.onSaveInstanceState(outState);
76    }
77
78    @Override
79    protected void onRestoreInstanceState(Bundle savedInstanceState) {
80        Log.v(TAG, "onRestoreInstanceState");
81        super.onRestoreInstanceState(savedInstanceState);
82    }
83
84    private static void addAction(List<GuidedAction> actions, long id, String title, String desc) {
85        actions.add(new GuidedAction.Builder()
86                .id(id)
87                .title(title)
88                .description(desc)
89                .build());
90    }
91
92    private static void addEditableAction(List<GuidedAction> actions, long id, String title, String desc) {
93        actions.add(new GuidedAction.Builder()
94                .id(id)
95                .title(title)
96                .description(desc)
97                .editable(true)
98                .build());
99    }
100
101    private static void addCheckedAction(List<GuidedAction> actions, int iconResId, Context context,
102            String title, String desc) {
103        actions.add(new GuidedAction.Builder()
104                .title(title)
105                .description(desc)
106                .checkSetId(OPTION_CHECK_SET_ID)
107                .iconResourceId(iconResId, context)
108                .build());
109    }
110
111    /**
112     * The first fragment is instantiated via XML, so it must be public.
113     */
114    public static class FirstStepFragment extends GuidedStepSupportFragment {
115        @Override
116        public int onProvideTheme() {
117            return R.style.Theme_Example_Leanback_GuidedStep_First;
118        }
119
120        @Override
121        public Guidance onCreateGuidance(Bundle savedInstanceState) {
122            String title = getString(R.string.guidedstep_first_title);
123            String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
124            String description = getString(R.string.guidedstep_first_description);
125            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
126            return new Guidance(title, description, breadcrumb, icon);
127        }
128
129        @Override
130        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
131            addAction(actions, CONTINUE, "Continue", "Let's do it");
132            addAction(actions, BACK, "Cancel", "Nevermind");
133        }
134
135        @Override
136        public void onGuidedActionClicked(GuidedAction action) {
137            FragmentManager fm = getFragmentManager();
138            if (action.getId() == CONTINUE) {
139                GuidedStepSupportFragment.add(fm, new SecondStepFragment(), android.R.id.content);
140            } else {
141                getActivity().finish();
142            }
143        }
144    }
145
146    public static class SecondStepFragment extends GuidedStepSupportFragment {
147
148        @Override
149        public Guidance onCreateGuidance(Bundle savedInstanceState) {
150            String title = getString(R.string.guidedstep_second_title);
151            String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
152            String description = getString(R.string.guidedstep_second_description);
153            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
154            return new Guidance(title, description, breadcrumb, icon);
155        }
156
157        @Override
158        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
159            addEditableAction(actions, FIRST_NAME, "Pat", "Your first name");
160            addEditableAction(actions, LAST_NAME, "Smith", "Your last name");
161        }
162
163        @Override
164        public void onGuidedActionClicked(GuidedAction action) {
165            if (action.getId() == LAST_NAME) {
166                FragmentManager fm = getFragmentManager();
167                GuidedStepSupportFragment.add(fm, new ThirdStepFragment());
168            }
169        }
170
171    }
172
173    public static class ThirdStepFragment extends GuidedStepSupportFragment {
174
175        private int mSelectedOption = DEFAULT_OPTION;
176
177        @Override
178        public Guidance onCreateGuidance(Bundle savedInstanceState) {
179            String title = getString(R.string.guidedstep_third_title);
180            String breadcrumb = getString(R.string.guidedstep_third_breadcrumb);
181            String description = getString(R.string.guidedstep_third_description);
182            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
183            return new Guidance(title, description, breadcrumb, icon);
184        }
185
186        @Override
187        public GuidanceStylist onCreateGuidanceStylist() {
188            return new GuidanceStylist() {
189                @Override
190                public int onProvideLayoutId() {
191                    return R.layout.guidedstep_second_guidance;
192                }
193            };
194        }
195
196        @Override
197        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
198            String desc = "The description can be quite long as well.  ";
199            desc += "Just be sure to set multilineDescription to true in the GuidedAction.";
200            actions.add(new GuidedAction.Builder()
201                    .title("Note that Guided Actions can have titles that are quite long.")
202                    .description(desc)
203                    .multilineDescription(true)
204                    .infoOnly(true)
205                    .enabled(false)
206                    .build());
207            for (int i = 0; i < OPTION_NAMES.length; i++) {
208                addCheckedAction(actions, OPTION_DRAWABLES[i], getActivity(), OPTION_NAMES[i],
209                        OPTION_DESCRIPTIONS[i]);
210                if (i == DEFAULT_OPTION) {
211                    actions.get(actions.size() -1).setChecked(true);
212                }
213            }
214            addAction(actions, CONTINUE, "Continue", "");
215        }
216
217        @Override
218        public void onGuidedActionClicked(GuidedAction action) {
219            if (action.getId() == CONTINUE) {
220                FragmentManager fm = getFragmentManager();
221                FourthStepFragment f = new FourthStepFragment();
222                Bundle arguments = new Bundle();
223                arguments.putInt(FourthStepFragment.EXTRA_OPTION, mSelectedOption);
224                f.setArguments(arguments);
225                GuidedStepSupportFragment.add(fm, f, android.R.id.content);
226            } else {
227                mSelectedOption = getSelectedActionPosition()-1;
228            }
229        }
230
231    }
232
233    public static class FourthStepFragment extends GuidedStepSupportFragment {
234        public static final String EXTRA_OPTION = "extra_option";
235
236        public FourthStepFragment() {
237        }
238
239        public int getOption() {
240            Bundle b = getArguments();
241            if (b == null) return 0;
242            return b.getInt(EXTRA_OPTION, 0);
243        }
244
245        @Override
246        public Guidance onCreateGuidance(Bundle savedInstanceState) {
247            String title = getString(R.string.guidedstep_fourth_title);
248            String breadcrumb = getString(R.string.guidedstep_fourth_breadcrumb);
249            String description = "You chose: " + OPTION_NAMES[getOption()];
250            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
251            return new Guidance(title, description, breadcrumb, icon);
252        }
253
254        @Override
255        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
256            addAction(actions, CONTINUE, "Done", "All finished");
257            addAction(actions, BACK, "Back", "Forgot something...");
258        }
259
260        @Override
261        public void onGuidedActionClicked(GuidedAction action) {
262            if (action.getId() == CONTINUE) {
263                getActivity().finish();
264            } else {
265                getFragmentManager().popBackStack();
266            }
267        }
268
269    }
270
271}
272