• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.example.android.leanback;
18 
19 import android.app.Activity;
20 import android.app.FragmentManager;
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.support.v17.leanback.app.GuidedStepFragment;
25 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
26 import android.support.v17.leanback.widget.GuidedAction;
27 import android.support.v4.content.res.ResourcesCompat;
28 import android.util.Log;
29 
30 import 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  */
37 public class GuidedStepHalfScreenActivity extends Activity {
38     private static final String TAG = "leanback.GuidedStepSupportHalfScreenActivity";
39 
40     @Override
onCreate(Bundle savedInstanceState)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
onCreateGuidance(Bundle savedInstanceState)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
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)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 
FirstStepFragment()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
onProvideTheme()85         public int onProvideTheme() {
86             return R.style.Theme_Example_Leanback_GuidedStep_Half;
87         }
88 
89         @Override
onGuidedActionClicked(GuidedAction action)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
onProvideTheme()103         public int onProvideTheme() {
104             return R.style.Theme_Example_Leanback_GuidedStep_Half;
105         }
106 
107         @Override
onCreateGuidance(Bundle savedInstanceState)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
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)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
onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState)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
onGuidedActionClicked(GuidedAction action)140         public void onGuidedActionClicked(GuidedAction action) {
141             FragmentManager fm = getFragmentManager();
142             fm.popBackStack();
143         }
144     }
145 }
146