/*
 * Copyright (c) 2016 大前良介 (OHMAE Ryosuke)
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/MIT
 */

package net.mm2d.dmsexplorer;

import android.app.Application;

import net.mm2d.dmsexplorer.domain.AppRepository;
import net.mm2d.dmsexplorer.settings.Settings;
import net.mm2d.dmsexplorer.util.update.UpdateChecker;
import net.mm2d.util.Log;

import io.reactivex.exceptions.UndeliverableException;
import io.reactivex.plugins.RxJavaPlugins;

/**
 * Log出力変更のための継承。
 *
 * @author <a href="mailto:ryo@mm2d.net">大前良介(OHMAE Ryosuke)</a>
 */
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        setUpDebugLog();
        RxJavaPlugins.setErrorHandler(e -> {
            if (e instanceof UndeliverableException) {
                Log.w(e.getCause());
                return;
            }
            Log.w(e);
        });
        Settings.initialize(this);
        EventLogger.initialize(this);
        Repository.set(new AppRepository(this));
        new UpdateChecker(this).check();
    }

    private void setUpDebugLog() {
        if (BuildConfig.DEBUG) {
            Log.setAppendCaller(true);
            Log.setLogLevel(Log.VERBOSE);
            Log.setPrint((level, tag, message) -> {
                final String[] lines = message.split("\n");
                for (final String line : lines) {
                    android.util.Log.println(level, tag, line);
                }
            });
            return;
        }
        Log.setLogLevel(Log.ASSERT);
        Log.setPrint((level, tag, message) -> {
        });
    }
}

@@ -199,16 +199,112 @@
 ication;
+%0Aimport android.os.Handler;%0Aimport android.os.Looper;%0Aimport android.support.annotation.NonNull;
 %0A%0Aimport
@@ -1228,16 +1228,17 @@
     if (
+!
 BuildCon
@@ -1269,16 +1269,299 @@
  Log.set
+LogLevel(Log.ASSERT);%0A            Log.setPrint(Log.EMPTY_PRINT);%0A            return;%0A        %7D%0A        final Looper mainLooper = Looper.getMainLooper();%0A        final Thread mainThread = mainLooper.getThread();%0A        final Handler handler = new Handler(mainLooper);%0A        Log.set
 AppendCa
@@ -1568,28 +1568,24 @@
 ller(true);%0A
-    
         Log.
@@ -1610,20 +1610,16 @@
 RBOSE);%0A
-    
         
@@ -1674,143 +1674,167 @@
     
-    final String%5B%5D lines = message.split(%22%5Cn%22);%0A                for (final String line : lines) %7B%0A                    android.util.Log.
+if (Thread.currentThread() == mainThread) %7B%0A                println(level, tag, message);%0A                return;%0A            %7D%0A            handler.post(() -%3E 
 prin
@@ -1853,12 +1853,16 @@
 ag, 
-line
+message)
 );%0A 
@@ -1864,33 +1864,60 @@
 ));%0A        
+%7D);%0A
     
+%7D%0A%0A
     
-%7D
+private void println(
 %0A           
@@ -1909,35 +1909,42 @@
 ln(%0A            
-%7D);
+int level,
 %0A            ret
@@ -1944,84 +1944,198 @@
     
-return;%0A        %7D%0A        Log.setLogLevel(Log.ASSERT);%0A        Log.setP
+@NonNull String tag,%0A            @NonNull String message) %7B%0A        final String%5B%5D lines = message.split(%22%5Cn%22);%0A        for (final String line : lines) %7B%0A            android.util.Log.p
 rint
-(
+ln
 (lev
@@ -2139,37 +2139,30 @@
 level, tag, 
-message) -%3E %7B
+line);
 %0A        %7D);
@@ -2155,23 +2155,21 @@
 );%0A        %7D
-);
 %0A    %7D%0A%7D%0A

modify logging system