@Override
  public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    // add digital watch check

    if (action.equals("org.metawatch.manager.APPLICATION_UPDATE")) {
      // boolean dither = false;
      // if (intent.hasExtra("requires_dither"))
      //	dither = true;
      if (intent.hasExtra("array")) {
        int[] array = intent.getIntArrayExtra("array");
        Application.updateAppMode(context, array);
      } else if (intent.hasExtra("buffer")) {
        byte[] buffer = intent.getByteArrayExtra("buffer");
        Application.updateAppMode(context, buffer);
      }
      return;
    }

    if (action.equals("org.metawatch.manager.WIDGET_UPDATE")) {
      Log.d(MetaWatch.TAG, "WIDGET_UPDATE received");
      WidgetManager.getFromIntent(context, intent);
      return;
    }

    if (action.equals("org.metawatch.manager.APPLICATION_START")) {
      Application.startAppMode();
      return;
    }

    if (action.equals("org.metawatch.manager.APPLICATION_STOP")) {
      Application.stopAppMode(context);
      return;
    }

    if (action.equals("org.metawatch.manager.NOTIFICATION")) {

      /* Set up vibrate pattern. */
      VibratePattern vibrate = getVibratePatternFromIntent(intent);

      if (intent.hasExtra("oled1")
          || intent.hasExtra("oled1a")
          || intent.hasExtra("oled1b")
          || intent.hasExtra("oled2")
          || intent.hasExtra("oled2a")
          || intent.hasExtra("oled2b")) {

        byte[] line1 = Protocol.createOled1line(context, null, "");
        byte[] line2 = Protocol.createOled1line(context, null, "");
        byte[] scroll = null;
        int scrollLen = 0;
        if (intent.hasExtra("oled1")) {
          line1 = Protocol.createOled1line(context, null, intent.getStringExtra("oled1"));
        } else {
          if (intent.hasExtra("oled1a") || intent.hasExtra("oled1b")) {
            String oled1a = "";
            String oled1b = "";
            if (intent.hasExtra("oled1a")) {
              oled1a = intent.getStringExtra("oled1a");
            }
            if (intent.hasExtra("oled1b")) {
              oled1b = intent.getStringExtra("oled1b");
            }
            line1 = Protocol.createOled2lines(context, oled1a, oled1b);
          }
        }
        if (intent.hasExtra("oled2")) {
          line2 = Protocol.createOled1line(context, null, intent.getStringExtra("oled2"));
        } else {
          if (intent.hasExtra("oled2a") || intent.hasExtra("oled2b")) {
            String oled2a = "";
            String oled2b = "";
            if (intent.hasExtra("oled2a")) {
              oled2a = intent.getStringExtra("oled2a");
            }
            if (intent.hasExtra("oled2b")) {
              oled2b = intent.getStringExtra("oled2b");
            }
            scroll = new byte[800];
            scrollLen = Protocol.createOled2linesLong(context, oled2b, scroll);
            line2 = Protocol.createOled2lines(context, oled2a, oled2b);
          }
        }
        Notification.addOledNotification(context, line1, line2, scroll, scrollLen, vibrate);

      } else if (intent.hasExtra("text")) {
        String text = intent.getStringExtra("text");
        Notification.addTextNotification(
            context, text, vibrate, Notification.getDefaultNotificationTimeout(context));
        if (Preferences.logging)
          Log.d(
              MetaWatch.TAG,
              "ApiIntentReceiver.onReceive(): sending text notification; text='" + text + "'");
      } else if (intent.hasExtra("array")) {
        int[] array = intent.getIntArrayExtra("array");
        Notification.addArrayNotification(context, array, vibrate);
      } else if (intent.hasExtra("buffer")) {
        byte[] buffer = intent.getByteArrayExtra("buffer");
        Notification.addBufferNotification(context, buffer, vibrate);
      }
      return;
    }

    if (action.equals("org.metawatch.manager.VIBRATE")) {
      /* Set up vibrate pattern. */
      VibratePattern vibrate = getVibratePatternFromIntent(intent);

      if (vibrate.vibrate) Protocol.vibrate(vibrate.on, vibrate.off, vibrate.cycles);

      return;
    }
  }
  void pressedButton(int button) {
    if (Preferences.logging) Log.d(MetaWatch.TAG, "button code: " + Integer.toString(button));

    wakeLock.acquire(10000);

    try {

      if (button > 0 && Preferences.hapticFeedback) Protocol.vibrate(5, 5, 2);

      if (Preferences.logging)
        Log.d(MetaWatch.TAG, "MetaWatchService.pressedButton(): watchState=" + watchState);
      switch (watchState) {
        case WatchStates.IDLE:
          {
            int idleAppButton = Idle.appButtonPressed(this, button);
            if (idleAppButton == ApplicationBase.BUTTON_NOT_USED) {

              switch (button) {
                case Idle.QUICK_BUTTON:
                  Idle.quickButtonAction(this);
                  break;

                case Idle.IDLE_NEXT_PAGE:
                  if (MetaWatchService.watchType == MetaWatchService.WatchType.DIGITAL) {
                    Idle.nextPage(this);
                    Idle.updateIdle(this, true);
                  }
                  break;

                case Idle.TOGGLE_SILENT:
                  MetaWatchService.setSilentMode(!silentMode);
                  Protocol.vibrate(500, 500, 2);
                  break;

                case Idle.IDLE_OLED_DISPLAY:
                  long time = System.currentTimeMillis();

                  if (time - lastOledCrownPress < 1000 * 5) {
                    Idle.nextPage(this);
                    Idle.updateIdle(this, true);
                  }

                  lastOledCrownPress = time;
                  Idle.sendOledIdle(this);
                  break;

                case Application.TOGGLE_APP:
                  Application.toggleApp(context, Idle.getCurrentApp());
                  break;
              }
            } else if (idleAppButton != ApplicationBase.BUTTON_USED_DONT_UPDATE) {
              Idle.updateIdle(this, false);
              if (MetaWatchService.watchType == MetaWatchService.WatchType.ANALOG)
                Idle.sendOledIdle(this);
            }
            break;
          }

        case WatchStates.APPLICATION:
          Application.buttonPressed(this, button);
          break;

        case WatchStates.NOTIFICATION:
          switch (button) {
            case Call.CALL_ANSWER:
              MediaControl.answerCall(this);
              break;
            case Call.CALL_DISMISS:
              MediaControl.ignoreCall(this);
              break;
            case Call.CALL_MENU:
              ActionManager.displayCallActions(this);
              break;
            default:
              Notification.buttonPressed(button);
              break;
          }
          break;
      }

    } finally {
      if (wakeLock != null && wakeLock.isHeld()) {
        wakeLock.release();
      }
    }
  }