1 /*
2  * Copyright (C) 2018 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 dalvik.system;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 
23 import java.util.Objects;
24 import java.util.TimeZone;
25 import java.util.function.Supplier;
26 import libcore.util.NonNull;
27 import libcore.util.Nullable;
28 
29 /**
30  * Provides lifecycle methods and other hooks for an Android runtime "container" to call into the
31  * runtime and core libraries during initialization. For example, from
32  * {@link com.android.internal.os.RuntimeInit}.
33  *
34  * <p>Having a facade class helps to limit the container's knowledge of runtime and core libraries
35  * internal details. All methods assume the container initialization is single threaded.
36  *
37  * @hide
38  */
39 @SystemApi(client = MODULE_LIBRARIES)
40 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
41 public final class RuntimeHooks {
42 
43     private static Supplier<String> zoneIdSupplier;
44 
RuntimeHooks()45     private RuntimeHooks() {
46         // No need to construct an instance. All methods are static.
47     }
48 
49     /**
50      * Sets the {@link Supplier} that is used by {@link TimeZone} to retrieve the current time zone
51      * ID iff the cached default is {@code null}.
52      *
53      * <p>This method also clears the current {@link TimeZone} default ensuring that the supplier
54      * will be used next time {@link TimeZone#getDefault()} is called (unless
55      * {@link TimeZone#setDefault(TimeZone)} is called with a non-{@code null} value in the interim).
56      *
57      * <p>Once set the supplier cannot be changed.
58      *
59      * @param zoneIdSupplier new {@link Supplier} of the time zone ID
60      *
61      * @hide
62      */
63     @SystemApi(client = MODULE_LIBRARIES)
64     @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
setTimeZoneIdSupplier(@onNull Supplier<String> zoneIdSupplier)65     public static void setTimeZoneIdSupplier(@NonNull Supplier<String> zoneIdSupplier) {
66         if (RuntimeHooks.zoneIdSupplier != null) {
67             throw new UnsupportedOperationException("zoneIdSupplier instance already set");
68         }
69         RuntimeHooks.zoneIdSupplier = Objects.requireNonNull(zoneIdSupplier);
70         TimeZone.setDefault(null);
71     }
72 
73     /**
74      * Returns the {@link Supplier} that should be used to discover the time zone.
75      *
76      * @hide
77      */
getTimeZoneIdSupplier()78     public static Supplier<String> getTimeZoneIdSupplier() {
79         return RuntimeHooks.zoneIdSupplier;
80     }
81 
82     /**
83      * Sets an {@link Thread.UncaughtExceptionHandler} that will be called before any
84      * returned by {@link Thread#getUncaughtExceptionHandler()}. To allow the standard
85      * handlers to run, this handler should never terminate this process. Any
86      * throwables thrown by the handler will be ignored by
87      * {@link Thread#dispatchUncaughtException(Throwable)}.
88      *
89      * @param uncaughtExceptionHandler handler for uncaught exceptions
90      *
91      * @hide
92      */
93     @SystemApi(client = MODULE_LIBRARIES)
94     @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
setUncaughtExceptionPreHandler( @ullable Thread.UncaughtExceptionHandler uncaughtExceptionHandler)95     public static void setUncaughtExceptionPreHandler(
96             @Nullable Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
97         Thread.setUncaughtExceptionPreHandler(uncaughtExceptionHandler);
98     }
99 }
100