package com.lemenzo.clock.bedtime;
import android.app.*;import android.content.*;import android.media.*;import android.net.Uri;import android.os.*;import android.provider.Settings;import com.lemenzo.clock.R;import com.lemenzo.clock.notifications.NotificationChannels;
public final class BedtimeWakeService extends android.app.Service {private MediaPlayer player;private Vibrator vibrator;@Override public int onStartCommand(Intent i,int flags,int startId){startForeground(3302,notification());ring();return START_NOT_STICKY;}private Notification notification(){PendingIntent full=PendingIntent.getActivity(this,3302,new Intent(this,BedtimeWakeActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP),PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);PendingIntent snooze=PendingIntent.getBroadcast(this,3303,new Intent(this,BedtimeWakeReceiver.class).setAction(BedtimeWakeReceiver.SNOOZE),PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);PendingIntent dismiss=PendingIntent.getBroadcast(this,3304,new Intent(this,BedtimeWakeReceiver.class).setAction(BedtimeWakeReceiver.DISMISS),PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);return new Notification.Builder(this,NotificationChannels.ALARMS).setSmallIcon(R.drawable.ic_notification_clock).setContentTitle(getString(R.string.wake_up)).setContentText(getString(R.string.wake_alarm_title)).setCategory(Notification.CATEGORY_ALARM).setVisibility(Notification.VISIBILITY_PUBLIC).setOngoing(true).setFullScreenIntent(full,true).addAction(new Notification.Action.Builder(null,getString(R.string.snooze),snooze).build()).addAction(new Notification.Action.Builder(null,getString(R.string.dismiss),dismiss).build()).build();}private void ring(){try{Uri uri=Settings.System.DEFAULT_ALARM_ALERT_URI;player=new MediaPlayer();player.setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build());player.setDataSource(this,uri);player.setLooping(true);player.prepare();player.start();}catch(Exception ignored){}vibrator=getSystemService(Vibrator.class);if(vibrator!=null)vibrator.vibrate(VibrationEffect.createWaveform(new long[]{0,700,350,700},0));}public static void scheduleSnooze(Context c){AlarmManager m=c.getSystemService(AlarmManager.class);if(m==null)return;PendingIntent p=PendingIntent.getBroadcast(c,5502,new Intent(c,BedtimeWakeReceiver.class).setAction(BedtimeWakeReceiver.FIRE),PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);m.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10*60*1000L,p);}@Override public void onDestroy(){if(player!=null){try{player.stop();}catch(Exception ignored){}player.release();}if(vibrator!=null)vibrator.cancel();stopForeground(STOP_FOREGROUND_REMOVE);super.onDestroy();}@Override public IBinder onBind(Intent i){return null;}}
