package me.yokeyword.eventbusactivityscope;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Activity-scope EventBus.
 * <p>
 * Created by YoKey on 17/10/17.
 */
public class EventBusActivityScope {
    private static final String TAG = EventBusActivityScope.class.getSimpleName();

    private static AtomicBoolean sInitialized = new AtomicBoolean(false);
    private static volatile EventBus sInvalidEventBus;

    private static final Map<Activity, EventBus> sActivityEventBusScopePool = new HashMap<>();

    static void init(Context context) {
        if (sInitialized.getAndSet(true)) {
            return;
        }

        ((Application) context.getApplicationContext())
                .registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                    private Handler mainHandler = new Handler(Looper.getMainLooper());

                    @Override
                    public void onActivityCreated(Activity activity, Bundle bundle) {
                        synchronized (sActivityEventBusScopePool) {
                            sActivityEventBusScopePool.put(activity, null);
                        }
                    }

                    @Override
                    public void onActivityStarted(Activity activity) {
                    }

                    @Override
                    public void onActivityResumed(Activity activity) {
                    }

                    @Override
                    public void onActivityPaused(Activity activity) {
                    }

                    @Override
                    public void onActivityStopped(Activity activity) {
                    }

                    @Override
                    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
                    }

                    @Override
                    public void onActivityDestroyed(final Activity activity) {
                        if (!sActivityEventBusScopePool.containsKey(activity)) return;

                        mainHandler.post(new Runnable() { // Make sure Fragment's onDestroy() has been called.
                            @Override
                            public void run() {
                                synchronized (sActivityEventBusScopePool) {
                                    sActivityEventBusScopePool.remove(activity);
                                }
                            }
                        });
                    }
                });
    }

    /**
     * Get the activity-scope EventBus instance
     */
    public static EventBus getDefault(Activity activity) {
        if (activity == null) {
            Log.e(TAG, "Can't find the Activity, the Activity is null!");
            return invalidEventBus();
        }

        synchronized (sActivityEventBusScopePool) {
            if (!sActivityEventBusScopePool.containsKey(activity)) {
                Log.e(TAG, "Can't find the Activity, it has been removed!");
                return invalidEventBus();
            }

            EventBus eventBus = sActivityEventBusScopePool.get(activity);
            if (eventBus == null) {
                eventBus = new EventBus();
                sActivityEventBusScopePool.put(activity, eventBus);
            }
            return eventBus;
        }
    }

    private static EventBus invalidEventBus() {
        if (sInvalidEventBus == null) {
            synchronized (EventBusActivityScope.class) {
                if (sInvalidEventBus == null) {
                    sInvalidEventBus = new EventBus();

                }
            }
        }
        return sInvalidEventBus;
    }
}

@@ -295,20 +295,16 @@
 va.util.
-Hash
 Map;%0Aimp
@@ -309,32 +309,57 @@
 mport java.util.
+concurrent.ConcurrentHash
 Map;%0Aimport java
@@ -764,24 +764,28 @@
 tivity, 
+Lazy
 EventBus
 %3E sActiv
@@ -776,16 +776,24 @@
 EventBus
+Instance
 %3E sActiv
@@ -819,16 +819,26 @@
 l = new 
+Concurrent
 HashMap%3C
@@ -1337,38 +1337,24 @@
             
-synchronized (
 sActivityEve
@@ -1371,113 +1371,51 @@
 Pool
-) %7B%0A                            sActivityEventBusScopePool.put(activity, null);%0A                        %7D
+.put(activity, new LazyEventBusInstance());
 %0A   
@@ -2476,88 +2476,8 @@
 ) %7B%0A
-                                synchronized (sActivityEventBusScopePool) %7B%0A    
     
@@ -2553,42 +2553,8 @@
 y);%0A
-                                %7D%0A
     
@@ -2946,125 +2946,139 @@
     
-synchronized (sActivityEventBusScopePool) %7B%0A            if (!sActivityEventBusScopePool.containsKey(activity)) %7B%0A    
+LazyEventBusInstance lazyEventBusInstance = sActivityEventBusScopePool.get(activity);%0A%0A        if (lazyEventBusInstance == null) %7B%0A
     
@@ -3150,36 +3150,32 @@
 %22);%0A            
-    
 return invalidEv
@@ -3196,89 +3196,218 @@
     
+%7D%0A%0A
     
-%7D%0A%0A
     
-        EventBus eventBus = sActivityEventBusScopePool.get(activity);%0A
+return lazyEventBusInstance.getInstance();%0A    %7D%0A%0A    private static EventBus invalidEventBus() %7B%0A        if (sInvalidEventBus == null) %7B%0A            synchronized (EventBusActivityScope.class) %7B%0A    
     
@@ -3418,17 +3418,25 @@
     if (
-e
+sInvalidE
 ventBus 
@@ -3462,17 +3462,29 @@
         
-e
+    sInvalidE
 ventBus 
@@ -3493,32 +3493,33 @@
 new EventBus();%0A
+%0A
                 
@@ -3522,59 +3522,9 @@
     
-sActivityEventBusScopePool.put(activity, eventBus);
+%7D
 %0A   
@@ -3530,32 +3530,38 @@
           %7D%0A    
+    %7D%0A
         return e
@@ -3559,17 +3559,25 @@
  return 
-e
+sInvalidE
 ventBus;
@@ -3585,21 +3585,55 @@
     
+%7D%0A%0A
     
-%7D
+static class LazyEventBusInstance %7B
 %0A    
-%7D%0A%0A
     
@@ -3632,38 +3632,40 @@
         private 
-static
+volatile
  EventBus invali
@@ -3662,49 +3662,69 @@
 Bus 
-invalidEventBus() %7B%0A        if (sInvalidE
+eventBus;%0A%0A        EventBus getInstance() %7B%0A            if (e
 vent
@@ -3742,32 +3742,36 @@
 ) %7B%0A            
+    
 synchronized (Ev
@@ -3772,34 +3772,11 @@
 ed (
-EventBusActivityScope.clas
+thi
 s) %7B
@@ -3788,37 +3788,33 @@
             
-if (sInvalidE
+    if (e
 ventBus == n
@@ -3836,33 +3836,29 @@
             
-sInvalidE
+    e
 ventBus = ne
@@ -3863,32 +3863,53 @@
 new EventBus();%0A
+                    %7D
 %0A               
@@ -3921,38 +3921,32 @@
           %7D%0A    
-    %7D%0A
         return s
@@ -3944,33 +3944,35 @@
  return 
-sInvalidEventBus;
+eventBus;%0A        %7D
 %0A    %7D%0A%7D

Optimize concurrency, remove synchronized