1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.chrome.browser.banners;
6
7import android.content.Context;
8import android.content.res.Resources;
9import android.graphics.Bitmap;
10import android.graphics.BitmapFactory;
11import android.graphics.Canvas;
12import android.graphics.Matrix;
13import android.graphics.Rect;
14import android.util.AttributeSet;
15import android.view.View;
16
17import org.chromium.chrome.R;
18import org.chromium.ui.base.LocalizationUtils;
19
20/**
21 * Displays a set of stars representing a rating for an app.
22 */
23public class RatingView extends View {
24    private static final int MAX_INCREMENT = 10;
25
26    // Bitmaps used to draw the ratings.
27    private final Bitmap mStarFull;
28    private final Bitmap mStarHalf;
29    private final Bitmap mStarEmpty;
30
31    /** Stores whether or not the layout is left-to-right. */
32    private final boolean mIsLayoutLTR;
33
34    /** Variables used for drawing. */
35    private final Rect mDrawingRect;
36
37    /** Each increment represents 0.5 stars. */
38    private int mIncrements;
39
40    public RatingView(Context context, AttributeSet params) {
41        super(context, params);
42        mIsLayoutLTR = !LocalizationUtils.isLayoutRtl();
43        mDrawingRect = new Rect();
44
45        // Cache the Bitmaps.
46        Resources res = getResources();
47        Bitmap starHalf = BitmapFactory.decodeResource(res, R.drawable.btn_star_half);
48        if (!mIsLayoutLTR) {
49            // RTL mode requires flipping the Bitmap.
50            int width = starHalf.getWidth();
51            int height = starHalf.getHeight();
52            Matrix m = new Matrix();
53            m.preScale(-1, 1);
54            starHalf = Bitmap.createBitmap(starHalf, 0, 0, width, height, m, false);
55        }
56        mStarHalf = starHalf;
57        mStarFull = BitmapFactory.decodeResource(res, R.drawable.btn_star_full);
58        mStarEmpty = BitmapFactory.decodeResource(res, R.drawable.btn_star_empty);
59    }
60
61    /**
62     * Initializes the RatingView.
63     * @param rating How many stars to display.
64     */
65    void initialize(float rating) {
66        // Ratings are rounded to the nearest 0.5 increment, like in the Play Store.
67        mIncrements = Math.round(rating * 2);
68    }
69
70    @Override
71    public void onDraw(Canvas canvas) {
72        int starSize = canvas.getHeight();
73
74        // Start off on the left for LTR mode, on the right for RTL.
75        mDrawingRect.top = 0;
76        mDrawingRect.bottom = starSize;
77        mDrawingRect.left = mIsLayoutLTR ? 0 : (canvas.getWidth() - starSize);
78        mDrawingRect.right = mDrawingRect.left + starSize;
79
80        // Draw all the stars.
81        for (int i = 0; i < MAX_INCREMENT; i += 2) {
82            Bitmap toShow = mStarEmpty;
83            if (i < mIncrements) {
84                boolean isFullStar = (mIncrements - i) >= 2;
85                toShow = isFullStar ? mStarFull : mStarHalf;
86            }
87            canvas.drawBitmap(toShow, null, mDrawingRect, null);
88
89            // Scooch over to show the next star.
90            mDrawingRect.left += (mIsLayoutLTR ? 1 : -1) * starSize;
91            mDrawingRect.right = mDrawingRect.left + starSize;
92        }
93    }
94}
95