Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onKeyDown Callthrough listener always consuming event #117

Closed
ghost opened this issue Jun 18, 2014 · 14 comments
Closed

onKeyDown Callthrough listener always consuming event #117

ghost opened this issue Jun 18, 2014 · 14 comments

Comments

@ghost
Copy link

ghost commented Jun 18, 2014

For some reason, the following registered listener callthrough listeners seem to always consume the event, thus making it impossible to leave the application by pressing the back key:

    public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (productListVisible == true) {
                showRandomProductList();
                return true;
            }
        }
        return false;
    }

The generated code looks like this:

   @Override
    public boolean onKeyDown(int int$$1, KeyEvent keyEvent$$0) {
        return searchActivity$$0 .onKeyDown(int$$1, keyEvent$$0);
    }

Any ideas on how it would be possible to make android consume the event itself and exiting the app when false is returned?

@ghost
Copy link
Author

ghost commented Jun 18, 2014

Maybe some generated code like this would work?:

 @Override
    public boolean onKeyDown(int int$$1, KeyEvent keyEvent$$0) {
        boolean consumed;
        consumed = searchActivity$$0 .onKeyDown(int$$1, keyEvent$$0);
        if(consumed){return true;}
        return super(int$$1, keyEvent$$0);
    }

no idea how to implement this, tough

@ghost
Copy link
Author

ghost commented Jun 20, 2014

I've looked at the source code. Seems to be related to buildActivityDelegateRegistrationGeneratorFactory in RegistrationAnalyzer.java.
But once again, I have no idea what needs to be changed.
To start with, I have problems understanding the concept of ASTType and how to generate code.

This one is really important, as there is no workaround. Probably the last showstopper for my project, which has come quite far by now.

I'd be really grateful, if somebody could help me on this.

@ghost
Copy link
Author

ghost commented Jun 20, 2014

How comes a solution always pops up once I've posted a request?
A workaround for this specific problem (back key) looks like this:

    public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (productListVisible == true) {
                showRandomProductList();
                return true;
            } else {
                activity.onBackPressed();
                return true;
            }
        }
        return false;
    }

Hope this is useful for others, too.

@johncarl81
Copy link
Owner

This looks like an ok workaround, but are you really looking to make the super call (Related: #101)?

ASTType is just a wrapper to unify the Element and Class, mainly intended for testing the annotation processor api using regular Classes. Think of then as Classes and their related information (ASTMethod, ASTField, etc).

@ghost
Copy link
Author

ghost commented Jun 20, 2014

Regarding #101, does this allow to consume the back key event? It is a void method, after all.
If I'm not mistaken, the call to super is necessary for the system to consume the back key event if it isn't already consumed.
To be honest regarding ASTType, I still don't quite get it. Is there some existing tutorial for the kind of code generation you are using in transfuse?

@johncarl81
Copy link
Owner

Yes, the intent is to allow for a super call to properly handle the back key event in the case that your application doesn't handle it. As with #115 this functionality is waiting for a major refactoring of the codebase (0.3.0).

ASTType is defined within Transfuse. You can find the relevant classes here: https://github.com/johncarl81/transfuse/tree/master/transfuse-support/src/main/java/org/androidtransfuse/adapter. This set of classes follow the adapter pattern, which allows us to treat the Element api (annotation processor-centric) and the Class api (Reflection-centric) the same. In turn, this makes unit testing annotation processors much easier.

If you're interested in contributing to Transfuse, I'm more than happy to help explain more regarding the core. Shoot me an email with specific questions and I'll try my best to answer.

@ghost
Copy link
Author

ghost commented Jun 24, 2014

Hi John,

so it's on a possible future feature - thats great :)
Thanks for your explanation, I'm slowly understanding transfuse a bit better.
Regarding contribution, I fear that for most of the part I can't keep up with your code quality.
But I'd be glad to help out with simple repetitive tasks, like adding code for registering Listeners.

@johncarl81
Copy link
Owner

@Airblaster, any contributions are greatly appreciated. Just reporting issues, engaging in specific features and believing in the project is huge. We also have documentation that could use some tending on the transfuse-jeykll-site branch.

@johncarl81
Copy link
Owner

#70

@johncarl81
Copy link
Owner

@Airblaster, I believe we're addressed this issue. Can I close?

@ghost
Copy link
Author

ghost commented Oct 6, 2014

@johncarl81 :
Sorry for the late reply, I was on holiday. Still got to catch up on many things, so I probably can't take a look at this before the end of the week.

@ghost
Copy link
Author

ghost commented Oct 8, 2014

@johncarl81 :
on 0.3.0-beta-2 the code still looks like in post two of this issue.
Is the fix only in SNAPSHOT so far?

@johncarl81
Copy link
Owner

Ah, this is what you'll need to do:

    @ManualSuper(name = "onKeyDown", parameters = {int.class, KeyEvent.class})
    public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (productListVisible == true) {
                showRandomProductList();
                return true;
            }
        }
        return GeneratedActivity.SUPER.onKeyDown(keyCode, event);
    }

Just replace GeneratedActivity with the name of the generated activity for your component.

Let me know if this works for you.

@ghost
Copy link
Author

ghost commented Oct 10, 2014

Ah, so thats how you use it.
Working fine for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant