summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/discovery/AppDiscoveryItemView.java
blob: 809d7249db3a4d00cf1b2d1bb78b6e249da03b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.discovery;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.launcher3.R;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class AppDiscoveryItemView extends RelativeLayout {

    private static boolean SHOW_REVIEW_COUNT = false;

    private ImageView mImage;
    private TextView mTitle;
    private TextView mRatingText;
    private RatingView mRatingView;
    private TextView mReviewCount;
    private TextView mPrice;
    private OnLongClickListener mOnLongClickListener;

    public AppDiscoveryItemView(Context context) {
        this(context, null);
    }

    public AppDiscoveryItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppDiscoveryItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        this.mImage = (ImageView) findViewById(R.id.image);
        this.mTitle = (TextView) findViewById(R.id.title);
        this.mRatingText = (TextView) findViewById(R.id.rating);
        this.mRatingView = (RatingView) findViewById(R.id.rating_view);
        this.mPrice = (TextView) findViewById(R.id.price);
        this.mReviewCount = (TextView) findViewById(R.id.review_count);
    }

    public void init(OnClickListener clickListener,
                     AccessibilityDelegate accessibilityDelegate,
                     OnLongClickListener onLongClickListener) {
        setOnClickListener(clickListener);
        mImage.setOnClickListener(clickListener);
        setAccessibilityDelegate(accessibilityDelegate);
        mOnLongClickListener = onLongClickListener;
    }

    public void apply(@NonNull AppDiscoveryAppInfo info) {
        setTag(info);
        mImage.setTag(info);
        mImage.setImageBitmap(info.iconBitmap);
        mImage.setOnLongClickListener(info.isDragAndDropSupported() ? mOnLongClickListener : null);
        mTitle.setText(info.title);
        mPrice.setText(info.priceFormatted != null ? info.priceFormatted : "");
        mReviewCount.setVisibility(SHOW_REVIEW_COUNT ? View.VISIBLE : View.GONE);
        if (info.rating >= 0) {
            mRatingText.setText(new DecimalFormat("#.0").format(info.rating));
            mRatingView.setRating(info.rating);
            mRatingView.setVisibility(View.VISIBLE);
            String reviewCountFormatted = NumberFormat.getInstance().format(info.reviewCount);
            mReviewCount.setText("(" + reviewCountFormatted + ")");
        } else {
            // if we don't have a rating
            mRatingView.setVisibility(View.GONE);
            mRatingText.setText("");
            mReviewCount.setText("");
        }
    }
}