-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBatteryConservationController.java
More file actions
207 lines (178 loc) · 7.21 KB
/
BatteryConservationController.java
File metadata and controls
207 lines (178 loc) · 7.21 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.border.*;
import java.util.concurrent.*;
public class BatteryConservationController extends JFrame {
private static final String CONSERVATION_PATH = "/sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode";
private JLabel statusLabel;
private Timer notificationTimer;
private JWindow notificationWindow;
public BatteryConservationController() {
setupWindow();
createGUI();
checkRoot();
checkPath();
updateStatus();
}
private void setupWindow() {
setTitle("Battery Conservation Mode");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
private void createGUI() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("Battery Conservation Mode");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Status Panel
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel statusTextLabel = new JLabel("Current Status: ");
statusTextLabel.setFont(new Font("Arial", Font.PLAIN, 16));
statusLabel = new JLabel("Unknown");
statusLabel.setFont(new Font("Arial", Font.BOLD, 16));
statusPanel.add(statusTextLabel);
statusPanel.add(statusLabel);
// Buttons Panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
JButton enableButton = createStyledButton("Enable", new Color(46, 204, 113));
JButton disableButton = createStyledButton("Disable", new Color(231, 76, 60));
enableButton.addActionListener(e -> setConservationMode(true));
disableButton.addActionListener(e -> setConservationMode(false));
buttonPanel.add(enableButton);
buttonPanel.add(disableButton);
// Main Panel Components ^^
mainPanel.add(Box.createVerticalStrut(20));
mainPanel.add(titleLabel);
mainPanel.add(Box.createVerticalStrut(30));
mainPanel.add(statusPanel);
mainPanel.add(Box.createVerticalStrut(30));
mainPanel.add(buttonPanel);
add(mainPanel);
}
private JButton createStyledButton(String text, Color color) {
JButton button = new JButton(text);
button.setPreferredSize(new Dimension(120, 40));
button.setFont(new Font("Arial", Font.BOLD, 14));
button.setBackground(color);
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
return button;
}
private void checkRoot() {
if (!isRoot()) {
JOptionPane.showMessageDialog(this,
"This application needs root privileges.\nPlease run with sudo.",
"Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private void checkPath() {
File path = new File(CONSERVATION_PATH);
if (!path.exists()) {
JOptionPane.showMessageDialog(this,
"Conservation mode path not found.\nIs this a Lenovo IdeaPad laptop?",
"Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private boolean isRoot() {
try {
Process process = Runtime.getRuntime().exec("id -u");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String result = reader.readLine();
return result != null && result.equals("0");
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private void updateStatus() {
try {
BufferedReader reader = new BufferedReader(new FileReader(CONSERVATION_PATH));
String status = reader.readLine();
reader.close();
boolean isEnabled = "1".equals(status);
statusLabel.setText(isEnabled ? "Enabled" : "Disabled");
statusLabel.setForeground(isEnabled ? new Color(46, 204, 113) : new Color(231, 76, 60));
} catch (IOException e) {
statusLabel.setText("Error");
statusLabel.setForeground(Color.RED);
}
}
private void setConservationMode(boolean enable) {
try {
ProcessBuilder pb = new ProcessBuilder("sudo", "tee", CONSERVATION_PATH);
pb.redirectInput(ProcessBuilder.Redirect.PIPE);
Process process = pb.start();
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) {
writer.println(enable ? "1" : "0");
}
int result = process.waitFor();
if (result == 0) {
updateStatus();
showNotification(enable ? "Conservation mode enabled" : "Conservation mode disabled");
} else {
throw new IOException("Failed to set conservation mode");
}
} catch (IOException | InterruptedException e) {
JOptionPane.showMessageDialog(this,
"Error setting conservation mode: " + e.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void showNotification(String message) {
if (notificationWindow != null) {
notificationWindow.dispose();
}
if (notificationTimer != null) {
notificationTimer.stop();
}
notificationWindow = new JWindow();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.GRAY),
BorderFactory.createEmptyBorder(10, 20, 10, 20)
));
panel.setBackground(new Color(0, 0, 0, 200));
JLabel label = new JLabel(message);
label.setForeground(Color.WHITE);
label.setFont(new Font("Arial", Font.PLAIN, 14));
panel.add(label);
notificationWindow.add(panel);
notificationWindow.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
notificationWindow.setLocation(
screenSize.width - notificationWindow.getWidth() - 20,
screenSize.height - notificationWindow.getHeight() - 40
);
notificationWindow.setVisible(true);
notificationTimer = new Timer(2000, e -> {
notificationWindow.dispose();
((Timer)e.getSource()).stop();
});
notificationTimer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new BatteryConservationController().setVisible(true);
});
}
}