summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/ui/TimeLineTitleMaker.java
blob: 52a3af28fd57199a88b49eef28cd9f857c205b65 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 * Not a Contribution
 *
 * Copyright (C) 2012 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.gallery3d.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;

import com.android.gallery3d.util.ThreadPool;
import com.android.gallery3d.util.ThreadPool.JobContext;
import com.android.photos.data.GalleryBitmapPool;
import org.codeaurora.gallery.R;

import java.util.Locale;

public class TimeLineTitleMaker {

    private final String TAG = "TimelineTitleMaker";
    private static final int BORDER_SIZE = 0;

    private final TimeLineSlotRenderer.LabelSpec mSpec;
    private final TextPaint mTitlePaint;
    private final TextPaint mCountPaint;
    private final Context mContext;
    private final TimeLineSlotView mTimeLineSlotView;

    private final int TIMELINETITLE_START_X;

    public TimeLineTitleMaker(Context context, TimeLineSlotRenderer.LabelSpec spec, TimeLineSlotView slotView) {
        mContext = context;
        mSpec = spec;
        mTimeLineSlotView = slotView;
        mTitlePaint = getTextPaint(spec.timeLineTitleFontSize, spec.timeLineTitleTextColor , true);
        mCountPaint = getTextPaint(spec.timeLineTitleFontSize, spec.timeLineNumberTextColor, true);
        TIMELINETITLE_START_X = context.getResources().getDimensionPixelSize(
                R.dimen.timeline_title_margin);
    }

    private static TextPaint getTextPaint(
            int textSize, int color, boolean isBold) {
        TextPaint paint = new TextPaint();
        paint.setTextSize(textSize);
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setTypeface(Typeface.SANS_SERIF);
        if (isBold) {
            paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        }
        return paint;
    }

    public ThreadPool.Job<Bitmap> requestTimeLineTitle( String title,
            int photoCount, int videoCount) {
        return new TimeLineTitle(title,photoCount, videoCount, mContext );
    }

    static void drawText(Canvas canvas,
                         int x, int y, String text, int lengthLimit, TextPaint p) {
        canvas.drawText(text, x, y - p.getFontMetricsInt().ascent, p);
    }

    private class TimeLineTitle implements ThreadPool.Job<Bitmap> {
        private String mTitle;
        private int mVideoCount;
        private int mImageCount;
        private Context mContext;
        public TimeLineTitle(String title, int imageCount, int videoCount, Context context) {
            mTitle = title;
            mImageCount = imageCount;
            mVideoCount = videoCount;
            mContext = context;

        }

        @Override
        public Bitmap run(JobContext jc) {
            TimeLineSlotRenderer.LabelSpec spec = mSpec;

            Bitmap bitmap;
            int width = mTimeLineSlotView.getTitleWidth();
            int height= spec.timeLineTitleHeight;
            synchronized (this) {
                bitmap = GalleryBitmapPool.getInstance().get(width, height);
            }
            if (bitmap == null) {
                int borders = 2 * BORDER_SIZE;
                bitmap = Bitmap.createBitmap(width + borders,
                        height + borders, Config.ARGB_8888);
            }
            if (bitmap == null) {
                return null;
            }
            Canvas canvas = new Canvas(bitmap);
            canvas.clipRect(BORDER_SIZE, BORDER_SIZE,
                    bitmap.getWidth() - BORDER_SIZE,
                    bitmap.getHeight() - BORDER_SIZE);
            canvas.drawColor(mSpec.timeLineTitleBackgroundColor, PorterDuff.Mode.SRC);

            canvas.translate(BORDER_SIZE, BORDER_SIZE);


            StringBuilder sb = new StringBuilder();
            if(mImageCount != 0) {
                sb.append(mContext.getResources().getQuantityString(R.plurals.number_of_photos, mImageCount, mImageCount));
                if(mVideoCount!=0) {
                    sb.append("  " +mContext.getResources().getQuantityString(
                             R.plurals.number_of_videos, mVideoCount, mVideoCount));
                }
            } else {
                if(mVideoCount != 0) {
                    sb.append(mContext.getResources().getQuantityString(
                            R.plurals.number_of_videos, mVideoCount, mVideoCount));
                }
            }
            String countString = sb.toString();

            if (jc.isCancelled()) return null;


            int x;
            int y = 0;
            if (mTitle != null) {
                mTitle = mTitle.toUpperCase();
                x = TIMELINETITLE_START_X;
                y = (height - spec.timeLineTitleFontSize)/2;
                // re-calculate x for RTL
                if (View.LAYOUT_DIRECTION_RTL == TextUtils
                        .getLayoutDirectionFromLocale(Locale.getDefault())) {
                    Rect titleBounds = new Rect();
                    mTitlePaint.getTextBounds(
                        mTitle, 0, mTitle.length(), titleBounds);
                    int w = titleBounds.width();
                    x = width - mTitle.length() - w;
                }
                drawText(canvas, x, y, mTitle, width-x, mTitlePaint);
            }

            if (countString != null) {

                Rect mediaCountBounds = new Rect();
                mCountPaint.getTextBounds(
                        countString, 0, countString.length(), mediaCountBounds);
                int w = mediaCountBounds.width();
                y = (height - spec.timeLineTitleFontSize)/2;
                x = width - TIMELINETITLE_START_X - w;
                // re-calculate x for RTL
                if (View.LAYOUT_DIRECTION_RTL == TextUtils
                        .getLayoutDirectionFromLocale(Locale.getDefault())) {
                    x = TIMELINETITLE_START_X;
                }
                drawText(canvas, x, y, countString,
                        width - x, mCountPaint);
            }
            return bitmap;
        }
    }
}