package me.yokeyword.eventbusactivityscope;

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

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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 ArrayList<Activity> sActivityList = new ArrayList<>();
    private static final Map<Activity, EventBus> sActivityEventBusScopePool = new ConcurrentHashMap<>();

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

        ((Application) context.getApplicationContext())
                .registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                    @Override
                    public void onActivityCreated(Activity activity, Bundle bundle) {
                        synchronized (sActivityList) {
                            sActivityList.add(activity);
                        }
                    }

                    @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(Activity activity) {
                        synchronized (sActivityList) {
                            sActivityList.remove(activity);
                        }

                        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 (sActivityList) {
            if (!sActivityList.contains(activity)) {
                Log.e(TAG, "Can't find the Activity, it has been removed!");
                return invalidEventBus();
            }
        }

        EventBus eventBus = sActivityEventBusScopePool.get(activity);

        if (eventBus == null) {
            synchronized (sActivityEventBusScopePool) {
                if (sActivityEventBusScopePool.get(activity) == 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;
    }
}

@@ -157,16 +157,69 @@
 Bundle;%0A
+import android.os.Handler;%0Aimport android.os.Looper;%0A
 import a
@@ -299,83 +299,34 @@
 til.
-ArrayList;%0Aimport java.util.Map;%0Aimport java.util.concurrent.ConcurrentHash
+HashMap;%0Aimport java.util.
 Map;
@@ -708,88 +708,8 @@
 s;%0A%0A
-    private static final ArrayList%3CActivity%3E sActivityList = new ArrayList%3C%3E();%0A
     
@@ -790,18 +790,8 @@
 new 
-Concurrent
 Hash
@@ -1066,24 +1066,112 @@
 llbacks() %7B%0A
+                    private Handler mainHandler = new Handler(Looper.getMainLooper());%0A%0A
             
@@ -1313,36 +1313,49 @@
 nized (sActivity
-List
+EventBusScopePool
 ) %7B%0A            
@@ -1383,16 +1383,29 @@
 vity
-List.add
+EventBusScopePool.put
 (act
@@ -1401,32 +1401,38 @@
 ool.put(activity
+, null
 );%0A             
@@ -2193,16 +2193,22 @@
 stroyed(
+final 
 Activity
@@ -2248,126 +2248,277 @@
     
-synchronized (sActivityList) %7B%0A                            sActivityList.remove(activity);%0A                        %7D%0A%0A
+if (!sActivityEventBusScopePool.containsKey(activity)) return;%0A%0A                        mainHandler.post(new Runnable() %7B // Make sure Fragment's onDestroy() has been called.%0A                            @Override%0A                            public void run() %7B%0A        
     
@@ -2601,32 +2601,40 @@
                 
+        
 sActivityEventBu
@@ -2678,33 +2678,99 @@
                 
-%7D
+        %7D%0A                            %7D%0A                        %7D);
 %0A               
@@ -3112,20 +3112,33 @@
 Activity
-List
+EventBusScopePool
 ) %7B%0A    
@@ -3163,12 +3163,25 @@
 vity
-List
+EventBusScopePool
 .con
@@ -3185,16 +3185,19 @@
 contains
+Key
 (activit
@@ -3331,35 +3331,29 @@
           %7D%0A
-    
+%0A
     
-%7D%0A%0A
         Even
@@ -3414,157 +3414,32 @@
 y);%0A
-%0A        if (eventBus == null) %7B%0A            synchronized (sActivityEventBusScopePool) %7B%0A                if (sActivityEventBusScopePool.get(activity)
+            if (eventBus
  == 
@@ -3438,36 +3438,32 @@
 tBus == null) %7B%0A
-    
                 
@@ -3481,36 +3481,32 @@
 new EventBus();%0A
-    
                 
@@ -3573,39 +3573,14 @@
     
-  
-  
 %7D%0A    
-        %7D%0A        %7D%0A%0A
     
@@ -3596,24 +3596,34 @@
 n eventBus;%0A
+        %7D%0A
     %7D%0A%0A    p

Make sure Fragment's onDestroy() has been called. Optimize concurrency