PageRenderTime 1470ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/cyanogenmod/ListenSwitch.java

https://bitbucket.org/cyanogenmod/android_packages_apps_dockaudio
Java | 70 lines | 55 code | 10 blank | 5 comment | 9 complexity | 2d1e077701b9bf59304efdc1a539b38f MD5 | raw file
  1. package com.cyanogenmod.dockaudio;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.os.UEventObserver;
  6. import android.util.Log;
  7. public class ListenSwitch extends Service {
  8. private static final String LOG_TAG = "DockAudio";
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. return null;
  12. }
  13. @Override
  14. public void onCreate() {
  15. mUEventObserver.startObserving("DEVPATH=/devices/virtual/switch/smartdock");
  16. mUEventObserver.startObserving("DEVPATH=/devices/virtual/switch/emuconn");
  17. Log.i(LOG_TAG, "Dock Audio service started");
  18. }
  19. @Override
  20. public int onStartCommand(Intent intent, int flags, int startId) {
  21. Log.i(LOG_TAG, "Received start id " + startId + ": " + intent);
  22. // We want this service to continue running until it is explicitly
  23. // stopped, so return sticky.
  24. return START_STICKY;
  25. }
  26. @Override
  27. public void onDestroy() {
  28. Log.i(LOG_TAG, "Dock Audio service stopped");
  29. }
  30. /*
  31. * Listens for uevent messages from the kernel
  32. */
  33. private final UEventObserver mUEventObserver = new UEventObserver() {
  34. @Override
  35. public void onUEvent(UEventObserver.UEvent event) {
  36. Log.i(LOG_TAG, "DockAudio UEVENT: " + event.toString());
  37. String name = "";
  38. int state = 0;
  39. try {
  40. name = event.get("SWITCH_NAME");
  41. state = Integer.parseInt(event.get("SWITCH_STATE"));
  42. } catch (NumberFormatException e) {
  43. Log.e(LOG_TAG, "Error parsing switch state!");
  44. }
  45. Intent intent = new Intent();
  46. if (0 == state) { //No Device
  47. intent.setAction("com.cyanogenmod.dockaudio.ENABLE_SPEAKER_AUDIO");
  48. Log.i(LOG_TAG, "ENABLE_SPEAKER_AUDIO");
  49. } else if (!"smartdock".equals(name) && (1 == state || 2 == state)) { // Mono out or Stereo out
  50. intent.setAction("com.cyanogenmod.dockaudio.ENABLE_ANALOG_AUDIO");
  51. Log.i(LOG_TAG, "ENABLE_ANALOG_AUDIO");
  52. } else { //SPDIF audio out, smartdock ("lapdock")
  53. intent.setAction("com.cyanogenmod.dockaudio.ENABLE_DIGITAL_AUDIO");
  54. Log.i(LOG_TAG, "ENABLE_DIGITAL_AUDIO");
  55. }
  56. sendBroadcast(intent);
  57. Log.i(LOG_TAG, "Broadcasted intent for state " + state);
  58. }
  59. };
  60. }