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            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) list.getLayoutParams();
61            // Use content view to check layout direction while view is being created.
62            if (getResources().getConfiguration().getLayoutDirection()
63                    == View.LAYOUT_DIRECTION_LTR) {
64                marginLayoutParams.rightMargin = doneButtonWidth;
65            } else {
66                marginLayoutParams.leftMargin = doneButtonWidth;
67            }
68        } else {
69            // Content fragment.
70            guidanceLayoutParams.width = getResources().getDimensionPixelOffset(
71                    R.dimen.setup_guidedstep_guidance_section_width_2pane);
72        }
73        // gridView Alignment
74        VerticalGridView gridView = getGuidedActionsStylist().getActionsGridView();
75        // Workaround of b/28274171
76        // TODO: Remove the following line once b/28274171 is resolved.
77        gridView.setFocusable(true);
78        int offset = getResources().getDimensionPixelOffset(
79                R.dimen.setup_guidedactions_selector_margin_top);
80        gridView.setWindowAlignmentOffset(offset);
81        gridView.setWindowAlignmentOffsetPercent(0);
82        gridView.setItemAlignmentOffsetPercent(0);
83        ((ViewGroup) view.findViewById(R.id.guidedactions_list)).setTransitionGroup(false);
84        // Needed for the shared element transition.
85        // content_frame is defined in leanback.
86        ViewGroup group = (ViewGroup) view.findViewById(R.id.content_frame);
87        group.setClipChildren(false);
88        group.setClipToPadding(false);
89        // Workaround b/26205201
90        view.findViewById(R.id.guidedactions_list2).setFocusable(false);
91        return view;
92    }
93
94    @Override
95    public GuidanceStylist onCreateGuidanceStylist() {
96        return new GuidanceStylist() {
97            @Override
98            public View onCreateView(LayoutInflater inflater, ViewGroup container,
99                    Guidance guidance) {
100                View view = super.onCreateView(inflater, container, guidance);
101                if (guidance.getIconDrawable() == null) {
102                    // Icon view should not take up space when we don't use image.
103                    getIconView().setVisibility(View.GONE);
104                }
105                return view;
106            }
107        };
108    }
109
110    abstract protected String getActionCategory();
111
112    @Override
113    public void onGuidedActionClicked(GuidedAction action) {
114        SetupActionHelper.onActionClick(this, getActionCategory(), (int) action.getId());
115    }
116
117    @Override
118    protected void onProvideFragmentTransitions() {
119        // Don't use the fragment transition defined in GuidedStepFragment.
120    }
121
122    @Override
123    public boolean isFocusOutEndAllowed() {
124        return true;
125    }
126}
127