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 
17 package com.android.tv.common.ui.setup;
18 
19 import android.os.Bundle;
20 import android.support.annotation.Nullable;
21 import android.util.Log;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.ViewGroup.MarginLayoutParams;
26 import com.android.tv.common.R;
27 
28 /** A fragment for channel source info/setup. */
29 public abstract class SetupMultiPaneFragment extends SetupFragment {
30     private static final String TAG = "SetupMultiPaneFragment";
31     private static final boolean DEBUG = false;
32 
33     public static final int ACTION_DONE = Integer.MAX_VALUE;
34     public static final int ACTION_SKIP = ACTION_DONE - 1;
35     public static final int MAX_SUBCLASSES_ID = ACTION_SKIP - 1;
36 
37     public static final String CONTENT_FRAGMENT_TAG = "content_fragment";
38 
39     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)40     public View onCreateView(
41             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
42         if (DEBUG) {
43             Log.d(
44                     TAG,
45                     "onCreateView("
46                             + inflater
47                             + ", "
48                             + container
49                             + ", "
50                             + savedInstanceState
51                             + ")");
52         }
53         View view = super.onCreateView(inflater, container, savedInstanceState);
54         if (savedInstanceState == null) {
55             SetupGuidedStepFragment contentFragment = onCreateContentFragment();
56             getChildFragmentManager()
57                     .beginTransaction()
58                     .replace(
59                             R.id.guided_step_fragment_container,
60                             contentFragment,
61                             CONTENT_FRAGMENT_TAG)
62                     .commit();
63         }
64         if (needsDoneButton()) {
65             setOnClickAction(view.findViewById(R.id.button_done), getActionCategory(), ACTION_DONE);
66         }
67         if (needsSkipButton()) {
68             view.findViewById(R.id.button_skip).setVisibility(View.VISIBLE);
69             setOnClickAction(view.findViewById(R.id.button_skip), getActionCategory(), ACTION_SKIP);
70         }
71         if (!needsDoneButton() && !needsSkipButton()) {
72             View doneButtonContainer = view.findViewById(R.id.done_button_container);
73             // Use content view to check layout direction while view is being created.
74             if (getResources().getConfiguration().getLayoutDirection()
75                     == View.LAYOUT_DIRECTION_LTR) {
76                 ((MarginLayoutParams) doneButtonContainer.getLayoutParams()).rightMargin =
77                         -getResources()
78                                 .getDimensionPixelOffset(R.dimen.setup_done_button_container_width);
79             } else {
80                 ((MarginLayoutParams) doneButtonContainer.getLayoutParams()).leftMargin =
81                         -getResources()
82                                 .getDimensionPixelOffset(R.dimen.setup_done_button_container_width);
83             }
84             view.findViewById(R.id.button_done).setFocusable(false);
85         }
86         return view;
87     }
88 
89     @Override
getLayoutResourceId()90     protected int getLayoutResourceId() {
91         return R.layout.fragment_setup_multi_pane;
92     }
93 
onCreateContentFragment()94     protected abstract SetupGuidedStepFragment onCreateContentFragment();
95 
96     @Nullable
getContentFragment()97     protected SetupGuidedStepFragment getContentFragment() {
98         return (SetupGuidedStepFragment)
99                 getChildFragmentManager().findFragmentByTag(CONTENT_FRAGMENT_TAG);
100     }
101 
getActionCategory()102     protected abstract String getActionCategory();
103 
needsDoneButton()104     protected boolean needsDoneButton() {
105         return true;
106     }
107 
needsSkipButton()108     protected boolean needsSkipButton() {
109         return false;
110     }
111 
112     @Override
getParentIdsForDelay()113     protected int[] getParentIdsForDelay() {
114         return new int[] {
115             androidx.leanback.R.id.content_fragment,
116             androidx.leanback.R.id.guidedactions_list
117         };
118     }
119 
120     @Override
getSharedElementIds()121     public int[] getSharedElementIds() {
122         return new int[] {
123             androidx.leanback.R.id.action_fragment_background, R.id.done_button_container
124         };
125     }
126 }
127