/**
  * Device Connectにイベントを送信する.
  *
  * @param event イベントパラメータ
  * @param bundle パラメータ
  * @return 送信成功の場合true、アクセストークンエラーの場合はfalseを返す。
  */
 public final boolean sendEvent(final Event event, final Bundle bundle) {
   Intent intent = EventManager.createEventMessage(event);
   Bundle original = intent.getExtras();
   original.putAll(bundle);
   intent.putExtras(original);
   return sendEvent(intent, event.getAccessToken());
 }
        @Override
        public void onReceive(final Context context, final Intent intent) {
          String action = intent.getAction();
          if (Intent.ACTION_POWER_CONNECTED.equals(action)
              || Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
            // バッテリーが充電された時
            mHostBatteryManager.setBatteryRequest(intent);
            List<Event> events =
                EventManager.INSTANCE.getEventList(
                    mServiceId,
                    HostBatteryProfile.PROFILE_NAME,
                    null,
                    HostBatteryProfile.ATTRIBUTE_ON_CHARGING_CHANGE);

            for (int i = 0; i < events.size(); i++) {
              Event event = events.get(i);
              Intent mIntent = EventManager.createEventMessage(event);
              HostBatteryProfile.setAttribute(
                  mIntent, HostBatteryProfile.ATTRIBUTE_ON_CHARGING_CHANGE);
              Bundle charging = new Bundle();
              if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
                HostBatteryProfile.setCharging(charging, true);
              } else {
                HostBatteryProfile.setCharging(charging, false);
              }
              HostBatteryProfile.setBattery(mIntent, charging);
              sendEvent(mIntent, event.getAccessToken());
            }
          }
        }
        @Override
        public void onReceive(final Context context, final Intent intent) {
          String action = intent.getAction();
          if (Intent.ACTION_BATTERY_CHANGED.equals(action)
              || Intent.ACTION_BATTERY_LOW.equals(action)
              || Intent.ACTION_BATTERY_OKAY.equals(action)) {
            // バッテリーが変化した時
            mHostBatteryManager.setBatteryRequest(intent);
            List<Event> events =
                EventManager.INSTANCE.getEventList(
                    mServiceId,
                    HostBatteryProfile.PROFILE_NAME,
                    null,
                    HostBatteryProfile.ATTRIBUTE_ON_BATTERY_CHANGE);

            for (int i = 0; i < events.size(); i++) {
              Event event = events.get(i);
              Intent mIntent = EventManager.createEventMessage(event);
              HostBatteryProfile.setAttribute(
                  mIntent, HostBatteryProfile.ATTRIBUTE_ON_BATTERY_CHANGE);
              Bundle battery = new Bundle();
              double level =
                  ((double) (mHostBatteryManager.getBatteryLevel())) / ((double) getBatteryScale());
              HostBatteryProfile.setLevel(battery, level);
              HostBatteryProfile.setBattery(mIntent, battery);
              sendEvent(mIntent, event.getAccessToken());
            }
          }
        }
 private void sendOnRecordingChangeEvent(
     final String serviceId, final ThetaDevice.Recorder recorder, final RecordingState state) {
   List<Event> events = getOnRecordingChangeEventList(serviceId);
   mLogger.info("Send onrecordingchange events: " + events.size());
   for (Iterator<Event> it = events.iterator(); it.hasNext(); ) {
     Event event = it.next();
     Intent message = EventManager.createEventMessage(event);
     Bundle media = new Bundle();
     setStatus(media, state);
     setMIMEType(media, recorder.getMimeType());
     setMedia(message, media);
     getService().sendEvent(message, event.getAccessToken());
   }
 }
 private void sendOnPhotoEvent(
     final String serviceId, final String mimeType, final String uri, final String path) {
   List<Event> events = getOnPhotoEventList(serviceId);
   mLogger.info("Send onphoto events: " + events.size());
   for (Iterator<Event> it = events.iterator(); it.hasNext(); ) {
     Event event = it.next();
     Intent message = EventManager.createEventMessage(event);
     Bundle photoInfo = new Bundle();
     setUri(photoInfo, uri);
     setPath(photoInfo, path);
     setMIMEType(photoInfo, mimeType);
     setPhoto(message, photoInfo);
     getService().sendEvent(message, event.getAccessToken());
   }
 }
  /**
   * 状態変化のイベントを通知.
   *
   * @param status ステータス
   */
  public void sendOnStatusChangeEvent(final String status) {

    if (mOnStatusChangeEventFlag) {
      List<Event> events =
          EventManager.INSTANCE.getEventList(
              mServiceId,
              MediaPlayerProfile.PROFILE_NAME,
              null,
              MediaPlayerProfile.ATTRIBUTE_ON_STATUS_CHANGE);

      AudioManager manager =
          (AudioManager) this.getContext().getSystemService(Context.AUDIO_SERVICE);

      double maxVolume = 1;
      double mVolume = 0;

      mVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
      maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

      double mVolumeValue = mVolume / maxVolume;

      for (int i = 0; i < events.size(); i++) {

        Event event = events.get(i);
        Intent intent = EventManager.createEventMessage(event);

        MediaPlayerProfile.setAttribute(intent, MediaPlayerProfile.ATTRIBUTE_ON_STATUS_CHANGE);
        Bundle mediaPlayer = new Bundle();
        MediaPlayerProfile.setStatus(mediaPlayer, status);
        MediaPlayerProfile.setMediaId(mediaPlayer, mMyCurrentFilePath);
        MediaPlayerProfile.setMIMEType(mediaPlayer, mMyCurrentFileMIMEType);
        MediaPlayerProfile.setPos(mediaPlayer, mMyCurrentMediaPosition / UNIT_SEC);
        MediaPlayerProfile.setVolume(mediaPlayer, mVolumeValue);
        MediaPlayerProfile.setMediaPlayer(intent, mediaPlayer);
        sendEvent(intent, event.getAccessToken());
      }
    }
  }
  @Override
  public void onSensorChanged(final SensorEvent sensorEvent) {
    processSensorData(sensorEvent);

    if (mIsAccellReady.get() && mIsGravityReady.get() && mIsGyroReady.get()) {
      mAccelLastTime = System.currentTimeMillis();

      Bundle orientation = createOrientation();

      List<Event> events =
          EventManager.INSTANCE.getEventList(
              mServiceId,
              DeviceOrientationProfile.PROFILE_NAME,
              null,
              DeviceOrientationProfile.ATTRIBUTE_ON_DEVICE_ORIENTATION);

      for (int i = 0; i < events.size(); i++) {
        Event event = events.get(i);
        Intent intent = EventManager.createEventMessage(event);
        intent.putExtra(DeviceOrientationProfile.PARAM_ORIENTATION, orientation);
        getContext().sendBroadcast(intent);
      }
    }
  }
  @Override
  public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null) {
      return START_STICKY;
    }

    String action = intent.getAction();
    if (CameraOverlay.DELETE_PREVIEW_ACTION.equals(action)) {
      if (mCameraOverlay.isShow()) {
        stopWebServer();
      }
      return START_STICKY;
    } else if ("android.intent.action.NEW_OUTGOING_CALL".equals(action)) {
      // Phone
      List<Event> events =
          EventManager.INSTANCE.getEventList(
              mServiceId,
              HostPhoneProfile.PROFILE_NAME,
              null,
              HostPhoneProfile.ATTRIBUTE_ON_CONNECT);

      for (int i = 0; i < events.size(); i++) {
        Event event = events.get(i);
        Intent mIntent = EventManager.createEventMessage(event);
        HostPhoneProfile.setAttribute(mIntent, HostPhoneProfile.ATTRIBUTE_ON_CONNECT);
        Bundle phoneStatus = new Bundle();
        HostPhoneProfile.setPhoneNumber(
            phoneStatus, intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
        HostPhoneProfile.setState(phoneStatus, CallState.START);
        HostPhoneProfile.setPhoneStatus(mIntent, phoneStatus);
        sendEvent(mIntent, event.getAccessToken());
      }
      return START_STICKY;
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)
        || WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
      // Wifi
      List<Event> events =
          EventManager.INSTANCE.getEventList(
              mServiceId,
              HostConnectProfile.PROFILE_NAME,
              null,
              HostConnectProfile.ATTRIBUTE_ON_WIFI_CHANGE);

      for (int i = 0; i < events.size(); i++) {
        Event event = events.get(i);
        Intent mIntent = EventManager.createEventMessage(event);
        HostConnectProfile.setAttribute(mIntent, HostConnectProfile.ATTRIBUTE_ON_WIFI_CHANGE);
        Bundle wifiConnecting = new Bundle();
        WifiManager wifiMgr = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        HostConnectProfile.setEnable(wifiConnecting, wifiMgr.isWifiEnabled());
        HostConnectProfile.setConnectStatus(mIntent, wifiConnecting);
        sendEvent(mIntent, event.getAccessToken());
      }
      return START_STICKY;
    } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
      List<Event> events =
          EventManager.INSTANCE.getEventList(
              mServiceId,
              HostConnectProfile.PROFILE_NAME,
              null,
              HostConnectProfile.ATTRIBUTE_ON_BLUETOOTH_CHANGE);

      for (int i = 0; i < events.size(); i++) {
        Event event = events.get(i);
        Intent mIntent = EventManager.createEventMessage(event);
        HostConnectProfile.setAttribute(mIntent, HostConnectProfile.ATTRIBUTE_ON_BLUETOOTH_CHANGE);
        Bundle bluetoothConnecting = new Bundle();
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        HostConnectProfile.setEnable(bluetoothConnecting, mBluetoothAdapter.isEnabled());
        HostConnectProfile.setConnectStatus(mIntent, bluetoothConnecting);
        sendEvent(mIntent, event.getAccessToken());
      }
      return START_STICKY;
    }
    return super.onStartCommand(intent, flags, startId);
  }