Beispiel #1
0
 public static AlertPlayer getPlayer() {
   if (singletone == null) {
     Log.i(TAG, "getPlayer: Creating a new AlertPlayer");
     singletone = new AlertPlayer();
   } else {
     Log.i(TAG, "getPlayer: Using existing AlertPlayer");
   }
   return singletone;
 }
Beispiel #2
0
  private void Vibrate(
      Context ctx,
      AlertType alert,
      String bgValue,
      Boolean overrideSilent,
      int timeFromStartPlaying) {
    Log.d(TAG, "Vibrate called timeFromStartPlaying = " + timeFromStartPlaying);
    Log.d("ALARM", "setting vibrate alarm");
    int profile = getAlertProfile(ctx);

    // We use timeFromStartPlaying as a way to force vibrating/ non vibrating...
    if (profile != ALERT_PROFILE_ASCENDING) {
      // We start from the non ascending part...
      timeFromStartPlaying = MAX_ASCENDING;
    }

    String title = bgValue + " " + alert.name;
    String content = "BG LEVEL ALERT: " + bgValue;
    Intent intent = new Intent(ctx, SnoozeActivity.class);

    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(ctx)
            .setSmallIcon(R.drawable.ic_action_communication_invert_colors_on)
            .setContentTitle(title)
            .setContentText(content)
            .setContentIntent(notificationIntent(ctx, intent))
            .setDeleteIntent(snoozeIntent(ctx));
    if (profile != ALERT_PROFILE_VIBRATE_ONLY && profile != ALERT_PROFILE_SILENT) {
      if (timeFromStartPlaying >= MAX_VIBRATING) {
        // Before this, we only vibrate...
        float volumeFrac =
            (float) (timeFromStartPlaying - MAX_VIBRATING) / (MAX_ASCENDING - MAX_VIBRATING);
        volumeFrac = Math.min(volumeFrac, 1);
        if (profile == ALERT_PROFILE_MEDIUM) {
          volumeFrac = (float) 0.7;
        }
        Log.d(TAG, "Vibrate volumeFrac = " + volumeFrac);
        boolean isRingTone = EditAlertActivity.isPathRingtone(ctx, alert.mp3_file);
        if (isRingTone && !overrideSilent) {
          builder.setSound(Uri.parse(alert.mp3_file));
        } else {
          if (overrideSilent || isLoudPhone(ctx)) {
            PlayFile(ctx, alert.mp3_file, volumeFrac);
          }
        }
      }
    }
    if (profile != ALERT_PROFILE_SILENT && alert.vibrate) {
      builder.setVibrate(Notifications.vibratePattern);
    }
    NotificationManager mNotifyMgr =
        (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyMgr.cancel(Notifications.exportAlertNotificationId);
    mNotifyMgr.notify(Notifications.exportAlertNotificationId, builder.build());
  }
Beispiel #3
0
 public synchronized void PreSnooze(Context ctx, String uuid, int repeatTime) {
   Log.i(TAG, "PreSnooze called repeatTime = " + repeatTime);
   stopAlert(ctx, true, false);
   ActiveBgAlert.Create(uuid, true, new Date().getTime() + repeatTime * 60000);
   ActiveBgAlert activeBgAlert = ActiveBgAlert.getOnly();
   if (activeBgAlert == null) {
     Log.wtf(TAG, "Just created the alert, where did it go...");
     return;
   }
   activeBgAlert.snooze(repeatTime);
 }
Beispiel #4
0
 public synchronized void Snooze(Context ctx, int repeatTime) {
   Log.i(TAG, "Snooze called repeatTime = " + repeatTime);
   stopAlert(ctx, false, false);
   ActiveBgAlert activeBgAlert = ActiveBgAlert.getOnly();
   if (activeBgAlert == null) {
     Log.w(
         TAG,
         "Error, snooze was called but no alert is active. alert was probably removed in ui ");
     return;
   }
   activeBgAlert.snooze(repeatTime);
 }
Beispiel #5
0
  public synchronized void startAlert(
      Context ctx, boolean trendingToAlertEnd, AlertType newAlert, String bgValue) {
    Log.d(TAG, "startAlert called, Threadid " + Thread.currentThread().getId());
    if (trendingToAlertEnd) {
      Log.d(TAG, "startAlert: This alert is trending to it's end will not do anything");
      return;
    }

    stopAlert(ctx, true, false);
    int alertIn = newAlert.minutes_between;
    if (alertIn < 1) {
      alertIn = 1;
    }
    ActiveBgAlert.Create(newAlert.uuid, false, new Date().getTime() + alertIn * 60000);
    Vibrate(ctx, newAlert, bgValue, newAlert.override_silent_mode, 0);
  }
Beispiel #6
0
  private void PlayFile(Context ctx, String FileName, float VolumeFrac) {
    Log.i(TAG, "PlayFile: called FileName = " + FileName);
    if (mediaPlayer != null) {
      Log.i(TAG, "ERROR, PlayFile:going to leak a mediaplayer !!!");
    }
    if (FileName != null && FileName.length() > 0) {
      mediaPlayer = MediaPlayer.create(ctx, Uri.parse(FileName), null);
    }
    if (mediaPlayer == null) {
      Log.i(
          TAG,
          "PlayFile: Creating mediaplayer with file " + FileName + " failed. using default alarm");
      mediaPlayer = MediaPlayer.create(ctx, R.raw.default_alert);
    }
    if (mediaPlayer != null) {
      AudioManager manager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
      int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      volumeBeforeAlert = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
      volumeForThisAlert = (int) (maxVolume * VolumeFrac);
      manager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeForThisAlert, 0);
      context = ctx;

      mediaPlayer.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
              Log.i(TAG, "PlayFile: onCompletion called (finished playing) ");
              AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
              int currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
              if (volumeForThisAlert == currentVolume) {
                // If the user has changed the volume, don't change it again.
                manager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeBeforeAlert, 0);
              }
            }
          });
      Log.i(TAG, "PlayFile: calling mediaPlayer.start() ");
      mediaPlayer.start();
    } else {
      // TODO, what should we do here???
      Log.wtf(TAG, "PlayFile: Starting an alert failed, what should we do !!!");
    }
  }
Beispiel #7
0
 private static int getAlertProfile(Context ctx) {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
   String profile = prefs.getString("bg_alert_profile", "ascending");
   if (profile.equals("High")) {
     Log.i(TAG, "getAlertProfile returning ALERT_PROFILE_HIGH");
     return ALERT_PROFILE_HIGH;
   }
   if (profile.equals("ascending")) {
     Log.i(TAG, "getAlertProfile returning ALERT_PROFILE_ASCENDING");
     return ALERT_PROFILE_ASCENDING;
   }
   if (profile.equals("medium")) {
     Log.i(TAG, "getAlertProfile returning ALERT_PROFILE_MEDIUM");
     return ALERT_PROFILE_MEDIUM;
   }
   if (profile.equals("vibrate only")) {
     Log.i(TAG, "getAlertProfile returning ALERT_PROFILE_VIBRATE_ONLY");
     return ALERT_PROFILE_VIBRATE_ONLY;
   }
   if (profile.equals("Silent")) {
     Log.i(TAG, "getAlertProfile returning ALERT_PROFILE_SILENT");
     return ALERT_PROFILE_SILENT;
   }
   Log.wtf(TAG, "getAlertProfile unknown value " + profile + " ALERT_PROFILE_ASCENDING");
   return ALERT_PROFILE_ASCENDING;
 }
Beispiel #8
0
  // Check the state and alrarm if needed
  public void ClockTick(Context ctx, boolean trendingToAlertEnd, String bgValue) {
    if (trendingToAlertEnd) {
      Log.d(TAG, "ClockTick: This alert is trending to it's end will not do anything");
      return;
    }
    ActiveBgAlert activeBgAlert = ActiveBgAlert.getOnly();
    if (activeBgAlert == null) {
      // Nothing to do ...
      return;
    }
    if (activeBgAlert.ready_to_alarm()) {
      stopAlert(ctx, false, false);

      int timeFromStartPlaying = activeBgAlert.getUpdatePlayTime();
      AlertType alert = AlertType.get_alert(activeBgAlert.alert_uuid);
      if (alert == null) {
        Log.w(TAG, "ClockTick: The alert was already deleted... will not play");
        ActiveBgAlert.ClearData();
        return;
      }
      Log.d(TAG, "ClockTick: Playing the alert again");
      Vibrate(ctx, alert, bgValue, alert.override_silent_mode, timeFromStartPlaying);
    }
  }
Beispiel #9
0
  public synchronized void stopAlert(
      Context ctx, boolean ClearData, boolean clearIfSnoozeFinished) {

    Log.d(
        TAG,
        "stopAlert: stop called ClearData "
            + ClearData
            + "  ThreadID "
            + Thread.currentThread().getId());
    if (ClearData) {
      ActiveBgAlert.ClearData();
    }
    if (clearIfSnoozeFinished) {
      ActiveBgAlert.ClearIfSnoozeFinished();
    }
    notificationDismiss(ctx);
    if (mediaPlayer != null) {
      mediaPlayer.stop();
      mediaPlayer.release();
      mediaPlayer = null;
    }
  }