165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko/*
265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * Copyright (C) 2016 The Android Open Source Project
365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko *
465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * Licensed under the Apache License, Version 2.0 (the "License");
565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * you may not use this file except in compliance with the License.
665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * You may obtain a copy of the License at
765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko *
865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko *      http://www.apache.org/licenses/LICENSE-2.0
965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko *
1065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * Unless required by applicable law or agreed to in writing, software
1165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * distributed under the License is distributed on an "AS IS" BASIS,
1265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * See the License for the specific language governing permissions and
1465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko * limitations under the License
1565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko */
1665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
1765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkopackage com.android.tv.dvr.ui.list;
1865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
1965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.content.Context;
2065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.graphics.Canvas;
2165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.graphics.Paint;
2265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.graphics.RectF;
2365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.text.TextUtils;
2465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.util.AttributeSet;
2565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport android.view.View;
2665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport com.android.tv.R;
2765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
2895961816a768da387f0b5523cf4363ace2044089Nick Chalko/** A view used for focus in schedules list. */
2965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkopublic class DvrSchedulesFocusView extends View {
3065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final Paint mPaint;
3165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final RectF mRoundRectF = new RectF();
3265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final int mRoundRectRadius;
3365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
3465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final String mViewTag;
3565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final String mHeaderFocusViewTag;
3665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final String mItemFocusViewTag;
3765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
3865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    public DvrSchedulesFocusView(Context context) {
3965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        this(context, null, 0);
4065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    }
4165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
4265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    public DvrSchedulesFocusView(Context context, AttributeSet attrs) {
4365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        this(context, attrs, 0);
4465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    }
4565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
4665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    public DvrSchedulesFocusView(Context context, AttributeSet attrs, int defStyleAttr) {
4765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        super(context, attrs, defStyleAttr);
4865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        mHeaderFocusViewTag = getContext().getString(R.string.dvr_schedules_header_focus_view);
4965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        mItemFocusViewTag = getContext().getString(R.string.dvr_schedules_item_focus_view);
5065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        mViewTag = (String) getTag();
5165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        mPaint = createPaint(context);
5265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        mRoundRectRadius = getRoundRectRadius();
5365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    }
5465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
5565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    @Override
5665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    protected void onDraw(Canvas canvas) {
5765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        super.onDraw(canvas);
5865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        if (TextUtils.equals(mViewTag, mHeaderFocusViewTag)) {
5965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko            mRoundRectF.set(0, 0, getWidth(), getHeight());
6065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        } else if (TextUtils.equals(mViewTag, mItemFocusViewTag)) {
6165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko            int drawHeight = 2 * mRoundRectRadius;
6265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko            int drawOffset = (drawHeight - getHeight()) / 2;
6365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko            mRoundRectF.set(0, -drawOffset, getWidth(), getHeight() + drawOffset);
6465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        }
6565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        canvas.drawRoundRect(mRoundRectF, mRoundRectRadius, mRoundRectRadius, mPaint);
6665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    }
6765fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
6865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private Paint createPaint(Context context) {
6965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        Paint paint = new Paint();
7065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        paint.setColor(context.getColor(R.color.dvr_schedules_list_item_selector));
7165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        return paint;
7265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    }
7365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko
7465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private int getRoundRectRadius() {
7565fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        if (TextUtils.equals(mViewTag, mHeaderFocusViewTag)) {
7695961816a768da387f0b5523cf4363ace2044089Nick Chalko            return getResources()
7795961816a768da387f0b5523cf4363ace2044089Nick Chalko                    .getDimensionPixelSize(R.dimen.dvr_schedules_header_selector_radius);
7865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        } else if (TextUtils.equals(mViewTag, mItemFocusViewTag)) {
7965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko            return getResources().getDimensionPixelSize(R.dimen.dvr_schedules_selector_radius);
8065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        }
8165fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        return 0;
8265fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    }
8365fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko}
84