Initial commit
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
package com.philliphsu.clock;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 5/26/2016.
|
||||
*/
|
||||
@AutoValue
|
||||
public abstract class Alarm {
|
||||
private static final int MAX_MINUTES_CAN_SNOOZE = 30;
|
||||
// Define our own day constants because those in the
|
||||
// Calendar class are not zero-based.
|
||||
public static final int SUNDAY = 0;
|
||||
public static final int MONDAY = 1;
|
||||
public static final int TUESDAY = 2;
|
||||
public static final int WEDNESDAY = 3;
|
||||
public static final int THURSDAY = 4;
|
||||
public static final int FRIDAY = 5;
|
||||
public static final int SATURDAY = 6;
|
||||
public static final int NUM_DAYS = 7;
|
||||
|
||||
// =========== MUTABLE ===========
|
||||
private long snoozingUntilMillis;
|
||||
private boolean enabled;
|
||||
// ===============================
|
||||
public abstract long id(); // TODO: Counter in the repository. Set this field as the repo creates instances.
|
||||
public abstract int hour();
|
||||
public abstract int minutes();
|
||||
@SuppressWarnings("mutable")
|
||||
// TODO: Consider using an immutable collection instead
|
||||
public abstract boolean[] recurringDays(); // array itself is immutable, but elements are not
|
||||
public abstract String label();
|
||||
public abstract String ringtone();
|
||||
public abstract boolean vibrates();
|
||||
/** Initializes a Builder to the same property values as this instance */
|
||||
public abstract Builder toBuilder();
|
||||
|
||||
public static void main(String[] args) {
|
||||
Alarm a = Alarm.builder().build();
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
// Unfortunately, default values must be provided for generated Builders.
|
||||
// Fields that were not set when build() is called will throw an exception.
|
||||
return new AutoValue_Alarm.Builder()
|
||||
.id(-1)
|
||||
.hour(0)
|
||||
.minutes(0)
|
||||
.recurringDays(new boolean[NUM_DAYS])
|
||||
.label("")
|
||||
.ringtone("")
|
||||
.vibrates(false);
|
||||
}
|
||||
|
||||
public final void snooze(int minutes) {
|
||||
if (minutes <= 0 || minutes > MAX_MINUTES_CAN_SNOOZE)
|
||||
throw new IllegalArgumentException("Cannot snooze for "+minutes+" minutes");
|
||||
snoozingUntilMillis = System.currentTimeMillis() + minutes * 60000;
|
||||
}
|
||||
|
||||
public final long snoozingUntil() {
|
||||
return snoozingUntilMillis;
|
||||
}
|
||||
|
||||
public final boolean isSnoozed() {
|
||||
if (snoozingUntilMillis <= System.currentTimeMillis()) {
|
||||
snoozingUntilMillis = 0;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public final void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public final boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public final void setRecurring(int day, boolean recurring) {
|
||||
checkDay(day);
|
||||
recurringDays()[day] = recurring;
|
||||
}
|
||||
|
||||
public final boolean isRecurring(int day) {
|
||||
checkDay(day);
|
||||
return recurringDays()[day];
|
||||
}
|
||||
|
||||
public final boolean hasRecurrence() {
|
||||
for (boolean b : recurringDays())
|
||||
if (b) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public final long ringsAt() {
|
||||
// Always with respect to the current date and time
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, hour());
|
||||
calendar.set(Calendar.MINUTE, minutes());
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
|
||||
// The specified time has passed for today
|
||||
// TODO: This should be wrapped in an if (!hasRecurrence())?
|
||||
calendar.add(Calendar.HOUR_OF_DAY, 24);
|
||||
// TODO: Else, compute ring time for the next closest recurring day
|
||||
}
|
||||
|
||||
/* // Not fully thought out or completed!
|
||||
// TODO: Compute ring time for the next closest recurring day
|
||||
for (int i = 0; i < NUM_DAYS; i++) {
|
||||
// The constants for the days defined in Calendar are
|
||||
// not zero-based, but we are, so we must add 1.
|
||||
int day = i + 1; // day for this index
|
||||
int calendarDay = mCalendar.get(Calendar.DAY_OF_WEEK);
|
||||
if (mRecurringDays[day]) {
|
||||
mCalendar.add(Calendar.DAY_OF_WEEK, day);
|
||||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
return calendar.getTimeInMillis();
|
||||
}
|
||||
|
||||
public final long ringsIn() {
|
||||
return ringsAt() - System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/** @return true if this Alarm will ring in the next {@code hours} hours */
|
||||
public final boolean ringsWithinHours(int hours) {
|
||||
return ringsIn() <= hours * 3600000;
|
||||
}
|
||||
|
||||
@AutoValue.Builder
|
||||
public abstract static class Builder {
|
||||
// Builder is mutable, so these are inherently setter methods.
|
||||
// By omitting the set- prefix, we reduce the number of changes required to define the Builder
|
||||
// class after copying and pasting the accessor fields here.
|
||||
public abstract Builder id(long id);
|
||||
public abstract Builder hour(int hour);
|
||||
public abstract Builder minutes(int minutes);
|
||||
/* // TODO: If using an immutable collection instead, can use its Builder instance
|
||||
// and provide an "accumulating" method
|
||||
abstract boolean[] recurringDays();
|
||||
public final Builder setRecurring(int day, boolean recurs) {
|
||||
recurringDays()[day] = recurs;
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
public abstract Builder recurringDays(boolean[] recurringDays);
|
||||
public abstract Builder label(String label);
|
||||
public abstract Builder ringtone(String ringtone);
|
||||
public abstract Builder vibrates(boolean vibrates);
|
||||
// To enforce preconditions, split the build method into two. autoBuild() is hidden from
|
||||
// callers and is generated. You implement the public build(), which calls the generated
|
||||
// autoBuild() and performs your desired validations.
|
||||
/*not public*/abstract Alarm autoBuild();
|
||||
|
||||
public final Alarm build() {
|
||||
Alarm alarm = autoBuild();
|
||||
if (alarm.hour() < 0 || alarm.hour() > 23 || alarm.minutes() < 0 || alarm.minutes() > 59) {
|
||||
throw new IllegalStateException("Hour and minutes invalid");
|
||||
}
|
||||
return alarm;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDay(int day) {
|
||||
if (day < SUNDAY || day > SATURDAY)
|
||||
throw new IllegalArgumentException("Invalid day " + day);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package com.philliphsu.clock;
|
||||
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
/**
|
||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||
* fragments for each of the sections. We use a
|
||||
* {@link FragmentPagerAdapter} derivative, which will keep every
|
||||
* loaded fragment in memory. If this becomes too memory intensive, it
|
||||
* may be best to switch to a
|
||||
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
|
||||
*/
|
||||
private SectionsPagerAdapter mSectionsPagerAdapter;
|
||||
|
||||
/**
|
||||
* The {@link ViewPager} that will host the section contents.
|
||||
*/
|
||||
private ViewPager mViewPager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
// Create the adapter that will return a fragment for each of the three
|
||||
// primary sections of the activity.
|
||||
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
||||
|
||||
// Set up the ViewPager with the sections adapter.
|
||||
mViewPager = (ViewPager) findViewById(R.id.container);
|
||||
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||
|
||||
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
|
||||
tabLayout.setupWithViewPager(mViewPager);
|
||||
|
||||
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle action bar item clicks here. The action bar will
|
||||
// automatically handle clicks on the Home/Up button, so long
|
||||
// as you specify a parent activity in AndroidManifest.xml.
|
||||
int id = item.getItemId();
|
||||
|
||||
//noinspection SimplifiableIfStatement
|
||||
if (id == R.id.action_settings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* A placeholder fragment containing a simple view.
|
||||
*/
|
||||
public static class PlaceholderFragment extends Fragment {
|
||||
/**
|
||||
* The fragment argument representing the section number for this
|
||||
* fragment.
|
||||
*/
|
||||
private static final String ARG_SECTION_NUMBER = "section_number";
|
||||
|
||||
public PlaceholderFragment() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new instance of this fragment for the given section
|
||||
* number.
|
||||
*/
|
||||
public static PlaceholderFragment newInstance(int sectionNumber) {
|
||||
PlaceholderFragment fragment = new PlaceholderFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
|
||||
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
|
||||
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
|
||||
return rootView;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
|
||||
* one of the sections/tabs/pages.
|
||||
*/
|
||||
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
||||
|
||||
public SectionsPagerAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
// getItem is called to instantiate the fragment for the given page.
|
||||
// Return a PlaceholderFragment (defined as a static inner class below).
|
||||
return PlaceholderFragment.newInstance(position + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
// Show 3 total pages.
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
switch (position) {
|
||||
case 0:
|
||||
return "SECTION 1";
|
||||
case 1:
|
||||
return "SECTION 2";
|
||||
case 2:
|
||||
return "SECTION 3";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user