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.

Add @WakeLock(WakeLockLevel) annotation for a method #863

Closed

Description

@Bendr0id

You can get the WakeLock with something like this:

@SystemService
PowerManager powerManager;

public void foo()
{
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    wakeLock.acquire();
    doSomething();
    wakeLock.release();
}

But it would be very helpfull and clean when you can do something like:

@WakeLock(level = WakeLockLevel.PARTIAL_WAKE_LOCK)
public void foo()
{
    doSomething();
}

Which will wrap the wakeLock acquire and release around the method. Like the @UiThread or @Background annotation.

Activity

  1. DayS commented on Jan 6, 2014

    @DayS
    Contributor

    Good idea. But the cleanest code would be like this :

    @WakeLock(level = WakeLockLevel.PARTIAL_WAKE_LOCK)
    public void doSomething()
    {
        // ....
    }
  2. Bendr0id commented on Jan 6, 2014

    @Bendr0id
    Author

    Sure. Was just a quick sample.

  3. modified the milestones: 3.2, 3.1 on May 11, 2014
  4. dodgex commented on Sep 2, 2014

    @dodgex
    Member

    I have implemented this feature.

    as i have to do some cleanups before proposing a PR i'll first give a short info about what i've done.

    the syntax is like @DayS suggested. beside level thare is a value tag and one flags

    tag defaults to "NO_TAG" and that results in the simple name of the enclosing element (enhanced class)
    level defaults to PARTIAL_WAKE_LOCK
    flags defaults to {} aka no flags

    also @WakeLock plays well with @Background and @UiHandler as the wakelock is taken inside of the Runnable/Task.

    As any application using a WakeLock must request the android.permission.WAKE_LOCK permission, i added the permission check to validation.

    at the moment there is just one known issue with @WakeLock and @Trace if the method has a return value. in that case @Trace swallows the wakelock acquisition as it trys to store the return value but only calls the super method then. I'm not sure how to handle this. At the moment i see thre ways. The easy (and somehow dirty) way to add a warning if @Trace and @WakeLock where used on a method with a return type. The easy (and cleaner but more restrictive) way to not allow return values for @WakeLock. or the hard (if not impossible?!) way of changing @Trace behaviour for return values. while i have a potential (and possibly even easy) solution for the hard way, i think it would be VERY ugly... (wrapping the original method body in an annonymous class).

  5. WonderCsabo commented on Sep 2, 2014

    @WonderCsabo
    Member

    Great job! I love the validation, but what about library projects? They can use the WakeLock code without any permissions in the manifest, since the app can declare those. But it can be also a good idea to declare the permission at the library level, so it get picked up in the app after manifest merging, also the user of the library can see what permissions the lib needs. To be honest i only developed a library project once, so i do not know the practices here, but i would vote to declare the permission at the library level (so your validation is OK).

    About the issue: since @Trace is just a debug annotation, and it is even disabled by default, i think we can even go the dirty way. I do not like the easy way, since that limits the usage of the annotation because of @Trace, but the user may not employ @Trace at all. About the hard way: Creating an anonymous class for this is really not nice.

    Either way, roll out a PR, we should discuss implementation detail there. Also @yDelouis WDYT about this?

  6. dodgex commented on Sep 2, 2014

    @dodgex
    Member

    As mentioned i want to clean up the Code first before i create a PR. I think tomorrow evening i'll get it done.

    I implemented the validation the same way like the hasInternetPermission. Actually i refactored it a little bit (you'll see that tomorrow). So for library projects the permission validation is off.

    Ok. So i'll add a warning if @Trace is present and the method returns something.

  7. WonderCsabo commented on Sep 2, 2014

    @WonderCsabo
    Member

    OK, i see now, we do not check against lib project permissions. Shoot, i should better know our codebase... I look forward to the PR.

  8. dodgex commented on Sep 3, 2014

    @dodgex
    Member

    @WonderCsabo i thought about the permission validation on library projects.

    maybe it is a good idea to add a warning for required permissions if an annotation with permission requirement is used in a library project. so that the library developer is aware of the fact that there is a permission that might be required. this way he can decide if he adds the permission to its own manifest (eg. if the annotated method is a core function that is most likely to be called) or if he only notifies the user in his docs/readme (eg. for methods that could be used but not in all use-cases)

    i think simply ignoring the fact that there is a required permission is the wrong way.

  9. WonderCsabo commented on Sep 3, 2014

    @WonderCsabo
    Member

    i think simply ignoring the fact that there is a required permission is the wrong way.

    I agree with you, as i have written in my older comment. @yDelouis ?

  10. dodgex commented on Sep 3, 2014

    @dodgex
    Member

    Note to self: test how @Click and other listeners works together with this annotations. not sure how those call the method.

  11. WonderCsabo commented on Sep 3, 2014

    @WonderCsabo
    Member

    Listener annotations do not override the method, just call them, so they should just work fine.

  12. Bendr0id commented on Sep 3, 2014

    @Bendr0id
    Author

    @dodgex Great job! Thanks for that!

  13. WonderCsabo commented on Sep 20, 2014

    @WonderCsabo
    Member

    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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions