Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
91690f06
authored
Apr 15, 2020
by
christosts
Committed by
Ian Baker
Apr 15, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add Clock#currentTimeMillis()
PiperOrigin-RevId: 306602043
parent
82341976
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
12 deletions
library/core/src/main/java/com/google/android/exoplayer2/util/Clock.java
library/core/src/main/java/com/google/android/exoplayer2/util/SystemClock.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java
testutils/src/test/java/com/google/android/exoplayer2/testutil/FakeClockTest.java
library/core/src/main/java/com/google/android/exoplayer2/util/Clock.java
View file @
91690f06
...
@@ -30,6 +30,13 @@ public interface Clock {
...
@@ -30,6 +30,13 @@ public interface Clock {
*/
*/
Clock
DEFAULT
=
new
SystemClock
();
Clock
DEFAULT
=
new
SystemClock
();
/**
* Returns the current time in milliseconds since the Unix Epoch.
*
* @see System#currentTimeMillis()
*/
long
currentTimeMillis
();
/** @see android.os.SystemClock#elapsedRealtime() */
/** @see android.os.SystemClock#elapsedRealtime() */
long
elapsedRealtime
();
long
elapsedRealtime
();
...
...
library/core/src/main/java/com/google/android/exoplayer2/util/SystemClock.java
View file @
91690f06
...
@@ -26,6 +26,11 @@ import androidx.annotation.Nullable;
...
@@ -26,6 +26,11 @@ import androidx.annotation.Nullable;
/* package */
final
class
SystemClock
implements
Clock
{
/* package */
final
class
SystemClock
implements
Clock
{
@Override
@Override
public
long
currentTimeMillis
()
{
return
System
.
currentTimeMillis
();
}
@Override
public
long
elapsedRealtime
()
{
public
long
elapsedRealtime
()
{
return
android
.
os
.
SystemClock
.
elapsedRealtime
();
return
android
.
os
.
SystemClock
.
elapsedRealtime
();
}
}
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java
View file @
91690f06
...
@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.testutil;
...
@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.testutil;
import
android.os.Handler.Callback
;
import
android.os.Handler.Callback
;
import
android.os.Looper
;
import
android.os.Looper
;
import
android.os.Message
;
import
android.os.Message
;
import
androidx.annotation.GuardedBy
;
import
androidx.annotation.Nullable
;
import
androidx.annotation.Nullable
;
import
com.google.android.exoplayer2.util.Clock
;
import
com.google.android.exoplayer2.util.Clock
;
import
com.google.android.exoplayer2.util.HandlerWrapper
;
import
com.google.android.exoplayer2.util.HandlerWrapper
;
...
@@ -29,16 +30,31 @@ public class FakeClock implements Clock {
...
@@ -29,16 +30,31 @@ public class FakeClock implements Clock {
private
final
List
<
Long
>
wakeUpTimes
;
private
final
List
<
Long
>
wakeUpTimes
;
private
final
List
<
HandlerMessageData
>
handlerMessages
;
private
final
List
<
HandlerMessageData
>
handlerMessages
;
private
final
long
bootTimeMs
;
private
long
currentTimeMs
;
@GuardedBy
(
"this"
)
private
long
timeSinceBootMs
;
/**
/**
* Create {@link FakeClock} with an arbitrary initial timestamp.
* Creates a fake clock assuming the system was booted exactly at time {@code 0} (the Unix Epoch)
* and {@code initialTimeMs} milliseconds have passed since system boot.
*
*
* @param initialTimeMs
Initial timestamp
in milliseconds.
* @param initialTimeMs
The initial elapsed time since the boot time,
in milliseconds.
*/
*/
public
FakeClock
(
long
initialTimeMs
)
{
public
FakeClock
(
long
initialTimeMs
)
{
this
.
currentTimeMs
=
initialTimeMs
;
this
(
/* bootTimeMs= */
0
,
initialTimeMs
);
}
/**
* Creates a fake clock specifying when the system was booted and how much time has passed since
* then.
*
* @param bootTimeMs The time the system was booted since the Unix Epoch, in milliseconds.
* @param initialTimeMs The initial elapsed time since the boot time, in milliseconds.
*/
public
FakeClock
(
long
bootTimeMs
,
long
initialTimeMs
)
{
this
.
bootTimeMs
=
bootTimeMs
;
this
.
timeSinceBootMs
=
initialTimeMs
;
this
.
wakeUpTimes
=
new
ArrayList
<>();
this
.
wakeUpTimes
=
new
ArrayList
<>();
this
.
handlerMessages
=
new
ArrayList
<>();
this
.
handlerMessages
=
new
ArrayList
<>();
}
}
...
@@ -49,23 +65,28 @@ public class FakeClock implements Clock {
...
@@ -49,23 +65,28 @@ public class FakeClock implements Clock {
* @param timeDiffMs The amount of time to add to the timestamp in milliseconds.
* @param timeDiffMs The amount of time to add to the timestamp in milliseconds.
*/
*/
public
synchronized
void
advanceTime
(
long
timeDiffMs
)
{
public
synchronized
void
advanceTime
(
long
timeDiffMs
)
{
currentTime
Ms
+=
timeDiffMs
;
timeSinceBoot
Ms
+=
timeDiffMs
;
for
(
Long
wakeUpTime
:
wakeUpTimes
)
{
for
(
Long
wakeUpTime
:
wakeUpTimes
)
{
if
(
wakeUpTime
<=
currentTime
Ms
)
{
if
(
wakeUpTime
<=
timeSinceBoot
Ms
)
{
notifyAll
();
notifyAll
();
break
;
break
;
}
}
}
}
for
(
int
i
=
handlerMessages
.
size
()
-
1
;
i
>=
0
;
i
--)
{
for
(
int
i
=
handlerMessages
.
size
()
-
1
;
i
>=
0
;
i
--)
{
if
(
handlerMessages
.
get
(
i
).
maybeSendToTarget
(
currentTime
Ms
))
{
if
(
handlerMessages
.
get
(
i
).
maybeSendToTarget
(
timeSinceBoot
Ms
))
{
handlerMessages
.
remove
(
i
);
handlerMessages
.
remove
(
i
);
}
}
}
}
}
}
@Override
@Override
public
synchronized
long
currentTimeMillis
()
{
return
bootTimeMs
+
timeSinceBootMs
;
}
@Override
public
synchronized
long
elapsedRealtime
()
{
public
synchronized
long
elapsedRealtime
()
{
return
currentTime
Ms
;
return
timeSinceBoot
Ms
;
}
}
@Override
@Override
...
@@ -78,9 +99,9 @@ public class FakeClock implements Clock {
...
@@ -78,9 +99,9 @@ public class FakeClock implements Clock {
if
(
sleepTimeMs
<=
0
)
{
if
(
sleepTimeMs
<=
0
)
{
return
;
return
;
}
}
Long
wakeUpTimeMs
=
currentTime
Ms
+
sleepTimeMs
;
Long
wakeUpTimeMs
=
timeSinceBoot
Ms
+
sleepTimeMs
;
wakeUpTimes
.
add
(
wakeUpTimeMs
);
wakeUpTimes
.
add
(
wakeUpTimeMs
);
while
(
currentTime
Ms
<
wakeUpTimeMs
)
{
while
(
timeSinceBoot
Ms
<
wakeUpTimeMs
)
{
try
{
try
{
wait
();
wait
();
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
...
@@ -98,7 +119,7 @@ public class FakeClock implements Clock {
...
@@ -98,7 +119,7 @@ public class FakeClock implements Clock {
/** Adds a handler post to list of pending messages. */
/** Adds a handler post to list of pending messages. */
protected
synchronized
boolean
addHandlerMessageAtTime
(
protected
synchronized
boolean
addHandlerMessageAtTime
(
HandlerWrapper
handler
,
Runnable
runnable
,
long
timeMs
)
{
HandlerWrapper
handler
,
Runnable
runnable
,
long
timeMs
)
{
if
(
timeMs
<=
currentTime
Ms
)
{
if
(
timeMs
<=
timeSinceBoot
Ms
)
{
return
handler
.
post
(
runnable
);
return
handler
.
post
(
runnable
);
}
}
handlerMessages
.
add
(
new
HandlerMessageData
(
timeMs
,
handler
,
runnable
));
handlerMessages
.
add
(
new
HandlerMessageData
(
timeMs
,
handler
,
runnable
));
...
@@ -108,7 +129,7 @@ public class FakeClock implements Clock {
...
@@ -108,7 +129,7 @@ public class FakeClock implements Clock {
/** Adds an empty handler message to list of pending messages. */
/** Adds an empty handler message to list of pending messages. */
protected
synchronized
boolean
addHandlerMessageAtTime
(
protected
synchronized
boolean
addHandlerMessageAtTime
(
HandlerWrapper
handler
,
int
message
,
long
timeMs
)
{
HandlerWrapper
handler
,
int
message
,
long
timeMs
)
{
if
(
timeMs
<=
currentTime
Ms
)
{
if
(
timeMs
<=
timeSinceBoot
Ms
)
{
return
handler
.
sendEmptyMessage
(
message
);
return
handler
.
sendEmptyMessage
(
message
);
}
}
handlerMessages
.
add
(
new
HandlerMessageData
(
timeMs
,
handler
,
message
));
handlerMessages
.
add
(
new
HandlerMessageData
(
timeMs
,
handler
,
message
));
...
...
testutils/src/test/java/com/google/android/exoplayer2/testutil/FakeClockTest.java
View file @
91690f06
...
@@ -36,6 +36,25 @@ public final class FakeClockTest {
...
@@ -36,6 +36,25 @@ public final class FakeClockTest {
private
static
final
long
TIMEOUT_MS
=
10000
;
private
static
final
long
TIMEOUT_MS
=
10000
;
@Test
@Test
public
void
currentTimeMillis_withoutBootTime
()
{
FakeClock
fakeClock
=
new
FakeClock
(
/* initialTimeMs= */
10
);
assertThat
(
fakeClock
.
currentTimeMillis
()).
isEqualTo
(
10
);
}
@Test
public
void
currentTimeMillis_withBootTime
()
{
FakeClock
fakeClock
=
new
FakeClock
(
/* bootTimeMs= */
150
,
/* initialTimeMs= */
200
);
assertThat
(
fakeClock
.
currentTimeMillis
()).
isEqualTo
(
350
);
}
@Test
public
void
currentTimeMillis_advanceTime_currentTimeHasAdvanced
()
{
FakeClock
fakeClock
=
new
FakeClock
(
/* bootTimeMs= */
100
,
/* initialTimeMs= */
50
);
fakeClock
.
advanceTime
(
/* timeDiffMs */
250
);
assertThat
(
fakeClock
.
currentTimeMillis
()).
isEqualTo
(
400
);
}
@Test
public
void
testAdvanceTime
()
{
public
void
testAdvanceTime
()
{
FakeClock
fakeClock
=
new
FakeClock
(
2000
);
FakeClock
fakeClock
=
new
FakeClock
(
2000
);
assertThat
(
fakeClock
.
elapsedRealtime
()).
isEqualTo
(
2000
);
assertThat
(
fakeClock
.
elapsedRealtime
()).
isEqualTo
(
2000
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment