1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.support.v17.leanback.supportleanbackshowcase.cards.presenters;
16
17import android.content.Context;
18import android.support.v17.leanback.supportleanbackshowcase.R;
19import android.support.v17.leanback.supportleanbackshowcase.models.Card;
20import android.support.v17.leanback.widget.BaseCardView;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.TextView;
25
26import com.squareup.picasso.Picasso;
27
28/**
29 * This Presenter will display a card consisting of an image on the left side of the card followed
30 * by text on the right side. The image and text have equal width. The text will work like a info
31 * box, thus it will be hidden if the parent row is inactive. This behavior is unique to this card
32 * and requires a special focus handler.
33 */
34public class SideInfoCardPresenter extends AbstractCardPresenter<BaseCardView> {
35
36    public SideInfoCardPresenter(Context context) {
37        super(context);
38    }
39
40    @Override
41    protected BaseCardView onCreateView() {
42        final BaseCardView cardView = new BaseCardView(getContext(), null,
43                R.style.SideInfoCardStyle);
44        cardView.setFocusable(true);
45        cardView.addView(LayoutInflater.from(getContext()).inflate(R.layout.side_info_card, null));
46        return cardView;
47    }
48
49    @Override
50    public void onBindViewHolder(Card card, BaseCardView cardView) {
51        ImageView imageView = (ImageView) cardView.findViewById(R.id.main_image);
52        if (card.getLocalImageResourceName() != null) {
53            int width = (int) getContext().getResources()
54                    .getDimension(R.dimen.sidetext_image_card_width);
55            int height = (int) getContext().getResources()
56                    .getDimension(R.dimen.sidetext_image_card_height);
57            int resourceId = getContext().getResources()
58                    .getIdentifier(card.getLocalImageResourceName(),
59                            "drawable", getContext().getPackageName());
60            Picasso.with(getContext()).load(resourceId).resize(width, height).centerCrop()
61                    .into(imageView);
62        }
63
64        TextView primaryText = (TextView) cardView.findViewById(R.id.primary_text);
65        primaryText.setText(card.getTitle());
66
67        TextView secondaryText = (TextView) cardView.findViewById(R.id.secondary_text);
68        secondaryText.setText(card.getDescription());
69
70        TextView extraText = (TextView) cardView.findViewById(R.id.extra_text);
71        extraText.setText(card.getExtraText());
72    }
73
74}
75