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.content.Context;
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.support.v17.leanback.app.GuidedStepFragment;
24import android.support.v17.leanback.widget.GuidanceStylist;
25import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
26import android.support.v17.leanback.widget.GuidedAction;
27
28import java.util.List;
29
30/**
31 * Activity that showcases different aspects of GuidedStepFragments.
32 */
33public class DetailsPresenterSelectionActivity extends Activity {
34
35    private static final int OPTION_CHECK_SET_ID = 10;
36
37    private static final long ACTION_ID_SWITCH_LEGACY_ON = 10000;
38    private static final long ACTION_ID_SWITCH_LEGACY_OFF = 10001;
39
40    public static boolean USE_LEGACY_PRESENTER = false;
41
42    private static final String[] OPTION_NAMES = { "Use new details presenter", "Use legacy details presenter" };
43    private static final String[] OPTION_DESCRIPTIONS = { "Use new details presenter",
44            "Use legacy details presenter"};
45    private static final long[] OPTION_IDS = {ACTION_ID_SWITCH_LEGACY_OFF, ACTION_ID_SWITCH_LEGACY_ON};
46
47    @Override
48    protected void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50        GuidedStepFragment.addAsRoot(this, new SetupFragment(), android.R.id.content);
51    }
52
53    private static void addAction(List<GuidedAction> actions, long id, String title, String desc) {
54        actions.add(new GuidedAction.Builder(null)
55                .id(id)
56                .title(title)
57                .description(desc)
58                .build());
59    }
60
61    private static void addCheckedAction(List<GuidedAction> actions, Context context,
62            long id, String title, String desc, boolean checked) {
63        actions.add(new GuidedAction.Builder(null)
64                .title(title)
65                .description(desc)
66                .id(id)
67                .checkSetId(OPTION_CHECK_SET_ID)
68                .checked(checked)
69                .build());
70    }
71
72    private static class SetupFragment extends GuidedStepFragment {
73
74        @Override
75        public Guidance onCreateGuidance(Bundle savedInstanceState) {
76            String title = getString(R.string.guidedstep_second_title);
77            String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
78            String description = getString(R.string.guidedstep_second_description);
79            Drawable icon = getActivity().getResources().getDrawable(R.drawable.ic_main_icon);
80            return new Guidance(title, description, breadcrumb, icon);
81        }
82
83        @Override
84        public GuidanceStylist onCreateGuidanceStylist() {
85            return new GuidanceStylist() {
86                @Override
87                public int onProvideLayoutId() {
88                    return R.layout.guidedstep_second_guidance;
89                }
90            };
91        }
92
93        @Override
94        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
95            for (int i = 0; i < OPTION_NAMES.length; i++) {
96                boolean checked = false;
97                if (OPTION_IDS[i] == ACTION_ID_SWITCH_LEGACY_ON) {
98                    if (USE_LEGACY_PRESENTER) {
99                        checked = true;
100                    }
101                } else if (OPTION_IDS[i] == ACTION_ID_SWITCH_LEGACY_OFF) {
102                    if (!USE_LEGACY_PRESENTER) {
103                        checked = true;
104                    }
105                }
106                addCheckedAction(actions, getActivity(), OPTION_IDS[i], OPTION_NAMES[i],
107                        OPTION_DESCRIPTIONS[i], checked);
108            }
109        }
110
111        @Override
112        public void onGuidedActionClicked(GuidedAction action) {
113            if (action.getId() == ACTION_ID_SWITCH_LEGACY_ON) {
114                USE_LEGACY_PRESENTER = action.isChecked();
115            } else if (action.getId() == ACTION_ID_SWITCH_LEGACY_OFF) {
116                USE_LEGACY_PRESENTER = !action.isChecked();
117            }
118            getActivity().finish();
119        }
120
121    }
122
123}
124