package com.mrzhang.component.componentlib.router;

import com.mrzhang.component.componentlib.applicationlike.IApplicationLike;

import java.util.HashMap;

/**
 * Created by mrzhang on 2017/6/15.
 */

public class Router {

    private HashMap<String, Object> services = new HashMap<>();

    private static volatile Router instance;

    private Router() {
    }

    public static Router getInstance() {
        if (instance == null) {
            synchronized (Router.class) {
                if (instance == null) {
                    instance = new Router();
                }
            }
        }
        return instance;
    }

    public synchronized void addService(String serviceName, Object serviceImpl) {
        if (serviceName == null || serviceImpl == null) {
            return;
        }
        services.put(serviceName, serviceImpl);
    }

    public synchronized Object getService(String serviceName) {
        if (serviceName == null) {
            return null;
        }
        return services.get(serviceName);
    }

    public synchronized void removeService(String serviceName) {
        if (serviceName == null) {
            return;
        }
        services.remove(serviceName);
    }

    public static void registerComponent(String classname) {
        try {
            Class clazz = Class.forName(classname);
            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
            applicationLike.onCreate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void unregisterComponent(String classname) {
        try {
            Class clazz = Class.forName(classname);
            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();
            applicationLike.onStop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

@@ -45,16 +45,92 @@
 outer;%0A%0A
+import android.support.annotation.Nullable;%0Aimport android.text.TextUtils;%0A%0A
 import c
@@ -197,16 +197,16 @@
 onLike;%0A
-
 %0Aimport 
@@ -356,16 +356,114 @@
 Map%3C%3E();
+%0A    //%E6%B3%A8%E5%86%8C%E7%9A%84%E7%BB%84%E4%BB%B6%E7%9A%84%E9%9B%86%E5%90%88%0A    private static HashMap%3CString, IApplicationLike%3E components = new HashMap%3C%3E();
 %0A%0A    pr
@@ -1381,32 +1381,34 @@
 ceName);%0A    %7D%0A%0A
+//
     public stati
@@ -1444,32 +1444,973 @@
 ng classname) %7B%0A
+//        try %7B%0A//            Class clazz = Class.forName(classname);%0A//            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();%0A//            applicationLike.onCreate();%0A//        %7D catch (Exception e) %7B%0A//            e.printStackTrace();%0A//        %7D%0A//    %7D%0A//%0A//    public static void unregisterComponent(String classname) %7B%0A//        try %7B%0A//            Class clazz = Class.forName(classname);%0A//            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();%0A//            applicationLike.onStop();%0A//        %7D catch (Exception e) %7B%0A//            e.printStackTrace();%0A//        %7D%0A//    %7D%0A%0A%0A    /**%0A     * %E6%B3%A8%E5%86%8C%E7%BB%84%E4%BB%B6%0A     *%0A     * @param classname %E7%BB%84%E4%BB%B6%E5%90%8D%0A     */%0A    public static void registerComponent(@Nullable String classname) %7B%0A        if (TextUtils.isEmpty(classname)) %7B%0A            return;%0A        %7D%0A        if (components.keySet().contains(classname)) %7B%0A            return;%0A        %7D%0A
         try %7B%0A  
@@ -2582,24 +2582,80 @@
 onCreate();%0A
+            components.put(classname, applicationLike);%0A
         %7D ca
@@ -2716,32 +2716,96 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * %E5%8F%8D%E6%B3%A8%E5%86%8C%E7%BB%84%E4%BB%B6%0A     *%0A     * @param classname %E7%BB%84%E4%BB%B6%E5%90%8D%0A     */%0A
     public stati
@@ -2823,32 +2823,42 @@
 gisterComponent(
+@Nullable 
 String classname
@@ -2873,254 +2873,237 @@
     
-try %7B%0A            Class clazz = Class.forName(classname);%0A            IApplicationLike applicationLike = (IApplicationLike) clazz.newInstance();%0A            applicationLike.onStop();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace()
+if (TextUtils.isEmpty(classname)) %7B%0A            return;%0A        %7D%0A        if (components.keySet().contains(classname)) %7B%0A            components.get(classname).onStop();%0A            components.remove(classname);%0A            return
 ;%0A  

fix bug in unregisterComponent