-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathLights.cpp
140 lines (114 loc) · 3.87 KB
/
Lights.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2021-2024 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "Lights.h"
#define LOG_TAG "Lights"
#include <android-base/logging.h>
#include "Utils.h"
namespace aidl {
namespace android {
namespace hardware {
namespace light {
#define AutoHwLight(light) \
{ .id = static_cast<int32_t>(light), .ordinal = 0, .type = light }
Lights::Lights() {
if (mDevices.hasBacklightDevices()) {
mLights.push_back(AutoHwLight(LightType::BACKLIGHT));
}
if (mDevices.hasButtonDevices()) {
mLights.push_back(AutoHwLight(LightType::BUTTONS));
}
if (mDevices.hasKeyboardDevices()) {
mLights.push_back(AutoHwLight(LightType::KEYBOARD));
}
if (mDevices.hasNotificationDevices()) {
mLights.push_back(AutoHwLight(LightType::BATTERY));
mLights.push_back(AutoHwLight(LightType::NOTIFICATIONS));
mLights.push_back(AutoHwLight(LightType::ATTENTION));
}
}
ndk::ScopedAStatus Lights::setLightState(int32_t id, const HwLightState& state) {
rgb color(state.color);
LightType type = static_cast<LightType>(id);
switch (type) {
case LightType::BACKLIGHT:
mDevices.setBacklightColor(color);
break;
case LightType::KEYBOARD:
mDevices.setKeyboardColor(color);
break;
case LightType::BUTTONS:
mDevices.setButtonsColor(color);
break;
case LightType::BATTERY:
mLastBatteryState = state;
updateNotificationColor();
break;
case LightType::NOTIFICATIONS:
mLastNotificationsState = state;
updateNotificationColor();
break;
case LightType::ATTENTION:
mLastAttentionState = state;
updateNotificationColor();
break;
default:
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
break;
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus Lights::getLights(std::vector<HwLight>* _aidl_return) {
for (const auto& light : mLights) {
_aidl_return->push_back(light);
}
return ndk::ScopedAStatus::ok();
}
binder_status_t Lights::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
dprintf(fd, "Lights AIDL:\n");
dprintf(fd, "\n");
dprintf(fd, "Lights:\n");
for (const auto& light : mLights) {
dprintf(fd, "- %d: LightType::%s\n", light.id, toString(light.type).c_str());
}
dprintf(fd, "\n");
dprintf(fd, "Devices:\n");
mDevices.dump(fd);
dprintf(fd, "\n");
return STATUS_OK;
}
void Lights::updateNotificationColor() {
std::lock_guard<std::mutex> lock(mLedMutex);
bool isBatteryLit = rgb(mLastBatteryState.color).isLit();
bool isAttentionLit = rgb(mLastAttentionState.color).isLit();
bool isNotificationsLit = rgb(mLastNotificationsState.color).isLit();
const HwLightState state = isNotificationsLit ? mLastNotificationsState
: isAttentionLit ? mLastAttentionState
: isBatteryLit ? mLastBatteryState
: HwLightState();
rgb color(state.color);
LightMode lightMode;
switch (state.flashMode) {
case FlashMode::NONE:
lightMode = LightMode::STATIC;
break;
case FlashMode::TIMED:
lightMode = LightMode::TIMED;
break;
case FlashMode::HARDWARE:
lightMode = LightMode::BREATH;
break;
default:
LOG(ERROR) << "Unknown flash mode: " << static_cast<int>(state.flashMode);
lightMode = LightMode::STATIC;
break;
}
mDevices.setNotificationColor(color, lightMode, state.flashOnMs, state.flashOffMs);
return;
}
} // namespace light
} // namespace hardware
} // namespace android
} // namespace aidl