summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/app/TimeBar.java
blob: 1d08989aabdc667a86d599ea7c398ca3df78834f (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
 * Copyright (C) 2011 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.app;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.android.gallery3d.R;
import com.android.gallery3d.common.Utils;

import java.util.Locale;

/**
 * The time bar view, which includes the current and total time, the progress
 * bar, and the scrubber.
 */
public class TimeBar extends View {

    public interface Listener {
        void onScrubbingStart();

        void onScrubbingMove(int time);

        void onScrubbingEnd(int time, int start, int end);
    }

    // Padding around the scrubber to increase its touch target
    private static final int SCRUBBER_PADDING_IN_DP = 10;

    // The total padding, top plus bottom
    private static final int V_PADDING_IN_DP = 30;

    private static final int TEXT_SIZE_IN_DP = 14;

    private static final String TAG = "Gallery3D/TimeBar";
    private static final boolean LOG = true;
    public static final int UNKNOWN = -1;

    protected final Listener mListener;

    // the bars we use for displaying the progress
    protected final Rect mProgressBar;
    protected final Rect mPlayedBar;

    protected final Paint mProgressPaint;
    protected final Paint mPlayedPaint;
    protected final Paint mTimeTextPaint;

    protected final Bitmap mScrubber;
    protected int mScrubberPadding; // adds some touch tolerance around the
                                    // scrubber

    protected int mScrubberLeft;
    protected int mScrubberTop;
    protected int mScrubberCorrection;
    protected boolean mScrubbing;
    protected boolean mShowTimes;
    protected boolean mShowScrubber;
    private boolean mEnableScrubbing;

    protected int mTotalTime;
    protected int mCurrentTime;

    protected final Rect mTimeBounds;

    protected int mVPaddingInPx;
    private int mLastShowTime = UNKNOWN;

    private ITimeBarSecondaryProgressExt mSecondaryProgressExt = new TimeBarSecondaryProgressExtImpl();
    private ITimeBarInfoExt mInfoExt = new TimeBarInfoExtImpl();
    private ITimeBarLayoutExt mLayoutExt = new TimeBarLayoutExtImpl();

    public TimeBar(Context context, Listener listener) {
        super(context);
        mListener = Utils.checkNotNull(listener);

        mShowTimes = true;
        mShowScrubber = true;

        mProgressBar = new Rect();
        mPlayedBar = new Rect();

        mProgressPaint = new Paint();
        mProgressPaint.setColor(0xFF808080);
        mPlayedPaint = new Paint();
        mPlayedPaint.setColor(0xFFFFFFFF);

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        float textSizeInPx = metrics.density * TEXT_SIZE_IN_DP;
        mTimeTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTimeTextPaint.setColor(0xFFCECECE);
        mTimeTextPaint.setTextSize(textSizeInPx);
        mTimeTextPaint.setTextAlign(Paint.Align.CENTER);

        mTimeBounds = new Rect();
        mTimeTextPaint.getTextBounds("0:00:00", 0, 7, mTimeBounds);

        mScrubber = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_knob);
        mScrubberPadding = (int) (metrics.density * SCRUBBER_PADDING_IN_DP);

        mVPaddingInPx = (int) (metrics.density * V_PADDING_IN_DP);
        mLayoutExt.init(mScrubberPadding, mVPaddingInPx);
        mInfoExt.init(textSizeInPx);
        mSecondaryProgressExt.init();
    }

    private void update() {
        mPlayedBar.set(mProgressBar);

        if (mTotalTime > 0) {
            if (View.LAYOUT_DIRECTION_RTL == TextUtils
                    .getLayoutDirectionFromLocale(Locale.getDefault())) {
                // The progress bar should be reversed in RTL.
                mPlayedBar.left = mPlayedBar.right
                        - (int) ((mProgressBar.width() * (long) mCurrentTime) / mTotalTime);
            } else {
                mPlayedBar.right = mPlayedBar.left
                        + (int) ((mProgressBar.width() * (long) mCurrentTime) / mTotalTime);
            }
            /*
             * M: if duration is not accurate, here just adjust playedBar we
             * also show the accurate position text to final user.
             */
            if (mPlayedBar.right > mProgressBar.right) {
                mPlayedBar.right = mProgressBar.right;
            }
        } else {
            if (View.LAYOUT_DIRECTION_RTL == TextUtils
                    .getLayoutDirectionFromLocale(Locale.getDefault())) {
                // The progress bar should be reversed in RTL.
                mPlayedBar.left = mProgressBar.right;
            } else {
                mPlayedBar.right = mProgressBar.left;
            }
        }

        if (!mScrubbing) {
            if (View.LAYOUT_DIRECTION_RTL == TextUtils.getLayoutDirectionFromLocale(
                    Locale.getDefault())) {
                // The progress bar should be reversed in RTL.
                mScrubberLeft = mPlayedBar.left - mScrubber.getWidth() / 2;
            } else {
                mScrubberLeft = mPlayedBar.right - mScrubber.getWidth() / 2;
            }
        }
        // update text bounds when layout changed or time changed
        updateBounds();
        mInfoExt.updateVisibleText(this, mProgressBar, mTimeBounds);
        invalidate();
    }

    /**
     * @return the preferred height of this view, including invisible padding
     */
    public int getPreferredHeight() {
        int preferredHeight = mTimeBounds.height() + mVPaddingInPx + mScrubberPadding;
        return mLayoutExt.getPreferredHeight(preferredHeight, mTimeBounds);
    }

    /**
     * @return the height of the time bar, excluding invisible padding
     */
    public int getBarHeight() {
        int barHeight = mTimeBounds.height() + mVPaddingInPx;
        return mLayoutExt.getBarHeight(barHeight, mTimeBounds);
    }

    public void setTime(int currentTime, int totalTime,
            int trimStartTime, int trimEndTime) {
        if (mCurrentTime == currentTime && mTotalTime == totalTime) {
            return;
        }
        mCurrentTime = currentTime;
        mTotalTime = Math.abs(totalTime);
        if (totalTime <= 0) { /// M: disable scrubbing before mediaplayer ready.
            setScrubbing(false);
        }
        update();
    }

    private boolean inScrubber(float x, float y) {
        int scrubberRight = mScrubberLeft + mScrubber.getWidth();
        int scrubberBottom = mScrubberTop + mScrubber.getHeight();
        return mScrubberLeft - mScrubberPadding < x && x < scrubberRight + mScrubberPadding
                && mScrubberTop - mScrubberPadding < y && y < scrubberBottom + mScrubberPadding;
    }

    private void clampScrubber() {
        int half = mScrubber.getWidth() / 2;
        int max = mProgressBar.right - half;
        int min = mProgressBar.left - half;
        mScrubberLeft = Math.min(max, Math.max(min, mScrubberLeft));
    }

    private int getScrubberTime() {
        if (View.LAYOUT_DIRECTION_RTL == TextUtils
                .getLayoutDirectionFromLocale(Locale.getDefault())) {
            // The progress bar's scrubber time should be reversed in RTL.
            return (int) ((long) (mProgressBar.width() - (mScrubberLeft
                    + mScrubber.getWidth() / 2 - mProgressBar.left))
                    * mTotalTime / mProgressBar.width());
        } else {
            return (int) ((long) (mScrubberLeft + mScrubber.getWidth() / 2 - mProgressBar.left)
                    * mTotalTime / mProgressBar.width());
        }
  }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = r - l;
        int h = b - t;
        if (!mShowTimes && !mShowScrubber) {
            mProgressBar.set(0, 0, w, h);
        } else {
            int margin = mScrubber.getWidth() / 3;
            if (mShowTimes) {
                margin += mTimeBounds.width();
            }
            margin = mLayoutExt.getProgressMargin(margin);
            int progressY = (h + mScrubberPadding) / 2 + mLayoutExt.getProgressOffset(mTimeBounds);
            mScrubberTop = progressY - mScrubber.getHeight() / 2 + 1;
            mProgressBar.set(
                    getPaddingLeft() + margin, progressY,
                    w - getPaddingRight() - margin, progressY + 4);
        }
        update();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // draw progress bars
        canvas.drawRect(mProgressBar, mProgressPaint);
        mSecondaryProgressExt.draw(canvas, mProgressBar);
        canvas.drawRect(mPlayedBar, mPlayedPaint);

        // draw scrubber and timers
        if (mShowScrubber) {
            canvas.drawBitmap(mScrubber, mScrubberLeft, mScrubberTop, null);
        }
        if (mShowTimes) {
            if (View.LAYOUT_DIRECTION_RTL == TextUtils
                    .getLayoutDirectionFromLocale(Locale.getDefault())) {
                // The progress bar's time should be reversed in RTL.
                canvas.drawText(
                        stringForTime(mCurrentTime),
                        getWidth() - getPaddingRight() - mTimeBounds.width() / 2,
                        mTimeBounds.height() + mVPaddingInPx / 2 + mScrubberPadding + 1,
                        mTimeTextPaint);
                canvas.drawText(
                        stringForTime(mTotalTime),
                        mTimeBounds.width() / 2 + getPaddingLeft(),
                        mTimeBounds.height() + mVPaddingInPx / 2 + mScrubberPadding + 1,
                        mTimeTextPaint);
            } else {
                canvas.drawText(
                        stringForTime(mCurrentTime),
                        mTimeBounds.width() / 2 + getPaddingLeft(),
                        mTimeBounds.height() + mVPaddingInPx / 2 + mScrubberPadding + 1,
                        mTimeTextPaint);
                canvas.drawText(
                        stringForTime(mTotalTime),
                        getWidth() - getPaddingRight() - mTimeBounds.width() / 2,
                        mTimeBounds.height() + mVPaddingInPx / 2 + mScrubberPadding + 1,
                        mTimeTextPaint);
            }
        }
        mInfoExt.draw(canvas, mLayoutExt.getInfoBounds(this, mTimeBounds));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (LOG) {
            Log.v(TAG, "onTouchEvent() showScrubber=" + mShowScrubber
                    + ", enableScrubbing=" + mEnableScrubbing + ", totalTime="
                    + mTotalTime + ", scrubbing=" + mScrubbing + ", event="
                    + event);
        }
        if (mShowScrubber && mEnableScrubbing) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    mScrubberCorrection = inScrubber(x, y)
                            ? x - mScrubberLeft
                            : mScrubber.getWidth() / 2;
                    mScrubbing = true;
                    mListener.onScrubbingStart();
                }
                // fall-through
                case MotionEvent.ACTION_MOVE: {
                    mScrubberLeft = x - mScrubberCorrection;
                    clampScrubber();
                    mCurrentTime = getScrubberTime();
                    mListener.onScrubbingMove(mCurrentTime);
                    update();
                    invalidate();
                    return true;
                }
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (mScrubbing) {
                        mListener.onScrubbingEnd(getScrubberTime(), 0, 0);
                        mScrubbing = false;
                        update();
                        return true;
                    }
                    break;
            }
        }
        return false;
    }

    protected String stringForTime(long millis) {
        int totalSeconds = (int) millis / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        if (hours > 0) {
            return String.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return String.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    public void setSeekable(boolean canSeek) {
        mShowScrubber = canSeek;
    }

    private void updateBounds() {
        int showTime = mTotalTime > mCurrentTime ? mTotalTime : mCurrentTime;
        if (mLastShowTime == showTime) {
            // do not need to recompute the bounds.
            return;
        }
        String durationText = stringForTime(showTime);
        int length = durationText.length();
        mTimeTextPaint.getTextBounds(durationText, 0, length, mTimeBounds);
        mLastShowTime = showTime;
        if (LOG) {
            Log.v(TAG, "updateBounds() durationText=" + durationText + ", timeBounds="
                    + mTimeBounds);
        }
    }

    public void setScrubbing(boolean enable) {
        if (LOG) {
            Log.v(TAG, "setScrubbing(" + enable + ") scrubbing=" + mScrubbing);
        }
        mEnableScrubbing = enable;
        if (mScrubbing) { // if it is scrubbing, change it to false
            mListener.onScrubbingEnd(getScrubberTime(), 0, 0);
            mScrubbing = false;
        }
    }

    public boolean getScrubbing() {
        if (LOG) {
            Log.v(TAG, "mEnableScrubbing=" + mEnableScrubbing);
        }
        return mEnableScrubbing;
    }

    public void setInfo(String info) {
        if (LOG) {
            Log.v(TAG, "setInfo(" + info + ")");
        }
        mInfoExt.setInfo(info);
        mInfoExt.updateVisibleText(this, mProgressBar, mTimeBounds);
        invalidate();
    }

    public void setSecondaryProgress(int percent) {
        if (LOG) {
            Log.v(TAG, "setSecondaryProgress(" + percent + ")");
        }
        mSecondaryProgressExt.setSecondaryProgress(mProgressBar, percent);
        invalidate();
    }
}

interface ITimeBarInfoExt {
    void init(float textSizeInPx);

    void setInfo(String info);

    void draw(Canvas canvas, Rect infoBounds);

    void updateVisibleText(View parent, Rect progressBar, Rect timeBounds);
}

interface ITimeBarSecondaryProgressExt {
    void init();

    void setSecondaryProgress(Rect progressBar, int percent);

    void draw(Canvas canvas, Rect progressBounds);
}

interface ITimeBarLayoutExt {
    void init(int scrubberPadding, int vPaddingInPx);

    int getPreferredHeight(int originalPreferredHeight, Rect timeBounds);

    int getBarHeight(int originalBarHeight, Rect timeBounds);

    int getProgressMargin(int originalMargin);

    int getProgressOffset(Rect timeBounds);

    int getTimeOffset();

    Rect getInfoBounds(View parent, Rect timeBounds);
}

class TimeBarInfoExtImpl implements ITimeBarInfoExt {
    private static final String TAG = "TimeBarInfoExtensionImpl";
    private static final boolean LOG = true;
    private static final String ELLIPSE = "...";

    private Paint mInfoPaint;
    private Rect mInfoBounds;
    private String mInfoText;
    private String mVisibleText;
    private int mEllipseLength;

    @Override
    public void init(float textSizeInPx) {
        mInfoPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mInfoPaint.setColor(0xFFCECECE);
        mInfoPaint.setTextSize(textSizeInPx);
        mInfoPaint.setTextAlign(Paint.Align.CENTER);

        mEllipseLength = (int) Math.ceil(mInfoPaint.measureText(ELLIPSE));
    }

    @Override
    public void draw(Canvas canvas, Rect infoBounds) {
        if (mInfoText != null && mVisibleText != null) {
            canvas.drawText(mVisibleText, infoBounds.centerX(), infoBounds.centerY(), mInfoPaint);
        }
    }

    @Override
    public void setInfo(String info) {
        mInfoText = info;
    }

    public void updateVisibleText(View parent, Rect progressBar, Rect timeBounds) {
        if (mInfoText == null) {
            mVisibleText = null;
            return;
        }
        float tw = mInfoPaint.measureText(mInfoText);
        float space = progressBar.width() - timeBounds.width() * 2 - parent.getPaddingLeft()
                - parent.getPaddingRight();
        if (tw > 0 && space > 0 && tw > space) {
            // we need to cut the info text for visible
            float originalNum = mInfoText.length();
            int realNum = (int) ((space - mEllipseLength) * originalNum / tw);
            if (LOG) {
                Log.v(TAG, "updateVisibleText() infoText=" + mInfoText + " text width=" + tw
                        + ", space=" + space + ", originalNum=" + originalNum + ", realNum="
                        + realNum
                        + ", getPaddingLeft()=" + parent.getPaddingLeft() + ", getPaddingRight()="
                        + parent.getPaddingRight()
                        + ", progressBar=" + progressBar + ", timeBounds=" + timeBounds);
            }
            mVisibleText = mInfoText.substring(0, realNum) + ELLIPSE;
        } else {
            mVisibleText = mInfoText;
        }
        if (LOG) {
            Log.v(TAG, "updateVisibleText() infoText=" + mInfoText + ", visibleText="
                    + mVisibleText
                    + ", text width=" + tw + ", space=" + space);
        }
    }
}

class TimeBarSecondaryProgressExtImpl implements ITimeBarSecondaryProgressExt {
    private static final String TAG = "TimeBarSecondaryProgressExtensionImpl";
    private static final boolean LOG = true;

    private int mBufferPercent;
    private Rect mSecondaryBar;
    private Paint mSecondaryPaint;

    @Override
    public void init() {
        mSecondaryBar = new Rect();
        mSecondaryPaint = new Paint();
        mSecondaryPaint.setColor(0xFF5CA0C5);
    }

    @Override
    public void draw(Canvas canvas, Rect progressBounds) {
        if (mBufferPercent >= 0) {
            mSecondaryBar.set(progressBounds);
            mSecondaryBar.right = mSecondaryBar.left
                    + (int) (mBufferPercent * progressBounds.width() / 100);
            canvas.drawRect(mSecondaryBar, mSecondaryPaint);
        }
        if (LOG) {
            Log.v(TAG, "draw() bufferPercent=" + mBufferPercent + ", secondaryBar="
                    + mSecondaryBar);
        }
    }

    @Override
    public void setSecondaryProgress(Rect progressBar, int percent) {
        mBufferPercent = percent;
    }
}

class TimeBarLayoutExtImpl implements ITimeBarLayoutExt {
    private static final String TAG = "TimeBarLayoutExtensionImpl";
    private static final boolean LOG = true;

    private int mTextPadding;
    private int mVPaddingInPx;

    @Override
    public void init(int scrubberPadding, int vPaddingInPx) {
        mTextPadding = scrubberPadding / 2;
        mVPaddingInPx = vPaddingInPx;
    }

    @Override
    public int getPreferredHeight(int originalPreferredHeight, Rect timeBounds) {
        return originalPreferredHeight + timeBounds.height() + mTextPadding;
    }

    @Override
    public int getBarHeight(int originalBarHeight, Rect timeBounds) {
        return originalBarHeight + timeBounds.height() + mTextPadding;
    }

    @Override
    public int getProgressMargin(int originalMargin) {
        return 0;
    }

    @Override
    public int getProgressOffset(Rect timeBounds) {
        return (timeBounds.height() + mTextPadding) / 2;
    }

    @Override
    public int getTimeOffset() {
        return mTextPadding - mVPaddingInPx / 2;
    }

    @Override
    public Rect getInfoBounds(View parent, Rect timeBounds) {
        Rect bounds = new Rect(parent.getPaddingLeft(), 0,
                parent.getWidth() - parent.getPaddingRight(),
                (timeBounds.height() + mTextPadding * 3 + 1) * 2);
        return bounds;
    }
}