1 /*
2  * Copyright (C) 2013 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 
17 package com.android.inputmethod.keyboard.emoji;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.util.AttributeSet;
23 import android.view.View;
24 
25 public final class EmojiCategoryPageIndicatorView extends View {
26     private static final float BOTTOM_MARGIN_RATIO = 1.0f;
27     private final Paint mPaint = new Paint();
28     private int mCategoryPageSize = 0;
29     private int mCurrentCategoryPageId = 0;
30     private float mOffset = 0.0f;
31 
EmojiCategoryPageIndicatorView(final Context context, final AttributeSet attrs)32     public EmojiCategoryPageIndicatorView(final Context context, final AttributeSet attrs) {
33         this(context, attrs, 0);
34     }
35 
EmojiCategoryPageIndicatorView(final Context context, final AttributeSet attrs, final int defStyle)36     public EmojiCategoryPageIndicatorView(final Context context, final AttributeSet attrs,
37             final int defStyle) {
38         super(context, attrs, defStyle);
39     }
40 
setColors(final int foregroundColor, final int backgroundColor)41     public void setColors(final int foregroundColor, final int backgroundColor) {
42         mPaint.setColor(foregroundColor);
43         setBackgroundColor(backgroundColor);
44     }
45 
setCategoryPageId(final int size, final int id, final float offset)46     public void setCategoryPageId(final int size, final int id, final float offset) {
47         mCategoryPageSize = size;
48         mCurrentCategoryPageId = id;
49         mOffset = offset;
50         invalidate();
51     }
52 
53     @Override
onDraw(final Canvas canvas)54     protected void onDraw(final Canvas canvas) {
55         if (mCategoryPageSize <= 1) {
56             // If the category is not set yet or contains only one category,
57             // just clear and return.
58             canvas.drawColor(0);
59             return;
60         }
61         final float height = getHeight();
62         final float width = getWidth();
63         final float unitWidth = width / mCategoryPageSize;
64         final float left = unitWidth * mCurrentCategoryPageId + mOffset * unitWidth;
65         final float top = 0.0f;
66         final float right = left + unitWidth;
67         final float bottom = height * BOTTOM_MARGIN_RATIO;
68         canvas.drawRect(left, top, right, bottom, mPaint);
69     }
70 }
71