/**
 * OpenAtlasForAndroid Project
 * <p>
 * The MIT License (MIT)
 * Copyright (c) 2015 Bunny Blue
 * <p>
 * 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:
 * <p>
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 * <p>
 * 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.
 *
 * @author BunnyBlue
 */
package com.openatlas.android.initializer;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Build;
import android.os.Build.VERSION;
import android.util.Log;

import com.openatlas.android.task.Coordinator;
import com.openatlas.android.task.Coordinator.TaggedRunnable;
import com.openatlas.boot.Globals;
import com.openatlas.bundleInfo.BundleInfoList;
import com.openatlas.framework.Atlas;
import com.openatlas.framework.PlatformConfigure;
import com.openatlas.log.Logger;
import com.openatlas.log.LoggerFactory;
import com.openatlas.util.ApkUtils;

import java.util.Properties;

public class OpenAtlasInitializer {
	Logger log=LoggerFactory.getInstance("OpenAtlasInitializer");
    private static long initStartTime = 0;
    private static boolean inTargetApp;
    private Application mApplication;
    private String mPackageName;
    private BundleDebug mDebug;
    private boolean tryInstall;

    private Properties mProperties = new Properties();
    private boolean isUpdate = false;


    public OpenAtlasInitializer(Application application, String packagename, boolean isUpdate) {
        this.mApplication = application;
        this.mPackageName = packagename;

        this.isUpdate = isUpdate;
        if (application.getPackageName().equals(packagename)) {
            inTargetApp = true;
        }
    }

    public void init() {
    
        initStartTime = System.currentTimeMillis();
        setupMonitor();
        setupLog();
        try {
            Atlas.getInstance().init(this.mApplication);
            log.debug("OpenAtlas framework inited end " + this.mPackageName + " " + (System.currentTimeMillis() - initStartTime) + " ms");
        } catch (Throwable e) {
            Log.e("OpenAtlasInitializer", "Could not init atlas framework !!!", e);
            throw new RuntimeException("atlas initialization fail" + e.getMessage());
        }
    }

    public void startUp() {

        this.mProperties.put(PlatformConfigure.BOOT_ACTIVITY, PlatformConfigure.BOOT_ACTIVITY);
        this.mProperties.put(PlatformConfigure.COM_OPENATLAS_DEBUG_BUNDLES, "true");
        this.mProperties.put(PlatformConfigure.ATLAS_APP_DIRECTORY, this.mApplication.getFilesDir().getParent());

        try {

            Globals.init(this.mApplication, Atlas.getInstance().getDelegateClassLoader());
            this.mDebug = new BundleDebug();
            if (this.mApplication.getPackageName().equals(this.mPackageName)) {
                if (!( verifyRuntime() || !ApkUtils.isRootSystem())) {
                    this.mProperties.put(PlatformConfigure.OPENATLAS_PUBLIC_KEY, SecurityBundleListner.PUBLIC_KEY);
                    Atlas.getInstance().addBundleListener(new SecurityBundleListner());
                }
                if (this.isUpdate || this.mDebug.isDebugable()) {
                    this.mProperties.put("osgi.init", "true");
                }
            }
           BundlesInstaller mBundlesInstaller = BundlesInstaller.getInstance();
            OptDexProcess mOptDexProcess = OptDexProcess.getInstance();
            if (this.mApplication.getPackageName().equals(this.mPackageName) && (this.isUpdate || this.mDebug.isDebugable())) {
            	mBundlesInstaller.init(this.mApplication,  this.mDebug, inTargetApp);
                mOptDexProcess.init(this.mApplication);
            }
            log.debug("OpenAtlas framework prepare starting in process " + this.mPackageName + " " + (System.currentTimeMillis() - initStartTime) + " ms");
            Atlas.getInstance().setClassNotFoundInterceptorCallback(new ClassNotFoundInterceptor());
            if (InstallSolutionConfig.install_when_findclass && BundleInfoList.getInstance().getBundles()==null) {
            	InstallSolutionConfig.install_when_oncreate = true;
                this.tryInstall = true;
            }

            try {
                Atlas.getInstance().startup(this.mProperties);
                installBundles(mBundlesInstaller, mOptDexProcess);
                log.debug("OpenAtlas framework end startUp in process " + this.mPackageName + " " + (System.currentTimeMillis() - initStartTime) + " ms");
            } catch (Throwable e) {
                Log.e("OpenAtlasInitializer", "Could not start up atlas framework !!!", e);
                throw new RuntimeException(e);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Could not set Globals !!!", e);
        }
    }

    private void installBundles(final BundlesInstaller mBundlesInstaller, final OptDexProcess mOptDexProcess) {

        if (this.mDebug.isDebugable()) {
        	InstallSolutionConfig.install_when_oncreate = true;
        }
        if (this.mApplication.getPackageName().equals(this.mPackageName)) {
            if (InstallSolutionConfig.install_when_oncreate) {

            }
            AutoStartBundlesLaunch autoStartBundlesLaunch = new AutoStartBundlesLaunch();
            if (this.isUpdate || this.mDebug.isDebugable()) {
                if (InstallSolutionConfig.install_when_oncreate) {
                    Coordinator.postTask(new  TaggedRunnable("AtlasStartup") {
						@Override
						public void run() {
							mBundlesInstaller.process(true, false);
							mOptDexProcess.processPackages(true, false);
							
						}
					});

                    return;
                }
                Utils.UpdatePackageVersion(this.mApplication);
                Utils.saveAtlasInfoBySharedPreferences(this.mApplication);

                autoStartBundlesLaunch.startAutoBundles();
            } else if (!this.isUpdate) {
                if (this.tryInstall) {
                    Coordinator.postTask(new TaggedRunnable("AtlasStartup") {
						@Override
						public void run() {
							mBundlesInstaller.process(true, false);
							mOptDexProcess.processPackages(true, false);
						}
					});
                    return;
                }
                autoStartBundlesLaunch.startAutoBundles();
            }
        }
    }



    @SuppressLint({"DefaultLocale"})
    private boolean verifyRuntime() {
    
        if ((Build.BRAND == null || !Build.BRAND.toLowerCase().contains("xiaomi") || Build.HARDWARE == null || !Build.HARDWARE.toLowerCase().contains("mt65")) && VERSION.SDK_INT >= 14) {
            return false;
        }
        return true;
    }

    private void setupMonitor() {
    }

    private void setupLog() {

        Atlas.getInstance().setLogger(new ExternalLog());
    }
}

@@ -6406,35 +6406,36 @@
 staller.process(
-tru
+fals
 e, false);%0A%09%09%09%09%09
@@ -6459,35 +6459,36 @@
 processPackages(
-tru
+fals
 e, false);%0A%09%09%09%09%09
@@ -6985,19 +6985,20 @@
 process(
-tru
+fals
 e, false
@@ -7038,19 +7038,20 @@
 ackages(
-tru
+fals
 e, false

fix code