Android - What is the correct way of turning off screen

1k views Asked by At

This is a little bit tricky.

Background: there is an old GalaxyNexus whose power button is not functioning very well. I have to press hard to turn on/off screen and this is very annoying!

Solution: I have downloaded the newest AOSP and build my own firmware images. I modified two places to handle this problem.

1- In function interceptKeyBeforeQueueing() of PhoneWindowManager, I add the following handling codes:

if(keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
    if(event.getAction() == KeyEvent.ACTION_UP && !mPowerManager.isScreenOn()) {
        return ACTION_WAKE_UP;
    }
}

2- In the status bar source code, I modified the call back when you long-press the home button. I change it from triggering the google assistant to turn off screen (apology to google). In function onTrigger() in SearchPanelView, I modified the code as below:

//startAssistActivity();
vibrate();
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()) {
    pm.goToSleep(SystemClock.uptimeMillis());
}

I have comment out the original code of launching the assist activity and add my code of turning off the screen.

Result: It seems work fine, now I can

  1. Long press the home button and slide to turn off screen
  2. Press volume +/- button to turn on the screen

But, something is abnormal if I disable the lock screen. If the screen lock is set to NONE and I long press the home button and slide to turn off the screen, after I turn on the screen, no matter via volume key or power key, the UI has no reaction when you touch it. Unless you press the home again. It seems that the search panel view is still there and is hijacking my touch events.

I have also tried to modify the code in status bar as following:

mBar.animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_SEARCH_PANEL);
Slog.i(TAG, "onTrigger: post message to turn off screen after 100ms.");
v.postDelayed(new Runnable() {
    @Override
    public void run() {
        PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
        if(pm.isScreenOn()) {
            pm.goToSleep(SystemClock.uptimeMillis());
        }
    }
}, 100);

But the problem remains. This issue only happens in the scenario of turning off screen using the "long press the home and slide up" action. Which means to use PowerManager.gotoSleep() method. Is there anything missing here? Any additional handing should be taken?

Is there anybody could give me some hint or inspiration? Thanks a lot!

0

There are 0 answers