Home | History | Annotate | Download | only in leanback
      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 
     17 package com.example.android.leanback;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.graphics.Paint;
     23 
     24 /**
     25  * Sample PlaybackSeekDataProvider render time label as thumb.
     26  */
     27 class PlaybackSeekDataProviderSample extends PlaybackSeekAsyncDataProvider {
     28 
     29     Paint mPaint;
     30 
     31     PlaybackSeekDataProviderSample(long duration, long interval) {
     32         int size = (int) (duration / interval) + 1;
     33         long[] pos = new long[size];
     34         for (int i = 0; i < pos.length; i++) {
     35             pos[i] = i * duration / pos.length;
     36         }
     37         setSeekPositions(pos);
     38         mPaint = new Paint();
     39         mPaint.setTextSize(16);
     40         mPaint.setColor(Color.BLUE);
     41     }
     42 
     43     protected Bitmap doInBackground(Object task, int index, long position) {
     44         try {
     45             Thread.sleep(100);
     46         } catch (InterruptedException ex) {
     47             // Thread might be interrupted by cancel() call.
     48         }
     49         if (isCancelled(task)) {
     50             return null;
     51         }
     52         Bitmap bmp = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
     53         Canvas canvas = new Canvas(bmp);
     54         canvas.drawColor(Color.YELLOW);
     55         canvas.drawText(formatTime(position), 10, 80, mPaint);
     56         canvas.drawText(Integer.toString(index), 10, 150, mPaint);
     57         return bmp;
     58     }
     59 
     60     String formatTime(long ms) {
     61         long seconds = ms / 1000;
     62         float seconds2 = (ms - seconds * 1000) / 1000f;
     63         long minutes = seconds / 60;
     64         long hours = minutes / 60;
     65         seconds -= minutes * 60;
     66         minutes -= hours * 60;
     67 
     68         StringBuilder b = new StringBuilder();
     69         if (hours > 0) {
     70             b.append(hours).append(':');
     71             if (minutes < 10) {
     72                 b.append('0');
     73             }
     74         }
     75         b.append(minutes).append(':');
     76         if (seconds < 10) {
     77             b.append('0');
     78         }
     79         b.append(String.format("%.2f", ((float) seconds + seconds2)));
     80         return b.toString();
     81     }
     82 
     83 }
     84