package com.markupartist.sthlmtraveling;

import android.app.Application;
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;

import java.util.Locale;

public class MyApplication extends Application {
    public static String ANALYTICS_KEY = "JUSQKB45DEN62VRQVBU9";
    public static String APP_VERSION = BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")";
    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);
        }
    }
    
}

@@ -292,24 +292,68 @@
 .util.Log;%0A%0A
+import com.crashlytics.android.Crashlytics;%0A
 import com.m
@@ -409,16 +409,16 @@
 ervice;%0A
-
 import c
@@ -545,16 +545,57 @@
 ation %7B%0A
+    static String TAG = %22StApplication%22;%0A
     publ
@@ -759,49 +759,8 @@
 %22)%22;
-%0A    static String TAG = %22StApplication%22;
 %0A%0A  
@@ -823,24 +823,58 @@
 nCreate();%0A%0A
+        Crashlytics.start(this);%0A%0A
         fina
@@ -1013,24 +1013,24 @@
 ication();%0A%0A
+
         Data
@@ -1062,25 +1062,24 @@
 vice(this);%0A
-%0A
     %7D%0A%0A    /
@@ -2355,15 +2355,11 @@
 %7D%0A    %7D%0A
-    
 %0A%7D%0A

Add crashlytics init