/*
 * Copyright (C) 2016 team-cachebox.de
 *
 * Licensed under the : GNU General Public License (GPL);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * 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 de.longri.cachebox3;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.SharedLibraryLoader;
import de.longri.cachebox3.locator.Locator;
import org.oscim.android.gl.AndroidGL;
import org.oscim.backend.GLAdapter;
import org.oscim.gdx.GdxAssets;
import org.slf4j.impl.LibgdxLogger;
import org.sqldroid.SQLDroidDriver;

import java.io.File;

public class AndroidLauncher extends AndroidApplication {

    static {
        try {
            java.sql.DriverManager.registerDriver(new SQLDroidDriver());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    // Compass
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private float[] mCompassValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        File filesDir = getExternalFilesDir(null);
        FileHandle loggerConfig = new LibgdxLoggerAndroidFileHandle(filesDir, Files.FileType.Absolute).child(LibgdxLogger.CONFIGURATION_FILE_XML);
        LibgdxLogger.initial(loggerConfig);


        //create private path for Log
        // => /data/user/0/de.longri.cachebox3/files
        FileHandle fileHandle = LibgdxLogger.PROPERTIES_FILE_HANDLE.child("test.tmp");
        fileHandle.parent().mkdirs();


        // Don't change this LogLevel
        // Cachebox use the slf4j implematation for LibGdx as Log engine.
        // so set LogLevel on CB.class if you wont (USED_LOG_LEVEL)
        this.setLogLevel(LOG_DEBUG);

        //initialize platform bitmap factory
        org.oscim.android.canvas.AndroidGraphics.init();

        //initialize platform connector
        PlatformConnector.init(new AndroidPlatformConnector(this));

//        DisplayMetrics metrics = getResources().getDisplayMetrics();
//        CanvasAdapter.dpi = (int) Math.max(metrics.xdpi, metrics.ydpi);

        GdxAssets.init("");
        GLAdapter.init(new AndroidGL());

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        config.stencil = 8;
        config.numSamples = 2;
        new SharedLibraryLoader().load("vtm-jni");
        initialize(new CacheboxMain(), config);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

        setApplicationLogger(new Android_ApplicationLogger());


    }

    protected void onStart() {
        super.onStart();

        if (android.os.Build.VERSION.SDK_INT >= 23) {
            AndroidPermissionCheck.checkNeededPermissions(this);
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub

            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (mSensorManager != null)
            mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mSensorManager != null)
            mSensorManager.unregisterListener(mListener);

    }

    private float compassHeading = -1;

    private final SensorEventListener mListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            try {
                mCompassValues = event.values;
                compassHeading = mCompassValues[0];
                Locator.setHeading(compassHeading, Locator.CompassType.Magnetic);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

}

@@ -1263,16 +1263,73 @@
 Assets;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -1477,35 +1477,170 @@
 n %7B%0A
-%0A    static %7B%0A        try %7B
+    private final static Logger log = LoggerFactory.getLogger(AndroidLauncher.class);%0A%0A    static %7B%0A        try %7B%0A            log.debug(%22initial SQLDroidDriver%22);
 %0A   
@@ -1753,34 +1753,58 @@
         
-e.printStackTrace(
+log.error(%22With initial SQLDroidDriver%22, e
 );%0A     
@@ -2006,24 +2006,56 @@
 nceState) %7B%0A
+        log.debug(%22onStart()%22);%0A
         supe
@@ -2091,469 +2091,8 @@
 );%0A%0A
-        File filesDir = getExternalFilesDir(null);%0A        FileHandle loggerConfig = new LibgdxLoggerAndroidFileHandle(filesDir, Files.FileType.Absolute).child(LibgdxLogger.CONFIGURATION_FILE_XML);%0A        LibgdxLogger.initial(loggerConfig);%0A%0A%0A        //create private path for Log%0A        // =%3E /data/user/0/de.longri.cachebox3/files%0A        FileHandle fileHandle = LibgdxLogger.PROPERTIES_FILE_HANDLE.child(%22test.tmp%22);%0A        fileHandle.parent().mkdirs();%0A%0A
 %0A   
@@ -2522,154 +2522,8 @@
 );%0A%0A
-//        DisplayMetrics metrics = getResources().getDisplayMetrics();%0A//        CanvasAdapter.dpi = (int) Math.max(metrics.xdpi, metrics.ydpi);%0A%0A
     
@@ -3055,24 +3055,495 @@
 Logger());%0A%0A
+        //    ## INITIAL LOGGER ##                ################################%0A        File filesDir = getExternalFilesDir(null);%0A        FileHandle loggerConfig = Gdx.files.absolute(filesDir.getAbsolutePath())%0A                .child(LibgdxLogger.CONFIGURATION_FILE_XML);%0A        LibgdxLogger.initial(loggerConfig);%0A%0A        //create private path for Log and properties file%0A        // =%3E /data/user/0/de.longri.cachebox3/files%0A        loggerConfig.parent().mkdirs();
 %0A    %7D%0A%0A    
@@ -3565,24 +3565,56 @@
 onStart() %7B%0A
+        log.debug(%22onStart()%22);%0A
         supe
@@ -3978,24 +3978,57 @@
 nResume() %7B%0A
+        log.debug(%22onResume()%22);%0A
         supe
@@ -4220,24 +4220,55 @@
  onStop() %7B%0A
+        log.debug(%22onStop()%22);%0A
         supe

change initialization for logger