1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.leanback;
18
19import android.app.Activity;
20import android.app.FragmentManager;
21import android.content.Context;
22import android.graphics.drawable.Drawable;
23import android.os.Bundle;
24import android.support.v17.leanback.app.GuidedStepFragment;
25import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
26import android.support.v17.leanback.widget.GuidedAction;
27import android.support.v4.content.res.ResourcesCompat;
28import android.util.Log;
29
30import java.util.List;
31
32/**
33 * Activity that showcases different aspects of GuidedStepFragments in half
34 * screen mode. This is achieved by setting the theme for this activity
35 * to {@code Theme.Example.Leanback.GuidedStep.Half}.
36 */
37public class GuidedStepHalfScreenActivity extends Activity {
38    private static final String TAG = "leanback.GuidedStepSupportHalfScreenActivity";
39
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        Log.v(TAG, "onCreate");
43        super.onCreate(savedInstanceState);
44        setContentView(R.layout.guided_step_activity);
45        GuidedStepFragment.addAsRoot(this, new FirstStepFragment(), R.id.lb_guidedstep_host);
46    }
47
48    public static class FirstStepFragment extends GuidedStepFragment {
49
50       @Override
51        public Guidance onCreateGuidance(Bundle savedInstanceState) {
52            String title = getString(R.string.guidedstep_first_title);
53            String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
54            String description = getString(R.string.guidedstep_first_description);
55            final Context context = getActivity();
56            Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
57                    R.drawable.ic_main_icon, context.getTheme());
58            return new Guidance(title, description, breadcrumb, icon);
59        }
60
61        @Override
62        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
63            Context context = getActivity();
64            actions.add(new GuidedAction.Builder(context)
65                    .clickAction(GuidedAction.ACTION_ID_CONTINUE)
66                    .description("Just do it")
67                    .build());
68            actions.add(new GuidedAction.Builder(context)
69                    .clickAction(GuidedAction.ACTION_ID_CANCEL)
70                    .description("Never mind")
71                    .build());
72        }
73
74        public FirstStepFragment() {
75            setEntranceTransitionType(GuidedStepFragment.SLIDE_FROM_BOTTOM);
76        }
77
78        /**
79         * This fragment could be used by an activity using theme
80         * {@code Theme.Leanback.GuidedStep.Half} or something else (BrowseActivity).
81         * In order to provide a consistent half screen experience under
82         * both scenarios, we override onProvideTheme method.
83         */
84        @Override
85        public int onProvideTheme() {
86            return R.style.Theme_Example_Leanback_GuidedStep_Half;
87        }
88
89        @Override
90        public void onGuidedActionClicked(GuidedAction action) {
91            FragmentManager fm = getFragmentManager();
92            if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
93                GuidedStepFragment.add(fm, new SecondStepFragment(), R.id.lb_guidedstep_host);
94            } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL){
95                finishGuidedStepFragments();
96            }
97        }
98    }
99
100    public static class SecondStepFragment extends GuidedStepFragment {
101
102        @Override
103        public int onProvideTheme() {
104            return R.style.Theme_Example_Leanback_GuidedStep_Half;
105        }
106
107        @Override
108        public Guidance onCreateGuidance(Bundle savedInstanceState) {
109            String title = getString(R.string.guidedstep_second_title);
110            String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
111            String description = getString(R.string.guidedstep_second_description);
112            final Context context = getActivity();
113            Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
114                    R.drawable.ic_main_icon, context.getTheme());
115            return new Guidance(title, description, breadcrumb, icon);
116        }
117
118        @Override
119        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
120            Context context = getActivity();
121            actions.add(new GuidedAction.Builder(context)
122                    .clickAction(GuidedAction.ACTION_ID_FINISH)
123                    .description("Done")
124                    .build());
125            actions.add(new GuidedAction.Builder(context)
126                    .clickAction(GuidedAction.ACTION_ID_CANCEL)
127                    .description("Never mind")
128                    .build());
129        }
130
131        @Override
132        public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
133            actions.add(new GuidedAction.Builder(getActivity())
134                    .clickAction(GuidedAction.ACTION_ID_CANCEL)
135                    .description("Cancel")
136                    .build());
137        }
138
139        @Override
140        public void onGuidedActionClicked(GuidedAction action) {
141            FragmentManager fm = getFragmentManager();
142            fm.popBackStack();
143        }
144    }
145}
146