Last active
May 31, 2016 07:23
-
-
Save rpattabi/5252d38eb470e11bd4d0e8bb29f239bc to your computer and use it in GitHub Desktop.
Android - Fake toucher to prevent CPU scaling down due to user inactivity. UPDATE: This doesn't help! :--( Android understands that these fake touches are not coming from user and continues to scale down the CPU.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.SystemClock; | |
import android.support.annotation.NonNull; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
/* | |
NOTE | |
Android scales down CPU to save battery, when user is not interacting with the app. | |
But this will cause issues for audio processing apps which require full power of the CPU. | |
The work around is to do fake touch on the screen every second | |
making android think that user is interacting. | |
This terrible hack is recommended by Android audio team! https://youtu.be/F2ZDp-eNrh4?t=9m0s | |
I use a slightly different API here since their code caused ripples appearing on different widgets. | |
Suggestions, improvements, and corrections are welcome. | |
UPDATE: | |
This implementation doesn't help. | |
Android understands that these fake touches are not coming from user and continues to scale down the CPU. | |
Android audio team's recommendation, mentioned above, is the way to go. | |
But their recommendation causes ripples on different widgets. Please let me know in comments if you found a way to address this somehow. | |
*/ | |
public class FakeToucher { | |
private final View mViewToTouch; | |
private Timer mTimer; | |
private TimerTask mTouchTask; | |
private MotionEvent mUpMotionEvent; | |
private static final int DELAY = 1000; | |
private static final int PERIOD = 1000; | |
public FakeToucher(@NonNull View viewToTouch) { | |
mViewToTouch = viewToTouch; | |
} | |
public void start() { | |
mTouchTask = new TimerTask() { | |
@Override | |
public void run() { | |
if (mUpMotionEvent == null) | |
mUpMotionEvent = getUpMotionEvent(); | |
mViewToTouch.dispatchTouchEvent(mUpMotionEvent); | |
} | |
}; | |
mTimer = new Timer(); | |
mTimer.schedule(mTouchTask, DELAY, PERIOD); | |
} | |
public void stop() { | |
mTimer.cancel(); | |
mTimer = null; | |
mTouchTask = null; | |
mUpMotionEvent = null; | |
} | |
private MotionEvent getUpMotionEvent() { | |
return MotionEvent.obtain( | |
SystemClock.uptimeMillis(), | |
SystemClock.uptimeMillis(), | |
MotionEvent.ACTION_UP, | |
mViewToTouch.getX(), mViewToTouch.getY(), 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UPDATE:
This implementation doesn't help.
Android understands that these fake touches are not coming from user and continues to scale down the CPU.
Android audio team's recommendation, mentioned above, is the way to go. But their recommendation causes ripples on different widgets.
Please let me know in comments if you found a way to address this somehow.