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 17 package android.os; 18 19 import com.android.layoutlib.bridge.impl.DelegateManager; 20 import 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 */ 32 public class SystemClock_Delegate { 33 private static long sBootTime = System.currentTimeMillis(); 34 private static long sBootTimeNano = System.nanoTime(); 35 36 /** 37 * Returns milliseconds since boot, not counting time spent in deep sleep. 38 * <b>Note:</b> This value may get reset occasionally (before it would 39 * otherwise wrap around). 40 * 41 * @return milliseconds of non-sleep uptime since boot. 42 */ 43 @LayoutlibDelegate uptimeMillis()44 /*package*/ static long uptimeMillis() { 45 return System.currentTimeMillis() - sBootTime; 46 } 47 48 /** 49 * Returns milliseconds since boot, including time spent in sleep. 50 * 51 * @return elapsed milliseconds since boot. 52 */ 53 @LayoutlibDelegate elapsedRealtime()54 /*package*/ static long elapsedRealtime() { 55 return System.currentTimeMillis() - sBootTime; 56 } 57 58 /** 59 * Returns nanoseconds since boot, including time spent in sleep. 60 * 61 * @return elapsed nanoseconds since boot. 62 */ 63 @LayoutlibDelegate elapsedRealtimeNanos()64 /*package*/ static long elapsedRealtimeNanos() { 65 return System.nanoTime() - sBootTimeNano; 66 } 67 68 /** 69 * Returns milliseconds running in the current thread. 70 * 71 * @return elapsed milliseconds in the thread 72 */ 73 @LayoutlibDelegate currentThreadTimeMillis()74 /*package*/ static long currentThreadTimeMillis() { 75 return System.currentTimeMillis(); 76 } 77 78 /** 79 * Returns microseconds running in the current thread. 80 * 81 * @return elapsed microseconds in the thread 82 * 83 * @hide 84 */ 85 @LayoutlibDelegate currentThreadTimeMicro()86 /*package*/ static long currentThreadTimeMicro() { 87 return System.currentTimeMillis() * 1000; 88 } 89 90 /** 91 * Returns current wall time in microseconds. 92 * 93 * @return elapsed microseconds in wall time 94 * 95 * @hide 96 */ 97 @LayoutlibDelegate currentTimeMicro()98 /*package*/ static long currentTimeMicro() { 99 return elapsedRealtime() * 1000; 100 } 101 } 102