/actionslib/src/com/google/android/marvin/commands/impls/BatteryLevelCommand.java

http://eyes-free.googlecode.com/ · Java · 38 lines · 25 code · 6 blank · 7 comment · 4 complexity · 74f738c63c3c9ac41b1a92e7fbf22a8d MD5 · raw file

  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. package com.google.android.marvin.commands.impls;
  3. import com.google.android.marvin.actionslib.R;
  4. import com.google.android.marvin.commands.CommandExecutor;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.os.BatteryManager;
  9. /**
  10. * A command to speak the current battery level.
  11. *
  12. * @author clsimon@google.com (Cheryl Simon)
  13. *
  14. */
  15. public class BatteryLevelCommand implements CommandExecutor {
  16. public String executeCommand(Context context) {
  17. IntentFilter battFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  18. Intent intent = context.getApplicationContext().registerReceiver(null, battFilter);
  19. int rawlevel = intent.getIntExtra("level", -1);
  20. int scale = intent.getIntExtra("scale", -1);
  21. int status = intent.getIntExtra("status", -1);
  22. String message = "";
  23. if (rawlevel >= 0 && scale > 0) {
  24. int batteryLevel = (rawlevel * 100) / scale;
  25. message = Integer.toString(batteryLevel) + "%";
  26. }
  27. if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
  28. message = message + " " + context.getString(R.string.charging);
  29. }
  30. return message;
  31. }
  32. }