xref: /aosp_15_r20/external/icu/libandroidicuinit/android_icu_init.cpp (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
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       #ifdef ANDROID_ICU_NO_DAT
28         // If we're intentionally building ICU on Android without the .dat file, no
29         // need to run init.
30         runAndroidInit = false;
31       #else  // !ANDROID_ICU_NO_DAT
32         runAndroidInit = true;
33       #endif
34     #else // ART host testing environment has these env variables set.
35     runAndroidInit = getenv("ANDROID_DATA") != NULL &&
36                      getenv("ANDROID_TZDATA_ROOT") != NULL &&
37                      getenv("ANDROID_I18N_ROOT") != NULL;
38     #endif
39 
40     if (runAndroidInit) {
41         if (!android_icu_is_registered()) {
42             android_icu_register();
43         } else {
44             AICU_LOGE("libicuuc has already been initialized but android_icu_init() is called.");
45         }
46     }
47 }
48 
android_icu_cleanup()49 void android_icu_cleanup() {
50     if (android_icu_is_registered()) {
51         android_icu_deregister();
52     } else {
53         AICU_LOGW("libicuuc is not initialized and possibly never used, "
54               "but android_icu_cleanup() is called.");
55     }
56 }
57