1/*
2 * Copyright (C) 2015 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.android.tv.common.ui.setup;
18
19import android.os.Bundle;
20import android.support.v17.leanback.app.GuidedStepFragment;
21import android.support.v17.leanback.widget.GuidanceStylist;
22import android.support.v17.leanback.widget.GuidedAction;
23import android.support.v17.leanback.widget.VerticalGridView;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.view.ViewGroup.MarginLayoutParams;
28import android.widget.LinearLayout;
29
30import com.android.tv.common.R;
31
32/**
33 * A fragment for channel source info/setup.
34 */
35public abstract class SetupGuidedStepFragment extends GuidedStepFragment {
36    /**
37     * Key of the argument which indicate whether the parent of this fragment has three panes.
38     *
39     * <p>Value type: boolean
40     */
41    public static final String KEY_THREE_PANE = "key_three_pane";
42
43    @Override
44    public View onCreateView(LayoutInflater inflater, ViewGroup container,
45            Bundle savedInstanceState) {
46        View view = super.onCreateView(inflater, container, savedInstanceState);
47        Bundle arguments = getArguments();
48        view.findViewById(R.id.action_fragment_root).setPadding(0, 0, 0, 0);
49        LinearLayout.LayoutParams guidanceLayoutParams = (LinearLayout.LayoutParams)
50                view.findViewById(R.id.content_fragment).getLayoutParams();
51        guidanceLayoutParams.weight = 0;
52        if (arguments != null && arguments.getBoolean(KEY_THREE_PANE, false)) {
53            // Content fragment.
54            guidanceLayoutParams.width = getResources().getDimensionPixelOffset(
55                    R.dimen.setup_guidedstep_guidance_section_width_3pane);
56            int doneButtonWidth = getResources().getDimensionPixelOffset(
57                    R.dimen.setup_done_button_container_width);
58            // Guided actions list
59            View list = view.findViewById(R.id.guidedactions_list);
60            View list2 = view.findViewById(R.id.guidedactions_list2);
61            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) view.findViewById(
62                    R.id.guidedactions_list).getLayoutParams();
63            // Use content view to check layout direction while view is being created.
64            if (getResources().getConfiguration().getLayoutDirection()
65                    == View.LAYOUT_DIRECTION_LTR) {
66                marginLayoutParams.rightMargin = doneButtonWidth;
67            } else {
68                marginLayoutParams.leftMargin = doneButtonWidth;
69            }
70        } else {
71            // Content fragment.
72            guidanceLayoutParams.width = getResources().getDimensionPixelOffset(
73                    R.dimen.setup_guidedstep_guidance_section_width_2pane);
74        }
75        // gridView Alignment
76        VerticalGridView gridView = getGuidedActionsStylist().getActionsGridView();
77        int offset = getResources().getDimensionPixelOffset(
78                R.dimen.setup_guidedactions_selector_margin_top);
79        gridView.setWindowAlignmentOffset(offset);
80        gridView.setWindowAlignmentOffsetPercent(0);
81        gridView.setItemAlignmentOffsetPercent(0);
82        ((ViewGroup) view.findViewById(R.id.guidedactions_list)).setTransitionGroup(false);
83        // Needed for the shared element transition.
84        // content_frame is defined in leanback.
85        ViewGroup group = (ViewGroup) view.findViewById(R.id.content_frame);
86        group.setClipChildren(false);
87        group.setClipToPadding(false);
88        // Workaround b/26205201
89        view.findViewById(R.id.guidedactions_list2).setFocusable(false);
90        return view;
91    }
92
93    @Override
94    public GuidanceStylist onCreateGuidanceStylist() {
95        return new GuidanceStylist() {
96            @Override
97            public View onCreateView(LayoutInflater inflater, ViewGroup container,
98                    Guidance guidance) {
99                View view = super.onCreateView(inflater, container, guidance);
100                if (guidance.getIconDrawable() == null) {
101                    // Icon view should not take up space when we don't use image.
102                    getIconView().setVisibility(View.GONE);
103                }
104                return view;
105            }
106        };
107    }
108
109    abstract protected String getActionCategory();
110
111    @Override
112    public void onGuidedActionClicked(GuidedAction action) {
113        SetupActionHelper.onActionClick(this, getActionCategory(), (int) action.getId());
114    }
115
116    @Override
117    protected void onProvideFragmentTransitions() {
118        // Don't use the fragment transition defined in GuidedStepFragment.
119    }
120
121    @Override
122    public boolean isFocusOutEndAllowed() {
123        return true;
124    }
125}
126