Show dialog in hooked method by xposed running in thread

2.7k views Asked by At

I try to show an AlertDialog within a hooked method with xposed. The problem is that the method is running in a threads, and this thread is running in a thread, etc...

For example : Activity -> thread -> thread -> ... -> function

Is there a way to show my AlertDialog ? I have the Context, but since the hooked function is not in the main thread, it is useless.

EDIT (Some code) :

public class Xposed implements IXposedHookLoadPackage {
private Context ctx;
private Queue<String> queue = new LinkedList<>();

public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
    if (!lpparam.packageName.equals("xxx.xxx.xxx")) {
        return;
    }

    // Here I get the context from the static class extending Application
    findAndHookMethod("xxx.xxx.xxx", lpparam.classLoader, "attachBaseContext", Context.class, new XC_MethodHook() {
        @Override
        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
            XposedBridge.log("context modified");
            ctx = (Context) param.args[0];
        }
    });

    findAndHookMethod("com.xxx.xxx.xxx", lpparam.classLoader, "e", "com.xxx.xxx.xxx", String.class, new XC_MethodHook() {
        @Override
        protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
            if (!(param.args[1]).equals("xxxxxxxxxxxxxx")) {
                return ;
            }

            XposedBridge.log("New element detected detected");
            Object param = param.args[0];
            Object info = callMethod(param, "q");

            // Here, I want to show my alertdialog
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_POSITIVE) {

                    }
                }
            };

            // I get the classic error like what I can't modify the ui
            // in a thread that has not called Looper.prepare()
            AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
            builder.setMessage("Are you sure ?")
                    .setPositiveButton("Yes", dialogClickListener)
                    .setNegativeButton("No", dialogClickListener)
                    .show();
        }
    });
}

Thanks

3

There are 3 answers

2
4knahs On BEST ANSWER

You can always keep a reference of the current Activity (android.app.Instrumentation.newActivity). It goes something like this:

Class<?> instrumentation = XposedHelpers.findClass(
                "android.app.Instrumentation", lpparam.classLoader);

Method method = instrumentation.getMethod("newActivity",
                 ClassLoader.class, String.class, Intent.class);

XposedBridge.hookMethod(method, iHook);

In this case the ihook would be a hook of yours that just stores the current Activity statically so you can invoke the runOnUiThread from wherever you are:

public class ActivityHook extends XC_MethodHook{

    /* Assure latest read of write */
    private static volatile Activity _currentActivity = null;

    public static Activity getCurrentActivity() {
        return _currentActivity;
    }

    @Override
    protected Object afterHookedMethod(MethodHookParam param)
            throws Throwable {
        _currentActivity = (Activity) param.getResult();
    }
}

Then you will be able to do from anywhere:

ActivityHook.getCurrentActivity().runOnUiThread(...);

Good luck!

3
Bhargav On

You can use the runOnUiThread(runnable) method to do this if you are running this code in an activity or a fragment.

2
Programming4life On

Since you don't have access to an activity you could get the main looper and then post ui related tasks.

Note: When you need to get the current applications context in an xposed module you don't need to hook anything like attachBaseContext. The xposed api provides a helper method for obtaining the current applications context AndroidAppHelper.currentApplication().

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        //Do ui related stuff in here
        AlertDialog.Builder builder = new AlertDialog.Builder(AndroidAppHelper.currentApplication());
        builder.setMessage("Are you sure ?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener)
                .show();
    }
});

Looper.getMainLooper() documentation: http://developer.android.com/reference/android/os/Looper.html#getMainLooper()

Handler.post documentation: http://developer.android.com/reference/android/os/Handler.html#post(java.lang.Runnable)

AndroidAppHelper.currentApplication() Source code: https://github.com/rovo89/XposedBridge/blob/art/src/android/app/AndroidAppHelper.java#L143-L145