1/*
2 * Copyright 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
17package com.android.car.media.widgets;
18
19import android.annotation.NonNull;
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.view.Gravity;
23import android.view.LayoutInflater;
24import android.widget.ImageView;
25import android.widget.LinearLayout;
26import android.widget.TextView;
27
28import com.android.car.media.R;
29import com.android.car.media.common.MediaItemMetadata;
30
31/**
32 * A view representing a media item to be included in the tab bar at the top of the UI.
33 */
34public class MediaItemTabView extends LinearLayout {
35    private final TextView mTitleView;
36    private final ImageView mImageView;
37    private final MediaItemMetadata mItem;
38
39    /**
40     * Creates a new tab for the given media item.
41     */
42    public MediaItemTabView(@NonNull Context context, @NonNull MediaItemMetadata item) {
43        super(context);
44        LayoutInflater inflater = (LayoutInflater) context
45                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
46        inflater.inflate(R.layout.tab_view, this, true);
47        setOrientation(LinearLayout.VERTICAL);
48        setFocusable(true);
49        setGravity(Gravity.CENTER);
50        setBackground(context.getDrawable(R.drawable.app_item_background));
51
52        int[] attrs = new int[]{android.R.attr.selectableItemBackground};
53        TypedArray typedArray = context.obtainStyledAttributes(attrs);
54        int backgroundResource = typedArray.getResourceId(0, 0);
55        setBackgroundResource(backgroundResource);
56
57        mItem = item;
58        mImageView = findViewById(R.id.icon);
59        MediaItemMetadata.updateImageView(context, item, mImageView, 0);
60        mTitleView = findViewById(R.id.title);
61        mTitleView.setText(item.getTitle());
62    }
63
64    /**
65     * Returns the item represented by this view
66     */
67    @NonNull
68    public MediaItemMetadata getItem() {
69        return mItem;
70    }
71}
72