Search code examples
androidstaticsharedpreferenceshookxposed

How to keep value in Xposed module across hooks in same application


I'm developing a xposed module to insert text in EditTexts. The user should be able to click through diffrent strings with the volume up/down keys. I have the module running so far. I hooked the onFocusChanged method of View, then check if the View object is an instance of EditText. Then i set an OnKeyListener to the EditText.

So when i have focus in EditText field, and keep clicking volumeDown key, I want different Strings to appear in the field. But I'm not able to store a counter value. I made it static in my xposed class (I thought static variables were kept inside an application), but that doesnt work, either.

Is there a method to store a (couter)value? I would be able to check the text in the textfield, search for the same text in my text array, determine the current index and work from there, but i hope there is a "nicer" way :)

Here is the code to my text, the index variable is the problem:

    public class Xposed implements IXposedHookZygoteInit, IXposedHookLoadPackage {

    private static int index, indexMax;
    private static boolean first = true;
    private static String[] valuesX = {"bla1", "bla2", "bla3", "bla4"};
    private Context mCon;
    private EditText et;

    @Override
    public void initZygote(StartupParam startupParam) throws Throwable {
        findAndHookMethod(TextView.class, "onFocusChanged", boolean.class, int.class, Rect.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                if (param.thisObject instanceof EditText) {
                    et = (EditText) param.thisObject;
                    if ((boolean) param.args[0]) {
                        et.setText("I have focus!!! (1)"); 
                        XposedBridge.log("starting quickinsert after focus");                        
                        mCon = et.getContext().getApplicationContext();                        

                        View.OnKeyListener okl = new View.OnKeyListener() {
                            @Override
                            public boolean onKey(View v, int keyCode, KeyEvent event) {
                                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
                                        keyPressed(keyCode);
                                        return true;
                                    }
                                }
                                return false;
                            }
                        };;
                        et.setOnKeyListener(okl);
                    }                
                } 
            }
        });
}

private void keyPressed(int keyCode) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
        if (first) {
            first = false;
        } else {
            if (index < indexMax) {
                index++;
            } else {
                index = 0;
            }
        }
        et.setText(valuesX[index]);
        XposedBridge.log("keycode down consumed (index " + index + ")");
    } else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
        if (first) {
            first = false;
        } else {
            if (index > 0) {
                index--;
            } else {
                index = indexMax;
            }

        }
        et.setText(valuesX[index]);
        XposedBridge.log("keycode up consumed (index " + index + ")");
    }
}
}

Solution

  • Problem solved...

    General sulution: Make variables static to access them within same application hooks

    Solution to my problem: indexMax was not initialized. After setting indexMax = valuesX.length; everything worked