Skip to content

Commit

Permalink
Android App
Browse files Browse the repository at this point in the history
  • Loading branch information
otamurod committed Sep 5, 2022
1 parent e95abd8 commit 08befd4
Show file tree
Hide file tree
Showing 1,358 changed files with 27,498 additions and 0 deletions.
15 changes: 15 additions & 0 deletions AlarmManager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions AlarmManager/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions AlarmManager/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions AlarmManager/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions AlarmManager/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions AlarmManager/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
46 changes: 46 additions & 0 deletions AlarmManager/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
compileSdk 32

defaultConfig {
applicationId "com.otamurod.alarmmanager"
minSdk 16
targetSdk 32
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures{
viewBinding true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
21 changes: 21 additions & 0 deletions AlarmManager/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.otamurod.alarmmanager

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.otamurod.alarmmanager", appContext.packageName)
}
}
32 changes: 32 additions & 0 deletions AlarmManager/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.otamurod.alarmmanager">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AlarmManager"
tools:targetApi="31">
<activity
android:name=".DestinationActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".AlarmReceiver" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.otamurod.alarmmanager

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat

class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {

val i = Intent(context, DestinationActivity::class.java)
intent!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(context, 0, i, 0)

val builder = NotificationCompat.Builder(context!!, "channelId")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Alarm Manager")
.setContentText("Learn more about Android development")
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)

val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(123, builder.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.otamurod.alarmmanager

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.otamurod.alarmmanager.databinding.ActivityDestinationBinding

class DestinationActivity : AppCompatActivity() {
private lateinit var binding: ActivityDestinationBinding
private lateinit var alarmManager: AlarmManager
private lateinit var pendingIntent: PendingIntent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDestinationBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.cancelAlarmBtn.setOnClickListener {
cancelAlarm()
}
}

private fun cancelAlarm() {

alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)

pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)

alarmManager.cancel(pendingIntent)

Toast.makeText(this, "Alarm Cancelled", Toast.LENGTH_SHORT).show()

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.otamurod.alarmmanager

import android.app.AlarmManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import com.otamurod.alarmmanager.databinding.ActivityMainBinding
import java.util.*

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private lateinit var timePicker: MaterialTimePicker
private lateinit var calendar: Calendar
private lateinit var alarmManager: AlarmManager
private lateinit var pendingIntent: PendingIntent


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

createNotificationChannel()

calendar = Calendar.getInstance()

binding.selectTimeBtn.setOnClickListener {
showTimePicker()
}

binding.setAlarmBtn.setOnClickListener {
setAlarm()
}

binding.cancelAlarmBtn.setOnClickListener {
cancelAlarm()
}
}

private fun cancelAlarm() {

alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)

pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)

alarmManager.cancel(pendingIntent)

Toast.makeText(this, "Alarm Cancelled", Toast.LENGTH_SHORT).show()

}

private fun setAlarm() {

alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)

pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)

alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)

Toast.makeText(this, "Alarm Set Successfully", Toast.LENGTH_SHORT).show()

}

private fun showTimePicker() {
timePicker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(0)
.setTitleText("Select Alarm Time")
.build()

timePicker.show(supportFragmentManager, "time")

timePicker.addOnPositiveButtonClickListener {
if (timePicker.hour > 12) {
binding.selectedTime.text =
String.format("%02d:%02d PM", timePicker.hour - 12, timePicker.minute)
} else {
binding.selectedTime.text =
String.format("%02d:%02d AM", timePicker.hour, timePicker.minute)
}

calendar[Calendar.HOUR_OF_DAY] = timePicker.hour
calendar[Calendar.MINUTE] = timePicker.minute
calendar[Calendar.SECOND] = 0
calendar[Calendar.MILLISECOND] = 0

}
}

private fun createNotificationChannel() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name: CharSequence = "RemainderChannel"
val description = "Channel for Alarm Manager"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel("channelId", name, importance)
channel.description = description
val notificationManager = getSystemService(NotificationManager::class.java)

notificationManager.createNotificationChannel(channel)

}
}
}
Loading

0 comments on commit 08befd4

Please sign in to comment.