// Path: library/src/main/java/com/franmontiel/localechanger/matcher/LanguageMatchingAlgorithm.java
// public final class LanguageMatchingAlgorithm implements MatchingAlgorithm {
// 
//     @Override
//     public MatchingLocales findDefaultMatch(List<Locale> supportedLocales, List<Locale> systemLocales) {
//         MatchingLocales matchingPair = null;
//         for (Locale systemLocale : systemLocales) {
//             Locale matchingSupportedLocale = findMatchingLocale(systemLocale, supportedLocales);
//             if (matchingSupportedLocale != null) {
//                 matchingPair = new MatchingLocales(matchingSupportedLocale, systemLocale);
//                 break;
//             }
//         }
//         return matchingPair;
//     }
// 
//     @Override
//     public MatchingLocales findMatch(Locale supportedLocale, List<Locale> systemLocales) {
//         Locale matchingSystemLocale = findMatchingLocale(supportedLocale, systemLocales);
//         return matchingSystemLocale != null ?
//                 new MatchingLocales(supportedLocale, matchingSystemLocale) :
//                 null;
//     }
// 
//     private static Locale findMatchingLocale(Locale localeToMatch, List<Locale> candidates) {
//         Locale matchingLocale = null;
//         for (Locale candidate : candidates) {
//             LocaleMatcher.MatchLevel matchLevel = LocaleMatcher.match(localeToMatch, candidate);
// 
//             if (matchLevel != LocaleMatcher.MatchLevel.NoMatch) {
//                 matchingLocale = candidate;
//                 break;
//             }
//         }
//         return matchingLocale;
//     }
// }
// 
// Path: library/src/main/java/com/franmontiel/localechanger/matcher/MatchingAlgorithm.java
// public interface MatchingAlgorithm {
// 
//     /**
//      * Method that implements the algorithm to find two matching Locales between a list of supported and system Locales.
//      *
//      * @param supportedLocales a list of your app supported locales
//      * @param systemLocales    a list of the configured  locales in system preferences
//      * @return a {@link MatchingLocales} containing the pair of matching locales. If no match is found null is returned
//      */
//     MatchingLocales findDefaultMatch(List<Locale> supportedLocales, List<Locale> systemLocales);
// 
//     /**
//      * Method that implements the algorithm to find a matching Locale between the supported Locale and system Locales.
//      *
//      * @param supportedLocale one of your app supported locales
//      * @param systemLocales   a list of the configured  locales in system preferences
//      * @return a {@link MatchingLocales} containing the pair of matching locales. If no match is found null is returned
//      */
//     MatchingLocales findMatch(Locale supportedLocale, List<Locale> systemLocales);
// }
// 
// Path: library/src/main/java/com/franmontiel/localechanger/utils/SystemLocaleRetriever.java
// public class SystemLocaleRetriever {
// 
//     private SystemLocaleRetriever() {
//     }
// 
//     public static List<Locale> retrieve() {
//         List<Locale> systemLocales;
// 
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//             systemLocales = mapToListOfLocales(LocaleList.getDefault());
//         } else {
//             systemLocales = Collections.singletonList(Locale.getDefault());
//         }
//         return systemLocales;
//     }
// 
//     @RequiresApi(api = Build.VERSION_CODES.N)
//     private static List<Locale> mapToListOfLocales(LocaleList localeList) {
//         List<Locale> locales = new ArrayList<>();
//         for (int i = 0; i < localeList.size(); i++) {
//             locales.add(localeList.get(i));
//         }
//         return locales;
//     }
// }


import android.content.Context;
import com.franmontiel.localechanger.matcher.LanguageMatchingAlgorithm;
import com.franmontiel.localechanger.matcher.MatchingAlgorithm;
import com.franmontiel.localechanger.utils.SystemLocaleRetriever;
import java.util.List;
import java.util.Locale;
/*
 * Copyright (c)  2017  Francisco José Montiel Navarro.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.franmontiel.localechanger;




public class LocaleChanger {

    private static LocaleChangerDelegate delegate;

    private LocaleChanger() {
    }

    /**
     * Initialize the LocaleChanger, this method needs to be called before calling any other method.
     * <p>
     * If this method was never invoked before it sets a Locale from the supported list if a language match is found with the system Locales,
     * if no match is found the first Locale in the list will be set.<p>
     * If this method was invoked before it will load a Locale previously set.
     * <p>
     * If this method is invoked when the library is already initialized the new settings will be applied from now on.
     *
     * @param context
     * @param supportedLocales a list of your app supported Locales
     * @throws IllegalStateException if the LocaleChanger is already initialized
     */
    public static void initialize(Context context, List<Locale> supportedLocales) {
        initialize(context,
                supportedLocales,
// Path: library/src/main/java/com/franmontiel/localechanger/matcher/LanguageMatchingAlgorithm.java
// public final class LanguageMatchingAlgorithm implements MatchingAlgorithm {
// 
//     @Override
//     public MatchingLocales findDefaultMatch(List<Locale> supportedLocales, List<Locale> systemLocales) {
//         MatchingLocales matchingPair = null;
//         for (Locale systemLocale : systemLocales) {
//             Locale matchingSupportedLocale = findMatchingLocale(systemLocale, supportedLocales);
//             if (matchingSupportedLocale != null) {
//                 matchingPair = new MatchingLocales(matchingSupportedLocale, systemLocale);
//                 break;
//             }
//         }
//         return matchingPair;
//     }
// 
//     @Override
//     public MatchingLocales findMatch(Locale supportedLocale, List<Locale> systemLocales) {
//         Locale matchingSystemLocale = findMatchingLocale(supportedLocale, systemLocales);
//         return matchingSystemLocale != null ?
//                 new MatchingLocales(supportedLocale, matchingSystemLocale) :
//                 null;
//     }
// 
//     private static Locale findMatchingLocale(Locale localeToMatch, List<Locale> candidates) {
//         Locale matchingLocale = null;
//         for (Locale candidate : candidates) {
//             LocaleMatcher.MatchLevel matchLevel = LocaleMatcher.match(localeToMatch, candidate);
// 
//             if (matchLevel != LocaleMatcher.MatchLevel.NoMatch) {
//                 matchingLocale = candidate;
//                 break;
//             }
//         }
//         return matchingLocale;
//     }
// }
// 
// Path: library/src/main/java/com/franmontiel/localechanger/matcher/MatchingAlgorithm.java
// public interface MatchingAlgorithm {
// 
//     /**
//      * Method that implements the algorithm to find two matching Locales between a list of supported and system Locales.
//      *
//      * @param supportedLocales a list of your app supported locales
//      * @param systemLocales    a list of the configured  locales in system preferences
//      * @return a {@link MatchingLocales} containing the pair of matching locales. If no match is found null is returned
//      */
//     MatchingLocales findDefaultMatch(List<Locale> supportedLocales, List<Locale> systemLocales);
// 
//     /**
//      * Method that implements the algorithm to find a matching Locale between the supported Locale and system Locales.
//      *
//      * @param supportedLocale one of your app supported locales
//      * @param systemLocales   a list of the configured  locales in system preferences
//      * @return a {@link MatchingLocales} containing the pair of matching locales. If no match is found null is returned
//      */
//     MatchingLocales findMatch(Locale supportedLocale, List<Locale> systemLocales);
// }
// 
// Path: library/src/main/java/com/franmontiel/localechanger/utils/SystemLocaleRetriever.java
// public class SystemLocaleRetriever {
// 
//     private SystemLocaleRetriever() {
//     }
// 
//     public static List<Locale> retrieve() {
//         List<Locale> systemLocales;
// 
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//             systemLocales = mapToListOfLocales(LocaleList.getDefault());
//         } else {
//             systemLocales = Collections.singletonList(Locale.getDefault());
//         }
//         return systemLocales;
//     }
// 
//     @RequiresApi(api = Build.VERSION_CODES.N)
//     private static List<Locale> mapToListOfLocales(LocaleList localeList) {
//         List<Locale> locales = new ArrayList<>();
//         for (int i = 0; i < localeList.size(); i++) {
//             locales.add(localeList.get(i));
//         }
//         return locales;
//     }
// }


// Path: library/src/main/java/com/franmontiel/localechanger/LocaleChanger.java
import android.content.Context;
import com.franmontiel.localechanger.matcher.LanguageMatchingAlgorithm;
import com.franmontiel.localechanger.matcher.MatchingAlgorithm;
import com.franmontiel.localechanger.utils.SystemLocaleRetriever;
import java.util.List;
import java.util.Locale;

/*
 * Copyright (c)  2017  Francisco José Montiel Navarro.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.franmontiel.localechanger;




public class LocaleChanger {

    private static LocaleChangerDelegate delegate;

    private LocaleChanger() {
    }

    /**
     * Initialize the LocaleChanger, this method needs to be called before calling any other method.
     * <p>
     * If this method was never invoked before it sets a Locale from the supported list if a language match is found with the system Locales,
     * if no match is found the first Locale in the list will be set.<p>
     * If this method was invoked before it will load a Locale previously set.
     * <p>
     * If this method is invoked when the library is already initialized the new settings will be applied from now on.
     *
     * @param context
     * @param supportedLocales a list of your app supported Locales
     * @throws IllegalStateException if the LocaleChanger is already initialized
     */
    public static void initialize(Context context, List<Locale> supportedLocales) {
        initialize(context,
                supportedLocales,
                new LanguageMatchingAlgorithm(),