1/*
2 * Copyright (C) 2009 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
17package com.android.quicksearchbox;
18
19import android.os.SystemClock;
20
21/**
22 * Tracks latency in wall-clock time. Since {@link #getLatency} returns an {@code int},
23 * latencies over 2^31 ms (~ 25 days) cannot be measured.
24 * This class uses {@link SystemClock#uptimeMillis} which does not advance during deep sleep.
25 */
26public class LatencyTracker {
27
28    /**
29     * Start time, in milliseconds as returned by {@link SystemClock#uptimeMillis}.
30     */
31    private long mStartTime;
32
33    /**
34     * Creates a new latency tracker and sets the start time.
35     */
36    public LatencyTracker() {
37        mStartTime = SystemClock.uptimeMillis();
38    }
39
40    /**
41     * Resets the start time.
42     */
43    public void reset() {
44        mStartTime = SystemClock.uptimeMillis();
45    }
46
47    /**
48     * Gets the number of milliseconds since the object was created, or {@link #reset} was called.
49     */
50    public int getLatency() {
51        long now = SystemClock.uptimeMillis();
52        return (int) (now - mStartTime);
53    }
54
55}
56