summaryrefslogtreecommitdiff
path: root/src/android/net/util/Stopwatch.java
diff options
context:
space:
mode:
authorRemi NGUYEN VAN <reminv@google.com>2018-12-27 16:43:56 +0900
committerRemi NGUYEN VAN <reminv@google.com>2019-01-11 09:59:11 +0900
commit5daa3703cc174ab7764b7fd568e68688fd96fe32 (patch)
tree943f7a0184d8376f0c67c56b40a8b019d3c8a429 /src/android/net/util/Stopwatch.java
parent057bf20e86b62e744f823818c2741f659106e04d (diff)
Move NetworkMonitor to NetworkStack
Bug: b/112869080 Test: atest FrameworksNetTests NetworkStackTests Change-Id: I1b8b6a3f4390adbabf92fb9e48da61c47b08b2ec
Diffstat (limited to 'src/android/net/util/Stopwatch.java')
-rw-r--r--src/android/net/util/Stopwatch.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/android/net/util/Stopwatch.java b/src/android/net/util/Stopwatch.java
new file mode 100644
index 0000000..c316699
--- /dev/null
+++ b/src/android/net/util/Stopwatch.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.util;
+
+import android.os.SystemClock;
+
+
+/**
+ * @hide
+ */
+public class Stopwatch {
+ private long mStartTimeMs;
+ private long mStopTimeMs;
+
+ public boolean isStarted() {
+ return (mStartTimeMs > 0);
+ }
+
+ public boolean isStopped() {
+ return (mStopTimeMs > 0);
+ }
+
+ public boolean isRunning() {
+ return (isStarted() && !isStopped());
+ }
+
+ /**
+ * Start the Stopwatch.
+ */
+ public Stopwatch start() {
+ if (!isStarted()) {
+ mStartTimeMs = SystemClock.elapsedRealtime();
+ }
+ return this;
+ }
+
+ /**
+ * Stop the Stopwatch.
+ * @return the total time recorded, in milliseconds, or 0 if not started.
+ */
+ public long stop() {
+ if (isRunning()) {
+ mStopTimeMs = SystemClock.elapsedRealtime();
+ }
+ // Return either the delta after having stopped, or 0.
+ return (mStopTimeMs - mStartTimeMs);
+ }
+
+ /**
+ * Return the total time recorded to date, in milliseconds.
+ * If the Stopwatch is not running, returns the same value as stop(),
+ * i.e. either the total time recorded before stopping or 0.
+ */
+ public long lap() {
+ if (isRunning()) {
+ return (SystemClock.elapsedRealtime() - mStartTimeMs);
+ } else {
+ return stop();
+ }
+ }
+
+ /**
+ * Reset the Stopwatch. It will be stopped when this method returns.
+ */
+ public void reset() {
+ mStartTimeMs = 0;
+ mStopTimeMs = 0;
+ }
+}