Search in sources :

Example 1 with SoundData

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

the class AlarmSoundChooserFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sound_chooser_list, container, false);
    RecyclerView recyclerView = view.findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    List<SoundData> sounds = new ArrayList<>();
    RingtoneManager manager = new RingtoneManager(getContext());
    manager.setType(RingtoneManager.TYPE_ALARM);
    Cursor cursor = manager.getCursor();
    int count = cursor.getCount();
    if (count > 0 && cursor.moveToFirst()) {
        do {
            sounds.add(new SoundData(cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX), cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + cursor.getString(RingtoneManager.ID_COLUMN_INDEX)));
        } while (cursor.moveToNext());
    }
    SoundsAdapter adapter = new SoundsAdapter(getAlarmio(), sounds);
    adapter.setListener(this);
    recyclerView.setAdapter(adapter);
    return view;
}
Also used : RingtoneManager(android.media.RingtoneManager) SoundsAdapter(james.alarmio.adapters.SoundsAdapter) ArrayList(java.util.ArrayList) RecyclerView(android.support.v7.widget.RecyclerView) SoundData(james.alarmio.data.SoundData) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Cursor(android.database.Cursor) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) Nullable(android.support.annotation.Nullable)

Example 2 with SoundData

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

the class RadioSoundChooserFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sound_chooser_radio, container, false);
    prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    radioUrlEditText = view.findViewById(R.id.radioUrl);
    TextView errorTextView = view.findViewById(R.id.errorText);
    RecyclerView recycler = view.findViewById(R.id.recycler);
    testRadio = view.findViewById(R.id.testRadio);
    List<String> previousRadios = new ArrayList<>(prefs.getStringSet(PREF_RADIOS, new HashSet<String>()));
    Collections.sort(previousRadios, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            try {
                return Integer.parseInt(o1.split(SEPARATOR)[0]) - Integer.parseInt(o2.split(SEPARATOR)[0]);
            } catch (NumberFormatException e) {
                return 0;
            }
        }
    });
    sounds = new ArrayList<>();
    for (String string : previousRadios) {
        String url = string.split(SEPARATOR)[1];
        sounds.add(new SoundData(url, url));
    }
    recycler.setLayoutManager(new LinearLayoutManager(getContext()));
    SoundsAdapter adapter = new SoundsAdapter(getAlarmio(), sounds);
    adapter.setListener(this);
    recycler.setAdapter(adapter);
    testRadio.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (currentSound == null) {
                currentSound = new SoundData("", radioUrlEditText.getText().toString());
                try {
                    currentSound.preview(getAlarmio());
                    testRadio.setText(R.string.title_radio_stop);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            } else {
                if (currentSound.isPlaying(getAlarmio()))
                    currentSound.stop(getAlarmio());
                currentSound = null;
                testRadio.setText(R.string.title_radio_test);
            }
        }
    });
    view.findViewById(R.id.createRadio).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (URLUtil.isValidUrl(radioUrlEditText.getText().toString())) {
                onSoundChosen(new SoundData(getString(R.string.title_radio), radioUrlEditText.getText().toString()));
            }
        }
    });
    return view;
}
Also used : ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) SoundsAdapter(james.alarmio.adapters.SoundsAdapter) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) SoundData(james.alarmio.data.SoundData) HashSet(java.util.HashSet) Nullable(android.support.annotation.Nullable)

Example 3 with SoundData

use of james.alarmio.data.SoundData 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.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra(TimerReceiver.EXTRA_TIMER_ID, timerHolder.getAdapterPosition());
                context.startActivity(intent);
            }
        });
        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.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(prefs, 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, prefs, 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 TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                        AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                        alarm.time.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        alarm.time.set(Calendar.MINUTE, minute);
                        alarm.setTime(alarmio, prefs, alarmManager, alarm.time.getTimeInMillis());
                        alarmHolder.time.setText(FormatUtils.formatShort(alarmio, alarm.time.getTime()));
                    }
                }, alarm.time.get(Calendar.HOUR_OF_DAY), alarm.time.get(Calendar.MINUTE), DateFormat.is24HourFormat(alarmio)).show();
            }
        });
        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(prefs, alarm.days);
                notifyItemChanged(alarmHolder.getAdapterPosition());
            }
        });
        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(prefs, 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.ringtoneText.setText(alarm.hasSound() ? alarm.getSound().getName() : context.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(prefs, sound);
                        notifyItemChanged(position);
                    }
                });
                dialog.show(fragmentManager, null);
            }
        });
        alarmHolder.vibrateImage.setImageResource(alarm.isVibrate ? R.drawable.ic_vibrate : R.drawable.ic_none);
        alarmHolder.vibrate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                AlarmData alarm = getAlarm(alarmHolder.getAdapterPosition());
                alarm.setVibrate(prefs, !alarm.isVibrate);
                alarmHolder.vibrateImage.setImageResource(alarm.isVibrate ? R.drawable.ic_vibrate : R.drawable.ic_none);
                if (alarm.isVibrate)
                    view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            }
        });
        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.Builder(context).setMessage(alarmio.getString(R.string.msg_delete_confirmation, alarm.getName(alarmio))).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        alarmio.removeAlarm(getAlarm(alarmHolder.getAdapterPosition()));
                        dialog.dismiss();
                    }
                }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                }).show();
            }
        });
        int[][] states = new int[][] { new int[] { -android.R.attr.state_checked }, new int[] { android.R.attr.state_checked } };
        ColorStateList colorStateList = new ColorStateList(states, new int[] { Color.argb(100, 128, 128, 128), colorAccent });
        ColorStateList thumbStateList = new ColorStateList(states, new int[] { Color.argb(255, 128, 128, 128), colorAccent });
        ColorStateList trackStateList = new ColorStateList(states, new int[] { Color.argb(100, 128, 128, 128), Color.argb(100, Color.red(colorAccent), Color.green(colorAccent), Color.blue(colorAccent)) });
        CompoundButtonCompat.setButtonTintList(alarmHolder.enable, colorStateList);
        CompoundButtonCompat.setButtonTintList(alarmHolder.repeat, colorStateList);
        DrawableCompat.setTintList(DrawableCompat.wrap(alarmHolder.enable.getThumbDrawable()), thumbStateList);
        DrawableCompat.setTintList(DrawableCompat.wrap(alarmHolder.enable.getTrackDrawable()), trackStateList);
        alarmHolder.repeat.setTextColor(textColorPrimary);
        alarmHolder.delete.setTextColor(textColorPrimary);
        alarmHolder.ringtoneImage.setColorFilter(textColorPrimary);
        alarmHolder.vibrateImage.setColorFilter(textColorPrimary);
        alarmHolder.expandImage.setColorFilter(textColorPrimary);
        alarmHolder.extra.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        alarmHolder.expandImage.animate().rotation(isExpanded ? 180 : 0);
        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) {
                int previousPosition = expandedPosition;
                expandedPosition = isExpanded ? -1 : alarmHolder.getAdapterPosition();
                if (previousPosition != expandedPosition && previousPosition != -1)
                    notifyItemChanged(previousPosition);
                notifyItemChanged(alarmHolder.getAdapterPosition());
            }
        });
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) TimePicker(android.widget.TimePicker) SoundChooserListener(james.alarmio.interfaces.SoundChooserListener) DialogInterface(android.content.DialogInterface) ColorStateList(android.content.res.ColorStateList) MainActivity(james.alarmio.activities.MainActivity) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) SoundData(james.alarmio.data.SoundData) DaySwitch(james.alarmio.views.DaySwitch) Intent(android.content.Intent) TimePickerDialog(android.app.TimePickerDialog) ImageView(android.widget.ImageView) View(android.view.View) ProgressLineView(james.alarmio.views.ProgressLineView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) SoundChooserDialog(james.alarmio.dialogs.SoundChooserDialog) TimerData(james.alarmio.data.TimerData) CompoundButton(android.widget.CompoundButton) AlarmData(james.alarmio.data.AlarmData)

Example 4 with SoundData

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

the class RingtoneSoundChooserFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sound_chooser_list, container, false);
    RecyclerView recyclerView = view.findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    List<SoundData> sounds = new ArrayList<>();
    RingtoneManager manager = new RingtoneManager(getContext());
    manager.setType(RingtoneManager.TYPE_RINGTONE);
    Cursor cursor = manager.getCursor();
    int count = cursor.getCount();
    if (count > 0 && cursor.moveToFirst()) {
        do {
            sounds.add(new SoundData(cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX), cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + cursor.getString(RingtoneManager.ID_COLUMN_INDEX)));
        } while (cursor.moveToNext());
    }
    adapter = new SoundsAdapter(getAlarmio(), sounds);
    adapter.setListener(this);
    recyclerView.setAdapter(adapter);
    return view;
}
Also used : RingtoneManager(android.media.RingtoneManager) SoundsAdapter(james.alarmio.adapters.SoundsAdapter) ArrayList(java.util.ArrayList) RecyclerView(android.support.v7.widget.RecyclerView) SoundData(james.alarmio.data.SoundData) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Cursor(android.database.Cursor) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) Nullable(android.support.annotation.Nullable)

Example 5 with SoundData

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

the class SoundsAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    Observable<Integer> textColor;
    Observable<Integer> backgroundColor;
    if (position == 0) {
        holder.title.setText(R.string.title_sound_none);
        holder.icon.setOnClickListener(null);
        holder.icon.setImageResource(R.drawable.ic_ringtone_disabled);
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onSoundChosen(null);
            }
        });
        textColor = Aesthetic.get().textColorPrimary();
        backgroundColor = Aesthetic.get().colorPrimary();
    } else {
        SoundData sound = sounds.get(position - 1);
        holder.title.setText(sound.getName());
        holder.icon.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                SoundData sound = sounds.get(position - 1);
                if (sound.isPlaying(alarmio)) {
                    sound.stop(alarmio);
                    currentlyPlaying = -1;
                } else {
                    sound.preview(alarmio);
                    if (currentlyPlaying >= 0) {
                        sounds.get(currentlyPlaying - 1).stop(alarmio);
                        notifyItemChanged(currentlyPlaying);
                    }
                    currentlyPlaying = position;
                }
                notifyItemChanged(position);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onSoundChosen(sounds.get(holder.getAdapterPosition() - 1));
            }
        });
        if (sound.isPlaying(alarmio)) {
            holder.icon.setImageResource(R.drawable.ic_pause);
            textColor = Aesthetic.get().colorPrimary();
            backgroundColor = Aesthetic.get().textColorPrimary();
        } else {
            holder.icon.setImageResource(R.drawable.ic_play);
            textColor = Aesthetic.get().textColorPrimary();
            backgroundColor = Aesthetic.get().colorPrimary();
        }
    }
    textColor.take(1).subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer integer) throws Exception {
            holder.title.setTextColor(integer);
            holder.icon.setColorFilter(integer);
        }
    });
    backgroundColor.take(1).subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer integer) throws Exception {
            holder.itemView.setBackgroundColor(integer);
        }
    });
}
Also used : SoundData(james.alarmio.data.SoundData) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)5 View (android.view.View)5 SoundData (james.alarmio.data.SoundData)5 Nullable (android.support.annotation.Nullable)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 TextView (android.widget.TextView)3 SoundsAdapter (james.alarmio.adapters.SoundsAdapter)3 ArrayList (java.util.ArrayList)3 Cursor (android.database.Cursor)2 RingtoneManager (android.media.RingtoneManager)2 ImageView (android.widget.ImageView)2 TimePickerDialog (android.app.TimePickerDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 ColorStateList (android.content.res.ColorStateList)1 AlertDialog (android.support.v7.app.AlertDialog)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 CompoundButton (android.widget.CompoundButton)1 TimePicker (android.widget.TimePicker)1