/** Creates the notification. */
  @SuppressLint("NewApi")
  private Builder buildNotification() {
    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), options.getIcon());
    Uri sound = options.getSound();

    Builder notification =
        new Notification.Builder(context)
            .setContentTitle(options.getTitle())
            .setContentText(options.getMessage())
            .setNumber(options.getBadge())
            .setTicker(options.getMessage())
            .setSmallIcon(options.getSmallIcon())
            .setLargeIcon(icon)
            .setAutoCancel(options.getAutoCancel())
            .setOngoing(options.getOngoing());

    if (sound != null) {
      notification.setSound(sound);
    }

    if (Build.VERSION.SDK_INT > 16) {
      notification.setStyle(new Notification.BigTextStyle().bigText(options.getMessage()));
    }

    setClickEvent(notification);

    return notification;
  }
 protected void updateNotification() {
   if (mCarInterface != null
       && mCarInterface.getsStatus() == ElmInterface.STATUS_OPEN_MONITORING) {
     mNoticeBuilder.setContentText(getString(R.string.msg_monitoring));
   } else {
     mNoticeBuilder.setContentText(getString(R.string.msg_monitoring_stopped));
   }
   mNoticeManager.notify(mNoticeID, mNoticeBuilder.build());
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   playerController =
       (PlayerController)
           context
               .getApplicationContext()
               .getSystemService(ActiveMtlApplication.PLAYER_CONTROLLER);
   List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);
   int transition = LocationClient.getGeofenceTransition(intent);
   Log.i(
       GeofenceTransitionReceiver.class.getSimpleName(),
       "onReceive transition : "
           + (transition == Geofence.GEOFENCE_TRANSITION_ENTER ? "Enter" : "Exit"));
   String title = "";
   String content = "";
   Builder builder =
       new Notification.Builder(context)
           .setSmallIcon(R.drawable.icon)
           .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))
           .setDefaults(Notification.DEFAULT_ALL)
           .setContentIntent(HomeActivity.newPendingIntent(context, mId))
           .setAutoCancel(true);
   NotificationManager mNotificationManager =
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   if (transition == Geofence.GEOFENCE_TRANSITION_ENTER
       && !PreferenceHelper.isCheckedIn(context)) {
     title = context.getString(R.string.entering_court_zone);
     content = context.getString(R.string.you_just_entered_a_court);
     for (Geofence geofence : geofences) {
       playerController.checkInCourt(geofence.getRequestId(), addPlayerCallback);
     }
     PreferenceHelper.checkIn(context);
     builder.setContentTitle(title).setContentText(content);
     mNotificationManager.notify(mId, builder.build());
   } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT
       && PreferenceHelper.isCheckedIn(context)) {
     title = context.getString(R.string.leaving_court_zone);
     content = context.getString(R.string.you_just_leaved_a_court);
     for (Geofence geofence : geofences) {
       playerController.leaveCourt(geofence.getRequestId(), deletePlayerCallback);
     }
     PreferenceHelper.leaveCourt(context);
     builder.setContentTitle(title).setContentText(content);
     mNotificationManager.notify(mId, builder.build());
   }
   // mId allows you to update the notification later on.
 }
  @Override
  public void onCreate() {
    IntentFilter filterUsbDetached = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbDetachedReceiver, filterUsbDetached);

    Intent settingsIntent = new Intent(this, SteeringWheelInterfaceActivity.class);
    settingsIntent.setAction(Intent.ACTION_EDIT);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(SteeringWheelInterfaceActivity.class);
    stackBuilder.addNextIntent(settingsIntent);
    PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mNoticeBuilder.setContentIntent(resultPendingIntent);
    mNoticeBuilder.setSmallIcon(R.drawable.ic_notice);
    mNoticeBuilder.setContentTitle(getString(R.string.app_name));
    mNoticeBuilder.setContentText(getString(R.string.msg_app_starting));

    mNoticeManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNoticeManager.notify(mNoticeID, mNoticeBuilder.build());

    mCarInterface = new ElmInterface(getApplicationContext());
    mCarInterface.deviceOpenEvent_AddListener(mDeviceOpenListener);

    SharedPreferences settings =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    String baudDefault = getString(R.string.scantool_baud);
    int baudValue = Integer.parseInt(settings.getString("scantool_baud", baudDefault));
    mCarInterface.setBaudRate(baudValue);

    String deviceNumDefault = getString(R.string.scantool_device_number);
    int deviceNumValue =
        Integer.parseInt(settings.getString("scantool_device_number", deviceNumDefault));
    mCarInterface.setDeviceNumber(deviceNumValue);

    String protocolCommandDefault = getString(R.string.scantool_protocol);
    String protocolCommandValue = settings.getString("scantool_protocol", protocolCommandDefault);
    mCarInterface.setProtocolCommand(protocolCommandValue);

    String monitorCommandDefault = getString(R.string.scantool_monitor_command);
    String monitorCommandValue =
        settings.getString("scantool_monitor_command", monitorCommandDefault);
    mCarInterface.setMonitorCommand(monitorCommandValue);
  }
  private void sendNotification(NearbyPlayer nearbyPlayer) {

    Intent intent = new Intent(context, actionToNotificate);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pIntent =
        PendingIntent.getActivity(context, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Builder builder = new Builder(context);

    switch (nearbyPlayer.getFaction()) {
      case blue:
        builder.setSmallIcon(R.drawable.blue_small);
        break;
      case green:
        builder.setSmallIcon(R.drawable.green_small);
        break;
    }

    builder.setContentIntent(pIntent);
    builder.setContentTitle(nearbyPlayer.getName());
    builder.setContentInfo(
        nearbyPlayer.getHumanReadableDistance()
            + " "
            + nearbyPlayer.getDirection()
            + " "
            + nearbyPlayer.getHumanReadableTime()
            + " ago on "
            + nearbyPlayer.getLocation());

    Notification noti = builder.build();
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, noti);

    this.lastNotification = nearbyPlayer;
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(mNoticeID, mNoticeBuilder.build());

    carInterfaceRestartIfNeeded();

    watchdog_TimerReStart();

    return START_STICKY;
  }
  /** Shows the notification */
  @SuppressWarnings("deprecation")
  @SuppressLint("NewApi")
  private void showNotification(Builder notification) {
    NotificationManager mgr =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int id = 0;

    try {
      id = Integer.parseInt(options.getId());
    } catch (Exception e) {
    }

    if (Build.VERSION.SDK_INT < 16) {
      // build notification for HoneyComb to ICS
      mgr.notify(id, notification.getNotification());
    } else if (Build.VERSION.SDK_INT > 15) {
      // Notification for Jellybean and above
      mgr.notify(id, notification.build());
    }
  }
  /** Adds an onclick handler to the notification */
  private Builder setClickEvent(Builder notification) {
    Intent intent =
        new Intent(context, ReceiverActivity.class)
            .putExtra(OPTIONS, options.getJSONObject().toString())
            .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    int requestCode = new Random().nextInt();

    PendingIntent contentIntent =
        PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    return notification.setContentIntent(contentIntent);
  }