/*
 * Copyright (c) 2015 Akexorcist
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.akexorcist.localizationactivity;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;

import java.util.Locale;

/**
 * Created by Akexorcist on 7/20/15 AD.
 */
public class LocalizationActivity extends AppCompatActivity implements OnLocaleChangedListener {
    // Bundle key
    private static final String KEY_ACTIVIY_LOCALE_CHANGED = "activity_locale_changed";

    // Boolean flag to check that activity was recreated from locale changed.
    private boolean isLocalizationChanged = false;

    // Prepare default language.
    private String currentLanguage = LanguageSetting.getDefaultLanguage();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setupLanguage();
        checkBeforeLocaleChanging();
        super.onCreate(savedInstanceState);
    }

    // Provide method to set application language by country name.
    public final void setLanguage(String language) {
        if (!isDuplicatedLanguageSetting(language)) {
            LanguageSetting.setLanguage(this, language);
            notifyLanguageChanged();
        }
    }

    public final void setDefaultLanguage(String language) {
        LanguageSetting.setDefaultLanguage(language);
    }

    // Get current language
    public final String getLanguage() {
        return LanguageSetting.getLangauge(this);
    }

    // Check that bundle come from locale change.
    // If yes, bundle will obe remove and set boolean flag to "true".
    private void checkBeforeLocaleChanging() {
        boolean isLocalizationChanged = getIntent().getBooleanExtra(KEY_ACTIVIY_LOCALE_CHANGED, false);
        if(isLocalizationChanged) {
            this.isLocalizationChanged = true;
            getIntent().removeExtra(KEY_ACTIVIY_LOCALE_CHANGED);
        }
    }

    // Setup language to locale and language preference.
    // This method will called before onCreate.
    private void setupLanguage() {
        Locale locale = LanguageSetting.getLocale(this);
        setupLocale(locale);
        currentLanguage = locale.getLanguage();
        LanguageSetting.setLanguage(this, locale.getLanguage());
    }

    // Set locale configuration.
    private void setupLocale(Locale locale) {
        updateLocaleConfiguration(this, locale);
    }

    private void updateLocaleConfiguration(Context context, Locale locale) {
        Configuration config = new Configuration();
        config.locale = locale;
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        context.getResources().updateConfiguration(config, dm);
    }

    // Avoid duplicated setup
    private boolean isDuplicatedLanguageSetting(String language) {
        return language.toLowerCase(Locale.getDefault()).equals(LanguageSetting.getLangauge(this));
    }

    // Let's take it change! (Using recreate method that available on API 11 or more.
    private void notifyLanguageChanged() {
        onBeforeLocaleChanged();
        getIntent().putExtra(KEY_ACTIVIY_LOCALE_CHANGED, true);
        recreate();
    }

    // If activity is run to backstack. So we have to check if this activity is resume working.
    @Override
    protected void onResume() {
        super.onResume();
        checkLocaleChange();
        checkAfterLocaleChanging();
    }

    // Check if locale has change while this activity was run to backstack.
    private void checkLocaleChange() {
        if(!LanguageSetting.getLangauge(this).toLowerCase(Locale.getDefault())
                .equals(currentLanguage.toLowerCase(Locale.getDefault()))) {
            recreate();
        }
    }

    // Call override method if local is really changed
    private void checkAfterLocaleChanging() {
        if(isLocalizationChanged) {
            onAfterLocaleChanged();
            isLocalizationChanged = false;
        }
    }

    // Just override method locale change event
    @Override
    public void onBeforeLocaleChanged() { }

    @Override
    public void onAfterLocaleChanged() { }
}

@@ -2332,234 +2332,646 @@
     
-public final void setDefaultLanguage(String language) %7B%0A        LanguageSetting.setDefaultLanguage(language);%0A    %7D%0A%0A    // Get current language%0A    public final String getLanguage() %7B%0A        return LanguageSetting.getLangaug
+// Provide method to set application language by locale.%0A    public final void setLanguage(Locale locale) %7B%0A        setLanguage(locale.getLanguage());%0A    %7D%0A%0A    public final void setDefaultLanguage(String language) %7B%0A        LanguageSetting.setDefaultLanguage(language);%0A    %7D%0A%0A    public final void setDefaultLanguage(Locale locale) %7B%0A        LanguageSetting.setDefaultLanguage(locale.getLanguage());%0A    %7D%0A%0A    // Get current language%0A    public final String getLanguage() %7B%0A        return LanguageSetting.getLangauge(this);%0A    %7D%0A%0A    // Get current locale%0A    public final Locale getLocale() %7B%0A        return LanguageSetting.getLocal
 e(th

Add set language with Locale