1/*
2 * Copyright (C) 2017 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
17package com.example.android.leanback;
18
19import android.graphics.Bitmap;
20import android.graphics.BitmapFactory;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Paint;
24
25import java.io.File;
26
27/**
28 * Sample PlaybackSeekDataProvider that reads bitmaps stored on disk.
29 * e.g. new PlaybackSeekDiskDataProvider(duration, 1000, "/sdcard/frame_%04d.jpg")
30 * Expects the seek positions are 1000ms interval, snapshots are stored at
31 * /sdcard/frame_0001.jpg, ...
32 */
33class PlaybackSeekDiskDataProvider extends PlaybackSeekAsyncDataProvider {
34
35    final Paint mPaint;
36    final String mPathPattern;
37    PlaybackSeekDiskDataProvider(long duration, long interval, String pathPattern) {
38        mPathPattern = pathPattern;
39        int size = (int) (duration / interval) + 1;
40        long[] pos = new long[size];
41        for (int i = 0; i < pos.length; i++) {
42            pos[i] = i * duration / pos.length;
43        }
44        setSeekPositions(pos);
45        mPaint = new Paint();
46        mPaint.setTextSize(16);
47        mPaint.setColor(Color.BLUE);
48    }
49
50    protected Bitmap doInBackground(Object task, int index, long position) {
51        try {
52            Thread.sleep(100);
53        } catch (InterruptedException ex) {
54            // Thread might be interrupted by cancel() call.
55        }
56        if (isCancelled(task)) {
57            return null;
58        }
59        String path = String.format(mPathPattern, (index + 1));
60        if (new File(path).exists()) {
61            return BitmapFactory.decodeFile(path);
62        } else {
63            Bitmap bmp = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
64            Canvas canvas = new Canvas(bmp);
65            canvas.drawColor(Color.YELLOW);
66            canvas.drawText(path, 10, 80, mPaint);
67            canvas.drawText(Integer.toString(index), 10, 150, mPaint);
68            return bmp;
69        }
70    }
71
72}
73