1 /*
2  * Copyright (C) 2018 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.settings.suggestions;
18 
19 import android.animation.ObjectAnimator;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.LinearLayout;
25 
26 import com.android.tv.settings.R;
27 
28 
29 /**
30  * View for settings suggestion.
31  * Handles dismiss button focus behavior.
32  */
33 public class SuggestionItemView extends LinearLayout {
34     private View mDissmissButton;
35     private View mContainer;
36     private View mItemContainer;
37     private View mIcon;
38 
SuggestionItemView(Context context)39     public SuggestionItemView(Context context) {
40         super(context);
41     }
42 
SuggestionItemView(Context context, AttributeSet attrs)43     public SuggestionItemView(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
47     @Override
onFinishInflate()48     protected void onFinishInflate() {
49         super.onFinishInflate();
50         mDissmissButton = findViewById(R.id.dismiss_button);
51         mContainer = findViewById(R.id.main_container);
52         mIcon = findViewById(android.R.id.icon);
53         mItemContainer = findViewById(R.id.item_container);
54 
55         int translateX = getResources().getDimensionPixelSize(
56                 R.dimen.suggestion_item_change_focus_translate_x);
57 
58         if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
59             translateX = -translateX;
60         }
61 
62         ObjectAnimator containerSlideOut = ObjectAnimator.ofFloat(mContainer,
63                 View.TRANSLATION_X, 0, translateX);
64 
65         ObjectAnimator containerSlideIn = ObjectAnimator.ofFloat(mContainer,
66                 View.TRANSLATION_X, translateX, 0);
67 
68         mDissmissButton.setOnFocusChangeListener(new OnFocusChangeListener() {
69             @Override
70             public void onFocusChange(View v, boolean hasFocus) {
71                 if (hasFocus) {
72                     containerSlideOut.start();
73                     mDissmissButton.setAlpha(1.0f);
74                     mIcon.setAlpha(0.3f);
75                     mItemContainer.setAlpha(0.3f);
76                 } else {
77                     containerSlideIn.start();
78                     mDissmissButton.setAlpha(0.4f);
79                     mIcon.setAlpha(1.0f);
80                     mItemContainer.setAlpha(1.0f);
81                 }
82             }
83         });
84     }
85 
86     @Override
focusSearch(View focused, int direction)87     public View focusSearch(View focused, int direction) {
88         boolean isRTL =
89                 getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
90         if (focused.getId() == R.id.dismiss_button
91                 && ((isRTL && direction == ViewGroup.FOCUS_RIGHT)
92                 || (!isRTL && direction == ViewGroup.FOCUS_LEFT)))  {
93             return mContainer;
94         } else if (focused.getId() == R.id.main_container
95                 && ((isRTL && direction == ViewGroup.FOCUS_LEFT)
96                 || (!isRTL && direction == ViewGroup.FOCUS_RIGHT))) {
97             return  mDissmissButton;
98         }
99         return super.focusSearch(focused, direction);
100     }
101 }
102