Initial commit
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.philliphsu.clock;
|
||||
|
||||
import android.app.Application;
|
||||
import android.test.ApplicationTestCase;
|
||||
|
||||
/**
|
||||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
|
||||
*/
|
||||
public class ApplicationTest extends ApplicationTestCase<Application> {
|
||||
public ApplicationTest() {
|
||||
super(Application.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest package="com.philliphsu.clock"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme.NoActionBar">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true"
|
||||
tools:context="com.philliphsu.clock.MainActivity">
|
||||
|
||||
<android.support.design.widget.AppBarLayout
|
||||
android:id="@+id/appbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/appbar_padding_top"
|
||||
android:theme="@style/AppTheme.AppBarOverlay">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:background="?attr/colorPrimary"
|
||||
app:layout_scrollFlags="scroll|enterAlways"
|
||||
app:popupTheme="@style/AppTheme.PopupOverlay">
|
||||
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
<android.support.design.widget.TabLayout
|
||||
android:id="@+id/tabs"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
</android.support.design.widget.AppBarLayout>
|
||||
|
||||
<android.support.v4.view.ViewPager
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end|bottom"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
android:src="@android:drawable/ic_dialog_email"/>
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
@@ -0,0 +1,16 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="com.philliphsu.clock.MainActivity$PlaceholderFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/section_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -0,0 +1,10 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:context="com.philliphsu.clock.MainActivity">
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/action_settings"
|
||||
app:showAsAction="never"/>
|
||||
</menu>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 4.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
@@ -0,0 +1,9 @@
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme.NoActionBar">
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
|
||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,6 @@
|
||||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
</resources>
|
||||
@@ -0,0 +1,7 @@
|
||||
<resources>
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
<dimen name="fab_margin">16dp</dimen>
|
||||
<dimen name="appbar_padding_top">8dp</dimen>
|
||||
</resources>
|
||||
@@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
<string name="app_name">Clock+</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="section_format">Hello World from section: %1$d</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,20 @@
|
||||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.NoActionBar">
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
|
||||
|
||||
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.philliphsu.clock;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* To work on unit tests, switch the Test Artifact in the Build Variants view.
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user