package com.markupartist.sthlmtraveling;

import java.util.Locale;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;

import com.markupartist.sthlmtraveling.service.DataMigrationService;
import com.markupartist.sthlmtraveling.utils.ErrorReporter;

public class MyApplication extends Application {
    public static String ANALYTICS_KEY = "JUSQKB45DEN62VRQVBU9";
    static String TAG = "StApplication";

    @Override
    public void onCreate() {
        super.onCreate();

        final ErrorReporter reporter = ErrorReporter.getInstance();
        reporter.init(getApplicationContext());

        reloadLocaleForApplication();

        DataMigrationService.startService(this);
    }

    /* (non-Javadoc)
     * @see android.app.Application#onConfigurationChanged(android.content.res.Configuration)
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        reloadLocaleForApplication();
    }

    public void reloadLocaleForApplication() {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());

        String language = sharedPreferences
                .getString("prefered_language_preference", "system");
        Log.d(TAG, "Preferred language: " + language);

        Locale locale = null;
        if ("sv".equals(language)) {
            locale = new Locale("sv", "SE");
        } else if ("es".equals(language)) {
            locale = new Locale("es", "ES");
        } else if ("en".equals(language)) {
            locale = Locale.ENGLISH;
        }

        if (locale != null) {
            Log.d(TAG, "setting locale " + locale);
            Resources res = getResources(); 
            DisplayMetrics dm = res.getDisplayMetrics(); 
            Configuration conf = res.getConfiguration();
            conf.locale = locale;
            res.updateConfiguration(conf, dm);
        }
    }
    
}

@@ -120,56 +120,170 @@
 ent.
-Intent;%0Aimport android.content.SharedPreferences
+SharedPreferences;%0Aimport android.content.pm.PackageInfo;%0Aimport android.content.pm.PackageManager;%0Aimport android.content.pm.PackageManager.NameNotFoundException
 ;%0Aim
@@ -711,16 +711,54 @@
 QVBU9%22;%0A
+    public static String APP_VERSION;%0A
     stat
@@ -1065,23 +1065,489 @@
 e(this);
+%0A%0A        setAppVersion();%0A    %7D%0A%0A    protected void setAppVersion() %7B%0A        if (APP_VERSION == null) %7B%0A            PackageManager pm = getPackageManager();%0A            try %7B%0A                PackageInfo pi = pm.getPackageInfo(getPackageName(),%0A                        0);%0A                APP_VERSION = pi.versionName;%0A            %7D catch (NameNotFoundException e) %7B%0A                Log.e(TAG, %22Could not get the package info.%22);%0A            %7D%0A        %7D        %0A    %7D
 %0A    
-%7D%0A
 %0A    /* 

Add helper to retrieve app version.