Android:控制按键灯亮灭【button-backlight】

/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

1.导包
import java.io.DataOutputStream;
import java.io.FileOutputStream;

Handler mHandler3;

2.新建handler对象

public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {

 mHandler3 = new Handler();

3.延时处理方法

public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {


        // Basic policy based on interactive state.
        int result;
        boolean isWakeKey = (policyFlags & WindowManagerPolicy.FLAG_WAKE) != 0
                || event.isWakeKey();
        //*/ 20210513. for backlight control
        if(interactive) {
            writeFile("/sys/class/leds/button-backlight/brightness", "1");
            mHandler3.removeMessages(0);
            mHandler3.postDelayed(new Runnable() {
                public void run() {
                    writeFile("/sys/class/leds/button-backlight/brightness", "0");
                }
            }, 5 * 1000);
        }
        //*/

        if (interactive || (isInjected && !isWakeKey)) {
            // When the device is interactive or the key is injected pass the
            // key to the application.
            result = ACTION_PASS_TO_USER;
            isWakeKey = false;

            if (interactive) {
                // If the screen is awake, but the button pressed was the one that woke the device
                // then don't pass it to the application
                if (keyCode == mPendingWakeKey && !down) {
                    result = 0;
                }
                // Reset the pending key
                mPendingWakeKey = PENDING_KEY_NULL;
            }
        } else if (!interactive && shouldDispatchInputWhenNonInteractive(event)) {
            // If we're currently dozing with the screen on and the keyguard showing, pass the key
            // to the application but preserve its wake key status to make sure we still move
            // from dozing to fully interactive if we would normally go from off to fully
            // interactive.
            result = ACTION_PASS_TO_USER;
            // Since we're dispatching the input, reset the pending key
            mPendingWakeKey = PENDING_KEY_NULL;
        } else {
            // When the screen is off and the key is not injected, determine whether
            // to wake the device but don't pass the key to the application.
            result = 0;
            if (isWakeKey && (!down || !isWakeKeyWhenScreenOff(keyCode))) {
                isWakeKey = false;
            }
            // Cache the wake key on down event so we can also avoid sending the up event to the app
            if (isWakeKey && down) {
                mPendingWakeKey = keyCode;
            }
        }

4.屏灭时写0

 @Override
    public void screenTurningOff(ScreenOffListener screenOffListener) {
        mWindowManagerFuncs.screenTurningOff(screenOffListener);
        synchronized (mLock) {
            if (mKeyguardDelegate != null) {
                mKeyguardDelegate.onScreenTurningOff();
            }
        }
        //*/ 20231017 for backlight control
        writeFile("/sys/class/leds/button-backlight/brightness", "0");
        //*/

    }
 

5.写节点方法

//*/

private void writeFile(String filePath, String line) {
        File a = new File(filePath);
        if (a.exists()) {
            try {
                FileOutputStream fs = new FileOutputStream(a);
                DataOutputStream ds = new DataOutputStream(fs);
                ds.write(line.getBytes());
                ds.flush();
                ds.close();
                fs.close();
            } catch (Exception ex) {
                Log.e(TAG, "writeFile() Exception: " + filePath);
            }
        } else {
            Log.d(TAG, "writeFile() File not exist: " + filePath);
            try {
                if (a.createNewFile()) {
                    Log.d(TAG, "writeFile() File created: " + filePath);
                    try {
                        FileOutputStream fs = new FileOutputStream(a);
                        DataOutputStream ds = new DataOutputStream(fs);
                        ds.write(line.getBytes());
                        ds.flush();
                        ds.close();
                        fs.close();
                    } catch (Exception ex) {
                        Log.e(TAG, "writeFile() Exception: " + filePath);
                    }
                } else {
                    Log.d(TAG, "writeFile() Create file fail: " + filePath);
                }
            } catch (IOException e) {
                Log.e(TAG, "writeFile() creatFile Exception: " + filePath);
            }
        }
    }
    //*/

最后别忘了底层的支持 :

defconfig / debug_defconfig文件

CONFIG_BUTTON_BACKLIGHT_SUPPORT_GPIO=y

你可能感兴趣的:(计算机外设,android,java)