Skip to content

Commit 6a5043a

Browse files
committed
Initial commit
0 parents  commit 6a5043a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+994
-0
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

.idea/codeStyles/Project.xml

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android-extensions'
3+
apply plugin: 'kotlin-android'
4+
5+
android {
6+
compileSdkVersion 28
7+
defaultConfig {
8+
applicationId "com.example.alarms"
9+
minSdkVersion 15
10+
targetSdkVersion 28
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
implementation fileTree(dir: 'libs', include: ['*.jar'])
25+
implementation 'com.android.support:appcompat-v7:28.0.0'
26+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
27+
testImplementation 'junit:junit:4.12'
28+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
29+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
30+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
}
32+
repositories {
33+
mavenCentral()
34+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.alarms
2+
3+
import android.content.Context
4+
import android.support.test.InstrumentationRegistry
5+
import android.support.test.runner.AndroidJUnit4
6+
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
10+
import org.junit.Assert.*
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see [Testing documentation](http://d.android.com/tools/testing)
16+
*/
17+
@RunWith(AndroidJUnit4::class)
18+
class ExampleInstrumentedTest {
19+
@Test
20+
fun useAppContext() {
21+
// Context of the app under test.
22+
val appContext = InstrumentationRegistry.getTargetContext()
23+
24+
assertEquals("com.example.alarms", appContext.packageName)
25+
}
26+
}

app/src/main/AndroidManifest.xml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.alarms">
5+
6+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme"
14+
tools:ignore="GoogleAppIndexingWarning">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
23+
<receiver android:name=".TimeChangedReceiver">
24+
<intent-filter>
25+
<action android:name="android.intent.action.TIME_SET" />
26+
</intent-filter>
27+
</receiver>
28+
29+
<receiver
30+
android:name=".BootCompleteReceiver"
31+
android:enabled="false">
32+
<intent-filter>
33+
<action android:name="android.intent.action.BOOT_COMPLETED" />
34+
</intent-filter>
35+
</receiver>
36+
<receiver android:name=".NotificationReceiver" />
37+
38+
</application>
39+
40+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.alarms
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
7+
class BootCompleteReceiver : BroadcastReceiver() {
8+
9+
override fun onReceive(context: Context?, intent: Intent?) {
10+
11+
if (intent?.action == "android.intent.action.BOOT_COMPLETED") {
12+
// ideally we should be fetching the data from a database
13+
val sharedPref = context?.getSharedPreferences("MyPref", Context.MODE_PRIVATE) ?: return
14+
val timeInMilli = sharedPref.getLong("timeInMilli", 1)
15+
Utils.setAlarm(context, timeInMilli)
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.example.alarms
2+
3+
import android.app.TimePickerDialog
4+
import android.content.ComponentName
5+
import android.content.Context
6+
import android.content.pm.PackageManager
7+
import android.support.v7.app.AppCompatActivity
8+
import android.os.Bundle
9+
import android.widget.Toast
10+
import kotlinx.android.synthetic.main.activity_main.*
11+
import java.text.SimpleDateFormat
12+
import java.util.*
13+
14+
class MainActivity : AppCompatActivity() {
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
setContentView(R.layout.activity_main)
19+
20+
var timeInMilliSeconds: Long = 0
21+
val receiver = ComponentName(applicationContext, BootCompleteReceiver::class.java)
22+
23+
applicationContext.packageManager?.setComponentEnabledSetting(
24+
receiver,
25+
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
26+
PackageManager.DONT_KILL_APP
27+
)
28+
29+
startTimeText.setOnClickListener {
30+
// Get Current Time
31+
val calendar = Calendar.getInstance()
32+
val hour = calendar.get(Calendar.HOUR_OF_DAY)
33+
val minute = calendar.get(Calendar.MINUTE)
34+
35+
// Launch Time Picker Dialog
36+
val timePickerDialog = TimePickerDialog(this,
37+
TimePickerDialog.OnTimeSetListener { _, hourOfDay, minuteOfHour ->
38+
39+
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
40+
calendar.set(Calendar.MINUTE, minuteOfHour)
41+
calendar.set(Calendar.SECOND, 0)
42+
43+
val amPm = if (hourOfDay < 12) "am" else "pm"
44+
val formattedTime = String.format("%02d:%02d %s", hourOfDay, minuteOfHour, amPm)
45+
startTimeText.text = formattedTime
46+
47+
val sdf = SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault())
48+
val formattedDate = sdf.format(calendar.time)
49+
val date = sdf.parse(formattedDate)
50+
timeInMilliSeconds = date.time
51+
}, hour, minute, false)
52+
timePickerDialog.show()
53+
}
54+
55+
setAlarm.setOnClickListener {
56+
if (timeInMilliSeconds.toInt() != 0) {
57+
Toast.makeText(this, "Alarm has been set!", Toast.LENGTH_LONG).show()
58+
59+
val sharedPref = this.getSharedPreferences("MyPref", Context.MODE_PRIVATE)
60+
?: return@setOnClickListener
61+
with(sharedPref.edit()) {
62+
putLong("timeInMilli", timeInMilliSeconds)
63+
apply()
64+
}
65+
Utils.setAlarm(this, timeInMilliSeconds)
66+
} else {
67+
Toast.makeText(this, "Please enter the time first!", Toast.LENGTH_LONG).show()
68+
}
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.alarms
2+
3+
import android.app.NotificationChannel
4+
import android.app.NotificationManager
5+
import android.content.BroadcastReceiver
6+
import android.content.Context
7+
import android.content.Intent
8+
import android.os.Build
9+
import android.support.v4.app.NotificationCompat
10+
11+
class NotificationReceiver : BroadcastReceiver() {
12+
13+
override fun onReceive(context: Context?, intent: Intent?) {
14+
15+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
16+
// Create the NotificationChannel
17+
val name = "Alarm"
18+
val descriptionText = "Alarm details"
19+
val importance = NotificationManager.IMPORTANCE_DEFAULT
20+
val mChannel = NotificationChannel("AlarmId", name, importance)
21+
mChannel.description = descriptionText
22+
val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
23+
notificationManager.createNotificationChannel(mChannel)
24+
}
25+
26+
// Create the notification to be shown
27+
val mBuilder = NotificationCompat.Builder(context!!, "AlarmId")
28+
.setSmallIcon(R.mipmap.ic_launcher)
29+
.setContentTitle("Alarm")
30+
.setContentText("Here\'s your alarm!")
31+
.setAutoCancel(true)
32+
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
33+
34+
// Get the Notification manager service
35+
val am = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
36+
37+
// Generate an Id for each notification
38+
val id = System.currentTimeMillis() / 1000
39+
40+
// Show a notification
41+
am.notify(id.toInt(), mBuilder.build())
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.alarms
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
7+
8+
class TimeChangedReceiver : BroadcastReceiver() {
9+
10+
override fun onReceive(context: Context?, intent: Intent?) {
11+
12+
if (intent?.action == "android.intent.action.TIME_SET") {
13+
// ideally we should be fetching the data from a database
14+
val sharedPref = context?.getSharedPreferences("MyPref",Context.MODE_PRIVATE) ?: return
15+
val timeInMilli = sharedPref.getLong("timeInMilli", 1)
16+
Utils.setAlarm(context,timeInMilli)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)