├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── raw
│ │ │ │ └── alarm.mp3
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_repeat_black_24dp.xml
│ │ │ │ ├── ic_looks_one_black_24dp.xml
│ │ │ │ ├── ic_alarm_black_24dp.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── fragment_listalarms.xml
│ │ │ │ ├── activity_ring.xml
│ │ │ │ ├── item_alarm.xml
│ │ │ │ └── fragment_createalarm.xml
│ │ │ ├── navigation
│ │ │ │ └── nav_graph.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── learntodroid
│ │ │ │ └── simplealarmclock
│ │ │ │ ├── alarmslist
│ │ │ │ ├── OnToggleAlarmListener.java
│ │ │ │ ├── AlarmsListViewModel.java
│ │ │ │ ├── AlarmRecyclerViewAdapter.java
│ │ │ │ ├── AlarmViewHolder.java
│ │ │ │ └── AlarmsListFragment.java
│ │ │ │ ├── activities
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── RingActivity.java
│ │ │ │ ├── data
│ │ │ │ ├── AlarmDao.java
│ │ │ │ ├── AlarmRepository.java
│ │ │ │ ├── AlarmDatabase.java
│ │ │ │ └── Alarm.java
│ │ │ │ ├── createalarm
│ │ │ │ ├── TimePickerUtil.java
│ │ │ │ ├── CreateAlarmViewModel.java
│ │ │ │ ├── DayUtil.java
│ │ │ │ └── CreateAlarmFragment.java
│ │ │ │ ├── application
│ │ │ │ └── App.java
│ │ │ │ ├── service
│ │ │ │ ├── RescheduleAlarmsService.java
│ │ │ │ └── AlarmService.java
│ │ │ │ └── broadcastreceiver
│ │ │ │ └── AlarmBroadcastReceiver.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── learntodroid
│ │ │ └── simplealarmclock
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── learntodroid
│ │ └── simplealarmclock
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── vcs.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
├── jarRepositories.xml
└── codeStyles
│ └── Project.xml
├── settings.gradle
├── .gitattributes
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | Simple Alarm Clock
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='Simple Alarm Clock'
3 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/app/src/main/res/raw/alarm.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/raw/alarm.mp3
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Simple Alarm Clock
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/learntodroid/SimpleAlarmClock/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Mar 07 16:50:46 AEDT 2020
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-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/alarmslist/OnToggleAlarmListener.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.alarmslist;
2 |
3 | import com.learntodroid.simplealarmclock.data.Alarm;
4 |
5 | public interface OnToggleAlarmListener {
6 | void onToggle(Alarm alarm);
7 | }
8 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_repeat_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_looks_one_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/learntodroid/simplealarmclock/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/activities/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.activities;
2 |
3 | import android.os.Bundle;
4 |
5 | import androidx.annotation.Nullable;
6 | import androidx.appcompat.app.AppCompatActivity;
7 |
8 | import com.learntodroid.simplealarmclock.R;
9 |
10 | public class MainActivity extends AppCompatActivity {
11 |
12 | @Override
13 | protected void onCreate(@Nullable Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_main);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_alarm_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/data/AlarmDao.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.data;
2 |
3 | import androidx.lifecycle.LiveData;
4 | import androidx.room.Dao;
5 | import androidx.room.Insert;
6 | import androidx.room.Query;
7 | import androidx.room.Update;
8 |
9 | import java.util.List;
10 |
11 | @Dao
12 | public interface AlarmDao {
13 | @Insert
14 | void insert(Alarm alarm);
15 |
16 | @Query("DELETE FROM alarm_table")
17 | void deleteAll();
18 |
19 | @Query("SELECT * FROM alarm_table ORDER BY created ASC")
20 | LiveData> getAlarms();
21 |
22 | @Update
23 | void update(Alarm alarm);
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/createalarm/TimePickerUtil.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.createalarm;
2 |
3 | import android.os.Build;
4 | import android.widget.TimePicker;
5 |
6 | public final class TimePickerUtil {
7 | public static int getTimePickerHour(TimePicker tp) {
8 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
9 | return tp.getHour();
10 | } else {
11 | return tp.getCurrentHour();
12 | }
13 | }
14 |
15 | public static int getTimePickerMinute(TimePicker tp) {
16 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
17 | return tp.getMinute();
18 | } else {
19 | return tp.getCurrentMinute();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/createalarm/CreateAlarmViewModel.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.createalarm;
2 |
3 | import android.app.Application;
4 |
5 | import androidx.annotation.NonNull;
6 | import androidx.lifecycle.AndroidViewModel;
7 |
8 | import com.learntodroid.simplealarmclock.data.Alarm;
9 | import com.learntodroid.simplealarmclock.data.AlarmRepository;
10 |
11 | public class CreateAlarmViewModel extends AndroidViewModel {
12 | private AlarmRepository alarmRepository;
13 |
14 | public CreateAlarmViewModel(@NonNull Application application) {
15 | super(application);
16 |
17 | alarmRepository = new AlarmRepository(application);
18 | }
19 |
20 | public void insert(Alarm alarm) {
21 | alarmRepository.insert(alarm);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/createalarm/DayUtil.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.createalarm;
2 |
3 | import java.util.Calendar;
4 |
5 | public final class DayUtil {
6 | public static final String toDay(int day) throws Exception {
7 | switch (day) {
8 | case Calendar.SUNDAY:
9 | return "Sunday";
10 | case Calendar.MONDAY:
11 | return "Monday";
12 | case Calendar.TUESDAY:
13 | return "Tuesday";
14 | case Calendar.WEDNESDAY:
15 | return "Wednesday";
16 | case Calendar.THURSDAY:
17 | return "Thursday";
18 | case Calendar.FRIDAY:
19 | return "Friday";
20 | case Calendar.SATURDAY:
21 | return "Saturday";
22 | }
23 | throw new Exception("Could not locate day");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/learntodroid/simplealarmclock/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.learntodroid.simplealarmclock", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/data/AlarmRepository.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.data;
2 |
3 | import android.app.Application;
4 | import androidx.lifecycle.LiveData;
5 | import java.util.List;
6 |
7 | public class AlarmRepository {
8 | private AlarmDao alarmDao;
9 | private LiveData> alarmsLiveData;
10 |
11 | public AlarmRepository(Application application) {
12 | AlarmDatabase db = AlarmDatabase.getDatabase(application);
13 | alarmDao = db.alarmDao();
14 | alarmsLiveData = alarmDao.getAlarms();
15 | }
16 |
17 | public void insert(Alarm alarm) {
18 | AlarmDatabase.databaseWriteExecutor.execute(() -> {
19 | alarmDao.insert(alarm);
20 | });
21 | }
22 |
23 | public void update(Alarm alarm) {
24 | AlarmDatabase.databaseWriteExecutor.execute(() -> {
25 | alarmDao.update(alarm);
26 | });
27 | }
28 |
29 | public LiveData> getAlarmsLiveData() {
30 | return alarmsLiveData;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/application/App.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.application;
2 |
3 | import android.app.Application;
4 | import android.app.NotificationChannel;
5 | import android.app.NotificationManager;
6 | import android.os.Build;
7 |
8 | public class App extends Application {
9 | public static final String CHANNEL_ID = "ALARM_SERVICE_CHANNEL";
10 |
11 | @Override
12 | public void onCreate() {
13 | super.onCreate();
14 |
15 | createNotificationChannnel();
16 | }
17 |
18 | private void createNotificationChannnel() {
19 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
20 | NotificationChannel serviceChannel = new NotificationChannel(
21 | CHANNEL_ID,
22 | "Alarm Service Channel",
23 | NotificationManager.IMPORTANCE_DEFAULT
24 | );
25 |
26 | NotificationManager manager = getSystemService(NotificationManager.class);
27 | manager.createNotificationChannel(serviceChannel);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/alarmslist/AlarmsListViewModel.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.alarmslist;
2 |
3 | import android.app.Application;
4 |
5 | import androidx.annotation.NonNull;
6 | import androidx.lifecycle.AndroidViewModel;
7 | import androidx.lifecycle.LiveData;
8 |
9 | import com.learntodroid.simplealarmclock.data.Alarm;
10 | import com.learntodroid.simplealarmclock.data.AlarmRepository;
11 |
12 | import java.util.List;
13 |
14 | public class AlarmsListViewModel extends AndroidViewModel {
15 | private AlarmRepository alarmRepository;
16 | private LiveData> alarmsLiveData;
17 |
18 | public AlarmsListViewModel(@NonNull Application application) {
19 | super(application);
20 |
21 | alarmRepository = new AlarmRepository(application);
22 | alarmsLiveData = alarmRepository.getAlarmsLiveData();
23 | }
24 |
25 | public void update(Alarm alarm) {
26 | alarmRepository.update(alarm);
27 | }
28 |
29 | public LiveData> getAlarmsLiveData() {
30 | return alarmsLiveData;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/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 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.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 |
--------------------------------------------------------------------------------
/app/src/main/res/navigation/nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
16 |
17 |
21 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/data/AlarmDatabase.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.data;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.room.Database;
6 | import androidx.room.Room;
7 | import androidx.room.RoomDatabase;
8 |
9 | import java.util.concurrent.ExecutorService;
10 | import java.util.concurrent.Executors;
11 |
12 | @Database(entities = {Alarm.class}, version = 1, exportSchema = false)
13 | public abstract class AlarmDatabase extends RoomDatabase {
14 | public abstract AlarmDao alarmDao();
15 |
16 | private static volatile AlarmDatabase INSTANCE;
17 | private static final int NUMBER_OF_THREADS = 4;
18 | static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
19 |
20 | static AlarmDatabase getDatabase(final Context context) {
21 | if (INSTANCE == null) {
22 | synchronized (AlarmDatabase.class) {
23 | if (INSTANCE == null) {
24 | INSTANCE = Room.databaseBuilder(
25 | context.getApplicationContext(),
26 | AlarmDatabase.class,
27 | "alarm_database"
28 | ).build();
29 | }
30 | }
31 | }
32 | return INSTANCE;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/service/RescheduleAlarmsService.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.service;
2 |
3 | import android.app.Application;
4 | import android.app.Service;
5 | import android.content.Intent;
6 | import android.os.IBinder;
7 |
8 | import androidx.annotation.Nullable;
9 | import androidx.lifecycle.LifecycleService;
10 | import androidx.lifecycle.Observer;
11 |
12 | import com.learntodroid.simplealarmclock.data.Alarm;
13 | import com.learntodroid.simplealarmclock.data.AlarmRepository;
14 |
15 | import java.util.List;
16 |
17 | public class RescheduleAlarmsService extends LifecycleService {
18 | @Override
19 | public void onCreate() {
20 | super.onCreate();
21 | }
22 |
23 | @Override
24 | public int onStartCommand(Intent intent, int flags, int startId) {
25 | super.onStartCommand(intent, flags, startId);
26 |
27 | AlarmRepository alarmRepository = new AlarmRepository(getApplication());
28 |
29 | alarmRepository.getAlarmsLiveData().observe(this, new Observer>() {
30 | @Override
31 | public void onChanged(List alarms) {
32 | for (Alarm a : alarms) {
33 | if (a.isStarted()) {
34 | a.schedule(getApplicationContext());
35 | }
36 | }
37 | }
38 | });
39 |
40 | return START_STICKY;
41 | }
42 |
43 | @Override
44 | public void onDestroy() {
45 | super.onDestroy();
46 | }
47 |
48 | @Nullable
49 | @Override
50 | public IBinder onBind(Intent intent) {
51 | super.onBind(intent);
52 | return null;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_listalarms.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
12 |
13 |
21 |
22 |
27 |
28 |
29 |
30 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/alarmslist/AlarmRecyclerViewAdapter.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.alarmslist;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.recyclerview.widget.RecyclerView;
9 |
10 | import com.learntodroid.simplealarmclock.data.Alarm;
11 | import com.learntodroid.simplealarmclock.R;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | public class AlarmRecyclerViewAdapter extends RecyclerView.Adapter {
17 | private List alarms;
18 | private OnToggleAlarmListener listener;
19 |
20 | public AlarmRecyclerViewAdapter(OnToggleAlarmListener listener) {
21 | this.alarms = new ArrayList();
22 | this.listener = listener;
23 | }
24 |
25 | @NonNull
26 | @Override
27 | public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
28 | View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alarm, parent, false);
29 | return new AlarmViewHolder(itemView, listener);
30 | }
31 |
32 | @Override
33 | public void onBindViewHolder(@NonNull AlarmViewHolder holder, int position) {
34 | Alarm alarm = alarms.get(position);
35 | holder.bind(alarm);
36 | }
37 |
38 | @Override
39 | public int getItemCount() {
40 | return alarms.size();
41 | }
42 |
43 | public void setAlarms(List alarms) {
44 | this.alarms = alarms;
45 | notifyDataSetChanged();
46 | }
47 |
48 | @Override
49 | public void onViewRecycled(@NonNull AlarmViewHolder holder) {
50 | super.onViewRecycled(holder);
51 | holder.alarmStarted.setOnCheckedChangeListener(null);
52 | }
53 | }
54 |
55 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.2"
6 | defaultConfig {
7 | applicationId "com.learntodroid.simplealarmclock"
8 | minSdkVersion 15
9 | targetSdkVersion 29
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | compileOptions {
21 | sourceCompatibility = 1.8
22 | targetCompatibility = 1.8
23 | }
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(dir: 'libs', include: ['*.jar'])
28 | implementation 'androidx.appcompat:appcompat:1.1.0'
29 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
33 |
34 | // navigation dependencies
35 | def nav_version = "2.3.0-alpha03"
36 | implementation "androidx.navigation:navigation-fragment:$nav_version"
37 | implementation "androidx.navigation:navigation-ui:$nav_version"
38 |
39 | // lifecycle dependencies - viewmodel, livedata
40 | implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
41 |
42 | // room dependencies
43 | def room_version = "2.2.1"
44 | implementation "androidx.room:room-runtime:$room_version"
45 | annotationProcessor "androidx.room:room-compiler:$room_version"
46 | androidTestImplementation "androidx.room:room-testing:$room_version"
47 |
48 | // butterknife dependencies
49 | implementation 'com.jakewharton:butterknife:10.2.1'
50 | annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_ring.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
20 |
30 |
31 |
39 |
40 |
48 |
49 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_alarm.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
19 |
20 |
26 |
27 |
32 |
33 |
38 |
39 |
44 |
45 |
50 |
51 |
52 |
53 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/service/AlarmService.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.service;
2 |
3 | import android.app.Notification;
4 | import android.app.PendingIntent;
5 | import android.app.Service;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.media.MediaPlayer;
9 | import android.os.IBinder;
10 | import android.os.Vibrator;
11 | import android.widget.Toast;
12 |
13 | import androidx.annotation.Nullable;
14 | import androidx.core.app.NotificationCompat;
15 |
16 | import com.learntodroid.simplealarmclock.R;
17 | import com.learntodroid.simplealarmclock.activities.RingActivity;
18 |
19 | import static com.learntodroid.simplealarmclock.application.App.CHANNEL_ID;
20 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.TITLE;
21 |
22 | public class AlarmService extends Service {
23 | private MediaPlayer mediaPlayer;
24 | private Vibrator vibrator;
25 |
26 | @Override
27 | public void onCreate() {
28 | super.onCreate();
29 |
30 | mediaPlayer = MediaPlayer.create(this, R.raw.alarm);
31 | mediaPlayer.setLooping(true);
32 |
33 | vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
34 |
35 | }
36 |
37 | @Override
38 | public int onStartCommand(Intent intent, int flags, int startId) {
39 | Intent notificationIntent = new Intent(this, RingActivity.class);
40 | PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
41 |
42 | String alarmTitle = String.format("%s Alarm", intent.getStringExtra(TITLE));
43 |
44 | Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
45 | .setContentTitle(alarmTitle)
46 | .setContentText("Ring Ring .. Ring Ring")
47 | .setSmallIcon(R.drawable.ic_alarm_black_24dp)
48 | .setContentIntent(pendingIntent)
49 | .build();
50 |
51 | mediaPlayer.start();
52 |
53 | long[] pattern = { 0, 100, 1000 };
54 | vibrator.vibrate(pattern, 0);
55 |
56 | startForeground(1, notification);
57 |
58 | return START_STICKY;
59 | }
60 |
61 | @Override
62 | public void onDestroy() {
63 | super.onDestroy();
64 |
65 | mediaPlayer.stop();
66 | vibrator.cancel();
67 | }
68 |
69 | @Nullable
70 | @Override
71 | public IBinder onBind(Intent intent) {
72 | return null;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/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/learntodroid/simplealarmclock/alarmslist/AlarmViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.alarmslist;
2 |
3 | import android.util.Log;
4 | import android.view.View;
5 | import android.widget.CompoundButton;
6 | import android.widget.ImageView;
7 | import android.widget.Switch;
8 | import android.widget.TextView;
9 | import androidx.annotation.NonNull;
10 | import androidx.recyclerview.widget.RecyclerView;
11 |
12 | import com.learntodroid.simplealarmclock.data.Alarm;
13 | import com.learntodroid.simplealarmclock.R;
14 |
15 | public class AlarmViewHolder extends RecyclerView.ViewHolder {
16 | private TextView alarmTime;
17 | private ImageView alarmRecurring;
18 | private TextView alarmRecurringDays;
19 | private TextView alarmTitle;
20 |
21 | Switch alarmStarted;
22 |
23 | private OnToggleAlarmListener listener;
24 |
25 | public AlarmViewHolder(@NonNull View itemView, OnToggleAlarmListener listener) {
26 | super(itemView);
27 |
28 | alarmTime = itemView.findViewById(R.id.item_alarm_time);
29 | alarmStarted = itemView.findViewById(R.id.item_alarm_started);
30 | alarmRecurring = itemView.findViewById(R.id.item_alarm_recurring);
31 | alarmRecurringDays = itemView.findViewById(R.id.item_alarm_recurringDays);
32 | alarmTitle = itemView.findViewById(R.id.item_alarm_title);
33 |
34 | this.listener = listener;
35 | }
36 |
37 | public void bind(Alarm alarm) {
38 | String alarmText = String.format("%02d:%02d", alarm.getHour(), alarm.getMinute());
39 |
40 | alarmTime.setText(alarmText);
41 | alarmStarted.setChecked(alarm.isStarted());
42 |
43 | if (alarm.isRecurring()) {
44 | alarmRecurring.setImageResource(R.drawable.ic_repeat_black_24dp);
45 | alarmRecurringDays.setText(alarm.getRecurringDaysText());
46 | } else {
47 | alarmRecurring.setImageResource(R.drawable.ic_looks_one_black_24dp);
48 | alarmRecurringDays.setText("Once Off");
49 | }
50 |
51 | if (alarm.getTitle().length() != 0) {
52 | alarmTitle.setText(String.format("%s | %d | %d", alarm.getTitle(), alarm.getAlarmId(), alarm.getCreated()));
53 | } else {
54 | alarmTitle.setText(String.format("%s | %d | %d", "Alarm", alarm.getAlarmId(), alarm.getCreated()));
55 | }
56 |
57 | alarmStarted.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
58 | @Override
59 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
60 | listener.onToggle(alarm);
61 | }
62 | });
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/alarmslist/AlarmsListFragment.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.alarmslist;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.Button;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.annotation.Nullable;
11 | import androidx.fragment.app.Fragment;
12 | import androidx.lifecycle.Observer;
13 | import androidx.lifecycle.ViewModelProviders;
14 | import androidx.navigation.Navigation;
15 | import androidx.recyclerview.widget.LinearLayoutManager;
16 | import androidx.recyclerview.widget.RecyclerView;
17 |
18 | import com.learntodroid.simplealarmclock.data.Alarm;
19 | import com.learntodroid.simplealarmclock.R;
20 |
21 | import java.util.List;
22 |
23 | public class AlarmsListFragment extends Fragment implements OnToggleAlarmListener {
24 | private AlarmRecyclerViewAdapter alarmRecyclerViewAdapter;
25 | private AlarmsListViewModel alarmsListViewModel;
26 | private RecyclerView alarmsRecyclerView;
27 | private Button addAlarm;
28 |
29 | @Override
30 | public void onCreate(@Nullable Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 |
33 | alarmRecyclerViewAdapter = new AlarmRecyclerViewAdapter(this);
34 | alarmsListViewModel = ViewModelProviders.of(this).get(AlarmsListViewModel.class);
35 | alarmsListViewModel.getAlarmsLiveData().observe(this, new Observer>() {
36 | @Override
37 | public void onChanged(List alarms) {
38 | if (alarms != null) {
39 | alarmRecyclerViewAdapter.setAlarms(alarms);
40 | }
41 | }
42 | });
43 | }
44 |
45 | @Nullable
46 | @Override
47 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
48 | View view = inflater.inflate(R.layout.fragment_listalarms, container, false);
49 |
50 | alarmsRecyclerView = view.findViewById(R.id.fragment_listalarms_recylerView);
51 | alarmsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
52 | alarmsRecyclerView.setAdapter(alarmRecyclerViewAdapter);
53 |
54 | addAlarm = view.findViewById(R.id.fragment_listalarms_addAlarm);
55 | addAlarm.setOnClickListener(new View.OnClickListener() {
56 | @Override
57 | public void onClick(View v) {
58 | Navigation.findNavController(v).navigate(R.id.action_alarmsListFragment_to_createAlarmFragment);
59 | }
60 | });
61 |
62 | return view;
63 | }
64 |
65 | @Override
66 | public void onToggle(Alarm alarm) {
67 | if (alarm.isStarted()) {
68 | alarm.cancelAlarm(getContext());
69 | alarmsListViewModel.update(alarm);
70 | } else {
71 | alarm.schedule(getContext());
72 | alarmsListViewModel.update(alarm);
73 | }
74 | }
75 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/activities/RingActivity.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.activities;
2 |
3 | import android.animation.ObjectAnimator;
4 | import android.animation.ValueAnimator;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.view.View;
8 | import android.widget.Button;
9 | import android.widget.ImageView;
10 |
11 | import androidx.annotation.Nullable;
12 | import androidx.appcompat.app.AppCompatActivity;
13 |
14 | import com.learntodroid.simplealarmclock.R;
15 | import com.learntodroid.simplealarmclock.createalarm.TimePickerUtil;
16 | import com.learntodroid.simplealarmclock.data.Alarm;
17 | import com.learntodroid.simplealarmclock.service.AlarmService;
18 |
19 | import java.util.Calendar;
20 | import java.util.Random;
21 |
22 | import butterknife.BindView;
23 | import butterknife.ButterKnife;
24 |
25 | public class RingActivity extends AppCompatActivity {
26 | @BindView(R.id.activity_ring_dismiss) Button dismiss;
27 | @BindView(R.id.activity_ring_snooze) Button snooze;
28 | @BindView(R.id.activity_ring_clock) ImageView clock;
29 |
30 | @Override
31 | protected void onCreate(@Nullable Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_ring);
34 |
35 | ButterKnife.bind(this);
36 |
37 | dismiss.setOnClickListener(new View.OnClickListener() {
38 | @Override
39 | public void onClick(View v) {
40 | Intent intentService = new Intent(getApplicationContext(), AlarmService.class);
41 | getApplicationContext().stopService(intentService);
42 | finish();
43 | }
44 | });
45 |
46 | snooze.setOnClickListener(new View.OnClickListener() {
47 | @Override
48 | public void onClick(View v) {
49 | Calendar calendar = Calendar.getInstance();
50 | calendar.setTimeInMillis(System.currentTimeMillis());
51 | calendar.add(Calendar.MINUTE, 10);
52 |
53 | Alarm alarm = new Alarm(
54 | new Random().nextInt(Integer.MAX_VALUE),
55 | calendar.get(Calendar.HOUR_OF_DAY),
56 | calendar.get(Calendar.MINUTE),
57 | "Snooze",
58 | System.currentTimeMillis(),
59 | true,
60 | false,
61 | false,
62 | false,
63 | false,
64 | false,
65 | false,
66 | false,
67 | false
68 | );
69 |
70 | alarm.schedule(getApplicationContext());
71 |
72 | Intent intentService = new Intent(getApplicationContext(), AlarmService.class);
73 | getApplicationContext().stopService(intentService);
74 | finish();
75 | }
76 | });
77 |
78 | animateClock();
79 | }
80 |
81 | private void animateClock() {
82 | ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(clock, "rotation", 0f, 20f, 0f, -20f, 0f);
83 | rotateAnimation.setRepeatCount(ValueAnimator.INFINITE);
84 | rotateAnimation.setDuration(800);
85 | rotateAnimation.start();
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/createalarm/CreateAlarmFragment.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.createalarm;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.Button;
8 | import android.widget.CheckBox;
9 | import android.widget.CompoundButton;
10 | import android.widget.EditText;
11 | import android.widget.LinearLayout;
12 | import android.widget.TimePicker;
13 |
14 | import androidx.annotation.NonNull;
15 | import androidx.annotation.Nullable;
16 | import androidx.fragment.app.Fragment;
17 | import androidx.lifecycle.ViewModelProviders;
18 | import androidx.navigation.Navigation;
19 |
20 | import com.learntodroid.simplealarmclock.R;
21 | import com.learntodroid.simplealarmclock.data.Alarm;
22 |
23 | import java.util.Random;
24 |
25 | import butterknife.BindView;
26 | import butterknife.ButterKnife;
27 |
28 | public class CreateAlarmFragment extends Fragment {
29 | @BindView(R.id.fragment_createalarm_timePicker) TimePicker timePicker;
30 | @BindView(R.id.fragment_createalarm_title) EditText title;
31 | @BindView(R.id.fragment_createalarm_scheduleAlarm) Button scheduleAlarm;
32 | @BindView(R.id.fragment_createalarm_recurring) CheckBox recurring;
33 | @BindView(R.id.fragment_createalarm_checkMon) CheckBox mon;
34 | @BindView(R.id.fragment_createalarm_checkTue) CheckBox tue;
35 | @BindView(R.id.fragment_createalarm_checkWed) CheckBox wed;
36 | @BindView(R.id.fragment_createalarm_checkThu) CheckBox thu;
37 | @BindView(R.id.fragment_createalarm_checkFri) CheckBox fri;
38 | @BindView(R.id.fragment_createalarm_checkSat) CheckBox sat;
39 | @BindView(R.id.fragment_createalarm_checkSun) CheckBox sun;
40 | @BindView(R.id.fragment_createalarm_recurring_options) LinearLayout recurringOptions;
41 |
42 | private CreateAlarmViewModel createAlarmViewModel;
43 |
44 | @Override
45 | public void onCreate(@Nullable Bundle savedInstanceState) {
46 | super.onCreate(savedInstanceState);
47 |
48 | createAlarmViewModel = ViewModelProviders.of(this).get(CreateAlarmViewModel.class);
49 | }
50 |
51 | @Nullable
52 | @Override
53 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
54 | View view = inflater.inflate(R.layout.fragment_createalarm, container, false);
55 |
56 | ButterKnife.bind(this, view);
57 |
58 | recurring.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
59 | @Override
60 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
61 | if (isChecked) {
62 | recurringOptions.setVisibility(View.VISIBLE);
63 | } else {
64 | recurringOptions.setVisibility(View.GONE);
65 | }
66 | }
67 | });
68 |
69 | scheduleAlarm.setOnClickListener(new View.OnClickListener() {
70 | @Override
71 | public void onClick(View v) {
72 | scheduleAlarm();
73 | Navigation.findNavController(v).navigate(R.id.action_createAlarmFragment_to_alarmsListFragment);
74 | }
75 | });
76 |
77 | return view;
78 | }
79 |
80 | private void scheduleAlarm() {
81 | int alarmId = new Random().nextInt(Integer.MAX_VALUE);
82 |
83 | Alarm alarm = new Alarm(
84 | alarmId,
85 | TimePickerUtil.getTimePickerHour(timePicker),
86 | TimePickerUtil.getTimePickerMinute(timePicker),
87 | title.getText().toString(),
88 | System.currentTimeMillis(),
89 | true,
90 | recurring.isChecked(),
91 | mon.isChecked(),
92 | tue.isChecked(),
93 | wed.isChecked(),
94 | thu.isChecked(),
95 | fri.isChecked(),
96 | sat.isChecked(),
97 | sun.isChecked()
98 | );
99 |
100 | createAlarmViewModel.insert(alarm);
101 |
102 | alarm.schedule(getContext());
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/broadcastreceiver/AlarmBroadcastReceiver.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.broadcastreceiver;
2 |
3 | import android.app.Application;
4 | import android.content.BroadcastReceiver;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.AsyncTask;
8 | import android.os.Build;
9 | import android.util.Log;
10 | import android.widget.Toast;
11 |
12 | import androidx.lifecycle.Observer;
13 |
14 | import com.learntodroid.simplealarmclock.data.Alarm;
15 | import com.learntodroid.simplealarmclock.data.AlarmRepository;
16 | import com.learntodroid.simplealarmclock.service.AlarmService;
17 | import com.learntodroid.simplealarmclock.service.RescheduleAlarmsService;
18 |
19 | import java.util.ArrayList;
20 | import java.util.Calendar;
21 | import java.util.List;
22 |
23 | public class AlarmBroadcastReceiver extends BroadcastReceiver {
24 | public static final String MONDAY = "MONDAY";
25 | public static final String TUESDAY = "TUESDAY";
26 | public static final String WEDNESDAY = "WEDNESDAY";
27 | public static final String THURSDAY = "THURSDAY";
28 | public static final String FRIDAY = "FRIDAY";
29 | public static final String SATURDAY = "SATURDAY";
30 | public static final String SUNDAY = "SUNDAY";
31 | public static final String RECURRING = "RECURRING";
32 | public static final String TITLE = "TITLE";
33 |
34 | @Override
35 | public void onReceive(Context context, Intent intent) {
36 | if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
37 | String toastText = String.format("Alarm Reboot");
38 | Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show();
39 | startRescheduleAlarmsService(context);
40 | }
41 | else {
42 | String toastText = String.format("Alarm Received");
43 | Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show();
44 | if (!intent.getBooleanExtra(RECURRING, false)) {
45 | startAlarmService(context, intent);
46 | } {
47 | if (alarmIsToday(intent)) {
48 | startAlarmService(context, intent);
49 | }
50 | }
51 | }
52 | }
53 |
54 | private boolean alarmIsToday(Intent intent) {
55 | Calendar calendar = Calendar.getInstance();
56 | calendar.setTimeInMillis(System.currentTimeMillis());
57 | int today = calendar.get(Calendar.DAY_OF_WEEK);
58 |
59 | switch(today) {
60 | case Calendar.MONDAY:
61 | if (intent.getBooleanExtra(MONDAY, false))
62 | return true;
63 | return false;
64 | case Calendar.TUESDAY:
65 | if (intent.getBooleanExtra(TUESDAY, false))
66 | return true;
67 | return false;
68 | case Calendar.WEDNESDAY:
69 | if (intent.getBooleanExtra(WEDNESDAY, false))
70 | return true;
71 | return false;
72 | case Calendar.THURSDAY:
73 | if (intent.getBooleanExtra(THURSDAY, false))
74 | return true;
75 | return false;
76 | case Calendar.FRIDAY:
77 | if (intent.getBooleanExtra(FRIDAY, false))
78 | return true;
79 | return false;
80 | case Calendar.SATURDAY:
81 | if (intent.getBooleanExtra(SATURDAY, false))
82 | return true;
83 | return false;
84 | case Calendar.SUNDAY:
85 | if (intent.getBooleanExtra(SUNDAY, false))
86 | return true;
87 | return false;
88 | }
89 | return false;
90 | }
91 |
92 | private void startAlarmService(Context context, Intent intent) {
93 | Intent intentService = new Intent(context, AlarmService.class);
94 | intentService.putExtra(TITLE, intent.getStringExtra(TITLE));
95 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
96 | context.startForegroundService(intentService);
97 | } else {
98 | context.startService(intentService);
99 | }
100 | }
101 |
102 | private void startRescheduleAlarmsService(Context context) {
103 | Intent intentService = new Intent(context, RescheduleAlarmsService.class);
104 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
105 | context.startForegroundService(intentService);
106 | } else {
107 | context.startService(intentService);
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_createalarm.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
13 |
14 |
22 |
23 |
29 |
30 |
38 |
39 |
45 |
46 |
47 |
53 |
54 |
62 |
63 |
70 |
71 |
76 |
77 |
82 |
83 |
88 |
89 |
94 |
95 |
100 |
101 |
106 |
107 |
112 |
113 |
114 |
115 |
123 |
124 |
125 |
126 |
--------------------------------------------------------------------------------
/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/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/learntodroid/simplealarmclock/data/Alarm.java:
--------------------------------------------------------------------------------
1 | package com.learntodroid.simplealarmclock.data;
2 |
3 | import android.app.AlarmManager;
4 | import android.app.PendingIntent;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.util.Log;
8 | import android.widget.Toast;
9 |
10 | import androidx.annotation.NonNull;
11 | import androidx.room.Entity;
12 | import androidx.room.PrimaryKey;
13 |
14 | import com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver;
15 | import com.learntodroid.simplealarmclock.createalarm.DayUtil;
16 |
17 | import java.util.Calendar;
18 |
19 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.FRIDAY;
20 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.MONDAY;
21 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.RECURRING;
22 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.SATURDAY;
23 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.SUNDAY;
24 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.THURSDAY;
25 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.TITLE;
26 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.TUESDAY;
27 | import static com.learntodroid.simplealarmclock.broadcastreceiver.AlarmBroadcastReceiver.WEDNESDAY;
28 |
29 | @Entity(tableName = "alarm_table")
30 | public class Alarm {
31 | @PrimaryKey
32 | @NonNull
33 | private int alarmId;
34 |
35 | private int hour, minute;
36 | private boolean started, recurring;
37 | private boolean monday, tuesday, wednesday, thursday, friday, saturday, sunday;
38 | private String title;
39 |
40 | private long created;
41 |
42 | public Alarm(int alarmId, int hour, int minute, String title, long created, boolean started, boolean recurring, boolean monday, boolean tuesday, boolean wednesday, boolean thursday, boolean friday, boolean saturday, boolean sunday) {
43 | this.alarmId = alarmId;
44 | this.hour = hour;
45 | this.minute = minute;
46 | this.started = started;
47 |
48 | this.recurring = recurring;
49 |
50 | this.monday = monday;
51 | this.tuesday = tuesday;
52 | this.wednesday = wednesday;
53 | this.thursday = thursday;
54 | this.friday = friday;
55 | this.saturday = saturday;
56 | this.sunday = sunday;
57 |
58 | this.title = title;
59 |
60 | this.created = created;
61 | }
62 |
63 | public int getHour() {
64 | return hour;
65 | }
66 |
67 | public int getMinute() {
68 | return minute;
69 | }
70 |
71 | public boolean isStarted() {
72 | return started;
73 | }
74 |
75 | public int getAlarmId() {
76 | return alarmId;
77 | }
78 |
79 | public void setAlarmId(int alarmId) {
80 | this.alarmId = alarmId;
81 | }
82 |
83 | public boolean isRecurring() {
84 | return recurring;
85 | }
86 |
87 | public boolean isMonday() {
88 | return monday;
89 | }
90 |
91 | public boolean isTuesday() {
92 | return tuesday;
93 | }
94 |
95 | public boolean isWednesday() {
96 | return wednesday;
97 | }
98 |
99 | public boolean isThursday() {
100 | return thursday;
101 | }
102 |
103 | public boolean isFriday() {
104 | return friday;
105 | }
106 |
107 | public boolean isSaturday() {
108 | return saturday;
109 | }
110 |
111 | public boolean isSunday() {
112 | return sunday;
113 | }
114 |
115 | public void schedule(Context context) {
116 | AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
117 |
118 | Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
119 | intent.putExtra(RECURRING, recurring);
120 | intent.putExtra(MONDAY, monday);
121 | intent.putExtra(TUESDAY, tuesday);
122 | intent.putExtra(WEDNESDAY, wednesday);
123 | intent.putExtra(THURSDAY, thursday);
124 | intent.putExtra(FRIDAY, friday);
125 | intent.putExtra(SATURDAY, saturday);
126 | intent.putExtra(SUNDAY, sunday);
127 |
128 | intent.putExtra(TITLE, title);
129 |
130 | PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);
131 |
132 | Calendar calendar = Calendar.getInstance();
133 | calendar.setTimeInMillis(System.currentTimeMillis());
134 | calendar.set(Calendar.HOUR_OF_DAY, hour);
135 | calendar.set(Calendar.MINUTE, minute);
136 | calendar.set(Calendar.SECOND, 0);
137 | calendar.set(Calendar.MILLISECOND, 0);
138 |
139 | // if alarm time has already passed, increment day by 1
140 | if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
141 | calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
142 | }
143 |
144 | if (!recurring) {
145 | String toastText = null;
146 | try {
147 | toastText = String.format("One Time Alarm %s scheduled for %s at %02d:%02d", title, DayUtil.toDay(calendar.get(Calendar.DAY_OF_WEEK)), hour, minute, alarmId);
148 | } catch (Exception e) {
149 | e.printStackTrace();
150 | }
151 | Toast.makeText(context, toastText, Toast.LENGTH_LONG).show();
152 |
153 | alarmManager.setExact(
154 | AlarmManager.RTC_WAKEUP,
155 | calendar.getTimeInMillis(),
156 | alarmPendingIntent
157 | );
158 | } else {
159 | String toastText = String.format("Recurring Alarm %s scheduled for %s at %02d:%02d", title, getRecurringDaysText(), hour, minute, alarmId);
160 | Toast.makeText(context, toastText, Toast.LENGTH_LONG).show();
161 |
162 | final long RUN_DAILY = 24 * 60 * 60 * 1000;
163 | alarmManager.setRepeating(
164 | AlarmManager.RTC_WAKEUP,
165 | calendar.getTimeInMillis(),
166 | RUN_DAILY,
167 | alarmPendingIntent
168 | );
169 | }
170 |
171 | this.started = true;
172 | }
173 |
174 | public void cancelAlarm(Context context) {
175 | AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
176 | Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
177 | PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);
178 | alarmManager.cancel(alarmPendingIntent);
179 | this.started = false;
180 |
181 | String toastText = String.format("Alarm cancelled for %02d:%02d with id %d", hour, minute, alarmId);
182 | Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show();
183 | Log.i("cancel", toastText);
184 | }
185 |
186 | public String getRecurringDaysText() {
187 | if (!recurring) {
188 | return null;
189 | }
190 |
191 | String days = "";
192 | if (monday) {
193 | days += "Mo ";
194 | }
195 | if (tuesday) {
196 | days += "Tu ";
197 | }
198 | if (wednesday) {
199 | days += "We ";
200 | }
201 | if (thursday) {
202 | days += "Th ";
203 | }
204 | if (friday) {
205 | days += "Fr ";
206 | }
207 | if (saturday) {
208 | days += "Sa ";
209 | }
210 | if (sunday) {
211 | days += "Su ";
212 | }
213 |
214 | return days;
215 | }
216 |
217 | public String getTitle() {
218 | return title;
219 | }
220 |
221 | public long getCreated() {
222 | return created;
223 | }
224 |
225 | public void setCreated(long created) {
226 | this.created = created;
227 | }
228 | }
229 |
--------------------------------------------------------------------------------