androidandroid-alertdialogxposed-framework

Show dialog in hooked method by xposed running in thread


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


Solution

  • 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!