GuidedStepSupportActivity.java revision ee3c13ee552002263be40f6a5986f61c605b0bac
1/* This file is auto-generated from GuidedStepActivity.java.  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.text.InputType;
33import android.text.TextUtils;
34import android.util.Log;
35import android.view.ViewGroup;
36import android.view.ViewTreeObserver.OnGlobalLayoutListener;
37
38import java.util.List;
39
40/**
41 * Activity that showcases different aspects of GuidedStepSupportFragments.
42 */
43public class GuidedStepSupportActivity extends FragmentActivity {
44
45    private static final int BACK = 2;
46
47    private static final int FIRST_NAME = 3;
48    private static final int LAST_NAME = 4;
49    private static final int PASSWORD = 5;
50    private static final int PAYMENT = 6;
51
52    private static final int OPTION_CHECK_SET_ID = 10;
53    private static final int DEFAULT_OPTION = 0;
54    private static final String[] OPTION_NAMES = { "Option A", "Option B", "Option C" };
55    private static final String[] OPTION_DESCRIPTIONS = { "Here's one thing you can do",
56            "Here's another thing you can do", "Here's one more thing you can do" };
57    private static final int[] OPTION_DRAWABLES = { R.drawable.ic_guidedstep_option_a,
58            R.drawable.ic_guidedstep_option_b, R.drawable.ic_guidedstep_option_c };
59
60    private static final String TAG = GuidedStepSupportActivity.class.getSimpleName();
61
62    @Override
63    protected void onCreate(Bundle savedInstanceState) {
64        Log.v(TAG, "onCreate");
65        super.onCreate(savedInstanceState);
66        setContentView(R.layout.guided_step_activity);
67        GuidedStepSupportFragment.addAsRoot(this, new FirstStepFragment(), R.id.lb_guidedstep_host);
68    }
69
70    @Override
71    public void onConfigurationChanged(Configuration newConfig) {
72        Log.v(TAG, "onConfigurationChanged");
73        super.onConfigurationChanged(newConfig);
74    }
75
76    @Override
77    protected void onSaveInstanceState(Bundle outState) {
78        Log.v(TAG, "onSaveInstanceState");
79        super.onSaveInstanceState(outState);
80    }
81
82    @Override
83    protected void onRestoreInstanceState(Bundle savedInstanceState) {
84        Log.v(TAG, "onRestoreInstanceState");
85        super.onRestoreInstanceState(savedInstanceState);
86    }
87
88    private static void addAction(List<GuidedAction> actions, long id, String title, String desc) {
89        actions.add(new GuidedAction.Builder()
90                .id(id)
91                .title(title)
92                .description(desc)
93                .build());
94    }
95
96    private static void addEditableAction(List<GuidedAction> actions, long id, String title, String desc) {
97        actions.add(new GuidedAction.Builder()
98                .id(id)
99                .title(title)
100                .description(desc)
101                .editable(true)
102                .build());
103    }
104
105    private static void addEditableAction(List<GuidedAction> actions, long id, String title,
106            String editTitle, String desc) {
107        actions.add(new GuidedAction.Builder()
108                .id(id)
109                .title(title)
110                .editTitle(editTitle)
111                .description(desc)
112                .editable(true)
113                .build());
114    }
115
116    private static void addEditableAction(List<GuidedAction> actions, long id, String title,
117            String editTitle, int editInputType, String desc, String editDesc) {
118        actions.add(new GuidedAction.Builder()
119                .id(id)
120                .title(title)
121                .editTitle(editTitle)
122                .editInputType(editInputType)
123                .description(desc)
124                .editDescription(editDesc)
125                .editable(true)
126                .build());
127    }
128
129    private static void addEditableDescriptionAction(List<GuidedAction> actions, long id,
130            String title, String desc, String editDescription, int descriptionEditInputType) {
131        actions.add(new GuidedAction.Builder()
132                .id(id)
133                .title(title)
134                .description(desc)
135                .editDescription(editDescription)
136                .descriptionEditInputType(descriptionEditInputType)
137                .descriptionEditable(true)
138                .build());
139    }
140
141    private static void addCheckedAction(List<GuidedAction> actions, int iconResId, Context context,
142            String title, String desc) {
143        actions.add(new GuidedAction.Builder()
144                .title(title)
145                .description(desc)
146                .checkSetId(OPTION_CHECK_SET_ID)
147                .iconResourceId(iconResId, context)
148                .build());
149    }
150
151    public static class FirstStepFragment extends GuidedStepSupportFragment {
152        @Override
153        public int onProvideTheme() {
154            return R.style.Theme_Example_Leanback_GuidedStep_First;
155        }
156
157        @Override
158        public Guidance onCreateGuidance(Bundle savedInstanceState) {
159            String title = getString(R.string.guidedstep_first_title);
160            String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
161            String description = getString(R.string.guidedstep_first_description);
162            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
163            return new Guidance(title, description, breadcrumb, icon);
164        }
165
166        @Override
167        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
168            actions.add(new GuidedAction.Builder().constructContinue(getActivity())
169                    .description("Let's do it")
170                    .build());
171            actions.add(new GuidedAction.Builder().constructCancel(getActivity())
172                    .description("Never mind")
173                    .build());
174        }
175
176        @Override
177        public void onGuidedActionClicked(GuidedAction action) {
178            FragmentManager fm = getFragmentManager();
179            if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
180                GuidedStepSupportFragment.add(fm, new SecondStepFragment(), R.id.lb_guidedstep_host);
181            } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL){
182                finishGuidedStepSupportFragments();
183            }
184        }
185
186        @Override
187        protected int getContainerIdForBackground() {
188            return R.id.lb_guidedstep_background;
189        }
190    }
191
192    public static class SecondStepFragment extends GuidedStepSupportFragment {
193
194        @Override
195        public Guidance onCreateGuidance(Bundle savedInstanceState) {
196            String title = getString(R.string.guidedstep_second_title);
197            String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
198            String description = getString(R.string.guidedstep_second_description);
199            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
200            return new Guidance(title, description, breadcrumb, icon);
201        }
202
203        @Override
204        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
205            addEditableAction(actions, FIRST_NAME, "Pat", "Your first name");
206            addEditableAction(actions, LAST_NAME, "Smith", "Your last name");
207            addEditableAction(actions, PAYMENT, "Payment", "", InputType.TYPE_CLASS_NUMBER,
208                    "Input credit card number", "Input credit card number");
209            addEditableDescriptionAction(actions, PASSWORD, "Password", "", "",
210                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
211            actions.add(new GuidedAction.Builder().constructContinue(getActivity())
212                    .description("Continue")
213                    .build());
214            actions.get(actions.size() - 1).setEnabled(false);
215        }
216
217        @Override
218        public void onGuidedActionClicked(GuidedAction action) {
219            if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
220                FragmentManager fm = getFragmentManager();
221                GuidedStepSupportFragment.add(fm, new ThirdStepFragment(), R.id.lb_guidedstep_host);
222            }
223        }
224
225        @Override
226        public long onGuidedActionEditedAndProceed(GuidedAction action) {
227            if (action.getId() == PAYMENT) {
228                CharSequence editTitle = action.getEditTitle();
229                if (TextUtils.isDigitsOnly(editTitle) && editTitle.length() == 16) {
230                    editTitle = editTitle.subSequence(editTitle.length() - 4, editTitle.length());
231                    action.setDescription("Visa XXXX-XXXX-XXXX-"+editTitle);
232                    updateContinue(isPasswordValid());
233                    return GuidedAction.ACTION_ID_NEXT;
234                } else if (editTitle.length() == 0){
235                    action.setDescription("Input credit card number");
236                    updateContinue(false);
237                    return GuidedAction.ACTION_ID_CURRENT;
238                } else {
239                    action.setDescription("Error credit card number");
240                    updateContinue(false);
241                    return GuidedAction.ACTION_ID_CURRENT;
242                }
243            } else if (action.getId() == PASSWORD) {
244                CharSequence password = action.getEditDescription();
245                if (password.length() > 0) {
246                    if (isPaymentValid()) {
247                        updateContinue(true);
248                        return GuidedAction.ACTION_ID_NEXT;
249                    } else {
250                        updateContinue(false);
251                        return GuidedAction.ACTION_ID_CURRENT;
252                    }
253                } else {
254                    updateContinue(false);
255                    return GuidedAction.ACTION_ID_CURRENT;
256                }
257            }
258            return GuidedAction.ACTION_ID_NEXT;
259        }
260
261        boolean isPaymentValid() {
262            return findActionById(PAYMENT).getDescription().subSequence(0, 4).toString().equals("Visa");
263        }
264
265        boolean isPasswordValid() {
266            return findActionById(PASSWORD).getEditDescription().length() > 0;
267        }
268
269        void updateContinue(boolean enabled) {
270            findActionById(GuidedAction.ACTION_ID_CONTINUE).setEnabled(enabled);
271            notifyActionChanged(findActionPositionById(GuidedAction.ACTION_ID_CONTINUE));
272        }
273    }
274
275    public static class ThirdStepFragment extends GuidedStepSupportFragment {
276
277        private int mSelectedOption = DEFAULT_OPTION;
278
279        @Override
280        public Guidance onCreateGuidance(Bundle savedInstanceState) {
281            String title = getString(R.string.guidedstep_third_title);
282            String breadcrumb = getString(R.string.guidedstep_third_breadcrumb);
283            String description = getString(R.string.guidedstep_third_description);
284            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
285            return new Guidance(title, description, breadcrumb, icon);
286        }
287
288        @Override
289        public GuidanceStylist onCreateGuidanceStylist() {
290            return new GuidanceStylist() {
291                @Override
292                public int onProvideLayoutId() {
293                    return R.layout.guidedstep_second_guidance;
294                }
295            };
296        }
297
298        @Override
299        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
300            String desc = "The description can be quite long as well.  ";
301            desc += "Just be sure to set multilineDescription to true in the GuidedAction.";
302            actions.add(new GuidedAction.Builder()
303                    .title("Note that Guided Actions can have titles that are quite long.")
304                    .description(desc)
305                    .multilineDescription(true)
306                    .infoOnly(true)
307                    .enabled(true)
308                    .focusable(false)
309                    .build());
310            for (int i = 0; i < OPTION_NAMES.length; i++) {
311                addCheckedAction(actions, OPTION_DRAWABLES[i], getActivity(), OPTION_NAMES[i],
312                        OPTION_DESCRIPTIONS[i]);
313                if (i == DEFAULT_OPTION) {
314                    actions.get(actions.size() -1).setChecked(true);
315                }
316            }
317            actions.add(new GuidedAction.Builder().constructContinue(getActivity())
318                    .build());
319        }
320
321        @Override
322        public void onGuidedActionClicked(GuidedAction action) {
323            if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
324                FragmentManager fm = getFragmentManager();
325                FourthStepFragment f = new FourthStepFragment();
326                Bundle arguments = new Bundle();
327                arguments.putInt(FourthStepFragment.EXTRA_OPTION, mSelectedOption);
328                f.setArguments(arguments);
329                GuidedStepSupportFragment.add(fm, f, R.id.lb_guidedstep_host);
330            } else {
331                mSelectedOption = getSelectedActionPosition()-1;
332            }
333        }
334
335    }
336
337    public static class FourthStepFragment extends GuidedStepSupportFragment {
338        public static final String EXTRA_OPTION = "extra_option";
339
340        public FourthStepFragment() {
341        }
342
343        public int getOption() {
344            Bundle b = getArguments();
345            if (b == null) return 0;
346            return b.getInt(EXTRA_OPTION, 0);
347        }
348
349        @Override
350        public Guidance onCreateGuidance(Bundle savedInstanceState) {
351            String title = getString(R.string.guidedstep_fourth_title);
352            String breadcrumb = getString(R.string.guidedstep_fourth_breadcrumb);
353            String description = "You chose: " + OPTION_NAMES[getOption()];
354            Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
355            return new Guidance(title, description, breadcrumb, icon);
356        }
357
358        @Override
359        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
360            actions.add(new GuidedAction.Builder().constructFinish(getActivity())
361                    .description("All Done...")
362                    .build());
363            addAction(actions, BACK, "Start Over", "Let's try this again...");
364        }
365
366        @Override
367        public void onGuidedActionClicked(GuidedAction action) {
368            if (action.getId() == GuidedAction.ACTION_ID_FINISH) {
369                finishGuidedStepSupportFragments();
370            } else if (action.getId() == BACK) {
371                // pop 4, 3, 2
372                popBackStackToGuidedStepSupportFragment(SecondStepFragment.class,
373                        FragmentManager.POP_BACK_STACK_INCLUSIVE);
374            }
375        }
376
377    }
378
379}
380