• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package com.android.tv.common.util;
17 
18 import android.os.SystemClock;
19 
20 /**
21  * An interface through which system clocks can be read. The {@link #SYSTEM} implementation must be
22  * used for all non-test cases.
23  */
24 public interface Clock {
25     /**
26      * Returns the current time in milliseconds since January 1, 1970 00:00:00.0 UTC.
27      *
28      * @see System#currentTimeMillis().
29      */
currentTimeMillis()30     long currentTimeMillis();
31 
32     /**
33      * Returns milliseconds since boot, including time spent in sleep.
34      *
35      * @see SystemClock#elapsedRealtime()
36      */
elapsedRealtime()37     long elapsedRealtime();
38 
39     /**
40      * Returns milliseconds since boot, not counting time spent in deep sleep.
41      *
42      * @return milliseconds of non-sleep uptime since boot.
43      * @see SystemClock#uptimeMillis()
44      */
uptimeMillis()45     long uptimeMillis();
46 
47     /**
48      * Waits a given number of milliseconds (of uptimeMillis) before returning.
49      *
50      * @param ms to sleep before returning, in milliseconds of uptime.
51      * @see SystemClock#sleep(long)
52      */
sleep(long ms)53     void sleep(long ms);
54 
55     /** The default implementation of Clock. */
56     Clock SYSTEM =
57             new Clock() {
58                 @Override
59                 public long currentTimeMillis() {
60                     return System.currentTimeMillis();
61                 }
62 
63                 @Override
64                 public long elapsedRealtime() {
65                     return SystemClock.elapsedRealtime();
66                 }
67 
68                 @Override
69                 public void sleep(long ms) {
70                     SystemClock.sleep(ms);
71                 }
72 
73                 @Override
74                 public long uptimeMillis() {
75                     return SystemClock.uptimeMillis();
76                 }
77             };
78 }
79