frameworks/base
修訂 | e0cf1cfb2ff03a995e9be86007f87326e71fcc88 (tree) |
---|---|
時間 | 2011-08-05 17:26:22 |
作者 | Yi Sun <beyounn@gmai...> |
Commiter | Chih-Wei Huang |
Add battery status support for PC and VMs
@@ -445,7 +445,9 @@ class BatteryService extends Binder { | ||
445 | 445 | mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) { |
446 | 446 | return com.android.internal.R.drawable.stat_sys_battery; |
447 | 447 | } else { |
448 | - return com.android.internal.R.drawable.stat_sys_battery_unknown; | |
448 | + return mBatteryPresent ? | |
449 | + com.android.internal.R.drawable.stat_sys_battery_unknown : | |
450 | + com.android.internal.R.drawable.gpm_ac_adapter; | |
449 | 451 | } |
450 | 452 | } |
451 | 453 |
@@ -77,6 +77,8 @@ struct PowerSupplyPaths { | ||
77 | 77 | char* batteryHealthPath; |
78 | 78 | char* batteryPresentPath; |
79 | 79 | char* batteryCapacityPath; |
80 | + char* batteryChargeNowPath; | |
81 | + char* batteryChargeFullPath; | |
80 | 82 | char* batteryVoltagePath; |
81 | 83 | char* batteryTemperaturePath; |
82 | 84 | char* batteryTechnologyPath; |
@@ -194,14 +196,44 @@ static void setVoltageField(JNIEnv* env, jobject obj, const char* path, jfieldID | ||
194 | 196 | env->SetIntField(obj, fieldID, value); |
195 | 197 | } |
196 | 198 | |
199 | +static void setChargeLevel(JNIEnv* env, jobject obj, jfieldID fieldID) { | |
200 | + float now,full; | |
201 | + int value = 0; | |
202 | + char buf[128]; | |
203 | + | |
204 | + if (readFromFile(gPaths.batteryChargeNowPath, buf, 128) > 0 ) { | |
205 | + now = atoi(buf); | |
206 | + if (readFromFile(gPaths.batteryChargeFullPath, buf, 128) > 0 ) { | |
207 | + full = atoi(buf); | |
208 | + value = (int)((now/full) * 100); | |
209 | + } else { | |
210 | + value = now; | |
211 | + } | |
212 | + } | |
213 | + env->SetIntField(obj, fieldID, value); | |
214 | +} | |
197 | 215 | |
198 | 216 | static void android_server_BatteryService_update(JNIEnv* env, jobject obj) |
199 | 217 | { |
200 | 218 | setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline); |
201 | 219 | setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline); |
202 | 220 | setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent); |
221 | + if ( !gPaths.acOnlinePath && !gPaths.usbOnlinePath && !gPaths.batteryPresentPath) { | |
222 | + /* most likely, we have a PC here */ | |
223 | + env->SetBooleanField(obj, gFieldIds.mAcOnline, true); | |
224 | + } | |
203 | 225 | |
204 | - setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel); | |
226 | + if (gPaths.batteryPresentPath) { | |
227 | + if (gPaths.batteryCapacityPath) { | |
228 | + setIntField(env, obj, gPaths.batteryCapacityPath, | |
229 | + gFieldIds.mBatteryLevel); | |
230 | + } else { | |
231 | + setChargeLevel(env,obj, gFieldIds.mBatteryLevel); | |
232 | + } | |
233 | + } else { | |
234 | + /*This is a PC or VM, faking the level to full, we are on AC anyway */ | |
235 | + env->SetIntField(obj, gFieldIds.mBatteryLevel, 100); | |
236 | + } | |
205 | 237 | setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage); |
206 | 238 | setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature); |
207 | 239 |
@@ -226,6 +258,7 @@ static JNINativeMethod sMethods[] = { | ||
226 | 258 | {"native_update", "()V", (void*)android_server_BatteryService_update}, |
227 | 259 | }; |
228 | 260 | |
261 | + | |
229 | 262 | int register_android_server_BatteryService(JNIEnv* env) |
230 | 263 | { |
231 | 264 | char path[PATH_MAX]; |
@@ -273,8 +306,31 @@ int register_android_server_BatteryService(JNIEnv* env) | ||
273 | 306 | if (access(path, R_OK) == 0) |
274 | 307 | gPaths.batteryPresentPath = strdup(path); |
275 | 308 | snprintf(path, sizeof(path), "%s/%s/capacity", POWER_SUPPLY_PATH, name); |
276 | - if (access(path, R_OK) == 0) | |
309 | + if (access(path, R_OK) == 0) { | |
277 | 310 | gPaths.batteryCapacityPath = strdup(path); |
311 | + } else { | |
312 | + snprintf(path, sizeof(path), "%s/%s/charge_now", | |
313 | + POWER_SUPPLY_PATH, name); | |
314 | + if (access(path, R_OK) == 0) { | |
315 | + gPaths.batteryChargeNowPath = strdup(path); | |
316 | + snprintf(path, sizeof(path), "%s/%s/charge_full", | |
317 | + POWER_SUPPLY_PATH, name); | |
318 | + if (access(path, R_OK) == 0) { | |
319 | + gPaths.batteryChargeFullPath = strdup(path); | |
320 | + } | |
321 | + } else { | |
322 | + snprintf(path, sizeof(path), "%s/%s/energy_now", | |
323 | + POWER_SUPPLY_PATH, name); | |
324 | + if (access(path, R_OK) == 0) { | |
325 | + gPaths.batteryChargeNowPath = strdup(path); | |
326 | + snprintf(path, sizeof(path), "%s/%s/energy_full", | |
327 | + POWER_SUPPLY_PATH, name); | |
328 | + if (access(path, R_OK) == 0) { | |
329 | + gPaths.batteryChargeFullPath = strdup(path); | |
330 | + } | |
331 | + } | |
332 | + } | |
333 | + } | |
278 | 334 | |
279 | 335 | snprintf(path, sizeof(path), "%s/%s/voltage_now", POWER_SUPPLY_PATH, name); |
280 | 336 | if (access(path, R_OK) == 0) { |
@@ -314,7 +370,8 @@ int register_android_server_BatteryService(JNIEnv* env) | ||
314 | 370 | LOGE("batteryHealthPath not found"); |
315 | 371 | if (!gPaths.batteryPresentPath) |
316 | 372 | LOGE("batteryPresentPath not found"); |
317 | - if (!gPaths.batteryCapacityPath) | |
373 | + if (!gPaths.batteryCapacityPath && (!gPaths.batteryChargeNowPath || | |
374 | + !gPaths.batteryChargeFullPath)) | |
318 | 375 | LOGE("batteryCapacityPath not found"); |
319 | 376 | if (!gPaths.batteryVoltagePath) |
320 | 377 | LOGE("batteryVoltagePath not found"); |