1 /*
2  * Copyright (C) 2020 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 #include <stdbool.h>
18 
19 #include "androidicuinit/android_icu_init.h"
20 #include "IcuRegistration.h"
21 
android_icu_init()22 void android_icu_init() {
23     bool runAndroidInit = false;
24 
25     // We know that the environment variables are exported early in init.environ.rc on Android.
26     #ifdef __ANDROID__
27     runAndroidInit = true;
28     #else // ART host testing environment has these env variables set.
29     runAndroidInit = getenv("ANDROID_DATA") != NULL &&
30                      getenv("ANDROID_TZDATA_ROOT") != NULL &&
31                      getenv("ANDROID_I18N_ROOT") != NULL;
32     #endif
33 
34     if (runAndroidInit) {
35         if (!android_icu_is_registered()) {
36             android_icu_register();
37         } else {
38             AICU_LOGE("libicuuc has already been initialized but android_icu_init() is called.");
39         }
40     }
41 }
42 
android_icu_cleanup()43 void android_icu_cleanup() {
44     if (android_icu_is_registered()) {
45         android_icu_deregister();
46     } else {
47         AICU_LOGW("libicuuc is not initialized and possibly never used, "
48               "but android_icu_cleanup() is called.");
49     }
50 }