content: /*
   * Copyright (c) 2014 Amahi
   *
   * This file is part of Amahi.
   *
   * Amahi is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Amahi is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Amahi. If not, see <http ://www.gnu.org/licenses/>.
   */
  
  package org.amahi.anywhere.util;
  
  import android.annotation.TargetApi;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.content.res.Resources;
  import android.os.Build;
  import android.preference.PreferenceManager;
  
  import java.util.Locale;
  
  public class LocaleHelper{
      private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
      public static Context onAttach(Context context) {
          String lang = getPersistedData(context, Locale.getDefault().getLanguage());
          return setLocale(context, lang);
      }
  
      public static Context onAttach(Context context, String defaultLanguage) {
          String lang = getPersistedData(context, defaultLanguage);
          return setLocale(context, lang);
      }
  
      public static String getLanguage(Context context) {
          return getPersistedData(context, Locale.getDefault().getLanguage());
      }
      public static Context setLocale(Context context, String language) {
          persist(context, language);
  
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
              return updateResources(context, language);
          }
  
          return updateResourcesLegacy(context, language);
      }
  
      private static String getPersistedData(Context context, String defaultLanguage) {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
          return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
      }
  
      private static void persist(Context context, String language) {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
          SharedPreferences.Editor editor = preferences.edit();
  
          editor.putString(SELECTED_LANGUAGE, language);
          editor.apply();
      }
  
      @TargetApi(Build.VERSION_CODES.N)
      private static Context updateResources(Context context, String language) {
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
  
          Configuration configuration = context.getResources().getConfiguration();
          configuration.setLocale(locale);
          configuration.setLayoutDirection(locale);
  
          return context.createConfigurationContext(configuration);
      }
  
      private static Context updateResourcesLegacy(Context context, String language) {
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
  
          Resources resources = context.getResources();
  
          Configuration configuration = resources.getConfiguration();
          configuration.locale = locale;
          configuration.setLayoutDirection(locale);
  
          resources.updateConfiguration(configuration, resources.getDisplayMetrics());
  
          return context;
      }
  }
  