Home | History | Annotate | Download | only in util
      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.common.util;
     18 
     19 import android.os.SystemClock;
     20 import android.util.Log;
     21 import com.android.tv.common.BuildConfig;
     22 
     23 /** Times a duration. */
     24 public final class DurationTimer {
     25     private static final String TAG = "DurationTimer";
     26     public static final long TIME_NOT_SET = -1;
     27 
     28     private long mStartTimeMs = TIME_NOT_SET;
     29     private String mTag = TAG;
     30     private boolean mLogEngOnly;
     31 
     32     public DurationTimer() {}
     33 
     34     public DurationTimer(String tag, boolean logEngOnly) {
     35         mTag = tag;
     36         mLogEngOnly = logEngOnly;
     37     }
     38 
     39     /** Returns true if the timer is running. */
     40     public boolean isRunning() {
     41         return mStartTimeMs != TIME_NOT_SET;
     42     }
     43 
     44     /** Start the timer. */
     45     public void start() {
     46         mStartTimeMs = SystemClock.elapsedRealtime();
     47     }
     48 
     49     /** Returns true if timer is started. */
     50     public boolean isStarted() {
     51         return mStartTimeMs != TIME_NOT_SET;
     52     }
     53 
     54     /**
     55      * Returns the current duration in milliseconds or {@link #TIME_NOT_SET} if the timer is not
     56      * running.
     57      */
     58     public long getDuration() {
     59         return isRunning() ? SystemClock.elapsedRealtime() - mStartTimeMs : TIME_NOT_SET;
     60     }
     61 
     62     /**
     63      * Stops the timer and resets its value to {@link #TIME_NOT_SET}.
     64      *
     65      * @return the current duration in milliseconds or {@link #TIME_NOT_SET} if the timer is not
     66      *     running.
     67      */
     68     public long reset() {
     69         long duration = getDuration();
     70         mStartTimeMs = TIME_NOT_SET;
     71         return duration;
     72     }
     73 
     74     /** Adds information and duration time to the log. */
     75     public void log(String message) {
     76         if (isRunning() && (!mLogEngOnly || BuildConfig.ENG)) {
     77             Log.i(mTag, message + " : " + getDuration() + "ms");
     78         }
     79     }
     80 }
     81