1/*
2 * Copyright (C) 2010 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 android.os;
18
19import com.android.layoutlib.bridge.impl.DelegateManager;
20import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21
22/**
23 * Delegate implementing the native methods of android.os.SystemClock
24 *
25 * Through the layoutlib_create tool, the original native methods of SystemClock have been replaced
26 * by calls to methods of the same name in this delegate class.
27 *
28 * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager}
29 * around to map int to instance of the delegate.
30 *
31 */
32public class SystemClock_Delegate {
33    private static long sBootTime = System.currentTimeMillis();
34    private static long sBootTimeNano = System.nanoTime();
35
36    @LayoutlibDelegate
37    /*package*/ static boolean setCurrentTimeMillis(long millis) {
38        return true;
39    }
40
41    /**
42     * Returns milliseconds since boot, not counting time spent in deep sleep.
43     * <b>Note:</b> This value may get reset occasionally (before it would
44     * otherwise wrap around).
45     *
46     * @return milliseconds of non-sleep uptime since boot.
47     */
48    @LayoutlibDelegate
49    /*package*/ static long uptimeMillis() {
50        return System.currentTimeMillis() - sBootTime;
51    }
52
53    /**
54     * Returns milliseconds since boot, including time spent in sleep.
55     *
56     * @return elapsed milliseconds since boot.
57     */
58    @LayoutlibDelegate
59    /*package*/ static long elapsedRealtime() {
60        return System.currentTimeMillis() - sBootTime;
61    }
62
63    /**
64     * Returns nanoseconds since boot, including time spent in sleep.
65     *
66     * @return elapsed nanoseconds since boot.
67     */
68    @LayoutlibDelegate
69    /*package*/ static long elapsedRealtimeNanos() {
70        return System.nanoTime() - sBootTimeNano;
71    }
72
73    /**
74     * Returns milliseconds running in the current thread.
75     *
76     * @return elapsed milliseconds in the thread
77     */
78    @LayoutlibDelegate
79    /*package*/ static long currentThreadTimeMillis() {
80        return System.currentTimeMillis();
81    }
82
83    /**
84     * Returns microseconds running in the current thread.
85     *
86     * @return elapsed microseconds in the thread
87     *
88     * @hide
89     */
90    @LayoutlibDelegate
91    /*package*/ static long currentThreadTimeMicro() {
92        return System.currentTimeMillis() * 1000;
93    }
94
95    /**
96     * Returns current wall time in  microseconds.
97     *
98     * @return elapsed microseconds in wall time
99     *
100     * @hide
101     */
102    @LayoutlibDelegate
103    /*package*/ static long currentTimeMicro() {
104        return elapsedRealtime() * 1000;
105    }
106}
107