From 43a4dd416ea328741f2fe2016dfc93cacd949620 Mon Sep 17 00:00:00 2001 From: huyang <1184394624@qq.com> Date: Thu, 15 Jan 2026 10:47:23 +0800 Subject: [PATCH] Brightness: set brightness by "openfdelight" service --- app/src/main/aidl/android/openfde/ILight.aidl | 22 ++++++ .../com/boringdroid/systemui/utils/Light.java | 76 +++++++++++++++++++ .../com/boringdroid/systemui/utils/Utils.kt | 13 +--- 3 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 app/src/main/aidl/android/openfde/ILight.aidl create mode 100644 app/src/main/java/com/boringdroid/systemui/utils/Light.java diff --git a/app/src/main/aidl/android/openfde/ILight.aidl b/app/src/main/aidl/android/openfde/ILight.aidl new file mode 100644 index 00000000..17b64e17 --- /dev/null +++ b/app/src/main/aidl/android/openfde/ILight.aidl @@ -0,0 +1,22 @@ +/* +** +** Copyright (C) 2021 The WayDroid Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package android.openfde; +interface ILight { + int setBacklight(int brightness); + int getBacklight(); +} \ No newline at end of file diff --git a/app/src/main/java/com/boringdroid/systemui/utils/Light.java b/app/src/main/java/com/boringdroid/systemui/utils/Light.java new file mode 100644 index 00000000..11321656 --- /dev/null +++ b/app/src/main/java/com/boringdroid/systemui/utils/Light.java @@ -0,0 +1,76 @@ +package com.boringdroid.systemui.utils; + +import android.content.Context; +import android.openfde.ILight; +import android.os.IBinder; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.util.Log; + +public class Light { + private static final String TAG = "openfdelight"; + public static final String LIGHT_SERVICE = "openfdelight"; + + /** + * Unable to determine status, an error occured + */ + public static final int ERROR_UNDEFINED = -1; + + private static ILight sService; + private static Light sInstance; + + private Context mContext; + + private Light(Context context) { + mContext = context == null ? null : context.getApplicationContext(); + sService = getService(); + } + + public static Light getInstance(Context context) { + if (sInstance == null) { + sInstance = new Light(context); + } + return sInstance; + } + + public static ILight getService() { + if (sService != null) { + return sService; + } + IBinder b = ServiceManager.getService(LIGHT_SERVICE); + + if (b == null) { + Log.e(TAG, "null service. SAD!"); + return null; + } + + sService = ILight.Stub.asInterface(b); + return sService; + } + + public int setBacklight(int brightness) { + ILight service = getService(); + if (service == null) { + return ERROR_UNDEFINED; + } + try { + return service.setBacklight(brightness); + } catch (RemoteException e) { + Log.e(TAG, e.getLocalizedMessage(), e); + } + return ERROR_UNDEFINED; + } + + public int getBacklight() { + ILight service = getService(); + if (service == null) { + return ERROR_UNDEFINED; + } + try { + return service.getBacklight(); + } catch (RemoteException e) { + Log.e(TAG, e.getLocalizedMessage(), e); + } + return ERROR_UNDEFINED; + } +} diff --git a/app/src/main/java/com/boringdroid/systemui/utils/Utils.kt b/app/src/main/java/com/boringdroid/systemui/utils/Utils.kt index 60de5c53..e9c0d20a 100644 --- a/app/src/main/java/com/boringdroid/systemui/utils/Utils.kt +++ b/app/src/main/java/com/boringdroid/systemui/utils/Utils.kt @@ -400,17 +400,8 @@ object Utils { } fun getScreenBrightness(context: Context): Int { - return try { - // 获取系统亮度值(范围 0-255) - val brightness = Settings.System.getInt( - context.contentResolver, - Settings.System.SCREEN_BRIGHTNESS - ) - brightness - } catch (e: Settings.SettingNotFoundException) { - // 如果设置不存在,返回默认值(如 128) - 128 - } + val backlight = Light.getInstance(context).backlight + return if (backlight > 0) backlight else 255 } fun setScreenBrightness(context: Context, brightness: Int) {