1 /*
2  * Copyright (C) 2021 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 com.android.server.wm;
18 
19 import android.os.LocaleList;
20 
21 import java.util.Locale;
22 
23 /**
24  * Static utilities to overlay locales on top of another localeList.
25  *
26  * <p>This is used to overlay application-specific locales in
27  *  {@link com.android.server.wm.ActivityTaskManagerInternal.PackageConfigurationUpdater} on top of
28  *  system locales.
29  */
30 final class LocaleOverlayHelper {
31 
32     /**
33      * Combines the overlay locales and base locales.
34      * @return the combined {@link LocaleList} if the overlay locales is not empty/null else
35      * returns the empty/null LocaleList.
36      */
combineLocalesIfOverlayExists(LocaleList overlayLocales, LocaleList baseLocales)37     static LocaleList combineLocalesIfOverlayExists(LocaleList overlayLocales,
38             LocaleList baseLocales) {
39         if (overlayLocales == null || overlayLocales.isEmpty()) {
40             return overlayLocales;
41         }
42         return combineLocales(overlayLocales, baseLocales);
43     }
44 
45     /**
46      * Creates a combined {@link LocaleList} by placing overlay locales before base locales and
47      * dropping duplicates from the base locales.
48      */
combineLocales(LocaleList overlayLocales, LocaleList baseLocales)49     private static LocaleList combineLocales(LocaleList overlayLocales, LocaleList baseLocales) {
50         Locale[] combinedLocales = new Locale[overlayLocales.size() + baseLocales.size()];
51         for (int i = 0; i < overlayLocales.size(); i++) {
52             combinedLocales[i] = overlayLocales.get(i);
53         }
54         for (int i = 0; i < baseLocales.size(); i++) {
55             combinedLocales[i + overlayLocales.size()] = baseLocales.get(i);
56         }
57         // Constructor of {@link LocaleList} removes duplicates
58         return new LocaleList(combinedLocales);
59     }
60 
61 
62 }
63