Home | History | Annotate | Download | only in guide
      1 /*
      2  * Copyright (C) 2015 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.tv.guide;
     18 
     19 import java.util.concurrent.TimeUnit;
     20 
     21 public class GuideUtils {
     22     private static int sWidthPerHour = 0;
     23 
     24     /**
     25      * Sets the width in pixels that corresponds to an hour in program guide.
     26      * Assume that this is called from main thread only, so, no synchronization.
     27      */
     28     public static void setWidthPerHour(int widthPerHour) {
     29         sWidthPerHour = widthPerHour;
     30     }
     31 
     32     /**
     33      * Gets the number of pixels in program guide table that corresponds to the given milliseconds.
     34      */
     35     public static int convertMillisToPixel(long millis) {
     36         return (int) (millis * sWidthPerHour / TimeUnit.HOURS.toMillis(1));
     37     }
     38 
     39     /**
     40      * Gets the number of pixels in program guide table that corresponds to the given range.
     41      */
     42     public static int convertMillisToPixel(long startMillis, long endMillis) {
     43         // Convert to pixels first to avoid accumulation of rounding errors.
     44         return GuideUtils.convertMillisToPixel(endMillis)
     45                 - GuideUtils.convertMillisToPixel(startMillis);
     46     }
     47 
     48     /**
     49      * Gets the time in millis that corresponds to the given pixels in the program guide.
     50      */
     51     public static long convertPixelToMillis(int pixel) {
     52         return pixel * TimeUnit.HOURS.toMillis(1) / sWidthPerHour;
     53     }
     54 
     55     private GuideUtils() { }
     56 }
     57