Search in sources :

Example 6 with TimerData

use of james.alarmio.data.TimerData in project Alarmio by TheAndroidMaster.

the class AlarmActivity method onSlideLeft.

@Override
public void onSlideLeft() {
    final int[] minutes = new int[] { 2, 5, 10, 20, 30, 60 };
    CharSequence[] names = new CharSequence[minutes.length + 1];
    for (int i = 0; i < minutes.length; i++) {
        names[i] = FormatUtils.formatUnit(AlarmActivity.this, minutes[i]);
    }
    names[minutes.length] = getString(R.string.title_snooze_custom);
    stopAnnoyingness();
    new AlertDialog.Builder(AlarmActivity.this, isDark ? R.style.Theme_AppCompat_Dialog_Alert : R.style.Theme_AppCompat_Light_Dialog_Alert).setItems(names, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which < minutes.length) {
                TimerData timer = alarmio.newTimer();
                timer.setDuration(TimeUnit.MINUTES.toMillis(minutes[which]), alarmio);
                timer.setVibrate(AlarmActivity.this, isVibrate);
                timer.setSound(AlarmActivity.this, sound);
                timer.set(alarmio, ((AlarmManager) AlarmActivity.this.getSystemService(Context.ALARM_SERVICE)));
                alarmio.onTimerStarted();
                finish();
            } else {
                TimeChooserDialog timerDialog = new TimeChooserDialog(AlarmActivity.this);
                timerDialog.setListener(new TimeChooserDialog.OnTimeChosenListener() {

                    @Override
                    public void onTimeChosen(int hours, int minutes, int seconds) {
                        TimerData timer = alarmio.newTimer();
                        timer.setVibrate(AlarmActivity.this, isVibrate);
                        timer.setSound(AlarmActivity.this, sound);
                        timer.setDuration(TimeUnit.HOURS.toMillis(hours) + TimeUnit.MINUTES.toMillis(minutes) + TimeUnit.SECONDS.toMillis(seconds), alarmio);
                        timer.set(alarmio, ((AlarmManager) getSystemService(Context.ALARM_SERVICE)));
                        alarmio.onTimerStarted();
                        finish();
                    }
                });
                timerDialog.show();
            }
        }
    }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    }).show();
    overlay.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) DialogInterface(android.content.DialogInterface) TimerData(james.alarmio.data.TimerData) TimeChooserDialog(james.alarmio.dialogs.TimeChooserDialog) AlarmManager(android.app.AlarmManager)

Example 7 with TimerData

use of james.alarmio.data.TimerData in project Alarmio by TheAndroidMaster.

the class AlarmsAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (getItemViewType(position) == 0) {
        final TimerViewHolder timerHolder = (TimerViewHolder) holder;
        if (timerHolder.runnable != null)
            timerHolder.handler.removeCallbacks(timerHolder.runnable);
        timerHolder.runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    TimerData timer = getTimer(timerHolder.getAdapterPosition());
                    String text = FormatUtils.formatMillis(timer.getRemainingMillis());
                    timerHolder.time.setText(text.substring(0, text.length() - 3));
                    timerHolder.progress.update(1 - ((float) timer.getRemainingMillis() / timer.getDuration()));
                    timerHolder.handler.postDelayed(this, 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        timerHolder.stop.setColorFilter(textColorPrimary);
        timerHolder.stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                TimerData timer = getTimer(timerHolder.getAdapterPosition());
                alarmio.removeTimer(timer);
            }
        });
        timerHolder.handler.post(timerHolder.runnable);
    } else {
        final AlarmViewHolder alarmHolder = (AlarmViewHolder) holder;
        final boolean isExpanded = position == expandedPosition;
        AlarmData alarm = getAlarm(position);
        alarmHolder.name.setFocusable(isExpanded);
        alarmHolder.name.setEnabled(isExpanded);
        alarmHolder.name.clearFocus();
        alarmHolder.nameUnderline.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        alarmHolder.name.setText(alarm.getName(alarmio));
        alarmHolder.name.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                getAlarm(alarmHolder.getAdapterPosition()).setName(alarmio, alarmHolder.name.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        alarmHolder.enable.setOnCheckedChangeListener(null);
        alarmHolder.enable.setChecked(alarm.isEnabled);
        alarmHolder.enable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                getAlarm(alarmHolder.getAdapterPosition()).setEnabled(alarmio, alarmManager, b);
            }
        });
        alarmHolder.time.setText(FormatUtils.formatShort(alarmio, alarm.time.getTime()));
        alarmHolder.time.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                new AestheticTimeSheetPickerDialog(view.getContext(), alarm.time.get(Calendar.HOUR_OF_DAY), alarm.time.get(Calendar.MINUTE)).setListener(new PickerDialog.OnSelectedListener<LinearTimePickerView>() {

                    @Override
                    public void onSelect(PickerDialog<LinearTimePickerView> dialog, LinearTimePickerView view) {
                        AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                        alarm.time.set(Calendar.HOUR_OF_DAY, view.getHourOfDay());
                        alarm.time.set(Calendar.MINUTE, view.getMinute());
                        alarm.setTime(alarmio, alarmManager, alarm.time.getTimeInMillis());
                        alarmHolder.time.setText(FormatUtils.formatShort(alarmio, alarm.time.getTime()));
                    }

                    @Override
                    public void onCancel(PickerDialog<LinearTimePickerView> dialog) {
                    }
                }).show();
            }
        });
        alarmHolder.indicators.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
        if (isExpanded) {
            alarmHolder.repeat.setOnCheckedChangeListener(null);
            alarmHolder.repeat.setChecked(alarm.isRepeat());
            alarmHolder.repeat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                    for (int i = 0; i < 7; i++) {
                        alarm.days[i] = b;
                    }
                    alarm.setDays(alarmio, alarm.days);
                    Transition transition = new AutoTransition();
                    transition.setDuration(150);
                    TransitionManager.beginDelayedTransition(recycler, transition);
                    notifyDataSetChanged();
                }
            });
            alarmHolder.days.setVisibility(alarm.isRepeat() ? View.VISIBLE : View.GONE);
            DaySwitch.OnCheckedChangeListener listener = new DaySwitch.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(DaySwitch daySwitch, boolean b) {
                    AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                    alarm.days[alarmHolder.days.indexOfChild(daySwitch)] = b;
                    alarm.setDays(alarmio, alarm.days);
                    if (!alarm.isRepeat())
                        notifyItemChanged(alarmHolder.getAdapterPosition());
                }
            };
            for (int i = 0; i < 7; i++) {
                DaySwitch daySwitch = (DaySwitch) alarmHolder.days.getChildAt(i);
                daySwitch.setChecked(alarm.days[i]);
                daySwitch.setOnCheckedChangeListener(listener);
                switch(i) {
                    case 0:
                    case 6:
                        daySwitch.setText("S");
                        break;
                    case 1:
                        daySwitch.setText("M");
                        break;
                    case 2:
                    case 4:
                        daySwitch.setText("T");
                        break;
                    case 3:
                        daySwitch.setText("W");
                        break;
                    case 5:
                        daySwitch.setText("F");
                }
            }
            alarmHolder.ringtoneImage.setImageResource(alarm.hasSound() ? R.drawable.ic_ringtone : R.drawable.ic_ringtone_disabled);
            alarmHolder.ringtoneImage.setAlpha(alarm.hasSound() ? 1 : 0.333f);
            alarmHolder.ringtoneText.setText(alarm.hasSound() ? alarm.getSound().getName() : alarmio.getString(R.string.title_sound_none));
            alarmHolder.ringtone.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    SoundChooserDialog dialog = new SoundChooserDialog();
                    dialog.setListener(new SoundChooserListener() {

                        @Override
                        public void onSoundChosen(SoundData sound) {
                            int position = alarmHolder.getAdapterPosition();
                            AlarmData alarm = getAlarm(position);
                            alarm.setSound(alarmio, sound);
                            notifyItemChanged(position);
                        }
                    });
                    dialog.show(fragmentManager, null);
                }
            });
            AnimatedVectorDrawableCompat vibrateDrawable = AnimatedVectorDrawableCompat.create(alarmio, alarm.isVibrate ? R.drawable.ic_vibrate_to_none : R.drawable.ic_none_to_vibrate);
            alarmHolder.vibrateImage.setImageDrawable(vibrateDrawable);
            alarmHolder.vibrateImage.setAlpha(alarm.isVibrate ? 1 : 0.333f);
            alarmHolder.vibrate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                    alarm.setVibrate(alarmio, !alarm.isVibrate);
                    AnimatedVectorDrawableCompat vibrateDrawable = AnimatedVectorDrawableCompat.create(alarmio, alarm.isVibrate ? R.drawable.ic_none_to_vibrate : R.drawable.ic_vibrate_to_none);
                    if (vibrateDrawable != null) {
                        alarmHolder.vibrateImage.setImageDrawable(vibrateDrawable);
                        vibrateDrawable.start();
                    } else
                        alarmHolder.vibrateImage.setImageResource(alarm.isVibrate ? R.drawable.ic_vibrate : R.drawable.ic_vibrate_none);
                    alarmHolder.vibrateImage.animate().alpha(alarm.isVibrate ? 1 : 0.333f).setDuration(250).start();
                    if (alarm.isVibrate)
                        view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                }
            });
        } else {
            alarmHolder.repeatIndicator.setAlpha(alarm.isRepeat() ? 1 : 0.333f);
            alarmHolder.soundIndicator.setAlpha(alarm.hasSound() ? 1 : 0.333f);
            alarmHolder.vibrateIndicator.setAlpha(alarm.isVibrate ? 1 : 0.333f);
        }
        alarmHolder.expandImage.animate().rotationX(isExpanded ? 180 : 0).start();
        alarmHolder.delete.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        alarmHolder.delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                new AlertDialog(view.getContext()).setContent(alarmio.getString(R.string.msg_delete_confirmation, alarm.getName(alarmio))).setListener(new AlertDialog.Listener() {

                    @Override
                    public void onDismiss(AlertDialog dialog, boolean ok) {
                        if (ok)
                            alarmio.removeAlarm(getAlarm(alarmHolder.getAdapterPosition()));
                    }
                }).show();
            }
        });
        alarmHolder.repeat.setTextColor(textColorPrimary);
        alarmHolder.delete.setTextColor(textColorPrimary);
        alarmHolder.ringtoneImage.setColorFilter(textColorPrimary);
        alarmHolder.vibrateImage.setColorFilter(textColorPrimary);
        alarmHolder.expandImage.setColorFilter(textColorPrimary);
        alarmHolder.repeatIndicator.setColorFilter(textColorPrimary);
        alarmHolder.soundIndicator.setColorFilter(textColorPrimary);
        alarmHolder.vibrateIndicator.setColorFilter(textColorPrimary);
        alarmHolder.nameUnderline.setBackgroundColor(textColorPrimary);
        int visibility = isExpanded ? View.VISIBLE : View.GONE;
        if (visibility != alarmHolder.extra.getVisibility()) {
            alarmHolder.extra.setVisibility(visibility);
            Aesthetic.Companion.get().colorPrimary().take(1).subscribe(new Consumer<Integer>() {

                @Override
                public void accept(Integer integer) throws Exception {
                    ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), isExpanded ? integer : colorForeground, isExpanded ? colorForeground : integer);
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            alarmHolder.itemView.setBackgroundColor((int) animation.getAnimatedValue());
                        }
                    });
                    animator.addListener(new Animator.AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            alarmHolder.itemView.setBackgroundColor(isExpanded ? colorForeground : Color.TRANSPARENT);
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {
                        }
                    });
                    animator.start();
                }
            });
            ValueAnimator animator = ValueAnimator.ofFloat(isExpanded ? 0 : ConversionUtils.dpToPx(2), isExpanded ? ConversionUtils.dpToPx(2) : 0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewCompat.setElevation(alarmHolder.itemView, (float) animation.getAnimatedValue());
                }
            });
            animator.start();
        } else {
            alarmHolder.itemView.setBackgroundColor(isExpanded ? colorForeground : Color.TRANSPARENT);
            ViewCompat.setElevation(alarmHolder.itemView, isExpanded ? ConversionUtils.dpToPx(2) : 0);
        }
        alarmHolder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                expandedPosition = isExpanded ? -1 : alarmHolder.getAdapterPosition();
                Transition transition = new AutoTransition();
                transition.setDuration(250);
                TransitionManager.beginDelayedTransition(recycler, transition);
                notifyDataSetChanged();
            }
        });
    }
}
Also used : AlertDialog(james.alarmio.dialogs.AlertDialog) SoundChooserListener(james.alarmio.interfaces.SoundChooserListener) ArgbEvaluator(android.animation.ArgbEvaluator) ValueAnimator(android.animation.ValueAnimator) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) SoundData(james.alarmio.data.SoundData) DaySwitch(james.alarmio.views.DaySwitch) AutoTransition(androidx.transition.AutoTransition) AestheticTimeSheetPickerDialog(james.alarmio.dialogs.AestheticTimeSheetPickerDialog) ImageView(android.widget.ImageView) LinearTimePickerView(me.jfenn.timedatepickers.views.LinearTimePickerView) View(android.view.View) ProgressLineView(james.alarmio.views.ProgressLineView) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) SoundChooserDialog(james.alarmio.dialogs.SoundChooserDialog) Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) TimerData(james.alarmio.data.TimerData) PickerDialog(me.jfenn.timedatepickers.dialogs.PickerDialog) AestheticTimeSheetPickerDialog(james.alarmio.dialogs.AestheticTimeSheetPickerDialog) AutoTransition(androidx.transition.AutoTransition) Transition(androidx.transition.Transition) AnimatedVectorDrawableCompat(androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat) CompoundButton(android.widget.CompoundButton) AlarmData(james.alarmio.data.AlarmData) LinearTimePickerView(me.jfenn.timedatepickers.views.LinearTimePickerView)

Example 8 with TimerData

use of james.alarmio.data.TimerData in project Alarmio by TheAndroidMaster.

the class TimerService method getNotification.

@Nullable
private Notification getNotification() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        notificationManager.createNotificationChannel(new NotificationChannel(Alarmio.NOTIFICATION_CHANNEL_TIMERS, "Timers", NotificationManager.IMPORTANCE_DEFAULT));
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String string = "";
    for (TimerData timer : timers) {
        if (!timer.isSet())
            continue;
        String time = FormatUtils.formatMillis(timer.getRemainingMillis());
        time = time.substring(0, time.length() - 3);
        inboxStyle.addLine(time);
        string += "/" + time + "/";
    }
    if (notificationString != null && notificationString.equals(string))
        return null;
    notificationString = string;
    Intent intent = new Intent(this, MainActivity.class);
    if (timers.size() == 1)
        intent.putExtra(TimerReceiver.EXTRA_TIMER_ID, 0);
    return new NotificationCompat.Builder(this, Alarmio.NOTIFICATION_CHANNEL_TIMERS).setSmallIcon(R.drawable.ic_timer_notification).setContentTitle(getString(R.string.title_set_timer)).setContentText("").setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)).setStyle(inboxStyle).build();
}
Also used : NotificationChannel(android.app.NotificationChannel) TimerData(james.alarmio.data.TimerData) NotificationCompat(androidx.core.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Nullable(androidx.annotation.Nullable)

Aggregations

TimerData (james.alarmio.data.TimerData)8 Intent (android.content.Intent)3 AlarmData (james.alarmio.data.AlarmData)3 AlarmManager (android.app.AlarmManager)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 AnimatedVectorDrawableCompat (androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat)2 Alarmio (james.alarmio.Alarmio)2 SoundData (james.alarmio.data.SoundData)2 SoundChooserListener (james.alarmio.interfaces.SoundChooserListener)2 Animator (android.animation.Animator)1 ArgbEvaluator (android.animation.ArgbEvaluator)1 ValueAnimator (android.animation.ValueAnimator)1 NotificationChannel (android.app.NotificationChannel)1 PendingIntent (android.app.PendingIntent)1 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1