My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package nl.steffann.darkkeys;

import java.io.FileReader;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.IBinder;
import android.os.IHardwareService;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.Toast;

public class DarkKeysService extends Service
{
static String KEY_BL_STATE = "KeyboardBacklightState";
private WakeLock wakeLock;
private IHardwareService hardware;
private SharedPreferences mPrefs;

@Override
public void onCreate()
{
super.onCreate();

// Get the hardware service
this.hardware = IHardwareService.Stub.asInterface(ServiceManager.getService("hardware"));

// Get the wake lock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "DarkKeys");

// Init saved preferences
this.mPrefs = this.getSharedPreferences("NUkaartPrefs", 0);

// Pretend that the configuration just changed
Configuration config = this.getResources().getConfiguration();
this.onConfigurationChanged(config);

// Done
Log.i("DarkKeys", "Dark Keys service created");
}

@Override
public void onDestroy()
{
super.onDestroy();

// Release the wake lock
if (this.wakeLock.isHeld())
this.wakeLock.release();

// Done
Log.i("DarkKeys", "Dark Keys service destroyed");
}

private final IDarkKeysService.Stub mBinder = new IDarkKeysService.Stub()
{
public void setKeyboardBacklight( boolean state )
throws RemoteException
{
Log.d("DarkKeys", "Service request: set: "
+ (state ? "on" : "off"));
DarkKeysService.this.setKeyboardBacklight(state);
}

public boolean getKeyboardBacklight() throws RemoteException
{
Log.d("DarkKeys", "Service request: get");
return DarkKeysService.this.getKeyboardBacklight();
}

public void toggleKeyboardBacklight() throws RemoteException
{
Log.d("DarkKeys", "Service request: toggle");
DarkKeysService.this.toggleKeyboardBacklight();
}
};

@Override
public IBinder onBind( Intent arg0 )
{
return mBinder;
}

@Override
public void onConfigurationChanged( Configuration newConfig )
{
boolean lastState;

switch (newConfig.keyboardHidden)
{
case Configuration.KEYBOARDHIDDEN_NO:
// Keyboard is not hidden: activate wake lock
Log.d("DarkKeys", "Acquire full wake lock");
if (!this.wakeLock.isHeld())
this.wakeLock.acquire();

// Restore to last state
lastState = mPrefs.getBoolean(KEY_BL_STATE, false);
Log.i("DarkKeys", "Restoring keyboard backlight state to: " + (lastState ? "ON" : "OFF"));
setKeyboardBacklight(lastState);
break;

case Configuration.KEYBOARDHIDDEN_YES:
// Keyboard is hidden: release wake lock
Log.d("DarkKeys", "Release full wake lock");
if (this.wakeLock.isHeld())
this.wakeLock.release();

// Save the last state
lastState = getKeyboardBacklight();
Log.i("DarkKeys", "Saving keyboard backlight state as: " + (lastState ? "ON" : "OFF"));
Editor editPrefs = mPrefs.edit();
editPrefs.putBoolean(KEY_BL_STATE, lastState);
editPrefs.commit();
break;
}
}

public boolean getKeyboardBacklight()
{
// Read the current state of the back light
boolean state = false;

try
{
FileReader brightnessFile = new FileReader("/sys/class/leds/keyboard-backlight/brightness");
int brightness = brightnessFile.read();
brightnessFile.close();

// Check if first char of brightnessFile == '0'
state = (brightness != '0');
} catch (Exception e)
{
Log.e("DarkKeys", "Can't read keyboard backlight state", e);
}

Log.d("DarkKeys", "Current keyboard backlight state is: " + (state ? "ON" : "OFF"));
return state;
}

public void setKeyboardBacklight( boolean state )
{
Log.d("DarkKeys", "Request to set keyboard backlight to: " + (state ? "ON" : "OFF"));

Configuration config = this.getResources().getConfiguration();
if (config.keyboardHidden != Configuration.KEYBOARDHIDDEN_NO)
{
// Keyboard is not shown: don't change the backlight
Toast.makeText(this, "The keyboard backlight can only be switched on/off when the keyboard is visible",
Toast.LENGTH_LONG).show();
return;
}

// Don't do anything when the state is already as requested
if (this.getKeyboardBacklight() == state)
{
Log.d("DarkKeys", "Nothing to change");
return;
}

Log.i("DarkKeys", "Set keyboard backlight to: " + (state ? "ON" : "OFF"));
try
{
// Set the state
hardware.setKeyboardBacklight(state);

Toast.makeText(this, "Keyboard backlight switched " + (state ? "on" : "off"), Toast.LENGTH_SHORT).show();
} catch (Exception e)
{
Log.e("DarkKeys", "Sorry, doesn't seem to work", e);
}
}

public void toggleKeyboardBacklight()
{
boolean state = this.getKeyboardBacklight();
this.setKeyboardBacklight(!state);
}
}

Change log

r2 by maxandroid on May 1, 2009   Diff
initial import
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 6166 bytes, 187 lines
Powered by Google Project Hosting