package com.basgeekball.screenshotsnanny.activityassistant;

import android.app.Activity;
import android.os.Build;
import android.util.ArrayMap;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class ActivityHelper {
    private static final Class mActivityThreadClass;
    private static final Field mActivitiesField;
    private static final Field mPausedField;
    private static final Field mStoppedField;
    private static final Field mActivityField;

    // initialization
    static {
        Class tmpActivityThreadClass = null;
        Field tmpActivitiesField = null;
        Field tmpPausedField = null;
        Field tmpStoppedField = null;
        Field tmpActivityField = null;
        try {
            tmpActivityThreadClass = Class.forName("android.app.ActivityThread");
            tmpActivitiesField = tmpActivityThreadClass.getDeclaredField("mActivities");
            tmpActivitiesField.setAccessible(true);
            Class activityClientRecordClass = Class.forName("android.app.ActivityThread$ActivityClientRecord");
            tmpPausedField = activityClientRecordClass.getDeclaredField("paused");
            tmpStoppedField = activityClientRecordClass.getDeclaredField("stopped");
            tmpActivityField = activityClientRecordClass.getDeclaredField("activity");
            tmpPausedField.setAccessible(true);
            tmpStoppedField.setAccessible(true);
            tmpActivityField.setAccessible(true);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        mActivityThreadClass = tmpActivityThreadClass;
        mActivitiesField = tmpActivitiesField;
        mPausedField = tmpPausedField;
        mStoppedField = tmpStoppedField;
        mActivityField = tmpActivityField;
    }

    public static Activity getCurrentActivity() {
        Activity activity = null;
        try {
            Object currentActivityThread = mActivityThreadClass.getMethod("currentActivityThread").invoke(null);
            Map<Object, Object> activities;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                activities = (ArrayMap<Object, Object>) mActivitiesField.get(currentActivityThread);
            } else {
                activities = (HashMap<Object, Object>) mActivitiesField.get(currentActivityThread);
            }
            for (Object activityClientRecord : activities.values()) {
                boolean paused = mPausedField.getBoolean(activityClientRecord);
                boolean stopped = mStoppedField.getBoolean(activityClientRecord);
                if (!paused && !stopped) {
                    activity = (Activity) mActivityField.get(activityClientRecord);
                    break;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return activity;
    }
}

@@ -127,21 +127,187 @@
 oid.
-util.ArrayMap
+os.Handler;%0Aimport android.util.ArrayMap;%0Aimport android.util.Log;%0A%0Aimport com.basgeekball.screenshotsnanny.core.Constants;%0Aimport com.basgeekball.screenshotsnanny.helper.Callback
 ;%0A%0Ai
@@ -709,16 +709,151 @@
 Field;%0A%0A
+    private static final long mWait = 100;%0A    private static final int mRetriesMax = 20;%0A    private static int mRetriesCounter = 0;%0A%0A
     // i
@@ -3482,11 +3482,848 @@
 y;%0A    %7D
+%0A%0A    public static void doSomethingWhenActivityIsReady(final Class%3C?%3E T, final Callback callback) %7B%0A        if (mRetriesCounter %3C mRetriesMax) %7B%0A            if (T.isInstance(getCurrentActivity())) %7B%0A                Log.i(Constants.LOG_TAG, %22%E2%9A%92 Activity is ready, just do it%22);%0A                mRetriesCounter = 0;%0A                callback.execute();%0A            %7D else %7B%0A                new Handler().postDelayed(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        doSomethingWhenActivityIsReady(T, callback);%0A                    %7D%0A                %7D, mWait);%0A                mRetriesCounter += 1;%0A            %7D%0A        %7D else %7B%0A            Log.i(Constants.LOG_TAG, String.format(%22%E2%98%A0 Timeout: can not find %25s%22, T.getSimpleName()));%0A            mRetriesCounter = 0;%0A        %7D%0A    %7D
 %0A%7D%0A

Add activity readiness check method