Search in sources :

Example 1 with HmsView

use of com.doomonafireball.betterpickers.hmspicker.HmsView in project Shuttle by timusus.

the class SleepTimer method createTimer.

/**
     * Constructor for <code>SleepTimer</code>
     *
     * @param context   The {@link Activity} to use
     * @param active    True if the timer is active, false otherwise
     * @param remaining The remaining time of the current track
     */
public static void createTimer(final Context context, boolean active, final long remaining) {
    final View view = LayoutInflater.from(context).inflate(R.layout.dialog_timer, null);
    final HmsPicker hmsPicker = (HmsPicker) view.findViewById(R.id.hms_picker);
    final HmsView hmsView = (HmsView) view.findViewById(R.id.hms_view);
    ThemeUtils.themeHmsPicker(hmsPicker);
    ThemeUtils.themeHmsView(hmsView);
    final long timeMillis = remaining - System.currentTimeMillis();
    final int minutes = (int) ((timeMillis / (1000 * 60)) % 60);
    final int hours = (int) ((timeMillis / (1000 * 60 * 60)) % 24);
    int minutesFirstDigit = 0;
    int minuteSecondDigit = 0;
    if (minutes > 0) {
        minutesFirstDigit = minutes / 10;
        minuteSecondDigit = minutes % 10;
    }
    hmsView.setTime(hours, minutesFirstDigit, minuteSecondDigit);
    final SharedPreferences mPrefs;
    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
    checkBox.setChecked(mPrefs.getBoolean("sleep_timer_wait_til_end", true));
    checkBox.setOnCheckedChangeListener((compoundButton, b) -> mPrefs.edit().putBoolean("sleep_timer_wait_til_end", b).apply());
    final MaterialDialog.Builder builder = DialogUtils.getBuilder(context).customView(view, false).negativeText(R.string.close);
    if (active) {
        hmsView.setVisibility(View.VISIBLE);
        hmsPicker.setVisibility(View.GONE);
        builder.positiveText(R.string.timer_stop).onPositive((materialDialog, dialogAction) -> MusicUtils.stopTimer());
    } else {
        hmsView.setVisibility(View.GONE);
        hmsPicker.setVisibility(View.VISIBLE);
        builder.positiveText(R.string.timer_set).onPositive((materialDialog, dialogAction) -> {
            if (hmsPicker.getTime() != 0) {
                MusicUtils.setTimer(hmsPicker.getTime() * 1000);
            }
            hmsPicker.setVisibility(View.GONE);
            hmsView.setVisibility(View.VISIBLE);
        });
    }
    builder.show();
    new CountDownTimer(timeMillis, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            final long timeMillis = remaining - System.currentTimeMillis();
            final int minutes = (int) ((timeMillis / (1000 * 60)) % 60);
            final int hours = (int) ((timeMillis / (1000 * 60 * 60)) % 24);
            int minutesFirstDigit = 0;
            int minuteSecondDigit = 0;
            if (minutes > 0) {
                minutesFirstDigit = minutes / 10;
                minuteSecondDigit = minutes % 10;
            }
            hmsView.setTime(hours, minutesFirstDigit, minuteSecondDigit);
        }

        @Override
        public void onFinish() {
        }
    }.start();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SharedPreferences(android.content.SharedPreferences) CheckBox(android.widget.CheckBox) CountDownTimer(android.os.CountDownTimer) HmsPicker(com.doomonafireball.betterpickers.hmspicker.HmsPicker) View(android.view.View) HmsView(com.doomonafireball.betterpickers.hmspicker.HmsView) HmsView(com.doomonafireball.betterpickers.hmspicker.HmsView)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 CountDownTimer (android.os.CountDownTimer)1 View (android.view.View)1 CheckBox (android.widget.CheckBox)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 HmsPicker (com.doomonafireball.betterpickers.hmspicker.HmsPicker)1 HmsView (com.doomonafireball.betterpickers.hmspicker.HmsView)1