Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Suggest to add "OnKeyXXX" Annotations #1238

Closed

Description

@jiongxuan

Which examples looks better?

General:

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

AA which i want to:

    @KeyDown(KeyEvent.KEYCODE_BACK)
    public boolean onPressedBack() {
        return true;
    }

(Same as Key Up/Long Press/Shortcut etc.)
Support "dispatch" parameters.

Compared with the EventBus I mentioned previously, this looks easier to write, and more practical.
What do you think?

Activity

  1. jiongxuan commented on Nov 20, 2014

    @jiongxuan
    ContributorAuthor

    (1) OnKeyDown

        @KeyDown
        public boolean backPressed() {}
    
        @KeyDown(KeyEvent.KEYCODE_ESCAPE)
        public boolean onEscPressed(KeyEvent event) {}

    Generate:

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch(keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    return backPressed();
    
                case KeyEvent.KEYCODE_ESCAPE:
                    return onEscPressed(event);
            }
    
            return super.onKeyDown(keyCode, event);
        }

    (2) OnKeyUp/OnKeyLongPress/OnKeyShortcut are very similar to OnKeyDown

    (3) OnKeyMultiple

        @KeyMultiple
        public boolean backMultiplePressed() {}
    
        @KeyMultiple(KeyEvent.KEYCODE_ESCAPE)
        public void onEscMultiplePressed(KeyEvent event) {}
    
        @KeyMultiple(KeyEvent.KEYCODE_MENU)
        public boolean onMenuMultiplePressed(KeyEvent event, int repeatCount) {}

    Generate:

        @Override
        public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    return backMultiplePressed();
    
                case KeyEvent.KEYCODE_ESCAPE:
                    onEscMultiplePressed(event);
                    return true;
    
                case KeyEvent.KEYCODE_MENU;
                    return onMenuMultiplePressed(event, repeatCount);
            }
    
            return super.onKeyMultiple(keyCode, repeatCount, event);
        }

    (4) Multi-Keycodes

        @KeyUp({KeyEvent.KEYCODE_ESCAPE, KeyEvent.KEYCODE_MENU})
        public boolean onKeyPressed(int keyCode, KeyEvent event) {}

    Generate:

        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_MENU:
                case KeyEvent.KEYCODE_ESCAPE:
                    return onKeyPressed(keyCode, event);
            }
    
            return super.onKeyMultiple(keyCode, repeatCount, event);
        }
  2. jiongxuan commented on Nov 20, 2014

    @jiongxuan
    ContributorAuthor

    Limitation:

    (1) Each KeyCode can have only one correspondence method. Even if there are "Multi-Keycodes"

    Wrong:

        @KeyDown
        public boolean backPressed() {}
    
        // ERROR: Same keycode used in this activity
        @KeyDown(KeyEvent.KEYCODE_BACK)
        public boolean onBackPressed(KeyEvent event) {}
    
        // ERROR: Also apply for Multi-Keycodes
        @KeyDown({KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_MENU})
        public boolean onKeyPressed(int keyCode) {}

    You should do it manually:

        // OK
        @KeyDown(KeyEvent.KEYCODE_BACK)
        public boolean onBackPressed(KeyEvent event) {
            ...
            if (!backPressed()) {
                return onKeyPressed(KeyEvent.KEYCODE_BACK);
            }
            ...
            return onBackPressed(event);
        }

    (2) OnKeyLongPress can only used on Android 2.0+. OnKeyShortcut on Android 3.0+.
    We'll report an error at compile time. Also, we need to specify in the Wiki

    (3) At the moment, we don't support Fragment, because it does not support onKeyXX methods which write more complex.
    If I have more free time, I will consider write it in future releases (4.0+, maybe).

    @WonderCsabo @yDelouis @dodgex
    Any great idea? :)

  3. yDelouis commented on Nov 20, 2014

    @yDelouis
    Contributor

    Looks good to me.

  4. jiongxuan commented on Nov 20, 2014

    @jiongxuan
    ContributorAuthor

    My English is not good, in order to avoid such problems such as English grammar, I still want to you to write down the Wiki and Cookbook part. :)

    I take charge of to help you write the code, etc.

  5. WonderCsabo commented on Nov 20, 2014

    @WonderCsabo
    Member

    I have several comments:

    • you should use an array of key codes as the annotation parameter
    • why do you have the first limitation
    • why it is hard to work around the third limitation?
    • we can validate the second limitation
      On Nov 20, 2014 9:39 AM, "Jiongxuan Zhang" notifications@github.com wrote:

    My English is not good, in order to avoid such problems such as English
    grammar, I still want to you to write down the Wiki and Cookbook part. :)

    I take charge of to help you write the code, etc.

    Reply to this email directly or view it on GitHub
    #1238 (comment)
    .

  6. dodgex commented on Nov 20, 2014

    @dodgex
    Member
    • why do you have the first limitation

    i think this is because of the posibility to return a boolean if the key has been handled. what to do if a method wants to return true or false here? when there would be an ensured calling order that might be something that the developer could workaround, but due to the current nature of random order i think this limitation is valid.

  7. jiongxuan commented on Nov 20, 2014

    @jiongxuan
    ContributorAuthor

    I Edit it for a while

    you should use an array of key codes as the annotation parameter

    Okay

    why do you have the first limitation

    For ex.

        @KeyDown(KeyEvent.KEYCODE_BACK)
        public boolean onBackPressed1(KeyEvent event) {
            Log.d(TAG, "Pressed: 1");
            return false;
        }
    
        @KeyDown(KeyEvent.KEYCODE_BACK)
        public boolean onBackPressed2(KeyEvent event) {
            Log.d(TAG, "Pressed: 2");
            return true;
        }

    If we consider to run onBackPressed1 first, Is there no need to perform onBackPressed2?

    There are ruled out the "random call" situation

    why it is hard to work around the third limitation?

    Fragment does not have onKeyXX methods, we must:

    • Check if Inheritance class has onKeyXX methods which is own written
    • Add some code to Activity (you know i mean)
    • Generate some code cross java classes

    There was too mixed up for me, in fact :)

    we can validate the second limitation

    Sure.

  8. WonderCsabo commented on Nov 20, 2014

    @WonderCsabo
    Member

    Actually KeyEvent.Callback is implemented in lot of classes, not just in Activity. I think we should allow them in everywhere where they can be called and validate against whether it is implemented in the current class or not.

    It is not implemented in Fragments, but it would be definitely useful. You can call getActivity().onKeyXXX there instead of onKeyXXX if (getActvity() != null).

  9. WonderCsabo commented on Dec 23, 2014

    @WonderCsabo
    Member

    @jiongxuan any news on this?

  10. jiongxuan commented on Dec 26, 2014

    @jiongxuan
    ContributorAuthor

    @WonderCsabo
    I'm sorry about that. I really do not have time to do it recently. Because there are some unforeseen circumstances I need to deal with.
    I do this feature is expected to be completed after the Spring Festival (the end of February) .

    If you want this feature anxiously, you can let someone else do it.

    Thank you for understanding. :)

  11. WonderCsabo commented on Sep 12, 2015

    @WonderCsabo
    Member

    Finally implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions