Search in sources :

Example 1 with MusicService

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;
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) NonNull(androidx.annotation.NonNull) PreferenceUtil(com.kabouzeid.gramophone.util.PreferenceUtil) FrameLayout(android.widget.FrameLayout) MusicPlayerRemote(com.kabouzeid.gramophone.helper.MusicPlayerRemote) Dialog(android.app.Dialog) Intent(android.content.Intent) SystemClock(android.os.SystemClock) PendingIntent(android.app.PendingIntent) BindView(butterknife.BindView) ThemeSingleton(com.afollestad.materialdialogs.internal.ThemeSingleton) CheckBox(android.widget.CheckBox) Toast(android.widget.Toast) SeekArc(com.triggertrap.seekarc.SeekArc) R(com.kabouzeid.gramophone.R) PurchaseActivity(com.kabouzeid.gramophone.ui.activities.PurchaseActivity) DialogInterface(android.content.DialogInterface) MusicUtil(com.kabouzeid.gramophone.util.MusicUtil) AlarmManager(android.app.AlarmManager) App(com.kabouzeid.gramophone.App) DialogAction(com.afollestad.materialdialogs.DialogAction) CountDownTimer(android.os.CountDownTimer) TextView(android.widget.TextView) MusicService(com.kabouzeid.gramophone.service.MusicService) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogFragment(androidx.fragment.app.DialogFragment) MusicService(com.kabouzeid.gramophone.service.MusicService) SeekArc(com.triggertrap.seekarc.SeekArc) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) FrameLayout(android.widget.FrameLayout) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent) NonNull(androidx.annotation.NonNull)

Aggregations

AlarmManager (android.app.AlarmManager)1 Dialog (android.app.Dialog)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 CountDownTimer (android.os.CountDownTimer)1 SystemClock (android.os.SystemClock)1 CheckBox (android.widget.CheckBox)1 FrameLayout (android.widget.FrameLayout)1 TextView (android.widget.TextView)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 DialogFragment (androidx.fragment.app.DialogFragment)1 BindView (butterknife.BindView)1 ButterKnife (butterknife.ButterKnife)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 ThemeSingleton (com.afollestad.materialdialogs.internal.ThemeSingleton)1