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 public final class RuntimeHooks { 41 42 private static Supplier<String> zoneIdSupplier; 43 RuntimeHooks()44 private RuntimeHooks() { 45 // No need to construct an instance. All methods are static. 46 } 47 48 /** 49 * Sets the {@link Supplier} that is used by {@link TimeZone} to retrieve the current time zone 50 * ID iff the cached default is {@code null}. 51 * 52 * <p>This method also clears the current {@link TimeZone} default ensuring that the supplier 53 * will be used next time {@link TimeZone#getDefault()} is called (unless 54 * {@link TimeZone#setDefault(TimeZone)} is called with a non-{@code null} value in the interim). 55 * 56 * <p>Once set the supplier cannot be changed. 57 * 58 * @param zoneIdSupplier new {@link Supplier} of the time zone ID 59 * 60 * @hide 61 */ 62 @SystemApi(client = MODULE_LIBRARIES) setTimeZoneIdSupplier(@onNull Supplier<String> zoneIdSupplier)63 public static void setTimeZoneIdSupplier(@NonNull Supplier<String> zoneIdSupplier) { 64 if (RuntimeHooks.zoneIdSupplier != null) { 65 throw new UnsupportedOperationException("zoneIdSupplier instance already set"); 66 } 67 RuntimeHooks.zoneIdSupplier = Objects.requireNonNull(zoneIdSupplier); 68 TimeZone.setDefault(null); 69 } 70 71 /** 72 * @hide 73 */ 74 // VisibleForTesting clearTimeZoneIdSupplier()75 public static void clearTimeZoneIdSupplier() { 76 RuntimeHooks.zoneIdSupplier = null; 77 } 78 79 /** 80 * Returns the {@link Supplier} that should be used to discover the time zone. 81 * 82 * @hide 83 */ getTimeZoneIdSupplier()84 public static Supplier<String> getTimeZoneIdSupplier() { 85 return RuntimeHooks.zoneIdSupplier; 86 } 87 88 /** 89 * Sets an {@link Thread.UncaughtExceptionHandler} that will be called before any 90 * returned by {@link Thread#getUncaughtExceptionHandler()}. To allow the standard 91 * handlers to run, this handler should never terminate this process. Any 92 * throwables thrown by the handler will be ignored by 93 * {@link Thread#dispatchUncaughtException(Throwable)}. 94 * 95 * @param uncaughtExceptionHandler handler for uncaught exceptions 96 * 97 * @hide 98 */ 99 @SystemApi(client = MODULE_LIBRARIES) setUncaughtExceptionPreHandler( @ullable Thread.UncaughtExceptionHandler uncaughtExceptionHandler)100 public static void setUncaughtExceptionPreHandler( 101 @Nullable Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { 102 Thread.setUncaughtExceptionPreHandler(uncaughtExceptionHandler); 103 } 104 } 105