use of james.alarmio.data.AlarmData in project Alarmio by TheAndroidMaster.
the class HomeFragment method onCreateView.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
viewPager = view.findViewById(R.id.viewPager);
tabLayout = view.findViewById(R.id.tabLayout);
timePager = view.findViewById(R.id.timePager);
bottomSheet = view.findViewById(R.id.bottomSheet);
timeIndicator = view.findViewById(R.id.pageIndicator);
background = view.findViewById(R.id.background);
overlay = view.findViewById(R.id.overlay);
menu = view.findViewById(R.id.fabsMenu);
stopwatchFab = view.findViewById(R.id.stopwatchFab);
timerFab = view.findViewById(R.id.timerFab);
alarmFab = view.findViewById(R.id.alarmFab);
menu.setMenuUpdateListener(this);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setHideable(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
private int statusBarHeight = -1;
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED)
bottomSheet.setPadding(0, 0, 0, 0);
else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
if (statusBarHeight < 0)
statusBarHeight = ConversionUtils.getStatusBarHeight(getContext());
bottomSheet.setPadding(0, statusBarHeight, 0, 0);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
if (statusBarHeight < 0)
statusBarHeight = ConversionUtils.getStatusBarHeight(getContext());
bottomSheet.setPadding(0, (int) (slideOffset * statusBarHeight), 0, 0);
}
});
}
pagerAdapter = new SimplePagerAdapter(getChildFragmentManager(), new AlarmsFragment(), new SettingsFragment());
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() > 0) {
shouldCollapseBack = behavior.getState() != BottomSheetBehavior.STATE_EXPANDED;
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else if (shouldCollapseBack) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
shouldCollapseBack = false;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Bundle args = new Bundle();
args.putString(ClockFragment.EXTRA_TIME_ZONE, TimeZone.getAvailableIDs()[0]);
ClockFragment fragment = new ClockFragment();
fragment.setArguments(args);
timeAdapter = new SimplePagerAdapter(getChildFragmentManager(), new ClockFragment(), fragment);
timePager.setAdapter(timeAdapter);
timeIndicator.setViewPager(timePager);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
behavior.setPeekHeight(view.getMeasuredHeight() / 2);
view.findViewById(R.id.timeContainer).setLayoutParams(new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, view.getMeasuredHeight() / 2));
}
});
background.setImageDrawable(WallpaperManager.getInstance(getContext()).getFastDrawable());
colorPrimarySubscription = Aesthetic.get().colorPrimary().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
bottomSheet.setBackgroundColor(integer);
overlay.setBackgroundColor(integer);
}
});
colorAccentSubscription = Aesthetic.get().colorAccent().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
menu.getMenuButton().setBackgroundColor(integer);
stopwatchFab.setBackgroundColor(integer);
timerFab.setBackgroundColor(integer);
alarmFab.setBackgroundColor(integer);
}
});
textColorPrimarySubscription = Aesthetic.get().textColorPrimary().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
stopwatchFab.setTitleTextColor(integer);
timerFab.setTitleTextColor(integer);
alarmFab.setTitleTextColor(integer);
}
});
stopwatchFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
menu.collapseImmediately();
getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_up_sheet, R.anim.slide_out_up_sheet, R.anim.slide_in_down_sheet, R.anim.slide_out_down_sheet).replace(R.id.fragment, new StopwatchFragment()).addToBackStack(null).commit();
}
});
timerFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(0, false);
new TimerDialog(getContext(), getFragmentManager()).show();
menu.collapse();
}
});
alarmFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(0, false);
Calendar time = Calendar.getInstance();
new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
AlarmManager manager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
AlarmData alarm = getAlarmio().newAlarm();
alarm.time.set(Calendar.HOUR_OF_DAY, hourOfDay);
alarm.time.set(Calendar.MINUTE, minute);
alarm.setTime(getContext(), getAlarmio().getPrefs(), manager, alarm.time.getTimeInMillis());
alarm.setEnabled(getContext(), getAlarmio().getPrefs(), manager, true);
getAlarmio().onAlarmsChanged();
}
}, time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), DateFormat.is24HourFormat(getContext())).show();
menu.collapse();
}
});
return view;
}
use of james.alarmio.data.AlarmData in project Alarmio by TheAndroidMaster.
the class AlarmReceiver method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Alarmio alarmio = (Alarmio) context.getApplicationContext();
AlarmData alarm = alarmio.getAlarms().get(intent.getIntExtra(EXTRA_ALARM_ID, 0));
if (alarm.isRepeat())
alarm.set(context, manager);
else
alarm.setEnabled(context, alarmio.getPrefs(), manager, false);
alarmio.onAlarmsChanged();
Intent ringer = new Intent(context, AlarmActivity.class);
ringer.putExtra(AlarmActivity.EXTRA_ALARM, alarm);
context.startActivity(ringer);
}
use of james.alarmio.data.AlarmData in project Alarmio by TheAndroidMaster.
the class Alarmio method onCreate.
@Override
public void onCreate() {
super.onCreate();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
listeners = new ArrayList<>();
alarms = new ArrayList<>();
timers = new ArrayList<>();
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
player.addListener(this);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), null);
mediaSourceFactory = new HlsMediaSource.Factory(dataSourceFactory);
int alarmLength = PreferenceData.ALARM_LENGTH.getValue(this);
for (int id = 0; id < alarmLength; id++) {
alarms.add(new AlarmData(id, this, prefs));
}
int timerLength = PreferenceData.TIMER_LENGTH.getValue(this);
for (int id = 0; id < timerLength; id++) {
TimerData timer = new TimerData(id, prefs);
if (timer.isSet())
timers.add(timer);
}
if (timerLength > 0)
startService(new Intent(this, TimerService.class));
}
use of james.alarmio.data.AlarmData in project Alarmio by TheAndroidMaster.
the class Alarmio method newAlarm.
public AlarmData newAlarm() {
AlarmData alarm = new AlarmData(alarms.size(), Calendar.getInstance());
alarms.add(alarm);
onAlarmCountChanged();
return alarm;
}
use of james.alarmio.data.AlarmData 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());
}
});
}
}
Aggregations