├── app
├── .gitignore
├── .settings
│ └── org.eclipse.buildship.core.prefs
├── src
│ ├── main
│ │ ├── ic_launcher-web.png
│ │ ├── res
│ │ │ ├── font
│ │ │ │ ├── roboto.ttf
│ │ │ │ ├── roboto_bold.ttf
│ │ │ │ ├── roboto_light.ttf
│ │ │ │ └── roboto_medium.ttf
│ │ │ ├── web_hi_res_512.png
│ │ │ ├── mipmap-ldpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── ic_launcher_background.xml
│ │ │ │ ├── preference_keys.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── drawable
│ │ │ │ ├── button_fab_extended.xml
│ │ │ │ ├── button_close_activity.xml
│ │ │ │ ├── shadow.xml
│ │ │ │ ├── ic_add.xml
│ │ │ │ ├── button_fab_extended_drawable.xml
│ │ │ │ ├── button_fab_extended_no_background_drawable.xml
│ │ │ │ ├── ic_close.xml
│ │ │ │ ├── ic_list_ringtone.xml
│ │ │ │ ├── ic_empty_alarms_list.xml
│ │ │ │ ├── ic_bottom_navigation_sleepnow.xml
│ │ │ │ ├── ic_bottom_naviagation_alarms.xml
│ │ │ │ ├── ic_bottom_navigation_wakeupat.xml
│ │ │ │ ├── ic_list_alarm_add.xml
│ │ │ │ ├── ic_list_alarm_access.xml
│ │ │ │ ├── ic_empty_wakeupat_list.xml
│ │ │ │ ├── ic_list_ringtone_full_shape.xml
│ │ │ │ ├── ic_list_alarm_add_full_shape.xml
│ │ │ │ ├── button_close_activity_drawable.xml
│ │ │ │ ├── ic_list_alarm_access_full_shape.xml
│ │ │ │ └── button_fab_extended_no_background.xml
│ │ │ ├── color
│ │ │ │ ├── color_primary_selector.xml
│ │ │ │ ├── navigation_bar_selector.xml
│ │ │ │ └── color_secondary_selector.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── menu
│ │ │ │ ├── three_dot_menu.xml
│ │ │ │ └── bottom_navigation_menu.xml
│ │ │ ├── layout
│ │ │ │ ├── divider.xml
│ │ │ │ ├── dialog_set_hour_to_wake_up_at.xml
│ │ │ │ ├── activity_settings.xml
│ │ │ │ ├── fragment_alarms.xml
│ │ │ │ ├── item_empty_state.xml
│ │ │ │ ├── fragment_wake_up_at.xml
│ │ │ │ ├── dialog_item_ringtone.xml
│ │ │ │ ├── activity_alarm.xml
│ │ │ │ ├── item_alarm.xml
│ │ │ │ ├── fragment_sleep_now.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── anim
│ │ │ │ ├── fast_fade_out.xml
│ │ │ │ └── grow_fade_in_center.xml
│ │ │ ├── values-night
│ │ │ │ └── colors.xml
│ │ │ ├── xml
│ │ │ │ └── preferences.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── gmail
│ │ │ │ └── brunokawka
│ │ │ │ └── poland
│ │ │ │ └── sleepcyclealarm
│ │ │ │ ├── events
│ │ │ │ ├── RealmChangeEvent.java
│ │ │ │ ├── SetHourButtonClickedEvent.java
│ │ │ │ └── AddAlarmEvent.java
│ │ │ │ ├── tabs
│ │ │ │ ├── ui
│ │ │ │ │ ├── dialog
│ │ │ │ │ │ ├── ItemDialogContract.java
│ │ │ │ │ │ └── ItemDialog.java
│ │ │ │ │ ├── WakeUpAtSetHourButton.java
│ │ │ │ │ └── EmptyStateRecyclerView.java
│ │ │ │ ├── activealarms
│ │ │ │ │ ├── AlarmsContract.java
│ │ │ │ │ ├── AlarmScopeListener.java
│ │ │ │ │ └── AlarmsPresenter.java
│ │ │ │ ├── addalarm
│ │ │ │ │ ├── wakeupat
│ │ │ │ │ │ ├── WakeUpAtContract.java
│ │ │ │ │ │ ├── WakeUpAtSetTimeView.java
│ │ │ │ │ │ └── WakeUpAtPresenter.java
│ │ │ │ │ ├── sleepnow
│ │ │ │ │ │ └── SleepNowFragment.java
│ │ │ │ │ └── AddDialogFragment.java
│ │ │ │ └── adapters
│ │ │ │ │ ├── AddAlarmsAdapter.java
│ │ │ │ │ └── ActiveAlarmsAdapter.java
│ │ │ │ ├── utils
│ │ │ │ ├── itemsbuilder
│ │ │ │ │ ├── ItemsBuilderStrategy.java
│ │ │ │ │ ├── ItemsBuilder.java
│ │ │ │ │ ├── ItemsBuilderAbstraction.java
│ │ │ │ │ ├── SleepNowBuildingStrategy.java
│ │ │ │ │ └── WakeUpAtBuildingStrategy.java
│ │ │ │ ├── Const.java
│ │ │ │ ├── ThemeUtils.java
│ │ │ │ ├── TimeUtils.java
│ │ │ │ └── AlarmContentUtils.java
│ │ │ │ ├── app
│ │ │ │ ├── RealmInitialData.java
│ │ │ │ ├── CustomApp.java
│ │ │ │ ├── base
│ │ │ │ │ ├── MainContract.java
│ │ │ │ │ └── MainPresenter.java
│ │ │ │ └── RealmManager.java
│ │ │ │ ├── schedule
│ │ │ │ ├── AlarmReceiver.java
│ │ │ │ ├── AlarmController.java
│ │ │ │ ├── AlarmActivity.java
│ │ │ │ └── AlarmService.java
│ │ │ │ ├── data
│ │ │ │ ├── ItemsBuilderData.java
│ │ │ │ ├── pojo
│ │ │ │ │ ├── Item.java
│ │ │ │ │ └── Alarm.java
│ │ │ │ └── AlarmDAO.java
│ │ │ │ └── settings
│ │ │ │ ├── SettingsActivity.java
│ │ │ │ └── SettingsFragment.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ ├── android
│ │ │ └── util
│ │ │ │ └── Log.java
│ │ │ └── com
│ │ │ └── gmail
│ │ │ └── brunokawka
│ │ │ └── poland
│ │ │ └── sleepcyclealarm
│ │ │ ├── utils
│ │ │ ├── ThemeUtilsTest.java
│ │ │ ├── AlarmContentUtilsTest.java
│ │ │ ├── TimeUtilsRounderTest.java
│ │ │ └── itemsbuilder
│ │ │ │ └── ItemsBuilderTest.java
│ │ │ ├── tabs
│ │ │ └── adapters
│ │ │ │ └── AddAlarmsAdapterTest.java
│ │ │ └── MainPresenterTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── gmail
│ │ └── brunokawka
│ │ └── poland
│ │ └── sleepcyclealarm
│ │ └── utils
│ │ ├── AlarmContentUtilsTest.java
│ │ └── itemsbuilder
│ │ └── ItemsBuilderTest.java
├── .classpath
├── .project
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── preview
├── logo
│ ├── master.ai
│ ├── master.eps
│ ├── logomark.png
│ ├── logovertical.png
│ ├── logohorizontal.png
│ └── logomark.svg
└── screenshot.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── vcs.xml
├── compiler.xml
├── runConfigurations.xml
├── codeStyles
│ └── Project.xml
└── misc.xml
├── .settings
└── org.eclipse.buildship.core.prefs
├── .project
├── .travis.yml
├── gradle.properties
├── .gitignore
├── gradlew.bat
├── README.md
├── CODE_OF_CONDUCT.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/preview/logo/master.ai:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/preview/logo/master.ai
--------------------------------------------------------------------------------
/preview/logo/master.eps:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/preview/logo/master.eps
--------------------------------------------------------------------------------
/preview/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/preview/screenshot.png
--------------------------------------------------------------------------------
/app/.settings/org.eclipse.buildship.core.prefs:
--------------------------------------------------------------------------------
1 | connection.project.dir=..
2 | eclipse.preferences.version=1
3 |
--------------------------------------------------------------------------------
/preview/logo/logomark.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/preview/logo/logomark.png
--------------------------------------------------------------------------------
/preview/logo/logovertical.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/preview/logo/logovertical.png
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/app/src/main/res/font/roboto.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/res/font/roboto.ttf
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/preview/logo/logohorizontal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/preview/logo/logohorizontal.png
--------------------------------------------------------------------------------
/app/src/main/res/web_hi_res_512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/res/web_hi_res_512.png
--------------------------------------------------------------------------------
/app/src/main/res/font/roboto_bold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/res/font/roboto_bold.ttf
--------------------------------------------------------------------------------
/app/src/main/res/font/roboto_light.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/res/font/roboto_light.ttf
--------------------------------------------------------------------------------
/app/src/main/res/font/roboto_medium.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/res/font/roboto_medium.ttf
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/letelete/sleep-cycle-alarm/HEAD/app/src/main/res/mipmap-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFFFFF
4 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/events/RealmChangeEvent.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.events;
2 |
3 | public class RealmChangeEvent {
4 | }
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/events/SetHourButtonClickedEvent.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.events;
2 |
3 | public class SetHourButtonClickedEvent {
4 | }
5 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_fab_extended.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Sep 24 19:58:02 CEST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
7 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.buildship.core.prefs:
--------------------------------------------------------------------------------
1 | auto.sync=false
2 | build.scans.enabled=false
3 | connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
4 | connection.project.dir=
5 | eclipse.preferences.version=1
6 | gradle.user.home=
7 | offline.mode=false
8 | override.workspace.settings=true
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_close_activity.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/shadow.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/color/color_primary_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/color/navigation_bar_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/color/color_secondary_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/three_dot_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
8 |
9 |
--------------------------------------------------------------------------------
/app/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_add.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_fab_extended_drawable.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/divider.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_fab_extended_no_background_drawable.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_close.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/events/AddAlarmEvent.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.events;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
4 |
5 | public class AddAlarmEvent {
6 |
7 | private Item item;
8 |
9 | public AddAlarmEvent(Item item) {
10 | this.item = item;
11 | }
12 |
13 | public Item getItem() {
14 | return item;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | sleep-cycle-alarm
4 | Project sleep-cycle-alarm created by Buildship.
5 |
6 |
7 |
8 |
9 | org.eclipse.buildship.core.gradleprojectbuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.buildship.core.gradleprojectnature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/ui/dialog/ItemDialogContract.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.ui.dialog;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
4 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
5 |
6 | public interface ItemDialogContract {
7 |
8 | void bind(Item item);
9 |
10 | void bind(Alarm alarm);
11 |
12 | void setRingtone(String itemRingtone);
13 |
14 | String getRingtone();
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_list_ringtone.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_empty_alarms_list.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_bottom_navigation_sleepnow.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/ItemsBuilderStrategy.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
4 |
5 | import org.joda.time.DateTime;
6 |
7 | import java.util.List;
8 |
9 | public interface ItemsBuilderStrategy {
10 | List- getArrayOfItems(DateTime currentDate, DateTime executionDate);
11 |
12 | DateTime getNextAlarmDate(DateTime executionDate);
13 |
14 | boolean isPossibleToCreateNextItem(DateTime currentDate, DateTime dateToGoToSleep);
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_bottom_naviagation_alarms.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_bottom_navigation_wakeupat.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_list_alarm_add.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/Const.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | public class Const {
4 |
5 | public static class KEYS {
6 | public final static String ALARM_ID = "ALARM_ID";
7 | public static final String RINGTONE_ID = "RINGTONE_ID";
8 | }
9 |
10 | public static class DEFAULTS {
11 | public static final String ALARM_SOUND = "DEFAULT_SOUND";
12 | public final static int RING_DURATION_MS = 5 * 60 * 1000;
13 | public static final String THEME_ID = "1";
14 | public static final boolean ALARMS_HAVE_SAME_RINGTONE = false;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | jdk: oraclejdk8
3 | android:
4 | components:
5 | - tools
6 | - platform-tools
7 | - build-tools-28.0.3
8 | - android-28
9 | - android-22
10 | - extra-android-m2repository
11 | - sys-img-armeabi-v7a-android-22
12 |
13 | before_script:
14 | - echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a
15 | - emulator -avd test -no-skin -no-audio -no-window &
16 | - android-wait-for-emulator
17 | - adb shell input keyevent 82 &
18 |
19 | script:
20 | - ./gradlew build jacocoTestReport assembleAndroidTest
21 | - ./gradlew connectedCheck
22 |
23 | after_success:
24 | - bash <(curl -s https://codecov.io/bash)
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/app/RealmInitialData.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.app;
2 |
3 |
4 | import android.util.Log;
5 |
6 | import io.realm.Realm;
7 |
8 | public class RealmInitialData implements Realm.Transaction {
9 | @Override
10 | public void execute(Realm realm) {
11 | Log.d(getClass().getName(), "Execute RealmInitialData");
12 | }
13 |
14 | @Override
15 | public int hashCode() {
16 | return RealmInitialData.class.hashCode();
17 | }
18 |
19 | @Override
20 | public boolean equals(Object obj) {
21 | return obj instanceof RealmInitialData;
22 | }
23 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_list_alarm_access.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_empty_wakeupat_list.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | app
4 | Project app created by Buildship.
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.buildship.core.gradleprojectbuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.buildship.core.gradleprojectnature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_list_ringtone_full_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
7 |
8 |
10 |
11 |
14 |
15 |
16 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_list_alarm_add_full_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
7 |
8 |
10 |
11 |
14 |
15 |
16 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_close_activity_drawable.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
7 |
8 |
10 |
11 |
14 |
15 |
16 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_list_alarm_access_full_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
7 |
8 |
10 |
11 |
14 |
15 |
16 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/app/CustomApp.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.app;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | import java.lang.ref.WeakReference;
7 |
8 | import io.realm.Realm;
9 |
10 | public class CustomApp extends Application {
11 |
12 | private static WeakReference contextWeakReference;
13 |
14 | @Override
15 | public void onCreate() {
16 | super.onCreate();
17 | Realm.init(this);
18 | contextWeakReference = new WeakReference<>(getApplicationContext());
19 | }
20 |
21 | public static Context getContext() {
22 | return contextWeakReference.get();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/activealarms/AlarmsContract.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.activealarms;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
4 |
5 | public interface AlarmsContract {
6 | interface AlarmsView {
7 |
8 | void showEditAlarmDialog(Alarm alarm);
9 |
10 | void setupAdapter();
11 |
12 | }
13 |
14 | interface AlarmsPresenter {
15 | void bindView(AlarmsContract.AlarmsView viewContract);
16 |
17 | void unbindView();
18 |
19 | void handleRealmChange();
20 |
21 | void showEditDialog(Alarm alarm);
22 |
23 | void deleteAlarmById(final String id);
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_set_hour_to_wake_up_at.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/activealarms/AlarmScopeListener.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.activealarms;
2 |
3 | import android.support.v4.app.Fragment;
4 |
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.app.RealmManager;
6 |
7 | public class AlarmScopeListener extends Fragment {
8 | private AlarmsPresenter alarmsPresenter;
9 |
10 | public AlarmScopeListener() {
11 | setRetainInstance(true);
12 | RealmManager.incrementCount();
13 | alarmsPresenter = new AlarmsPresenter();
14 | }
15 |
16 | @Override
17 | public void onDestroy() {
18 | RealmManager.decrementCount();
19 | super.onDestroy();
20 | }
21 |
22 | public AlarmsPresenter getPresenter() {
23 | return alarmsPresenter;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/schedule/AlarmReceiver.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.schedule;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.util.Log;
8 |
9 | public class AlarmReceiver extends BroadcastReceiver {
10 |
11 | @Override
12 | public void onReceive(Context context, Intent intent) {
13 | Bundle extras = intent.getExtras();
14 | Intent service = new Intent(context, AlarmService.class);
15 | if (extras != null) {
16 | service.putExtras(extras);
17 | } else {
18 | Log.e(getClass().getName(), "onReceive(): extras == null");
19 | }
20 | context.startService(service);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/res/values/preference_keys.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | wakeupat_preferences
6 | key_last_execution_date
7 |
8 | key_select_theme
9 | key_alarm_vibrate_when_ringing
10 | key_alarms_has_same_ringtone
11 | key_ringtone_select
12 | key_ring_duration
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/bottom_navigation_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
9 |
14 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_fab_extended_no_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | -
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/test/java/android/util/Log.java:
--------------------------------------------------------------------------------
1 | package android.util;
2 |
3 | public class Log {
4 | public static int d(String tag, String msg) {
5 | System.out.println("DEBUG: " + tag + ": " + msg);
6 | return 0;
7 | }
8 |
9 | public static int i(String tag, String msg) {
10 | System.out.println("INFO: " + tag + ": " + msg);
11 | return 0;
12 | }
13 |
14 | public static int w(String tag, String msg) {
15 | System.out.println("WARN: " + tag + ": " + msg);
16 | return 0;
17 | }
18 |
19 | public static int e(String tag, String msg) {
20 | System.out.println("ERROR: " + tag + ": " + msg);
21 | return 0;
22 | }
23 |
24 | public static int wtf(String tag, String msg) {
25 | System.out.println("WHAT A TERRIBLE FAILURE: " + tag + ": " + msg);
26 | return 0;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/ThemeUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import android.support.v7.app.AppCompatDelegate;
4 |
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.assertEquals;
8 |
9 | public class ThemeUtilsTest {
10 |
11 | @Test
12 | public void testIfCanReturnCurrentTheme() {
13 | assertEquals(AppCompatDelegate.MODE_NIGHT_NO, ThemeUtils.getCurrentTheme("1"));
14 | assertEquals(AppCompatDelegate.MODE_NIGHT_AUTO, ThemeUtils.getCurrentTheme("2"));
15 | assertEquals(AppCompatDelegate.MODE_NIGHT_YES, ThemeUtils.getCurrentTheme("3"));
16 | }
17 |
18 | @Test
19 | public void testIfCanReturnDefaultMode() {
20 | assertEquals(AppCompatDelegate.MODE_NIGHT_NO, ThemeUtils.getCurrentTheme("0"));
21 | assertEquals(AppCompatDelegate.MODE_NIGHT_NO, ThemeUtils.getCurrentTheme("4"));
22 | }
23 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/data/ItemsBuilderData.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.data;
2 |
3 | public class ItemsBuilderData {
4 | private final static int MAX_AMOUNT_OF_ITEMS_IN_LIST = 8;
5 | private final static int ONE_SLEEP_CYCLE_DURATION_IN_MINUTES = 90;
6 | private final static int TIME_FOR_FALL_ASLEEP_IN_MINUTES = 15;
7 |
8 | public static int getMaxAmountOfItemsInList() {
9 | return MAX_AMOUNT_OF_ITEMS_IN_LIST;
10 | }
11 |
12 | public static int getOneSleepCycleDurationInMinutes() {
13 | return ONE_SLEEP_CYCLE_DURATION_IN_MINUTES;
14 | }
15 |
16 | public static int getTimeForFallAsleepInMinutes() {
17 | return TIME_FOR_FALL_ASLEEP_IN_MINUTES;
18 | }
19 |
20 | public static int getTotalOneSleepCycleDurationInMinutes() {
21 | return ONE_SLEEP_CYCLE_DURATION_IN_MINUTES + TIME_FOR_FALL_ASLEEP_IN_MINUTES;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/ItemsBuilder.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
4 |
5 | import org.joda.time.DateTime;
6 |
7 | import java.util.List;
8 |
9 | public class ItemsBuilder extends ItemsBuilderAbstraction {
10 |
11 | @Override
12 | public List- getItems(DateTime currentDate, DateTime executionDate) {
13 | return getBuildingStrategy().getArrayOfItems(currentDate, executionDate);
14 | }
15 |
16 | @Override
17 | public DateTime getNextAlarmDate(DateTime executionDate) {
18 | return getBuildingStrategy().getNextAlarmDate(executionDate);
19 | }
20 |
21 | @Override
22 | public boolean isPossibleToCreateNextItem(DateTime currentDate, DateTime executionDate) {
23 | return getBuildingStrategy().isPossibleToCreateNextItem(currentDate, executionDate);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/ThemeUtils.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import android.support.v7.app.AppCompatDelegate;
4 | import android.util.Log;
5 |
6 | public class ThemeUtils {
7 |
8 | public static int getCurrentTheme(String themeId) {
9 | int currentTheme;
10 |
11 | switch(themeId) {
12 | case "1":
13 | currentTheme = AppCompatDelegate.MODE_NIGHT_NO;
14 | break;
15 | case "2":
16 | currentTheme = AppCompatDelegate.MODE_NIGHT_AUTO;
17 | break;
18 | case "3":
19 | currentTheme = AppCompatDelegate.MODE_NIGHT_YES;
20 | break;
21 | default:
22 | Log.e(ThemeUtils.class.getName(), "Default case in setAppTheme switch");
23 | currentTheme = AppCompatDelegate.MODE_NIGHT_NO;
24 | break;
25 | }
26 |
27 | return currentTheme;
28 | }
29 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/ItemsBuilderAbstraction.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
4 |
5 | import org.joda.time.DateTime;
6 |
7 | import java.util.List;
8 |
9 | public abstract class ItemsBuilderAbstraction {
10 | private ItemsBuilderStrategy buildingStrategy;
11 |
12 | public void setBuildingStrategy(ItemsBuilderStrategy buildingStrategy) {
13 | this.buildingStrategy = buildingStrategy;
14 | }
15 |
16 | public ItemsBuilderStrategy getBuildingStrategy() {
17 | return this.buildingStrategy;
18 | }
19 |
20 | public abstract List
- getItems(DateTime currentDate, DateTime executionDate);
21 |
22 | public abstract DateTime getNextAlarmDate(DateTime executionDate);
23 |
24 | public abstract boolean isPossibleToCreateNextItem(DateTime currentDate,
25 | DateTime executionDate);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/fast_fade_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
20 |
21 |
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/app/base/MainContract.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.app.base;
2 |
3 | import android.os.Bundle;
4 |
5 | public interface MainContract {
6 |
7 | interface MainView {
8 | void setUpBottomNavigationBar();
9 | void openLatestFragment();
10 | void openDefaultFragment();
11 | void setUpToolbar();
12 | void openSleepNowFragment();
13 | void openWakeUpAtFragment();
14 | void openAlarmsFragment();
15 | void showWakeUpAtActionButton();
16 | void hideWakeUpAtActionButton();
17 | void openSettingsActivity();
18 | void showToastWithDoubleBackMessage();
19 | void countDownInMilliseconds(int seconds);
20 | void moveAppToBack();
21 | }
22 |
23 | interface MainPresenter {
24 | void setUpUi(Bundle savedInstanceState);
25 | void handleBottomNavigationTabClick(int menuItemId);
26 | void handleBackPress();
27 | void handleMenuItemClick(int menuItemId);
28 | void onCountedDown();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/data/pojo/Item.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo;
2 |
3 | import org.joda.time.DateTime;
4 |
5 | public class Item {
6 | private String title;
7 | private String summary;
8 | private DateTime currentDate;
9 | private DateTime executionDate;
10 |
11 | public String getTitle() {
12 | return title;
13 | }
14 |
15 | public void setTitle(String title) {
16 | this.title = title;
17 | }
18 |
19 | public String getSummary() {
20 | return summary;
21 | }
22 |
23 | public void setSummary(String summary) {
24 | this.summary = summary;
25 | }
26 |
27 | public DateTime getCurrentDate() {
28 | return currentDate;
29 | }
30 |
31 | public void setCurrentDate(DateTime currentDate) {
32 | this.currentDate = currentDate;
33 | }
34 |
35 | public DateTime getExecutionDate() {
36 | return executionDate;
37 | }
38 |
39 | public void setExecutionDate(DateTime executionDate) {
40 | this.executionDate = executionDate;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/app/src/main/res/values-night/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #000000
5 | #000000
6 |
7 | #111111
8 |
9 | #33ffffff
10 |
11 | @color/colorWhite87
12 | @color/colorWhite54
13 | @color/colorWhite38
14 |
15 | @color/colorBlack87
16 | @color/colorBlack54
17 | @color/colorBlack38
18 |
19 | @color/colorTextPrimary
20 | @color/colorWhite12
21 |
22 | #DEFFFFFF
23 | #DEFFFFFF
24 |
25 | #8AFFFFFF
26 |
27 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/adapters/AddAlarmsAdapterTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.adapters;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
4 |
5 | import org.junit.Test;
6 |
7 | import java.util.ArrayList;
8 | import java.util.Collections;
9 | import java.util.List;
10 |
11 | import static java.util.Arrays.asList;
12 | import static org.hamcrest.CoreMatchers.equalTo;
13 | import static org.junit.Assert.assertThat;
14 |
15 | public class AddAlarmsAdapterTest {
16 |
17 | @Test
18 | public void testIfCanReturnItemCount() {
19 | Item item = new Item();
20 |
21 | AddAlarmsAdapter addAlarmsAdapter = new AddAlarmsAdapter(asList(item, item, item));
22 | assertThat(addAlarmsAdapter.getItemCount(), equalTo(3));
23 |
24 | addAlarmsAdapter = new AddAlarmsAdapter(Collections.singletonList(item));
25 | assertThat(addAlarmsAdapter.getItemCount(), equalTo(1));
26 |
27 | List- emptyList = new ArrayList<>();
28 | addAlarmsAdapter = new AddAlarmsAdapter(emptyList);
29 | assertThat(addAlarmsAdapter.getItemCount(), equalTo(0));
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_alarms.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
23 |
24 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/addalarm/wakeupat/WakeUpAtContract.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.addalarm.wakeupat;
2 |
3 | import org.joda.time.DateTime;
4 |
5 | public interface WakeUpAtContract {
6 |
7 | interface WakeUpAtView {
8 |
9 | void updateCurrentDate();
10 |
11 | void showSetTimeDialog();
12 |
13 | void setLastExecutionDate(DateTime newDate);
14 |
15 | void setLastExecutionDateFromPreferences();
16 |
17 | void setupAdapter();
18 |
19 | void saveExecutionDateToPreferencesAsString();
20 |
21 | void updateDescription();
22 |
23 | interface DialogContract {
24 | DateTime getDateTime();
25 | }
26 | }
27 |
28 | interface WakeUpAtPresenter {
29 | void handleFloatingActionButtonClicked();
30 |
31 | void bindView(WakeUpAtContract.WakeUpAtView view);
32 |
33 | void unbindView();
34 |
35 | void setUpEnvironment();
36 |
37 | void showTimeDialog();
38 |
39 | void dismissTimeDialog();
40 |
41 | void tryToGenerateAListWithGivenValues(DateTime newChosenExecutionDate,
42 | DateTime currentDate, DateTime lastExecutionDate);
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_empty_state.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
23 |
24 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 24dp
5 |
6 | 20sp
7 | 16sp
8 | 14sp
9 | 14sp
10 | 12sp
11 | 12sp
12 |
13 | 4dp
14 | 8dp
15 | 16dp
16 | 24dp
17 | 48dp
18 |
19 | 40dp
20 | 24dp
21 |
22 | 3dp
23 | 7dp
24 |
25 | 14sp
26 |
27 | 12dp
28 | 36dp
29 |
30 | 38dp
31 | 38dp
32 | 144dp
33 |
34 | 64sp
35 | 64dp
36 | 8dp
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/activealarms/AlarmsPresenter.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.activealarms;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.AlarmDAO;
4 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
5 |
6 | public class AlarmsPresenter implements AlarmsContract.AlarmsPresenter {
7 |
8 | private AlarmsContract.AlarmsView view;
9 | private AlarmDAO alarmDAO;
10 |
11 | public static AlarmsPresenter getService() {
12 | return AlarmsFragment.getAlarmsPresenter();
13 | }
14 |
15 | @Override
16 | public void bindView(AlarmsContract.AlarmsView view) {
17 | this.view = view;
18 | alarmDAO = new AlarmDAO();
19 | }
20 |
21 | @Override
22 | public void unbindView() {
23 | this.view = null;
24 | alarmDAO.cleanUp();
25 | }
26 |
27 | @Override
28 | public void handleRealmChange() {
29 | if (view != null) {
30 | view.setupAdapter();
31 | }
32 | }
33 |
34 | @Override
35 | public void showEditDialog(Alarm alarm) {
36 | if(hasView()) {
37 | view.showEditAlarmDialog(alarm);
38 | }
39 | }
40 |
41 | @Override
42 | public void deleteAlarmById(final String id) {
43 | alarmDAO.removeFromRealmById(id);
44 | }
45 |
46 | private boolean hasView() {
47 | return view != null;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
12 | # Built application files
13 | *.apk
14 | *.ap_
15 |
16 | # Files for the ART/Dalvik VM
17 | *.dex
18 |
19 | # Java class files
20 | *.class
21 |
22 | # Generated files
23 | bin/
24 | gen/
25 | out/
26 |
27 | # Gradle files
28 | .gradle/
29 | build/
30 |
31 | # Local configuration file (sdk path, etc)
32 | local.properties
33 |
34 | # Proguard folder generated by Eclipse
35 | proguard/
36 |
37 | # Log Files
38 | *.log
39 |
40 | # Android Studio Navigation editor temp files
41 | .navigation/
42 |
43 | # Android Studio captures folder
44 | captures/
45 |
46 | # IntelliJ
47 | *.iml
48 | .idea/workspace.xml
49 | .idea/tasks.xml
50 | .idea/gradle.xml
51 | .idea/assetWizardSettings.xml
52 | .idea/dictionaries
53 | .idea/libraries
54 | .idea/caches
55 |
56 | # Keystore files
57 | # Uncomment the following line if you do not want to check your keystore files in.
58 | #*.jks
59 |
60 | # External native build folder generated in Android Studio 2.2 and later
61 | .externalNativeBuild
62 |
63 | # Google Services (e.g. APIs or Firebase)
64 | google-services.json
65 |
66 | # Freeline
67 | freeline.py
68 | freeline/
69 | freeline_project_description.json
70 |
71 | # fastlane
72 | fastlane/report.xml
73 | fastlane/Preview.html
74 | fastlane/screenshots
75 | fastlane/test_output
76 | fastlane/readme.md
77 |
78 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/grow_fade_in_center.xml:
--------------------------------------------------------------------------------
1 |
2 |
20 |
21 |
22 |
27 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/data/pojo/Alarm.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo;
2 |
3 | import io.realm.RealmObject;
4 | import io.realm.annotations.PrimaryKey;
5 | import io.realm.annotations.Required;
6 |
7 | public class Alarm extends RealmObject {
8 |
9 | @PrimaryKey
10 | @Required
11 | private String id;
12 |
13 | private String title;
14 | private String summary;
15 | private String ringtone;
16 | private String currentDate;
17 | private String executionDate;
18 |
19 | public String getId() {
20 | return id;
21 | }
22 |
23 | public void setId(String id) {
24 | this.id = id;
25 | }
26 |
27 | public String getTitle() {
28 | return title;
29 | }
30 |
31 | public void setTitle(String title) {
32 | this.title = title;
33 | }
34 |
35 | public String getSummary() {
36 | return summary;
37 | }
38 |
39 | public void setSummary(String summary) {
40 | this.summary = summary;
41 | }
42 |
43 | public String getRingtone() {
44 | return ringtone;
45 | }
46 |
47 | public void setRingtone(String ringtone) {
48 | this.ringtone = ringtone;
49 | }
50 |
51 | public String getCurrentDate() {
52 | return currentDate;
53 | }
54 |
55 | public void setCurrentDate(String currentDate) {
56 | this.currentDate = currentDate;
57 | }
58 |
59 | public String getExecutionDate() {
60 | return executionDate;
61 | }
62 |
63 | public void setExecutionDate(String executionDate) {
64 | this.executionDate = executionDate;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_wake_up_at.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
27 |
28 |
32 |
33 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_item_ringtone.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
21 |
22 |
29 |
30 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
18 |
19 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
32 |
33 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/TimeUtils.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import org.joda.time.DateTime;
4 |
5 | public class TimeUtils {
6 |
7 | public static DateTime getClosestTimeRound(DateTime dt) {
8 | if (isRoundToFullHourNeeded(dt)) {
9 | return getFullHourRound(dt);
10 | } else if (isRoundToHalfOfTenNeeded(dt)) {
11 | return getHalfOfTenRound(dt);
12 | } else {
13 | return getTheClosestTenRound(dt);
14 | }
15 | }
16 |
17 | private static boolean isRoundToFullHourNeeded(DateTime dt) {
18 | return dt.getMinuteOfHour() > 57 || dt.getMinuteOfHour() < 3;
19 | }
20 |
21 | private static boolean isRoundToHalfOfTenNeeded(DateTime dt) {
22 | int minuteOfMinutesOfHour = dt.getMinuteOfHour() % 10;
23 | return minuteOfMinutesOfHour >= 3 && minuteOfMinutesOfHour <= 7;
24 | }
25 |
26 | private static int getDifferenceBetweenMinuteAndHalfOfTen(int minute) {
27 | int halfOfTen = 5;
28 | return halfOfTen - (minute % 10);
29 | }
30 |
31 | private static DateTime getFullHourRound(DateTime dt) {
32 | return dt.hourOfDay().roundHalfFloorCopy();
33 | }
34 |
35 | private static DateTime getHalfOfTenRound(DateTime dt) {
36 | return dt.plusMinutes(getDifferenceBetweenMinuteAndHalfOfTen(dt.getMinuteOfHour()));
37 | }
38 |
39 | private static DateTime getTheClosestTenRound(DateTime dt) {
40 | int minuteOfMinutesOfHour = dt.getMinuteOfHour() % 10;
41 | int diffToTen = 10 - minuteOfMinutesOfHour;
42 | return minuteOfMinutesOfHour >= diffToTen
43 | ? dt.plusMinutes(diffToTen)
44 | : dt.minusMinutes(minuteOfMinutesOfHour);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_alarm.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
22 |
23 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_alarm.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
19 |
20 |
29 |
30 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/AlarmContentUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import android.util.Log;
4 |
5 | import org.joda.time.DateTime;
6 | import org.joda.time.format.DateTimeFormat;
7 | import org.junit.Test;
8 |
9 | import static org.hamcrest.CoreMatchers.is;
10 | import static org.junit.Assert.assertEquals;
11 | import static org.junit.Assert.assertThat;
12 |
13 | public class AlarmContentUtilsTest {
14 |
15 | private static DateTime getDateTime(String string) {
16 | return DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").parseDateTime(string);
17 | }
18 |
19 | @Test
20 | public void testIfCanReturnTitle() {
21 | assertEquals("06:30", AlarmContentUtils.getTitle(getDateTime("04/02/2011 06:30")));
22 | assertEquals("00:03", AlarmContentUtils.getTitle(getDateTime("04/02/2011 00:03")));
23 | assertEquals("23:59", AlarmContentUtils.getTitle(getDateTime("04/02/2011 23:59")));
24 | assertEquals("00:00", AlarmContentUtils.getTitle(getDateTime("04/02/2011 00:00")));
25 | }
26 |
27 | @Test
28 | public void testIfCanReturnTitleForWakeUpAt() {
29 | assertThat(AlarmContentUtils.getTitleForWakeUpAt(getDateTime("01/01/2001 23:00"), getDateTime("02/01/2001 6:30")),
30 | is("23:00 -> 06:30"));
31 | assertThat(AlarmContentUtils.getTitleForWakeUpAt(getDateTime("02/01/2001 00:00"), getDateTime("02/01/2001 01:30")),
32 | is("00:00 -> 01:30"));
33 | }
34 |
35 | @Test
36 | public void testIfCanReturnSimpleDate() {
37 | DateTime complexDate = DateTime.now();
38 | DateTime simpleDate = getDateTime(complexDate.getDayOfMonth() + "/" + complexDate.getMonthOfYear() + "/"
39 | + complexDate.getYear() + " " + complexDate.getHourOfDay() + ":" + complexDate.getMinuteOfHour());
40 | Log.d(getClass().getName(), simpleDate.toString());
41 | assertThat(AlarmContentUtils.getDateConvertedToSimple(complexDate),
42 | is(simpleDate));
43 | }
44 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #FFFFFF
5 | #FFFFFF
6 | #2ed573
7 |
8 | @color/colorPrimary
9 |
10 |
11 | #000000
12 | #DE000000
13 | #B3000000
14 | #8A000000
15 | #80000000
16 | #61000000
17 | #1F000000
18 |
19 |
20 | #FFFFFF
21 | #DEFFFFFF
22 | #B3FFFFFF
23 | #8AFFFFFF
24 | #8FFFFFF0
25 | #61FFFFFF
26 | #1FFFFFFF
27 |
28 | @color/colorBlack87
29 | @color/colorBlack54
30 | @color/colorBlack38
31 |
32 | @color/colorWhite100
33 | @color/colorWhite70
34 | @color/colorWhite50
35 |
36 | #82b1ff
37 | #1f000000
38 |
39 | #FFFFFF
40 |
41 | @color/colorTextPrimary
42 | @color/colorBlack12
43 |
44 | #DEFFFFFF
45 | #8A000000
46 |
47 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
11 |
20 |
21 |
28 |
29 |
34 |
35 |
40 |
41 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_sleep_now.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
14 |
15 |
19 |
20 |
27 |
28 |
34 |
35 |
36 |
37 |
38 |
39 |
48 |
49 |
53 |
54 |
58 |
59 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/SleepNowBuildingStrategy.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.ItemsBuilderData;
4 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
6 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.TimeUtils;
7 |
8 | import org.joda.time.DateTime;
9 |
10 | import java.util.ArrayList;
11 | import java.util.List;
12 |
13 | import javax.annotation.Nullable;
14 |
15 | public class SleepNowBuildingStrategy implements ItemsBuilderStrategy {
16 | private List- items;
17 | private DateTime currentDate;
18 | private DateTime executionDate;
19 |
20 | @Override
21 | public List
- getArrayOfItems(DateTime currentDate, @Nullable DateTime executionDate) {
22 | items = new ArrayList<>();
23 | this.currentDate = currentDate;
24 | this.executionDate = currentDate;
25 |
26 | for (int i = 0; i < ItemsBuilderData.getMaxAmountOfItemsInList(); i++) {
27 | if (isPossibleToCreateNextItem(currentDate, this.executionDate)) {
28 | this.executionDate = getNextAlarmDate(this.executionDate);
29 | createNextItemAndAddItToArray();
30 | }
31 | }
32 |
33 | return items;
34 | }
35 |
36 | @Override
37 | public DateTime getNextAlarmDate(DateTime executionDate) {
38 | return executionDate.plusMinutes(ItemsBuilderData.getOneSleepCycleDurationInMinutes());
39 | }
40 |
41 | @Override
42 | public boolean isPossibleToCreateNextItem(DateTime currentDate, DateTime dateToGoToSleep) {
43 | return true;
44 | }
45 |
46 | private void createNextItemAndAddItToArray() {
47 | DateTime executionDatePlusTimeToFallAsleepWithRoundedTime = TimeUtils
48 | .getClosestTimeRound(executionDate
49 | .plusMinutes(ItemsBuilderData.getTimeForFallAsleepInMinutes()));
50 |
51 | Item item = new Item();
52 |
53 | item.setTitle(AlarmContentUtils.getTitle(executionDatePlusTimeToFallAsleepWithRoundedTime));
54 | item.setSummary(AlarmContentUtils.getSummary(currentDate, executionDate));
55 | item.setCurrentDate(currentDate);
56 | item.setExecutionDate(executionDatePlusTimeToFallAsleepWithRoundedTime);
57 |
58 | items.add(item);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/app/RealmManager.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.app;
2 |
3 | import android.util.Log;
4 |
5 | import io.realm.Realm;
6 | import io.realm.RealmConfiguration;
7 |
8 | public class RealmManager {
9 |
10 | private static Realm realm;
11 | private static RealmConfiguration realmConfiguration;
12 | private static int activityCount = 0;
13 |
14 | public static void initializeRealmConfig() {
15 | if(realmConfiguration == null) {
16 | Log.d(RealmManager.class.getName(), "Initializing Realm configuration.");
17 | setRealmConfiguration(new RealmConfiguration.Builder()
18 | .initialData(new RealmInitialData())
19 | .deleteRealmIfMigrationNeeded()
20 | .build());
21 | }
22 | }
23 |
24 | private static void setRealmConfiguration(RealmConfiguration realmConfiguration) {
25 | RealmManager.realmConfiguration = realmConfiguration;
26 | Realm.setDefaultConfiguration(realmConfiguration);
27 | }
28 |
29 | public static Realm getRealm() {
30 | return realm;
31 | }
32 |
33 | public static void incrementCount() {
34 | if(activityCount == 0) {
35 | if(realm != null && !realm.isClosed()) {
36 | Log.w(RealmManager.class.getName(), "Unexpected open Realm found.");
37 | realm.close();
38 | }
39 | Log.d(RealmManager.class.getName(),
40 | "Incrementing Activity Count [0]: opening Realm.");
41 | realm = Realm.getDefaultInstance();
42 | }
43 | activityCount++;
44 | Log.d(RealmManager.class.getName(), "Increment: Count [" + activityCount + "]");
45 | }
46 |
47 | public static void decrementCount() {
48 | activityCount--;
49 | Log.d(RealmManager.class.getName(), "Decrement: Count [" + activityCount + "]");
50 | if(activityCount <= 0) {
51 | Log.d(RealmManager.class.getName(), "Decrementing Activity Count: closing Realm.");
52 | activityCount = 0;
53 | realm.close();
54 | // realmConfiguration null on Android 6 (Test with xiaomi Mi4)
55 | if (realmConfiguration == null) initializeRealmConfig();
56 | if(Realm.compactRealm(realmConfiguration)) {
57 | Log.d(RealmManager.class.getName(), "Realm compacted successfully.");
58 | }
59 | realm = null;
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/data/AlarmDAO.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.data;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.util.Log;
5 |
6 | import com.gmail.brunokawka.poland.sleepcyclealarm.app.RealmManager;
7 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
8 |
9 | import java.util.List;
10 |
11 | import io.realm.Realm;
12 | import io.realm.RealmQuery;
13 |
14 | public class AlarmDAO {
15 |
16 | public AlarmDAO() {
17 | RealmManager.incrementCount();
18 | }
19 |
20 | public void saveIfNotDuplicate(final Alarm alarm) {
21 | Realm realm = RealmManager.getRealm();
22 | realm.executeTransactionAsync(new Realm.Transaction() {
23 | @Override
24 | public void execute(@NonNull Realm realm) {
25 | if (isNotDuplicate(alarm, realm)) {
26 | realm.insertOrUpdate(alarm);
27 | }
28 | }
29 | });
30 | }
31 |
32 | private boolean isNotDuplicate(Alarm alarm, Realm realm) {
33 | Alarm duplicateAlarm = realm.where(Alarm.class)
34 | .equalTo("executionDate", alarm.getExecutionDate()).findFirst();
35 | return duplicateAlarm == null;
36 | }
37 |
38 | public void saveEvenIfDuplicate(final Alarm alarm) {
39 | Realm realm = RealmManager.getRealm();
40 | realm.executeTransactionAsync(new Realm.Transaction() {
41 | @Override
42 | public void execute(@NonNull Realm realm) {
43 | realm.insertOrUpdate(alarm);
44 | }
45 | });
46 | }
47 |
48 | public void removeFromRealmById(final String id) {
49 | Log.d(getClass().getName(), "Removing from realm...");
50 | Realm realm = RealmManager.getRealm();
51 |
52 | realm.executeTransactionAsync(new Realm.Transaction() {
53 | @Override
54 | public void execute(@NonNull Realm realm) {
55 | Alarm alarm = realm.where(Alarm.class).equalTo("id", id).findFirst();
56 | if(alarm != null) {
57 | alarm.deleteFromRealm();
58 | }
59 | }
60 | });
61 | }
62 |
63 | public List
getListOfAlarms() {
64 | Realm realm = RealmManager.getRealm();
65 | RealmQuery query = realm.where(Alarm.class);
66 | return query.findAll();
67 | }
68 |
69 | public void cleanUp() {
70 | RealmManager.decrementCount();
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/AlarmContentUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.ItemsBuilderData;
4 |
5 | import org.joda.time.DateTime;
6 | import org.joda.time.format.DateTimeFormat;
7 | import org.junit.Test;
8 |
9 | import java.util.Arrays;
10 | import java.util.List;
11 |
12 | import static org.junit.Assert.assertEquals;
13 |
14 | public class AlarmContentUtilsTest {
15 |
16 | private static final String CURRENT_DATE_STRING = "01/01/1111 22:20";
17 |
18 | private static final List SUMMARY_VARIABLES_FOR_CURRENT_DATE_EQUALS_2220 = Arrays.asList(
19 | "02/01/1111 00:05", "1h 30min", "Unhealthy",
20 | "02/01/1111 01:35", "3h", "Unhealthy",
21 | "02/01/1111 03:05", "4h 30min", "Optimal",
22 | "02/01/1111 04:35", "6h", "Optimal",
23 | "02/01/1111 06:05", "7h 30min", "Healthy",
24 | "02/01/1111 07:35", "9h", "Healthy",
25 | "02/01/1111 09:05", "10h 30min", "Not recommended",
26 | "02/01/1111 10:35", "12h", "Not recommended"
27 | );
28 |
29 | private static DateTime getDateTime(String string) {
30 | return DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").parseDateTime(string);
31 | }
32 |
33 | @Test
34 | public void testIfCanReturnSummaries() {
35 | int amountOfUsedItemsOnOneLoopRun = 3;
36 | int maxIndex = SUMMARY_VARIABLES_FOR_CURRENT_DATE_EQUALS_2220.size() - 1;
37 |
38 | String executionDateString;
39 | String sleepDurationString;
40 | String sleepQuality;
41 | String expected;
42 |
43 | DateTime executionDateWithoutTimeToFallAsleep;
44 |
45 | for (int index = 0; index < maxIndex; index += amountOfUsedItemsOnOneLoopRun) {
46 | executionDateString = SUMMARY_VARIABLES_FOR_CURRENT_DATE_EQUALS_2220.get(index);
47 | sleepDurationString = SUMMARY_VARIABLES_FOR_CURRENT_DATE_EQUALS_2220.get(index+1);
48 | sleepQuality = SUMMARY_VARIABLES_FOR_CURRENT_DATE_EQUALS_2220.get(index+2);
49 |
50 | expected = String.format("%1$s of sleep | %2$s", sleepDurationString, sleepQuality);
51 | executionDateWithoutTimeToFallAsleep = getDateTime(executionDateString).minusMinutes(ItemsBuilderData.getTimeForFallAsleepInMinutes());
52 |
53 | assertEquals(expected, AlarmContentUtils.getSummary(getDateTime(CURRENT_DATE_STRING), executionDateWithoutTimeToFallAsleep));
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/schedule/AlarmController.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.schedule;
2 |
3 | import android.app.AlarmManager;
4 | import android.app.PendingIntent;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.Build;
8 |
9 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
10 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.Const;
11 |
12 | import org.joda.time.DateTime;
13 |
14 | import java.util.UUID;
15 |
16 | public class AlarmController {
17 |
18 | private Context context;
19 | private AlarmManager alarmManager;
20 |
21 | public AlarmController(Context context) {
22 | this.context = context;
23 | alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
24 | }
25 |
26 | public void setAlarm(Alarm alarm) {
27 | PendingIntent setAlarmPendingIntent
28 | = getAlarmPendingIntent(alarm, PendingIntent.FLAG_UPDATE_CURRENT);
29 | long executionDateMs = DateTime.parse(alarm.getExecutionDate()).getMillis();
30 |
31 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
32 | alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, executionDateMs,
33 | setAlarmPendingIntent);
34 | } else {
35 | alarmManager.setExact(AlarmManager.RTC_WAKEUP, executionDateMs, setAlarmPendingIntent);
36 | }
37 | }
38 |
39 | public void cancelAlarm(Alarm alarm) {
40 | PendingIntent cancelServicePendingIntent = getAlarmPendingIntent(alarm, 0);
41 | alarmManager.cancel(cancelServicePendingIntent);
42 | }
43 |
44 | public void dismissCurrentlyPlayingAlarm() {
45 | context.stopService(new Intent(context, AlarmService.class));
46 | }
47 |
48 | private PendingIntent getAlarmPendingIntent(Alarm alarm, int flag) {
49 | String alarmId = alarm.getId();
50 | String ringtone = alarm.getRingtone();
51 | Intent alarmIntent = new Intent(context, AlarmReceiver.class);
52 | alarmIntent.putExtra(Const.KEYS.ALARM_ID, alarmId);
53 | alarmIntent.putExtra(Const.KEYS.RINGTONE_ID, ringtone);
54 |
55 | int serviceId = getMostSignificantBits(alarmId);
56 |
57 | return PendingIntent.getBroadcast(context, serviceId, alarmIntent, flag);
58 | }
59 |
60 | private int getMostSignificantBits(String id) {
61 | return (int) Math
62 | .abs(UUID.fromString(id).getMostSignificantBits());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/addalarm/sleepnow/SleepNowFragment.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.addalarm.sleepnow;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.v7.widget.LinearLayoutManager;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
11 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
12 | import com.gmail.brunokawka.poland.sleepcyclealarm.tabs.adapters.AddAlarmsAdapter;
13 | import com.gmail.brunokawka.poland.sleepcyclealarm.tabs.addalarm.AddDialogFragment;
14 | import com.gmail.brunokawka.poland.sleepcyclealarm.tabs.ui.EmptyStateRecyclerView;
15 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
16 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder.ItemsBuilder;
17 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder.SleepNowBuildingStrategy;
18 |
19 | import org.joda.time.DateTime;
20 |
21 | import java.util.List;
22 |
23 | import butterknife.BindView;
24 | import butterknife.ButterKnife;
25 |
26 | public class SleepNowFragment extends AddDialogFragment {
27 |
28 | private ItemsBuilder itemsBuilder;
29 |
30 | @BindView(R.id.rv_sleepnow)
31 | EmptyStateRecyclerView recycler;
32 |
33 | @Override
34 | public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container,
35 | Bundle savedInstanceState) {
36 | View view = layoutInflater.inflate(R.layout.fragment_sleep_now, container, false);
37 | ButterKnife.bind(this, view);
38 |
39 | itemsBuilder = new ItemsBuilder();
40 | itemsBuilder.setBuildingStrategy(new SleepNowBuildingStrategy());
41 |
42 | return view;
43 | }
44 |
45 | @Override
46 | public void onActivityCreated(Bundle savedInstanceState) {
47 | super.onActivityCreated(savedInstanceState);
48 | setupRecycler();
49 | }
50 |
51 | private void setupRecycler() {
52 | recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
53 | DateTime currentDateFormatted = AlarmContentUtils.getDateConvertedToSimple(DateTime.now());
54 | List- items = itemsBuilder.getItems(currentDateFormatted, null);
55 | recycler.setAdapter(new AddAlarmsAdapter(items));
56 | }
57 |
58 | @Override
59 | public void onDestroyView() {
60 | super.onDestroyView();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/addalarm/wakeupat/WakeUpAtSetTimeView.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.addalarm.wakeupat;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.support.annotation.Nullable;
6 | import android.util.AttributeSet;
7 | import android.widget.LinearLayout;
8 | import android.widget.TimePicker;
9 |
10 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
11 |
12 | import org.joda.time.DateTime;
13 |
14 | import butterknife.BindView;
15 | import butterknife.ButterKnife;
16 |
17 | public class WakeUpAtSetTimeView extends LinearLayout
18 | implements WakeUpAtContract.WakeUpAtView.DialogContract {
19 |
20 | @BindView(R.id.dateTimeSelect) protected TimePicker timePicker;
21 |
22 | private DateTime dateTime;
23 |
24 | public WakeUpAtSetTimeView(Context context) {
25 | super(context);
26 | }
27 |
28 | public WakeUpAtSetTimeView(Context context, @Nullable AttributeSet attrs) {
29 | super(context, attrs);
30 | }
31 |
32 | public WakeUpAtSetTimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
33 | super(context, attrs, defStyleAttr);
34 | }
35 |
36 | @TargetApi(21)
37 | public WakeUpAtSetTimeView(Context context, AttributeSet attrs,
38 | int defStyleAttr, int defStyleRes) {
39 | super(context, attrs, defStyleAttr, defStyleRes);
40 | }
41 |
42 | @Override
43 | protected void onFinishInflate() {
44 | super.onFinishInflate();
45 | ButterKnife.bind(this);
46 |
47 | timePicker.setIs24HourView(true);
48 | timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
49 | @Override
50 | public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
51 | setDateTime(hourOfDay, minute);
52 | }
53 | });
54 | }
55 |
56 | private void setDateTime(int hourOfDay, int minute) {
57 | DateTime currentDate = DateTime.now();
58 | DateTime pickedDate = currentDate.withHourOfDay(hourOfDay)
59 | .withMinuteOfHour(minute);
60 |
61 | this.dateTime = currentDate.getHourOfDay() > hourOfDay
62 | ? pickedDate.plusDays(1)
63 | : pickedDate;
64 | }
65 |
66 | @Override
67 | public DateTime getDateTime() {
68 | if (dateTime == null) {
69 | dateTime = DateTime.now();
70 | }
71 | return dateTime;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/ui/WakeUpAtSetHourButton.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.ui;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.ObjectAnimator;
6 | import android.view.View;
7 | import android.widget.Button;
8 |
9 | public class WakeUpAtSetHourButton extends AnimatorListenerAdapter {
10 |
11 | private final int ANIM_TIME_IN_MS = 400;
12 | private final float POS_TO_MAKE_BUTTON_GONE = 200f;
13 | private final float POS_TO_MAKE_BUTTON_VISIBLE = -150f;
14 |
15 | private int wakeUpAtButtonVisibilityAfterStartedAnimation = View.GONE;
16 | private ObjectAnimator wakeUpAtButtonAnimator;
17 | private Button wakeUpAtButton;
18 | private int finalViewId;
19 |
20 | public WakeUpAtSetHourButton(Button wakeUpAtButton) {
21 | this.wakeUpAtButton = wakeUpAtButton;
22 | }
23 |
24 | public void show() {
25 | this.finalViewId = View.VISIBLE;
26 | animateWakeUpAtButton(POS_TO_MAKE_BUTTON_GONE, POS_TO_MAKE_BUTTON_VISIBLE);
27 | }
28 |
29 | public void hide() {
30 | this.finalViewId = View.GONE;
31 | animateWakeUpAtButton(POS_TO_MAKE_BUTTON_VISIBLE, POS_TO_MAKE_BUTTON_GONE);
32 | }
33 |
34 | private void animateWakeUpAtButton(final float startPosY, final float endPosY) {
35 | if (wakeUpAtButtonVisibilityAfterStartedAnimation != finalViewId) {
36 | cancelAnimationIfNeeded();
37 |
38 | wakeUpAtButtonVisibilityAfterStartedAnimation = finalViewId;
39 | wakeUpAtButtonAnimator = ObjectAnimator.ofFloat(wakeUpAtButton,
40 | "translationY", startPosY, endPosY);
41 | wakeUpAtButtonAnimator.setDuration(ANIM_TIME_IN_MS);
42 | wakeUpAtButtonAnimator.addListener(this);
43 | wakeUpAtButtonAnimator.start();
44 | }
45 | }
46 |
47 | private void cancelAnimationIfNeeded() {
48 | if (wakeUpAtButtonAnimator !=null && wakeUpAtButtonAnimator.isRunning()){
49 | wakeUpAtButtonAnimator.cancel();
50 | }
51 | }
52 |
53 | @Override
54 | public void onAnimationEnd(Animator animation) {
55 | if (finalViewId != View.VISIBLE) {
56 | wakeUpAtButton.setVisibility(View.GONE);
57 | }
58 | }
59 | @Override
60 | public void onAnimationStart(Animator animation) {
61 | if (wakeUpAtButton.getVisibility() != View.VISIBLE) {
62 | wakeUpAtButton.setVisibility(View.VISIBLE);
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/settings/SettingsActivity.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.settings;
2 |
3 | import android.content.SharedPreferences;
4 | import android.os.Bundle;
5 | import android.preference.PreferenceManager;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v4.app.FragmentTransaction;
8 | import android.support.v7.app.ActionBar;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.support.v7.widget.Toolbar;
11 |
12 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
13 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.Const;
14 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.ThemeUtils;
15 |
16 | import butterknife.BindView;
17 | import butterknife.ButterKnife;
18 |
19 | public class SettingsActivity extends AppCompatActivity {
20 |
21 |
22 | @BindView(R.id.tb_settings)
23 | Toolbar tbSettings;
24 |
25 | @Override
26 | public void onCreate(Bundle savedInstanceState) {
27 | setAppTheme();
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_settings);
30 | ButterKnife.bind(this);
31 |
32 | setupToolbar();
33 |
34 | if (savedInstanceState == null) {
35 | Fragment fragment = getSupportFragmentManager().findFragmentByTag(SettingsFragment.TAG);
36 | if (fragment == null) {
37 | fragment = new SettingsFragment();
38 | }
39 |
40 | FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
41 | ft.replace(R.id.fl_settings_container, fragment, SettingsFragment.TAG);
42 | ft.commit();
43 | }
44 | }
45 |
46 | private void setAppTheme() {
47 | SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
48 | String key = getString(R.string.key_change_theme);
49 | int themeId = ThemeUtils.getCurrentTheme(pref.getString(key, Const.DEFAULTS.THEME_ID));
50 | getDelegate().setLocalNightMode(themeId);
51 | }
52 |
53 | private void setupToolbar() {
54 | setSupportActionBar(tbSettings);
55 | ActionBar ab = getSupportActionBar();
56 | if (ab != null) {
57 | ab.setHomeAsUpIndicator(R.drawable.ic_close);
58 | ab.setTitle(getString(R.string.menu_settings));
59 | ab.setDisplayShowTitleEnabled(true);
60 | ab.setDisplayHomeAsUpEnabled(true);
61 | }
62 | }
63 |
64 | @Override
65 | public boolean onSupportNavigateUp() {
66 | onBackPressed();
67 | return true;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/addalarm/wakeupat/WakeUpAtPresenter.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.addalarm.wakeupat;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder.ItemsBuilder;
4 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder.WakeUpAtBuildingStrategy;
5 |
6 | import org.joda.time.DateTime;
7 |
8 | public class WakeUpAtPresenter implements WakeUpAtContract.WakeUpAtPresenter {
9 |
10 | private WakeUpAtContract.WakeUpAtView view;
11 | private boolean isDialogShowing;
12 | private ItemsBuilder itemsBuilder;
13 |
14 | @Override
15 | public void handleFloatingActionButtonClicked() {
16 | view.updateCurrentDate();
17 | showTimeDialog();
18 | }
19 |
20 | private boolean hasView() {
21 | return view != null;
22 | }
23 |
24 | @Override
25 | public void bindView(WakeUpAtContract.WakeUpAtView view) {
26 | this.view = view;
27 |
28 | this.itemsBuilder = new ItemsBuilder();
29 | itemsBuilder.setBuildingStrategy(new WakeUpAtBuildingStrategy());
30 |
31 | if (isDialogShowing) {
32 | showTimeDialog();
33 | }
34 | }
35 |
36 | public void unbindView() {
37 | this.view = null;
38 | }
39 |
40 | @Override
41 | public void setUpEnvironment() {
42 | view.updateCurrentDate();
43 | view.setLastExecutionDateFromPreferences();
44 | }
45 |
46 | @Override
47 | public void showTimeDialog() {
48 | if (hasView() && !isDialogShowing) {
49 | isDialogShowing = true;
50 | view.showSetTimeDialog();
51 | }
52 | }
53 |
54 | @Override
55 | public void dismissTimeDialog() {
56 | isDialogShowing = false;
57 | }
58 |
59 | @Override
60 | public void tryToGenerateAListWithGivenValues(DateTime newChosenExecutionDate,
61 | DateTime currentDate,
62 | DateTime lastExecutionDate) {
63 | if (itemsBuilder.isPossibleToCreateNextItem(currentDate, newChosenExecutionDate)) {
64 | updateLastExecutionDate(lastExecutionDate, newChosenExecutionDate);
65 | view.setupAdapter();
66 | }
67 | }
68 |
69 | private void updateLastExecutionDate(DateTime lastExecutionDate, DateTime newExecutionDate) {
70 | if (!newExecutionDate.equals(lastExecutionDate)) {
71 | view.setLastExecutionDate(newExecutionDate);
72 | view.saveExecutionDateToPreferencesAsString();
73 | view.updateDescription();
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 |
Sleep Cycle Alarm is a free material-designed Android app which helps waking up during a light sleep, it also is ad-free and open source under GPLv3 license.
4 |
5 |
6 |
7 |
9 |
10 |
12 |
13 |
15 |
16 |
18 |
19 |
21 |
22 |
24 |
25 |
26 | #### Features
27 |
28 | * Custom alarm management system
29 | * Alarms customatization
30 | * Automatically cleans up used up alarms
31 | * Adds new alarm with only two taps from user
32 | * Shows list of hours to set alarm at to wake up with light sleep cycle
33 | * Shows list of hours to go to sleep at when user wants to wake up at specific hour
34 | * Dark and light theme
35 | * Informs user about sleep duration and quality of specific alarm
36 |
37 | #### Dependencies
38 |
39 | * [Butterknife](https://github.com/JakeWharton/butterknife)
40 | * [Realm](https://github.com/realm/realm-java)
41 | * [Android-Support-Preference-V7-Fix](https://github.com/Gericop/Android-Support-Preference-V7-Fix)
42 | * [Joda-time](https://github.com/JodaOrg/joda-time)
43 | * [EventBus](https://github.com/greenrobot/EventBus)
44 |
45 | #### Contributing
46 |
47 | You can trace the status of known issues [here](https://github.com/letelete/Sleep-Cycle-Alarm/issues),
48 | also feel free to file a new issue (helpful description, screenshots and logcat are appreciated), or join to my discord
49 | and send a message on #programming text channel if you have any questions.
51 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'realm-android'
3 | apply plugin: 'jacoco-android'
4 |
5 | android {
6 | compileSdkVersion 28
7 | buildToolsVersion "28.0.3"
8 |
9 | defaultConfig {
10 | vectorDrawables.useSupportLibrary = true
11 | applicationId "com.gmail.brunokawka.poland.sleepcyclealarm"
12 | minSdkVersion 21
13 | targetSdkVersion 28
14 | versionCode 1
15 | versionName "0.9"
16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
17 |
18 | lintOptions {
19 | abortOnError false
20 | }
21 | }
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26 | }
27 | debug {
28 | testCoverageEnabled true
29 | }
30 | }
31 | }
32 |
33 | realm {
34 | syncEnabled = true
35 | }
36 |
37 | dependencies {
38 | implementation fileTree(dir: 'libs', include: ['*.jar'])
39 |
40 | testImplementation 'junit:junit:4.12'
41 | testImplementation 'org.mockito:mockito-core:2.22.0'
42 | androidTestImplementation 'org.mockito:mockito-android:2.19.0'
43 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
44 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
45 | androidTestImplementation 'com.android.support.test:rules:1.0.2'
46 |
47 | implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
48 | implementation "com.android.support:animated-vector-drawable:$rootProject.ext.supportLibraryVersion"
49 | implementation "com.android.support:cardview-v7:$rootProject.ext.supportLibraryVersion"
50 | implementation "com.android.support:support-v4:$rootProject.ext.supportLibraryVersion"
51 | implementation "com.android.support:recyclerview-v7:$rootProject.ext.supportLibraryVersion"
52 | implementation "com.android.support:design:$rootProject.ext.supportLibraryVersion"
53 | implementation 'com.android.support:multidex:1.0.3'
54 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
55 |
56 | implementation 'com.jakewharton:butterknife:8.8.1'
57 | implementation 'io.realm:android-adapters:3.0.0'
58 | implementation 'com.takisoft.fix:preference-v7:28.0.0.0'
59 | implementation 'com.takisoft.fix:preference-v7-ringtone:28.0.0.0-alpha3'
60 | implementation group: 'joda-time', name: 'joda-time', version: '2.10'
61 | implementation 'org.greenrobot:eventbus:3.1.1'
62 |
63 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
64 | implementation 'org.jetbrains:annotations-java5:15.0'
65 | }
66 |
--------------------------------------------------------------------------------
/preview/logo/logomark.svg:
--------------------------------------------------------------------------------
1 | master
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/app/base/MainPresenter.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.app.base;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
6 |
7 |
8 | public class MainPresenter implements MainContract.MainPresenter {
9 |
10 | private MainContract.MainView view;
11 | private boolean isButtonAfterFirstPress;
12 |
13 | public MainPresenter(MainContract.MainView view) {
14 | this.view = view;
15 | }
16 |
17 | @Override
18 | public void setUpUi(Bundle savedInstanceState) {
19 | view.setUpBottomNavigationBar();
20 | view.setUpToolbar();
21 | handleFragmentToDisplay(savedInstanceState);
22 | }
23 |
24 | private void handleFragmentToDisplay(Bundle savedInstanceState) {
25 | if (savedInstanceState == null) {
26 | view.openDefaultFragment();
27 | } else {
28 | view.openLatestFragment();
29 | }
30 | }
31 |
32 | @Override
33 | public void handleMenuItemClick(int menuItemId) {
34 | switch (menuItemId) {
35 | case R.id.menu_settings:
36 | view.openSettingsActivity();
37 | break;
38 | default:
39 | break;
40 | }
41 | }
42 |
43 | @Override
44 | public void handleBottomNavigationTabClick(int menuItemId) {
45 | switch (menuItemId) {
46 | case R.id.action_wakeupat:
47 | view.openWakeUpAtFragment();
48 | break;
49 | case R.id.action_alarms:
50 | view.openAlarmsFragment();
51 | break;
52 | case R.id.action_sleepnow:
53 | default:
54 | view.openSleepNowFragment();
55 | break;
56 | }
57 |
58 | handleWakeUpAtButtonVisibility(menuItemId);
59 | }
60 |
61 | private void handleWakeUpAtButtonVisibility(int menuItemId) {
62 | if (isWakeUpAtTabOpen(menuItemId)) {
63 | view.showWakeUpAtActionButton();
64 | } else {
65 | view.hideWakeUpAtActionButton();
66 | }
67 | }
68 |
69 | private boolean isWakeUpAtTabOpen(int menuItemId) {
70 | return menuItemId == R.id.action_wakeupat;
71 | }
72 |
73 | @Override
74 | public void handleBackPress() {
75 | int milliseconds = 2000;
76 |
77 | if (isButtonAfterFirstPress) {
78 | view.moveAppToBack();
79 | } else {
80 | view.showToastWithDoubleBackMessage();
81 | isButtonAfterFirstPress = true;
82 | view.countDownInMilliseconds(milliseconds);
83 | }
84 | }
85 |
86 | @Override
87 | public void onCountedDown() {
88 | isButtonAfterFirstPress = false;
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/preferences.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
9 |
10 |
19 |
20 |
21 |
22 |
25 |
26 |
31 |
32 |
43 |
44 |
49 |
50 |
51 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/adapters/AddAlarmsAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.adapters;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.constraint.ConstraintLayout;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 |
12 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
13 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
14 | import com.gmail.brunokawka.poland.sleepcyclealarm.events.AddAlarmEvent;
15 |
16 | import org.greenrobot.eventbus.EventBus;
17 |
18 | import java.util.List;
19 |
20 | import butterknife.BindView;
21 | import butterknife.ButterKnife;
22 |
23 | public class AddAlarmsAdapter extends RecyclerView.Adapter {
24 |
25 | private List- listItems;
26 |
27 | public AddAlarmsAdapter(List
- listItems) {
28 | this.listItems = listItems;
29 | }
30 |
31 | @NonNull
32 | @Override
33 | public ListAdapterHolder onCreateViewHolder(@NonNull final ViewGroup viewGroup,
34 | final int index) {
35 | return new ListAdapterHolder(LayoutInflater.from(viewGroup.getContext())
36 | .inflate(R.layout.item_alarm, viewGroup, false));
37 | }
38 |
39 | @Override
40 | public void onBindViewHolder(@NonNull ListAdapterHolder listAdapterHolder, int position) {
41 | final Item item = listItems.get(position);
42 | if (item != null) {
43 | listAdapterHolder.bind(item);
44 | }
45 | }
46 |
47 | public class ListAdapterHolder extends RecyclerView.ViewHolder {
48 |
49 | @BindView(R.id.cl_item_alarm_root)
50 | ConstraintLayout clRoot;
51 |
52 | @BindView(R.id.iv_item_alarm_icon)
53 | ImageView ivIcon;
54 |
55 | @BindView(R.id.tv_item_alarm_title)
56 | TextView tvTitle;
57 |
58 | @BindView(R.id.tv_item_alarm_subtitle)
59 | TextView tvSubtitle;
60 |
61 | ListAdapterHolder(View view) {
62 | super(view);
63 | ButterKnife.bind(this, view);
64 | }
65 |
66 | public void bind(final Item item) {
67 | ivIcon.setImageResource(R.drawable.ic_list_alarm_add_full_shape);
68 | tvTitle.setText(item.getTitle());
69 | tvSubtitle.setText(item.getSummary());
70 |
71 | clRoot.setOnClickListener(new View.OnClickListener() {
72 | @Override
73 | public void onClick(View view) {
74 | EventBus.getDefault().post(new AddAlarmEvent(item));
75 | }
76 | });
77 | }
78 | }
79 |
80 | @Override
81 | public int getItemCount() {
82 | return listItems.size();
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/WakeUpAtBuildingStrategy.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.ItemsBuilderData;
4 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
6 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.TimeUtils;
7 |
8 | import org.joda.time.DateTime;
9 | import org.joda.time.Interval;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | public class WakeUpAtBuildingStrategy implements ItemsBuilderStrategy {
15 | private List
- items;
16 | private DateTime executionDate;
17 | private DateTime timeToGoToSleep;
18 |
19 | @Override
20 | public List
- getArrayOfItems(DateTime currentDate, DateTime executionDate) {
21 | items = new ArrayList<>();
22 | this.executionDate = executionDate;
23 | this.timeToGoToSleep = executionDate;
24 |
25 | int maxAmountOfItemsInList = ItemsBuilderData.getMaxAmountOfItemsInList();
26 |
27 | for (int itemCounter = 0; itemCounter < maxAmountOfItemsInList; itemCounter++) {
28 | if (isPossibleToCreateNextItem(currentDate, timeToGoToSleep)) {
29 | timeToGoToSleep = getNextAlarmDate(timeToGoToSleep);
30 | createNextItemAndAddItToArray();
31 | } else {
32 | break;
33 | }
34 | }
35 |
36 | return items;
37 | }
38 |
39 | @Override
40 | public DateTime getNextAlarmDate(DateTime executionDate) {
41 | return executionDate.minusMinutes(ItemsBuilderData.getOneSleepCycleDurationInMinutes());
42 | }
43 |
44 | @Override
45 | public boolean isPossibleToCreateNextItem(DateTime currentDate, DateTime dateToGoToSleep) {
46 | if (dateToGoToSleep.isAfter(currentDate)) {
47 | long oneMinuteInMillis = 1000 * 60;
48 | long periodInMinutes = new Interval(currentDate, dateToGoToSleep)
49 | .toDurationMillis() / oneMinuteInMillis;
50 |
51 | return (periodInMinutes >= ItemsBuilderData.getTotalOneSleepCycleDurationInMinutes());
52 | } else {
53 | return false;
54 | }
55 | }
56 |
57 | private void createNextItemAndAddItToArray() {
58 | int timeForFallAsleepInMinutes = ItemsBuilderData.getTimeForFallAsleepInMinutes();
59 | DateTime dateToGoToSleepPlusTimeToFallAsleepWithRoundedTime = TimeUtils
60 | .getClosestTimeRound(timeToGoToSleep.minusMinutes(timeForFallAsleepInMinutes));
61 |
62 | Item item = new Item();
63 |
64 | item.setTitle(AlarmContentUtils
65 | .getTitleForWakeUpAt(dateToGoToSleepPlusTimeToFallAsleepWithRoundedTime,
66 | executionDate));
67 | item.setSummary(AlarmContentUtils.getSummary(timeToGoToSleep, executionDate));
68 | item.setCurrentDate(dateToGoToSleepPlusTimeToFallAsleepWithRoundedTime);
69 | item.setExecutionDate(executionDate);
70 |
71 | items.add(0, item);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at brunokawka@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
20 |
28 |
29 |
30 |
31 |
41 |
42 |
43 |
44 |
58 |
59 |
71 |
72 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
9 |
13 |
16 |
19 |
22 |
24 |
25 |
31 |
32 |
33 |
34 |
35 |
36 |
39 |
41 |
42 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/TimeUtilsRounderTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import org.joda.time.DateTime;
4 | import org.joda.time.format.DateTimeFormat;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.assertEquals;
8 | import static org.junit.Assert.assertNotEquals;
9 |
10 | public class TimeUtilsRounderTest {
11 |
12 | private static DateTime getDateTime(String string) {
13 | return DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").parseDateTime(string);
14 | }
15 |
16 | @Test
17 | public void testIfCanRoundToFullHour() {
18 | assertEquals(getDateTime("01/01/2018 19:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 18:58")));
19 | assertEquals(getDateTime("01/01/2018 20:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 19:59")));
20 | assertEquals(getDateTime("01/01/2018 21:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 21:01")));
21 | assertEquals(getDateTime("01/01/2018 22:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 22:02")));
22 | assertEquals(getDateTime("02/01/2018 00:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 23:58")));
23 | assertEquals(getDateTime("01/01/2018 00:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 00:02")));
24 | }
25 |
26 | @Test
27 | public void testIfWillNotRoundToFullHour() {
28 | assertNotEquals(getDateTime("01/01/2018 20:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:03")));
29 | assertNotEquals(getDateTime("01/01/2018 21:00"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:57")));
30 | }
31 |
32 | @Test
33 | public void testIfCanRoundToHalfOfTen() {
34 | assertEquals(getDateTime("01/01/2018 17:45"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 17:43")));
35 | assertEquals(getDateTime("01/01/2018 18:45"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 18:44")));
36 | assertEquals(getDateTime("01/01/2018 19:45"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 19:46")));
37 | assertEquals(getDateTime("01/01/2018 00:45"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 00:47")));
38 | }
39 |
40 | @Test
41 | public void testIfWillNotRoundToHalfOfTen() {
42 | assertNotEquals(getDateTime("01/01/2018 20:35"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:32")));
43 | assertNotEquals(getDateTime("01/01/2018 20:45"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:49")));
44 | }
45 |
46 | @Test
47 | public void testIfCanRoundToClosestTen() {
48 | assertEquals(getDateTime("01/01/2018 20:10"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:08")));
49 | assertEquals(getDateTime("01/01/2018 20:10"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:11")));
50 | assertEquals(getDateTime("01/01/2018 20:20"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:22")));
51 | assertEquals(getDateTime("01/01/2018 20:30"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:28")));
52 | assertEquals(getDateTime("01/01/2018 20:40"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:39")));
53 | }
54 |
55 | @Test
56 | public void testIfWillNotRoundToClosestTen() {
57 | assertNotEquals(getDateTime("01/01/2018 20:40"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:43")));
58 | assertNotEquals(getDateTime("01/01/2018 20:50"), TimeUtils.getClosestTimeRound(getDateTime("01/01/2018 20:47")));
59 | }
60 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/adapters/ActiveAlarmsAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.adapters;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.constraint.ConstraintLayout;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.ImageView;
11 | import android.widget.TextView;
12 |
13 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
14 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
15 | import com.gmail.brunokawka.poland.sleepcyclealarm.schedule.AlarmController;
16 | import com.gmail.brunokawka.poland.sleepcyclealarm.tabs.activealarms.AlarmsPresenter;
17 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
18 |
19 | import butterknife.BindView;
20 | import butterknife.ButterKnife;
21 | import io.realm.RealmRecyclerViewAdapter;
22 | import io.realm.RealmResults;
23 |
24 | public class ActiveAlarmsAdapter extends RealmRecyclerViewAdapter {
26 |
27 | public ActiveAlarmsAdapter(RealmResults alarms) {
28 | super(alarms, true);
29 | }
30 |
31 | @NonNull
32 | @Override
33 | public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
34 | return new AlarmViewHolder(LayoutInflater.from(parent.getContext())
35 | .inflate(R.layout.item_alarm, parent, false));
36 | }
37 |
38 | @Override
39 | public void onBindViewHolder(@NonNull AlarmViewHolder alarmViewHolder, final int position) {
40 | final Alarm alarm = getItem(position);
41 | if(alarm != null) {
42 | alarmViewHolder.bind(alarm);
43 | }
44 | }
45 |
46 | public class AlarmViewHolder extends RecyclerView.ViewHolder {
47 |
48 | private final AlarmsPresenter alarmsPresenter;
49 | private AlarmController alarmController;
50 | private Context context;
51 |
52 | @BindView(R.id.cl_item_alarm_root)
53 | ConstraintLayout clRoot;
54 |
55 | @BindView(R.id.iv_item_alarm_icon)
56 | ImageView ivIcon;
57 |
58 | @BindView(R.id.tv_item_alarm_title)
59 | TextView tvTitle;
60 |
61 | @BindView(R.id.tv_item_alarm_subtitle)
62 | TextView tvSubtitle;
63 |
64 | public AlarmViewHolder(View itemView) {
65 | super(itemView);
66 | ButterKnife.bind(this, itemView);
67 | this.context = itemView.getContext();
68 | this.alarmsPresenter = AlarmsPresenter.getService();
69 | this.alarmController = new AlarmController(context);
70 | }
71 |
72 | public void bind(final Alarm alarm) {
73 | final String id = alarm.getId();
74 | ivIcon.setImageResource(R.drawable.ic_list_alarm_access_full_shape);
75 | String alarmExecutionDate = AlarmContentUtils.getTitle(alarm.getExecutionDate());
76 | tvTitle.setText(alarmExecutionDate);
77 | tvSubtitle.setText(alarm.getSummary());
78 |
79 | clRoot.setOnLongClickListener(new View.OnLongClickListener() {
80 | @Override
81 | public boolean onLongClick(View v) {
82 | alarmsPresenter.deleteAlarmById(id);
83 | alarmController.cancelAlarm(alarm);
84 | return false;
85 | }
86 | });
87 |
88 | clRoot.setOnClickListener(new View.OnClickListener() {
89 | @Override
90 | public void onClick(View v) {
91 | alarmsPresenter.showEditDialog(alarm);
92 | }
93 | });
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/ItemsBuilderTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
4 |
5 | import org.joda.time.DateTime;
6 | import org.joda.time.format.DateTimeFormat;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 |
10 | import java.util.List;
11 |
12 | import static org.junit.Assert.assertEquals;
13 | import static org.junit.Assert.assertFalse;
14 | import static org.junit.Assert.assertNotNull;
15 | import static org.junit.Assert.assertTrue;
16 |
17 | public class ItemsBuilderTest {
18 |
19 | private ItemsBuilder itemsBuilder;
20 |
21 | private static DateTime getDateTime(String string) {
22 | return DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").parseDateTime(string);
23 | }
24 |
25 | @Before
26 | public void setUp() {
27 | itemsBuilder = new ItemsBuilder();
28 | }
29 |
30 | @Test
31 | public void testIfCanReturnNextAlarmDateForSleepNowStrategy() {
32 | itemsBuilder.setBuildingStrategy(new SleepNowBuildingStrategy());
33 |
34 | assertEquals(getDateTime("05/02/2011 02:30"), itemsBuilder.getNextAlarmDate(getDateTime("05/02/2011 01:00")));
35 | assertEquals(getDateTime("06/02/2011 01:25"), itemsBuilder.getNextAlarmDate(getDateTime("05/02/2011 23:55")));
36 | assertEquals(getDateTime("05/02/2011 00:00"), itemsBuilder.getNextAlarmDate(getDateTime("04/02/2011 22:30")));
37 | }
38 |
39 | @Test
40 | public void testIfCanReturnNextAlarmDateForWakeUpAtStrategy() {
41 | itemsBuilder.setBuildingStrategy(new WakeUpAtBuildingStrategy());
42 |
43 | assertEquals(getDateTime("04/02/2011 23:30"), itemsBuilder.getNextAlarmDate(getDateTime("05/02/2011 01:00")));
44 | assertEquals(getDateTime("05/02/2011 13:30"), itemsBuilder.getNextAlarmDate(getDateTime("05/02/2011 15:00")));
45 | assertEquals(getDateTime("04/02/2011 22:32"), itemsBuilder.getNextAlarmDate(getDateTime("05/02/2011 00:02")));
46 | }
47 |
48 | @Test
49 | public void testIfIsPossibleToCreateNextItemForSleepNowStrategy() {
50 | itemsBuilder.setBuildingStrategy(new SleepNowBuildingStrategy());
51 | assertTrue(itemsBuilder.isPossibleToCreateNextItem(getDateTime("05/02/2011 22:32"), getDateTime("05/02/2011 00:02")));
52 | }
53 |
54 | @Test
55 | public void testIfIsPossibleToCreateNextItemForWakeUpAtBuildingStrategy() {
56 | itemsBuilder.setBuildingStrategy(new WakeUpAtBuildingStrategy());
57 | assertTrue(itemsBuilder.isPossibleToCreateNextItem(getDateTime("04/02/2011 18:30"), getDateTime("04/02/2011 23:00")));
58 | assertTrue(itemsBuilder.isPossibleToCreateNextItem(getDateTime("04/02/2011 20:30"), getDateTime("05/02/2011 20:20")));
59 | assertTrue(itemsBuilder.isPossibleToCreateNextItem(getDateTime("04/02/2011 00:30"), getDateTime("04/02/2011 02:30")));
60 | assertTrue(itemsBuilder.isPossibleToCreateNextItem(getDateTime("04/02/2011 11:54"), getDateTime("04/02/2011 14:54")));
61 | }
62 |
63 | @Test
64 | public void testIfItsNotPossibleToCreateNextItemForWakeUpAtBuildingStrategy() {
65 | itemsBuilder.setBuildingStrategy(new WakeUpAtBuildingStrategy());
66 | assertFalse(itemsBuilder.isPossibleToCreateNextItem(getDateTime("04/02/2011 20:30"), getDateTime("04/02/2011 20:20")));
67 | assertFalse(itemsBuilder.isPossibleToCreateNextItem(getDateTime("04/02/2011 23:53"), getDateTime("05/02/2011 00:53")));
68 | }
69 |
70 | @Test
71 | public void testIfCanReturnEmptyListWhenItsNotPossibleToCreateNextItemForWakeUpAtBuildingStrategy() {
72 | itemsBuilder.setBuildingStrategy(new WakeUpAtBuildingStrategy());
73 | List- returnedList = itemsBuilder.getItems(getDateTime("04/02/2011 20:00"), getDateTime("04/02/2011 21:00"));
74 | assertTrue(returnedList.isEmpty());
75 | assertNotNull(returnedList);
76 | }
77 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/gmail/brunokawka/poland/sleepcyclealarm/MainPresenterTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.app.base.MainContract;
6 | import com.gmail.brunokawka.poland.sleepcyclealarm.app.base.MainPresenter;
7 |
8 | import org.junit.Before;
9 | import org.junit.Rule;
10 | import org.junit.Test;
11 | import org.mockito.Mock;
12 | import org.mockito.junit.MockitoJUnit;
13 | import org.mockito.junit.MockitoRule;
14 |
15 | import static org.hamcrest.CoreMatchers.notNullValue;
16 | import static org.hamcrest.MatcherAssert.assertThat;
17 | import static org.mockito.ArgumentMatchers.any;
18 | import static org.mockito.Mockito.doNothing;
19 | import static org.mockito.Mockito.never;
20 | import static org.mockito.Mockito.times;
21 | import static org.mockito.Mockito.verify;
22 |
23 | public class MainPresenterTest {
24 |
25 | @Mock
26 | private MainContract.MainView view;
27 | private MainContract.MainPresenter presenter;
28 |
29 | @Rule
30 | public MockitoRule mockitoRule = MockitoJUnit.rule();
31 |
32 | @Before
33 | public void setUp() {
34 | presenter = new MainPresenter(view);
35 | }
36 |
37 | @Test
38 | public void testIfVariablesInitialized() {
39 | assertThat(presenter, notNullValue());
40 | }
41 |
42 | @Test
43 | public void openSleepNowFragment() {
44 | int fragmentId = R.id.action_sleepnow;
45 | presenter.handleBottomNavigationTabClick(fragmentId);
46 | verify(view).openSleepNowFragment();
47 | }
48 |
49 | @Test
50 | public void openSleepNowFragmentAsDefault() {
51 | int unexpectedId = -666;
52 | presenter.handleBottomNavigationTabClick(unexpectedId);
53 | verify(view).openSleepNowFragment();
54 | }
55 |
56 | @Test
57 | public void openWakeUpAtFragment() {
58 | int fragmentId = R.id.action_wakeupat;
59 | presenter.handleBottomNavigationTabClick(fragmentId);
60 | verify(view).openWakeUpAtFragment();
61 | }
62 |
63 | @Test
64 | public void openAlarmsFragment() {
65 | int fragmentId = R.id.action_alarms;
66 | presenter.handleBottomNavigationTabClick(fragmentId);
67 | verify(view).openAlarmsFragment();
68 | }
69 |
70 |
71 | @Test
72 | public void testIfCanHandleButtonVisibility() {
73 | doNothing().when(view).hideWakeUpAtActionButton();
74 |
75 | presenter.handleBottomNavigationTabClick(R.id.action_wakeupat);
76 | verify(view, times(1)).showWakeUpAtActionButton();
77 |
78 | presenter.handleBottomNavigationTabClick(R.id.action_sleepnow);
79 | presenter.handleBottomNavigationTabClick(R.id.action_alarms);
80 | verify(view, times(2)).hideWakeUpAtActionButton();
81 | }
82 |
83 | @Test
84 | public void handleSingleBackPress() {
85 | presenter.handleBackPress();
86 | verify(view).showToastWithDoubleBackMessage();
87 | verify(view).countDownInMilliseconds(any(Integer.class));
88 | verify(view, never()).moveAppToBack();
89 | }
90 |
91 | @Test
92 | public void handleDoubleBackPress() {
93 | doubleTapBackButton();
94 | verify(view).moveAppToBack();
95 | }
96 |
97 | private void doubleTapBackButton() {
98 | presenter.handleBackPress();
99 | presenter.handleBackPress();
100 | }
101 |
102 | @Test
103 | public void showDefaultFragmentIfBundleIsNull() {
104 | Bundle savedInstanceState = null;
105 | presenter.setUpUi(savedInstanceState);
106 | verify(view).openDefaultFragment();
107 | }
108 |
109 | @Test
110 | public void showLatestFragmentIfBundleIsNotNull() {
111 | Bundle savedInstanceState = new Bundle();
112 | presenter.setUpUi(savedInstanceState);
113 | verify(view).openLatestFragment();
114 | }
115 |
116 | @Test
117 | public void openSettings() {
118 | int menuItemId = R.id.menu_settings;
119 | presenter.handleMenuItemClick(menuItemId);
120 | verify(view).openSettingsActivity();
121 | }
122 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/AlarmContentUtils.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils;
2 |
3 | import android.content.Context;
4 |
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
6 | import com.gmail.brunokawka.poland.sleepcyclealarm.app.CustomApp;
7 |
8 | import org.joda.time.DateTime;
9 | import org.joda.time.Period;
10 | import org.joda.time.format.DateTimeFormat;
11 |
12 | public class AlarmContentUtils {
13 |
14 | private static String getString(int resourceId) {
15 | Context ctx = CustomApp.getContext();
16 | return ctx.getString(resourceId);
17 | }
18 |
19 | public static String getTitle(String executionDateString) {
20 | DateTime executionDate = DateTime.parse(executionDateString);
21 | return getTitle(executionDate);
22 | }
23 |
24 | public static String getTitle(DateTime executionDate) {
25 | return getFormattedTime(executionDate);
26 | }
27 |
28 | public static String getTitleForWakeUpAt(DateTime timeToGoToSleep, DateTime executionDate) {
29 | return new StringBuilder().append(getFormattedTime(timeToGoToSleep))
30 | .append(" -> ")
31 | .append(getFormattedTime(executionDate))
32 | .toString();
33 | }
34 |
35 | public static DateTime getDateConvertedToSimple(DateTime date) {
36 | return DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").parseDateTime(
37 | date.getDayOfMonth() + "/"
38 | + date.getMonthOfYear() + "/"
39 | + date.getYear() + " "
40 | + date.getHourOfDay()
41 | + ":" + date.getMinuteOfHour());
42 | }
43 |
44 |
45 | public static String getSummary(DateTime currentDate, DateTime executionDate) {
46 | String itemSummary = getString(R.string.list_element_summary_changeable);
47 |
48 | Period periodOfTime = getPeriodOfTime(currentDate, executionDate);
49 | long diffHours = periodOfTime.getHours();
50 | long diffMinutes = periodOfTime.getMinutes();
51 |
52 | return String.format(itemSummary,
53 | getSleepDurationString(diffHours, diffMinutes), getSleepHealthStatus(diffHours));
54 | }
55 |
56 | private static String getFormattedTime(DateTime date) {
57 | long hour = date.getHourOfDay();
58 | long minute = date.getMinuteOfHour();
59 | return getFormattedHourOrMinute(hour)
60 | + ":"
61 | + getFormattedHourOrMinute(minute);
62 | }
63 |
64 | private static Period getPeriodOfTime(DateTime currentDate, DateTime executionDate) {
65 | return new Period(currentDate, executionDate);
66 | }
67 |
68 | private static String getFormattedHourOrMinute(long hourOrMinute) {
69 | return hourOrMinute < 10
70 | ? "0" + String.valueOf(hourOrMinute)
71 | : String.valueOf(hourOrMinute);
72 | }
73 |
74 | private static String getSleepDurationString(long hour, long minute) {
75 | String hourMark = getString(R.string.global_hour_mark);
76 | String minuteMark = getString(R.string.global_minute_mark);
77 |
78 | return hour
79 | + hourMark
80 | + (minute <= 0 ? "" : " ")
81 | + (minute <= 0 ? "" : minute)
82 | + (minute <= 0 ? "" : minuteMark);
83 | }
84 |
85 | private static String getSleepHealthStatus(long hoursOfSleep) {
86 | String stateUnhealthy = getString(R.string.sleep_state_unhealthy);
87 | String stateOptimal = getString(R.string.sleep_state_optimal);
88 | String stateHealthy = getString(R.string.sleep_state_healthy);
89 | String stateNotRecommended = getString(R.string.sleep_state_notrecommended);
90 |
91 | if(hoursOfSleep < 4) {
92 | return stateUnhealthy;
93 | } else if (hoursOfSleep < 7) {
94 | return stateOptimal;
95 | } else if (hoursOfSleep <= 9) {
96 | return stateHealthy;
97 | } else {
98 | return stateNotRecommended;
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/ui/EmptyStateRecyclerView.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.ui;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.util.AttributeSet;
7 | import android.view.View;
8 | import android.widget.ImageView;
9 | import android.widget.TextView;
10 |
11 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
12 |
13 | import org.jetbrains.annotations.NotNull;
14 |
15 | import java.util.ArrayList;
16 | import java.util.List;
17 |
18 | public class EmptyStateRecyclerView extends RecyclerView {
19 |
20 | @Nullable
21 | private View vEmpty;
22 | private List
viewsToHideIfListEmpty = new ArrayList<>();
23 |
24 | public EmptyStateRecyclerView(Context context) { super(context); }
25 |
26 | public EmptyStateRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }
27 |
28 | public EmptyStateRecyclerView(Context context, AttributeSet attrs, int defStyle) {
29 | super(context, attrs, defStyle);
30 | }
31 |
32 | final @NotNull
33 | AdapterDataObserver observer = new AdapterDataObserver() {
34 | @Override public void onChanged() {
35 | super.onChanged();
36 | checkIfEmpty();
37 | }
38 |
39 | @Override
40 | public void onItemRangeInserted(int positionStart, int itemCount) {
41 | checkIfEmpty();
42 | }
43 |
44 | @Override
45 | public void onItemRangeRemoved(int positionStart, int itemCount) {
46 | checkIfEmpty();
47 | }
48 | };
49 |
50 | private void checkIfEmpty() {
51 | if (vEmpty != null && getAdapter() != null) {
52 | boolean showEmptyView = getAdapter().getItemCount() == 0;
53 | vEmpty.setVisibility(showEmptyView ? VISIBLE : GONE);
54 | setVisibility(showEmptyView ? GONE : VISIBLE);
55 |
56 | if (showEmptyView) {
57 | hideViewsToHideIfListEmpty();
58 | } else {
59 | showViewsToHideIfListEmpty();
60 | }
61 | }
62 | }
63 |
64 | private void hideViewsToHideIfListEmpty() {
65 | for (View v : viewsToHideIfListEmpty) {
66 | if (v.getVisibility() != View.GONE) {
67 | v.setVisibility(View.GONE);
68 | }
69 | }
70 | }
71 |
72 | private void showViewsToHideIfListEmpty() {
73 | for (View v : viewsToHideIfListEmpty) {
74 | if (v.getVisibility() != View.VISIBLE) {
75 | v.setVisibility(View.VISIBLE);
76 | }
77 | }
78 | }
79 |
80 | @Override public void setAdapter(@Nullable Adapter adapter) {
81 | final Adapter oldAdapter = getAdapter();
82 | if (oldAdapter != null) {
83 | oldAdapter.unregisterAdapterDataObserver(observer);
84 | }
85 | super.setAdapter(adapter);
86 | if (adapter != null) {
87 | adapter.registerAdapterDataObserver(observer);
88 | }
89 | checkIfEmpty();
90 | }
91 |
92 | public void setEmptyView(View emptyView,
93 | int resImage,
94 | int resTitle,
95 | int resSubtitle) {
96 | ((ImageView) emptyView.findViewById(R.id.iv_empty_state_icon))
97 | .setImageResource(resImage);
98 | setEmptyView(emptyView, resTitle, resSubtitle);
99 | }
100 |
101 | public void setEmptyView(View emptyView,
102 | int resTitle,
103 | int resSubtitle) {
104 | this.vEmpty = emptyView;
105 |
106 | String title = getResources().getString(resTitle);
107 | String subTitle = getResources().getString(resSubtitle);
108 |
109 | ((TextView) emptyView.findViewById(R.id.tv_empty_state_title)).setText(title);
110 | ((TextView) emptyView.findViewById(R.id.tv_empty_state_subtitle)).setText(subTitle);
111 | checkIfEmpty();
112 | }
113 |
114 | public void addViewToHideIfListEmpty(View viewToHideIfListEmpty) {
115 | viewsToHideIfListEmpty.add(viewToHideIfListEmpty);
116 | checkIfEmpty();
117 | }
118 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/settings/SettingsFragment.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.settings;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.os.Bundle;
5 | import android.preference.PreferenceManager;
6 | import android.support.annotation.NonNull;
7 | import android.support.annotation.Nullable;
8 | import android.support.v4.content.ContextCompat;
9 | import android.support.v7.preference.Preference;
10 | import android.support.v7.preference.PreferenceCategory;
11 | import android.support.v7.preference.PreferenceGroupAdapter;
12 | import android.support.v7.preference.PreferenceScreen;
13 | import android.support.v7.preference.PreferenceViewHolder;
14 | import android.support.v7.widget.RecyclerView;
15 | import android.view.View;
16 | import android.view.ViewGroup;
17 |
18 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
19 | import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;
20 |
21 | public class SettingsFragment extends PreferenceFragmentCompat {
22 |
23 | public static final String TAG = "SETTINGS_FRAGMENT_TAG";
24 | private boolean isConfigurationApplied;
25 |
26 | @Override
27 | public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {
28 | setPreferencesFromResource(R.xml.preferences, rootKey);
29 |
30 | bindPreferenceToListener(getPreferenceById(R.string.key_change_theme));
31 | bindPreferenceToListener(getPreferenceById(R.string.key_ring_duration));
32 | isConfigurationApplied = true;
33 | }
34 |
35 | private Preference getPreferenceById(int id) {
36 | return findPreference(getString(id));
37 | }
38 |
39 | private void bindPreferenceToListener(Preference preference) {
40 | preference.setOnPreferenceChangeListener(preferenceChangeListener);
41 | preferenceChangeListener.onPreferenceChange(preference,
42 | PreferenceManager
43 | .getDefaultSharedPreferences(preference.getContext())
44 | .getString(preference.getKey(), ""));
45 | }
46 |
47 | private Preference.OnPreferenceChangeListener preferenceChangeListener = new Preference
48 | .OnPreferenceChangeListener() {
49 | @Override
50 | public boolean onPreferenceChange(Preference preference, Object newValue) {
51 |
52 | if (preference.getTitle().equals(getString(R.string.pref_change_theme_title))
53 | && isConfigurationApplied) {
54 | applyNewTheme();
55 | }
56 |
57 | return true;
58 | }
59 | };
60 |
61 | private void applyNewTheme() {
62 | if (getActivity() != null) {
63 | getActivity().recreate();
64 | }
65 | }
66 |
67 | @Override
68 | public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
69 | super.onViewCreated(view, savedInstanceState);
70 | if (getContext() != null) {
71 | view.setBackgroundColor(
72 | ContextCompat.getColor(getContext(), R.color.colorPrimary));
73 | }
74 |
75 | }
76 |
77 | @Override
78 | protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
79 | return new PreferenceGroupAdapter(preferenceScreen) {
80 | @SuppressLint("RestrictedApi")
81 | @Override
82 | public void onBindViewHolder(PreferenceViewHolder holder, int position) {
83 | super.onBindViewHolder(holder, position);
84 | Preference preference = getItem(position);
85 | if (preference instanceof PreferenceCategory)
86 | setZeroPaddingToLayoutChildren(holder.itemView);
87 | else {
88 | View iconFrame = holder.itemView.findViewById(R.id.icon_frame);
89 | if (iconFrame != null) {
90 | iconFrame.setVisibility(preference.getIcon() == null
91 | ? View.GONE
92 | : View.VISIBLE);
93 | }
94 | }
95 | }
96 | };
97 | }
98 |
99 | private void setZeroPaddingToLayoutChildren(View view) {
100 | if (!(view instanceof ViewGroup))
101 | return;
102 | ViewGroup viewGroup = (ViewGroup) view;
103 | int childCount = viewGroup.getChildCount();
104 | for (int i = 0; i < childCount; i++) {
105 | setZeroPaddingToLayoutChildren(viewGroup.getChildAt(i));
106 |
107 | viewGroup.setPaddingRelative(0,
108 | viewGroup.getPaddingTop(),
109 | viewGroup.getPaddingEnd(),
110 | viewGroup.getPaddingBottom());
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/schedule/AlarmActivity.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.schedule;
2 |
3 | import android.content.Intent;
4 | import android.content.SharedPreferences;
5 | import android.os.Build;
6 | import android.os.Bundle;
7 | import android.os.Handler;
8 | import android.preference.PreferenceManager;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.text.TextUtils;
11 | import android.util.Log;
12 | import android.view.WindowManager;
13 | import android.widget.TextView;
14 |
15 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
16 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.AlarmDAO;
17 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
18 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.Const;
19 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.ThemeUtils;
20 |
21 | import org.joda.time.DateTime;
22 |
23 | import butterknife.BindView;
24 | import butterknife.ButterKnife;
25 | import butterknife.OnClick;
26 |
27 | public class AlarmActivity extends AppCompatActivity {
28 |
29 | private AlarmDAO alarmDAO;
30 | private AlarmController alarmController;
31 | private long ringDurationMs;
32 |
33 | @BindView(R.id.activityAlarmCurrentHour) protected TextView currentHourTextView;
34 |
35 | @OnClick(R.id.activityAlarmLayout)
36 | public void onAlarmDismissRequest() {
37 | finish();
38 | }
39 |
40 | @Override
41 | public void onCreate(Bundle savedInstanceState) {
42 | super.onCreate(savedInstanceState);
43 |
44 | setupWindowFlags();
45 | setAppTheme();
46 |
47 | setContentView(R.layout.activity_alarm);
48 | ButterKnife.bind(this);
49 | alarmDAO = new AlarmDAO();
50 | alarmController = new AlarmController(getApplicationContext());
51 |
52 | removeExecutedAlarmFromDatabase();
53 | showCurrentHour();
54 | setupRingDuration();
55 | countDownRingDuration();
56 | }
57 |
58 | private void setupWindowFlags() {
59 | if (Build.VERSION.SDK_INT >= 27) {
60 | setShowWhenLocked(true);
61 | setTurnScreenOn(true);
62 | } else {
63 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
64 | | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
65 | }
66 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
67 | }
68 |
69 | private void setAppTheme() {
70 | SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
71 | String key = getString(R.string.key_change_theme);
72 | int themeId = ThemeUtils.getCurrentTheme(pref.getString(key, Const.DEFAULTS.THEME_ID));
73 | getDelegate().setLocalNightMode(themeId);
74 | }
75 |
76 | private void removeExecutedAlarmFromDatabase() {
77 | Intent intent = getIntent();
78 | if (intent != null) {
79 | String alarmId = intent.getStringExtra(Const.KEYS.ALARM_ID);
80 | if (!TextUtils.isEmpty(alarmId)) {
81 | alarmDAO.removeFromRealmById(alarmId);
82 | } else {
83 | Log.e(getClass().getName(),
84 | "removeExecutedAlarmFromDatabase(): alarmId is empty");
85 | }
86 | } else {
87 | Log.e(getClass().getName(), "removeExecutedAlarmFromDatabase(): intent == null");
88 | }
89 | }
90 |
91 | private void showCurrentHour() {
92 | String currentHour = AlarmContentUtils.getTitle(DateTime.now());
93 | currentHourTextView.setText(currentHour);
94 | }
95 |
96 | private void setupRingDuration() {
97 | SharedPreferences preferences = PreferenceManager
98 | .getDefaultSharedPreferences(getApplicationContext());
99 | String ringDurationString = preferences.getString(getString(R.string.key_ring_duration),
100 | String.valueOf(Const.DEFAULTS.RING_DURATION_MS));
101 | ringDurationMs = Long.parseLong(ringDurationString);
102 | }
103 |
104 | private void countDownRingDuration() {
105 | new Handler().postDelayed(new Runnable() {
106 | @Override
107 | public void run() {
108 | finish();
109 | }
110 | }, ringDurationMs);
111 | }
112 |
113 | @Override
114 | public void onBackPressed() {
115 | finish();
116 | }
117 |
118 | @Override
119 | public void onUserLeaveHint() {
120 | super.onUserLeaveHint();
121 | finish();
122 | }
123 |
124 | @Override
125 | public void onDestroy() {
126 | super.onDestroy();
127 | alarmController.dismissCurrentlyPlayingAlarm();
128 | alarmDAO.cleanUp();
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Sleep Cycle Alarm
4 |
5 | Set alarm icon
6 |
7 |
8 | Unhealthy
9 | Optimal
10 | Healthy
11 | Not recommended
12 |
13 |
14 | Sleep now
15 | Wake up at
16 | Alarms
17 |
18 |
19 | Going to sleep right now?
20 | Choose any hour of listed below, so I will easily wake you up on time!
21 |
22 | Tap anywhere to dismiss alarm
23 |
24 |
25 | Set hour
26 |
27 |
28 | h
29 | min
30 | %1$d:%2$d
31 | %1$s of sleep | %2$s
32 | Wake up on time!
33 | Tap the \'Set hour\' button to pick a specific hour at which you want to wake up. I will create a list of hours you should fall asleep at.
34 | Keep an eye on your alarms!
35 | Don\'t worry I won\'t eat them… I just remove already launched alarms, and those active - I store them safely here.
36 |
37 |
38 | Settings
39 |
40 |
41 | Edit alarm
42 |
43 |
44 | Press once again to exit
45 |
46 |
47 |
48 |
49 | Theme
50 |
51 | Night mode schedule
52 |
53 | - Never
54 | - Auto (Depends on time)
55 | - Always
56 |
57 |
58 | - 1
59 | - 2
60 | - 3
61 |
62 |
63 |
64 |
65 |
66 |
67 | Alarm
68 |
69 | Phone vibration
70 | Vibrate when alarm rings
71 |
72 | Alarms have the same ringtone
73 | Avoid configuring ringtone for every single alarm.
74 | Alarm ringtone
75 | Tap to select ringtone
76 | Chosen ringtone: %s
77 |
78 | Ring duration
79 |
80 | - 1 minute
81 | - 5 minutes
82 | - 10 minutes
83 | - 15 minutes
84 | - 20 minutes
85 | - 25 minutes
86 | - 30 minutes
87 |
88 |
89 | - 60000
90 | - 300000
91 | - 600000
92 | - 900000
93 | - 1200000
94 | - 1500000
95 | - 1800000
96 |
97 |
98 | Add alarm
99 | Add
100 | Empty list image
101 | Alarm will ring at %1$s, which gives you %2$s type of rest
102 | Alarm ringtone
103 | Cancel
104 | Save
105 | To wake up at %1$s fully rested you should fall asleep at one of the listed hours below.
106 | Tap to set alarm
107 | Tap to edit, press and hold to delete
108 |
109 |
110 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/ui/dialog/ItemDialog.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.ui.dialog;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 | import android.media.Ringtone;
6 | import android.media.RingtoneManager;
7 | import android.net.Uri;
8 | import android.preference.PreferenceManager;
9 | import android.support.annotation.Nullable;
10 | import android.util.AttributeSet;
11 | import android.util.Log;
12 | import android.view.View;
13 | import android.widget.ImageView;
14 | import android.widget.LinearLayout;
15 | import android.widget.TextView;
16 |
17 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
18 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
19 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
20 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
21 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.Const;
22 |
23 | import butterknife.BindView;
24 | import butterknife.ButterKnife;
25 |
26 | public class ItemDialog extends LinearLayout implements ItemDialogContract {
27 |
28 | private String itemRingtone;
29 | private String itemExecutionDate;
30 | private String itemSummary;
31 | private String dialogTitle;
32 | private boolean alarmsHaveTheSameRingtone;
33 | private Context ctx;
34 |
35 | @BindView(R.id.tv_dialog_item_title)
36 | TextView tvDialogTitle;
37 |
38 | @BindView(R.id.tv_dialog_item_description)
39 | TextView tvDialogDescription;
40 |
41 | @BindView(R.id.i_dialog_item_ringtone)
42 | View vItemRingtone;
43 |
44 | public ItemDialog(Context context) {
45 | super(context);
46 | }
47 |
48 | public ItemDialog(Context context, @Nullable AttributeSet attrs) {
49 | super(context, attrs);
50 | }
51 |
52 | public ItemDialog(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
53 | super(context, attrs, defStyleAttr);
54 | }
55 |
56 | public ItemDialog(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
57 | super(context, attrs, defStyleAttr, defStyleRes);
58 | }
59 |
60 | @Override
61 | public void onFinishInflate() {
62 | super.onFinishInflate();
63 | ButterKnife.bind(this);
64 | }
65 |
66 | @Override
67 | public void bind(Item item) {
68 | ctx = getContext();
69 | dialogTitle = ctx.getString(R.string.add_alarm);
70 | itemSummary = item.getSummary();
71 | itemExecutionDate = AlarmContentUtils.getTitle(item.getExecutionDate());
72 |
73 | SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
74 | alarmsHaveTheSameRingtone = pref.
75 | getBoolean(ctx.getString(R.string.key_alarms_has_same_ringtone),
76 | Const.DEFAULTS.ALARMS_HAVE_SAME_RINGTONE);
77 |
78 | String ringtone = pref.getString(ctx.getString(R.string.key_ringtone_select),
79 | Const.DEFAULTS.ALARM_SOUND);
80 | setRingtone(ringtone);
81 | bind();
82 | handleRingtoneSelectionVisibility();
83 | }
84 |
85 | @Override
86 | public void bind(Alarm alarm) {
87 | dialogTitle = getContext().getString(R.string.edit_alarm);
88 | itemSummary = alarm.getSummary();
89 | itemExecutionDate = AlarmContentUtils.getTitle(alarm.getExecutionDate());
90 | String ringtone = alarm.getRingtone();
91 | setRingtone(ringtone);
92 | bind();
93 | }
94 |
95 | private void bind() {
96 | if (ctx == null) {
97 | ctx = getContext();
98 | }
99 | setDialogTitle();
100 | setRingtoneIcon();
101 | setRingtoneTitle();
102 | setDialogDescription();
103 | }
104 |
105 | public void setDialogTitle() {
106 | tvDialogTitle.setText(dialogTitle);
107 | }
108 |
109 | private void setRingtoneIcon() {
110 | ((ImageView) vItemRingtone.findViewById(R.id.iv_item_alarm_icon))
111 | .setImageResource(R.drawable.ic_list_ringtone_full_shape);
112 | }
113 |
114 | private void setRingtoneTitle() {
115 | String title = ctx.getString(R.string.alarm_ringtone);
116 | ((TextView) vItemRingtone.findViewById(R.id.tv_item_alarm_title)).setText(title);
117 | }
118 |
119 | public void setDialogDescription() {
120 | String alarmTitleBase = getContext().getString(R.string.dialog_alarm_description);
121 | String description = String.format(
122 | alarmTitleBase,
123 | itemExecutionDate,
124 | itemSummary);
125 | tvDialogDescription.setText(description);
126 | }
127 |
128 | @Override
129 | public void setRingtone(String itemRingtone) {
130 | this.itemRingtone = itemRingtone;
131 | updateRingtoneItem();
132 | }
133 |
134 | @Override
135 | public String getRingtone() {
136 | return itemRingtone;
137 | }
138 |
139 | private void updateRingtoneItem() {
140 | setRingtoneSubtitle();
141 | }
142 |
143 | private void setRingtoneSubtitle() {
144 | Context ctx = getContext();
145 | String summary = ctx.getString(R.string.pref_ringtone_select_default_summary);
146 | try {
147 | Uri uri = Uri.parse(itemRingtone);
148 | Ringtone ringtone = RingtoneManager.getRingtone(getContext(), uri);
149 | String title = ringtone.getTitle(getContext());
150 | String stringToParse = ctx.getString(R.string.pref_ringtone_select_selected_summary);
151 | summary = String.format(stringToParse, title);
152 | } catch (Exception e) {
153 | Log.e(getClass().getName(), "setRingtoneSubtitle(): " + e.getMessage());
154 | } finally {
155 | ((TextView) vItemRingtone.findViewById(R.id.tv_item_alarm_subtitle))
156 | .setText(summary);
157 | }
158 | }
159 |
160 | private void handleRingtoneSelectionVisibility() {
161 | vItemRingtone.setVisibility(alarmsHaveTheSameRingtone
162 | ? View.GONE
163 | : View.VISIBLE);
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/schedule/AlarmService.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.schedule;
2 |
3 | import android.app.Service;
4 | import android.content.ComponentName;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.SharedPreferences;
8 | import android.media.AudioManager;
9 | import android.media.MediaPlayer;
10 | import android.media.RingtoneManager;
11 | import android.net.Uri;
12 | import android.os.Bundle;
13 | import android.os.Handler;
14 | import android.os.HandlerThread;
15 | import android.os.IBinder;
16 | import android.os.Vibrator;
17 | import android.preference.PreferenceManager;
18 | import android.support.annotation.Nullable;
19 | import android.util.Log;
20 |
21 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
22 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.Const;
23 |
24 | public class AlarmService extends Service {
25 |
26 | private final static String HANDLER_THREAD_NAME = "HANDLER_THREAD_NAME";
27 |
28 | private final static int PAUSE_BETWEEN_VIBRATE_DELAY_IN_MS = 2000;
29 | private final static int VIBRATION_DURATION_IN_MS = 1000;
30 | private final static int VOLUME_INCREASE_DELAY_IN_MS = 600;
31 |
32 | private final static float VOLUME_INCREASE_STEP = 0.01f;
33 | private final static float MAX_VOLUME = 1.0f;
34 |
35 | private SharedPreferences preferences;
36 | private MediaPlayer player;
37 | private Vibrator vibrator;
38 | private float volumeLevel = 0.0f;
39 | private String alarmRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
40 | .toString();
41 | private String alarmId = "";
42 |
43 | private Handler handler = new Handler();
44 | private Runnable vibrationRunnable = new Runnable() {
45 | @Override
46 | public void run() {
47 | vibrator.vibrate(VIBRATION_DURATION_IN_MS);
48 | handler.postDelayed(vibrationRunnable,
49 | VIBRATION_DURATION_IN_MS + PAUSE_BETWEEN_VIBRATE_DELAY_IN_MS);
50 | }
51 | };
52 |
53 | private Runnable gentleVolumeRunnable = new Runnable() {
54 | @Override
55 | public void run() {
56 | if (player != null && volumeLevel < MAX_VOLUME) {
57 | volumeLevel += VOLUME_INCREASE_STEP;
58 | player.setVolume(volumeLevel, volumeLevel);
59 | handler.postDelayed(gentleVolumeRunnable, VOLUME_INCREASE_DELAY_IN_MS);
60 | }
61 | }
62 | };
63 |
64 | @Override
65 | public void onCreate() {
66 | HandlerThread ht = new HandlerThread(HANDLER_THREAD_NAME);
67 | ht.start();
68 | handler = new Handler(ht.getLooper());
69 | preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
70 | }
71 |
72 | @Override
73 | public int onStartCommand(Intent intent, int flags, int startId) {
74 | if (intent != null && intent.getExtras() != null) {
75 | setupAlarmVariablesWithPassedData(intent.getExtras());
76 | } else {
77 | Log.e(getClass().getName(), "onStartCommand(): setting up default variables");
78 | }
79 | startPlayer();
80 | startAlarmActivity();
81 | return START_NOT_STICKY;
82 | }
83 |
84 | private void setupAlarmVariablesWithPassedData(Bundle extras) {
85 | setupAlarmId(extras);
86 | setupRingtone(extras);
87 | }
88 |
89 | private void setupAlarmId(Bundle extras) {
90 | if (extras.getString(Const.KEYS.ALARM_ID) != null) {
91 | alarmId = extras.getString(Const.KEYS.ALARM_ID);
92 | } else {
93 | Log.e(getClass().getName(), "setupAlarmId(): extras value == null");
94 | }
95 | }
96 |
97 | private void setupRingtone(Bundle extras) {
98 | if (extras.getString(Const.KEYS.RINGTONE_ID) != null) {
99 | alarmRingtone = extras.getString(Const.KEYS.RINGTONE_ID);
100 | } else {
101 | Log.e(getClass().getName(), "setupRingtone() extras value == null");
102 | }
103 | }
104 |
105 | private void startPlayer() {
106 | player = new MediaPlayer();
107 |
108 | try {
109 | if (isVibrateEnabled()) {
110 | postVibrationHandler();
111 | }
112 | player.setDataSource(this, Uri.parse(alarmRingtone));
113 | player.setLooping(true);
114 | player.setAudioStreamType(AudioManager.STREAM_ALARM);
115 | player.setVolume(volumeLevel, volumeLevel);
116 | player.prepare();
117 | player.start();
118 | postDelayedVolumeHandler();
119 | } catch (Exception e) {
120 | if (player.isPlaying()) {
121 | player.stop();
122 | }
123 | Log.e(getClass().getName(), "startPlayer(): " + e.getMessage());
124 | stopSelf();
125 | }
126 | }
127 |
128 | private boolean isVibrateEnabled() {
129 | return preferences.getBoolean(getString(R.string.key_alarm_vibrate_when_ringing), true);
130 | }
131 |
132 | private void postVibrationHandler() {
133 | vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
134 | handler.post(vibrationRunnable);
135 | }
136 |
137 | private void postDelayedVolumeHandler() {
138 | handler.postDelayed(gentleVolumeRunnable, VOLUME_INCREASE_DELAY_IN_MS);
139 | }
140 |
141 | private void startAlarmActivity() {
142 | Intent alarmActivityIntent = new Intent(Intent.ACTION_MAIN);
143 | alarmActivityIntent.setComponent(new ComponentName(this, AlarmActivity.class));
144 | alarmActivityIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
145 | Intent.FLAG_ACTIVITY_NEW_TASK |
146 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
147 | alarmActivityIntent.putExtra(Const.KEYS.ALARM_ID, alarmId);
148 | startActivity(alarmActivityIntent);
149 | }
150 |
151 | @Nullable
152 | @Override
153 | public IBinder onBind(Intent intent) {
154 | return null;
155 | }
156 |
157 | @Override
158 | public void onDestroy() {
159 | cleanUpPlayerIfPlaying();
160 | handler.removeCallbacksAndMessages(null);
161 | super.onDestroy();
162 | }
163 |
164 | private void cleanUpPlayerIfPlaying() {
165 | if (player.isPlaying()) {
166 | player.stop();
167 | player.release();
168 | player = null;
169 | }
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gmail/brunokawka/poland/sleepcyclealarm/tabs/addalarm/AddDialogFragment.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.tabs.addalarm;
2 |
3 | import android.app.Activity;
4 | import android.app.AlertDialog;
5 | import android.content.Context;
6 | import android.content.DialogInterface;
7 | import android.content.Intent;
8 | import android.content.SharedPreferences;
9 | import android.media.RingtoneManager;
10 | import android.net.Uri;
11 | import android.preference.PreferenceManager;
12 | import android.support.v4.app.Fragment;
13 | import android.util.Log;
14 | import android.view.View;
15 |
16 | import com.gmail.brunokawka.poland.sleepcyclealarm.R;
17 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.AlarmDAO;
18 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Alarm;
19 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
20 | import com.gmail.brunokawka.poland.sleepcyclealarm.events.AddAlarmEvent;
21 | import com.gmail.brunokawka.poland.sleepcyclealarm.schedule.AlarmController;
22 | import com.gmail.brunokawka.poland.sleepcyclealarm.tabs.ui.dialog.ItemDialogContract;
23 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.Const;
24 |
25 | import org.greenrobot.eventbus.EventBus;
26 | import org.greenrobot.eventbus.Subscribe;
27 |
28 | import java.util.UUID;
29 |
30 | public abstract class AddDialogFragment extends Fragment {
31 |
32 | private static final int RINGTONE_INTENT_REQUEST_CODE = 1;
33 | private AlertDialog dialog;
34 | private ItemDialogContract dialogContract;
35 |
36 | @Subscribe
37 | public void handleAddAlarmEvent(AddAlarmEvent addAlarmEvent) {
38 | boolean isDialogHidden = dialog == null || !dialog.isShowing();
39 | if (isDialogHidden) {
40 | final Item item = addAlarmEvent.getItem();
41 | showDialog(item);
42 | }
43 | }
44 |
45 | public void showDialog(final Item item) {
46 | String positiveButtonText = getString(R.string.add);
47 | String negativeButtonText = getString(R.string.cancel);
48 | final View content = getLayoutInflater().inflate(R.layout.dialog_item_ringtone, null);
49 | dialogContract = (ItemDialogContract) content;
50 | dialogContract.bind(item);
51 | AlertDialog.Builder builder
52 | = new AlertDialog.Builder(getActivity(), R.style.Theme_SleepCycleAlarm_Dialog);
53 | builder.setView(content)
54 | .setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
55 | @Override
56 | public void onClick(DialogInterface dialogInterface, int i) {
57 | Alarm alarm = getAlarmFromItem(item);
58 | addAlarm(alarm);
59 | }
60 | })
61 | .setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
62 | @Override
63 | public void onClick(DialogInterface dialogInterface, int i) {
64 | dialog.cancel();
65 | }
66 | });
67 | content.findViewById(R.id.i_dialog_item_ringtone)
68 | .setOnClickListener(new View.OnClickListener() {
69 | @Override
70 | public void onClick(View view) {
71 | startRingtonePickerActivityForResult();
72 | }
73 | });
74 | dialog = builder.create();
75 | dialog.show();
76 | }
77 |
78 | public void addAlarm(Alarm alarm) {
79 | Context ctx = getContext();
80 | if (ctx != null) {
81 | new AlarmController(ctx).setAlarm(alarm);
82 | new AlarmDAO().saveIfNotDuplicate(alarm);
83 | } else {
84 | Log.e(getClass().getName(), "addAlarm(): ctx == null");
85 | }
86 | }
87 |
88 | private void startRingtonePickerActivityForResult() {
89 | Uri selectedRingtoneUri = Uri.parse(dialogContract.getRingtone());
90 | Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
91 | intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM);
92 | intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
93 | getString(R.string.pref_ringtone_select_title));
94 | intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, selectedRingtoneUri);
95 | intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
96 | intent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, selectedRingtoneUri);
97 | startActivityForResult(intent, RINGTONE_INTENT_REQUEST_CODE);
98 | }
99 |
100 | @Override
101 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
102 | if (resultCode == Activity.RESULT_OK && requestCode == RINGTONE_INTENT_REQUEST_CODE) {
103 | Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
104 |
105 | if (uri != null && dialogContract != null) {
106 | String chosenRingtone = uri.toString();
107 | dialogContract.setRingtone(chosenRingtone);
108 | }
109 | }
110 | }
111 |
112 | public Alarm getAlarmFromItem(Item item) {
113 | Context ctx = getContext();
114 | SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
115 |
116 | final String id = UUID.randomUUID().toString();
117 | final String title = item.getTitle();
118 | final String summary = item.getSummary();
119 | final String ringtone = dialogContract != null
120 | ? dialogContract.getRingtone()
121 | : pref.getString(ctx.getString(R.string.key_ringtone_select),
122 | Const.DEFAULTS.ALARM_SOUND);
123 | final String currentDate = item.getCurrentDate().toString();
124 | final String executionDate = item.getExecutionDate().toString();
125 |
126 | Alarm alarm = new Alarm();
127 | alarm.setId(id);
128 | alarm.setTitle(title);
129 | alarm.setSummary(summary);
130 | alarm.setRingtone(ringtone);
131 | alarm.setCurrentDate(currentDate);
132 | alarm.setExecutionDate(executionDate);
133 |
134 | return alarm;
135 | }
136 |
137 | @Override
138 | public void onStart() {
139 | super.onStart();
140 | if (!isEventBusRegistered()) {
141 | EventBus.getDefault().register(this);
142 | }
143 | }
144 |
145 | @Override
146 | public void onStop() {
147 | super.onStop();
148 | if (isEventBusRegistered()) {
149 | EventBus.getDefault().unregister(this);
150 | }
151 | }
152 |
153 | private boolean isEventBusRegistered() {
154 | return EventBus.getDefault().isRegistered(this);
155 | }
156 |
157 | @Override
158 | public void onDestroy() {
159 | super.onDestroy();
160 | if (dialog != null && dialog.isShowing()) {
161 | dialog.dismiss();
162 | }
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gmail/brunokawka/poland/sleepcyclealarm/utils/itemsbuilder/ItemsBuilderTest.java:
--------------------------------------------------------------------------------
1 | package com.gmail.brunokawka.poland.sleepcyclealarm.utils.itemsbuilder;
2 |
3 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.ItemsBuilderData;
4 | import com.gmail.brunokawka.poland.sleepcyclealarm.data.pojo.Item;
5 | import com.gmail.brunokawka.poland.sleepcyclealarm.utils.AlarmContentUtils;
6 |
7 | import org.joda.time.DateTime;
8 | import org.joda.time.format.DateTimeFormat;
9 | import org.junit.Before;
10 | import org.junit.Test;
11 |
12 | import java.util.Arrays;
13 | import java.util.List;
14 |
15 | import static org.hamcrest.CoreMatchers.is;
16 | import static org.junit.Assert.assertEquals;
17 | import static org.junit.Assert.assertNotEquals;
18 | import static org.junit.Assert.assertThat;
19 |
20 | public class ItemsBuilderTest {
21 | private ItemsBuilder itemsBuilder;
22 |
23 | private final DateTime CURRENT_DATE_FOR_SLEEPNOW_BUILDING_STRATEGY = getDateTime("01/01/1111 22:20");
24 | private final List EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY = Arrays.asList(
25 | "02/01/1111 00:05",
26 | "02/01/1111 01:35",
27 | "02/01/1111 03:05",
28 | "02/01/1111 04:35",
29 | "02/01/1111 06:05",
30 | "02/01/1111 07:35",
31 | "02/01/1111 09:05",
32 | "02/01/1111 10:35"
33 | );
34 | private List- itemsForSleepNowBuildingStrategy;
35 |
36 | private final DateTime CURRENT_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY = getDateTime("02/01/2011 08:00");
37 | private final DateTime EXECUTION_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY = getDateTime("02/01/2011 23:00");
38 | private final List
EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY = Arrays.asList(
39 | "02/01/2011 10:45",
40 | "02/01/2011 12:15",
41 | "02/01/2011 13:45",
42 | "02/01/2011 15:15",
43 | "02/01/2011 16:45",
44 | "02/01/2011 18:15",
45 | "02/01/2011 19:45",
46 | "02/01/2011 21:15"
47 | );
48 | private List- itemsForWakeUpAtBuildingStrategy;
49 |
50 |
51 | private static DateTime getDateTime(String string) {
52 | return DateTimeFormat.forPattern("dd/MM/yyyy HH:mm").parseDateTime(string);
53 | }
54 |
55 | @Before
56 | public void setUp() {
57 | itemsBuilder = new ItemsBuilder();
58 |
59 | itemsBuilder.setBuildingStrategy(new SleepNowBuildingStrategy());
60 | itemsForSleepNowBuildingStrategy = itemsBuilder.getItems(CURRENT_DATE_FOR_SLEEPNOW_BUILDING_STRATEGY, null);
61 |
62 | itemsBuilder.setBuildingStrategy(new WakeUpAtBuildingStrategy());
63 | itemsForWakeUpAtBuildingStrategy = itemsBuilder.getItems(CURRENT_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY, EXECUTION_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY);
64 | }
65 |
66 | @Test
67 | public void testIfVariablesInitialized() {
68 | assertNotEquals(null, itemsBuilder);
69 | assertNotEquals(null, itemsForSleepNowBuildingStrategy);
70 | assertNotEquals(null, itemsForWakeUpAtBuildingStrategy);
71 | }
72 |
73 | @Test
74 | public void testIfCanReturnCurrentDatesForSleepNowBuildingStrategy() {
75 | for (int index = 0; index < EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.size(); index++) {
76 | assertEquals(CURRENT_DATE_FOR_SLEEPNOW_BUILDING_STRATEGY, itemsForSleepNowBuildingStrategy.get(index).getCurrentDate());
77 | }
78 | }
79 |
80 | @Test
81 | public void testIfCanReturnCurrentDatesForWakeUpAtBuildingStrategy() {
82 | DateTime expected;
83 | for (int index = 0; index < EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.size(); index++) {
84 | expected = getDateTime(EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.get(index));
85 | assertEquals(expected, itemsForWakeUpAtBuildingStrategy.get(index).getCurrentDate());
86 | }
87 | }
88 |
89 | @Test
90 | public void testIfCanReturnExecutionDatesForSleepNowBuildingStrategy() {
91 | DateTime expected;
92 | for (int index = 0; index < EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.size(); index++) {
93 | expected = getDateTime(EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.get(index));
94 | assertEquals(expected, itemsForSleepNowBuildingStrategy.get(index).getExecutionDate());
95 | }
96 | }
97 |
98 | @Test
99 | public void testIfCanReturnExecutionDatesForWakeUpAtBuildingStrategy() {
100 | for (int index = 0; index < EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.size(); index++) {
101 | assertEquals(EXECUTION_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY, itemsForWakeUpAtBuildingStrategy.get(index).getExecutionDate());
102 | }
103 | }
104 |
105 | @Test
106 | public void testIfCanReturnCorrectTitlesForSleepNowBuildingStrategy() {
107 | DateTime executionDate;
108 | String expected;
109 | for (int index = 0; index < EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.size(); index++) {
110 | executionDate = getDateTime(EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.get(index));
111 | expected = AlarmContentUtils.getTitle(executionDate);
112 |
113 | assertEquals(expected, itemsForSleepNowBuildingStrategy.get(index).getTitle());
114 | }
115 | }
116 |
117 | @Test
118 | public void testIfCanReturnCorrectTitlesForWakeUpAtBuildingStrategy() {
119 | String expected;
120 | for (int index = 0; index < EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.size(); index++) {
121 | expected = AlarmContentUtils.getTitleForWakeUpAt(getDateTime(EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.get(index)),
122 | EXECUTION_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY);
123 | assertThat(expected, is(itemsForWakeUpAtBuildingStrategy.get(index).getTitle()));
124 | }
125 | }
126 |
127 | @Test
128 | public void testIfCanReturnCorrectSummariesForSleepNowBuildingStrategy() {
129 | DateTime expectedDate;
130 | String expected;
131 | for (int index = 0; index < EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.size(); index++) {
132 | expectedDate = getDateTime(EXPECTED_FOR_SLEEPNOW_BUILDING_STRATEGY.get(index));
133 | expected = AlarmContentUtils.getSummary(CURRENT_DATE_FOR_SLEEPNOW_BUILDING_STRATEGY, expectedDate.minusMinutes(ItemsBuilderData.getTimeForFallAsleepInMinutes()));
134 |
135 | assertEquals(expected, itemsForSleepNowBuildingStrategy.get(index).getSummary());
136 | }
137 | }
138 |
139 | @Test
140 | public void testIfCanReturnCorrectSummariesForWakeUpAtBuildingStrategy() {
141 | DateTime expectedDate;
142 | String expected;
143 | DateTime executionDateWithoutTimeToFallAsleep;
144 | for (int index = 0; index < EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.size(); index++) {
145 | expectedDate = getDateTime(EXPECTED_FOR_WAKEUPAT_BUILDING_STRATEGY.get(index));
146 | executionDateWithoutTimeToFallAsleep = EXECUTION_DATE_FOR_WAKEUPAT_BUILDING_STRATEGY.minusMinutes(ItemsBuilderData.getTimeForFallAsleepInMinutes());
147 | expected = AlarmContentUtils.getSummary(expectedDate, executionDateWithoutTimeToFallAsleep);
148 |
149 | assertEquals(expected, itemsForWakeUpAtBuildingStrategy.get(index).getSummary());
150 | }
151 | }
152 | }
--------------------------------------------------------------------------------