1 /*
2  * Copyright (C) 2014 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 package com.android.internal.lang;
17 
18 import com.android.layoutlib.bridge.android.BridgeContext;
19 
20 import java.util.WeakHashMap;
21 import java.util.concurrent.TimeUnit;
22 
23 import static com.android.layoutlib.bridge.impl.RenderAction.getCurrentContext;
24 
25 /**
26  * Provides alternative implementations of methods that don't exist on the host VM.
27  * This also providers a time control that allows to set a specific system time.
28  *
29  * @see ReplaceMethodCallsAdapter
30  */
31 @SuppressWarnings("unused")
32 public class System_Delegate {
log(String message)33     public static void log(String message) {
34         // ignore.
35     }
36 
log(String message, Throwable th)37     public static void log(String message, Throwable th) {
38         // ignore.
39     }
40 
setNanosTime(long nanos)41     public static void setNanosTime(long nanos) {
42         BridgeContext context = getCurrentContext();
43         if (context != null) {
44             context.getSessionInteractiveData().setNanosTime(nanos);
45         }
46     }
47 
setBootTimeNanos(long nanos)48     public static void setBootTimeNanos(long nanos) {
49         BridgeContext context = getCurrentContext();
50         if (context != null) {
51             context.getSessionInteractiveData().setBootNanosTime(nanos);
52         }
53     }
54 
nanoTime()55     public static long nanoTime() {
56         BridgeContext context = getCurrentContext();
57         if (context != null) {
58             return context.getSessionInteractiveData().getNanosTime();
59         }
60         return 0;
61     }
62 
currentTimeMillis()63     public static long currentTimeMillis() {
64         return TimeUnit.NANOSECONDS.toMillis(nanoTime());
65     }
66 
bootTime()67     public static long bootTime() {
68         BridgeContext context = getCurrentContext();
69         if (context != null) {
70             return context.getSessionInteractiveData().getBootNanosTime();
71         }
72         return 0;
73     }
74 
bootTimeMillis()75     public static long bootTimeMillis() {
76         return TimeUnit.NANOSECONDS.toMillis(bootTime());
77     }
78 
79     // This is no-op since layoutlib infrastructure loads all the native libraries.
loadLibrary(String libname)80     public static void loadLibrary(String libname) {
81         // ignore.
82     }
83 }
84