1 /*
2  * Copyright (C) 2016 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.dvr.ui.list;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.graphics.RectF;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import com.android.tv.R;
27 
28 /** A view used for focus in schedules list. */
29 public class DvrSchedulesFocusView extends View {
30     private final Paint mPaint;
31     private final RectF mRoundRectF = new RectF();
32     private final int mRoundRectRadius;
33 
34     private final String mViewTag;
35     private final String mHeaderFocusViewTag;
36     private final String mItemFocusViewTag;
37 
DvrSchedulesFocusView(Context context)38     public DvrSchedulesFocusView(Context context) {
39         this(context, null, 0);
40     }
41 
DvrSchedulesFocusView(Context context, AttributeSet attrs)42     public DvrSchedulesFocusView(Context context, AttributeSet attrs) {
43         this(context, attrs, 0);
44     }
45 
DvrSchedulesFocusView(Context context, AttributeSet attrs, int defStyleAttr)46     public DvrSchedulesFocusView(Context context, AttributeSet attrs, int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48         mHeaderFocusViewTag = getContext().getString(R.string.dvr_schedules_header_focus_view);
49         mItemFocusViewTag = getContext().getString(R.string.dvr_schedules_item_focus_view);
50         mViewTag = (String) getTag();
51         mPaint = createPaint(context);
52         mRoundRectRadius = getRoundRectRadius();
53     }
54 
55     @Override
onDraw(Canvas canvas)56     protected void onDraw(Canvas canvas) {
57         super.onDraw(canvas);
58         if (TextUtils.equals(mViewTag, mHeaderFocusViewTag)) {
59             mRoundRectF.set(0, 0, getWidth(), getHeight());
60         } else if (TextUtils.equals(mViewTag, mItemFocusViewTag)) {
61             int drawHeight = 2 * mRoundRectRadius;
62             int drawOffset = (drawHeight - getHeight()) / 2;
63             mRoundRectF.set(0, -drawOffset, getWidth(), getHeight() + drawOffset);
64         }
65         canvas.drawRoundRect(mRoundRectF, mRoundRectRadius, mRoundRectRadius, mPaint);
66     }
67 
createPaint(Context context)68     private Paint createPaint(Context context) {
69         Paint paint = new Paint();
70         paint.setColor(context.getColor(R.color.dvr_schedules_list_item_selector));
71         return paint;
72     }
73 
getRoundRectRadius()74     private int getRoundRectRadius() {
75         if (TextUtils.equals(mViewTag, mHeaderFocusViewTag)) {
76             return getResources()
77                     .getDimensionPixelSize(R.dimen.dvr_schedules_header_selector_radius);
78         } else if (TextUtils.equals(mViewTag, mItemFocusViewTag)) {
79             return getResources().getDimensionPixelSize(R.dimen.dvr_schedules_selector_radius);
80         }
81         return 0;
82     }
83 }
84