use of com.kabouzeid.gramophone.service.MusicService in project Phonograph by kabouzeid.
the class SleepTimerDialog method onCreateDialog.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
timerUpdater = new TimerUpdater();
materialDialog = new MaterialDialog.Builder(getActivity()).title(getActivity().getResources().getString(R.string.action_sleep_timer)).positiveText(R.string.action_set).onPositive((dialog, which) -> {
if (getActivity() == null) {
return;
}
if (!App.isProVersion()) {
Toast.makeText(getActivity(), getString(R.string.sleep_timer_is_a_pro_feature), Toast.LENGTH_LONG).show();
startActivity(new Intent(getContext(), PurchaseActivity.class));
return;
}
PreferenceUtil.getInstance(getActivity()).setSleepTimerFinishMusic(shouldFinishLastSong.isChecked());
final int minutes = seekArcProgress;
PendingIntent pi = makeTimerPendingIntent(PendingIntent.FLAG_CANCEL_CURRENT);
final long nextSleepTimerElapsedTime = SystemClock.elapsedRealtime() + minutes * 60 * 1000;
PreferenceUtil.getInstance(getActivity()).setNextSleepTimerElapsedRealtime(nextSleepTimerElapsedTime);
AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextSleepTimerElapsedTime, pi);
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.sleep_timer_set, minutes), Toast.LENGTH_SHORT).show();
}).onNeutral((dialog, which) -> {
if (getActivity() == null) {
return;
}
final PendingIntent previous = makeTimerPendingIntent(PendingIntent.FLAG_NO_CREATE);
if (previous != null) {
AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
am.cancel(previous);
previous.cancel();
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.sleep_timer_canceled), Toast.LENGTH_SHORT).show();
}
MusicService musicService = MusicPlayerRemote.musicService;
if (musicService != null && musicService.pendingQuit) {
musicService.pendingQuit = false;
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.sleep_timer_canceled), Toast.LENGTH_SHORT).show();
}
}).showListener(dialog -> {
if (makeTimerPendingIntent(PendingIntent.FLAG_NO_CREATE) != null) {
timerUpdater.start();
}
}).customView(R.layout.dialog_sleep_timer, false).build();
if (getActivity() == null || materialDialog.getCustomView() == null) {
return materialDialog;
}
ButterKnife.bind(this, materialDialog.getCustomView());
boolean finishMusic = PreferenceUtil.getInstance(getActivity()).getSleepTimerFinishMusic();
shouldFinishLastSong.setChecked(finishMusic);
seekArc.setProgressColor(ThemeSingleton.get().positiveColor.getDefaultColor());
seekArc.setThumbColor(ThemeSingleton.get().positiveColor.getDefaultColor());
seekArc.post(() -> {
int width = seekArc.getWidth();
int height = seekArc.getHeight();
int small = Math.min(width, height);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(seekArc.getLayoutParams());
layoutParams.height = small;
seekArc.setLayoutParams(layoutParams);
});
seekArcProgress = PreferenceUtil.getInstance(getActivity()).getLastSleepTimerValue();
updateTimeDisplayTime();
seekArc.setProgress(seekArcProgress);
seekArc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
@Override
public void onProgressChanged(@NonNull SeekArc seekArc, int i, boolean b) {
if (i < 1) {
seekArc.setProgress(1);
return;
}
seekArcProgress = i;
updateTimeDisplayTime();
}
@Override
public void onStartTrackingTouch(SeekArc seekArc) {
}
@Override
public void onStopTrackingTouch(SeekArc seekArc) {
PreferenceUtil.getInstance(getActivity()).setLastSleepTimerValue(seekArcProgress);
}
});
return materialDialog;
}
Aggregations