Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion newProject/.idea/compiler.xml

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

2 changes: 2 additions & 0 deletions newProject/.idea/gradle.xml

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

2 changes: 1 addition & 1 deletion newProject/.idea/misc.xml

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

17 changes: 17 additions & 0 deletions newProject/.idea/runConfigurations.xml

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

3 changes: 3 additions & 0 deletions newProject/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
viewBinding {
enable = true
}
}

dependencies {
Expand Down
3 changes: 2 additions & 1 deletion newProject/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -21,6 +21,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RandomActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,20 +1,87 @@
package com.example.assignment

import android.Manifest
import android.app.NotificationManager
import android.content.Context
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.assignment.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var notificationHelper: NotificationHelper
private var count: Int = 0

private val postResultLauncher: ActivityResultLauncher<String> =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted)
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show()
else
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

권한이 거부되었을 때 사용되는 메소드가 따로 있습니다! 나중에 사용해보세용

shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
binding = ActivityMainBinding.inflate(layoutInflater)
notificationHelper = NotificationHelper(this)
setContentView(binding.root)

if (intent.getBooleanExtra("closeNotification", false)) {
val activityData = getSharedPreferences("activityData", Context.MODE_PRIVATE)
count = activityData.getInt("count", 0)

val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(NotificationHelper.NOTIFICATION_ID)
} else {
count = 0
}
binding.countText.text = count.toString()

if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
showRationaleDialog(
"Permission Demo requires Notification Permission",
"Notification Permission is denied"
)
} else {
postResultLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}

val btnShowToast = binding.toastButton
btnShowToast.setOnClickListener() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
btnShowToast.setOnClickListener() {
binding.toastButton.setOnClickListener() {

Toast.makeText(this, "toast message", Toast.LENGTH_SHORT).show()
}

val btnIncrementCount = binding.countButton
val countText = binding.countText
btnIncrementCount.setOnClickListener() {
count++
countText.text = count.toString()
}

val randomData = intent.getIntExtra("randomData", 0)
if (randomData != 0) {
count = randomData
countText.text = count.toString()
}

val btnNavigateToRandom = binding.randomButton
btnNavigateToRandom.setOnClickListener() {
notificationHelper.showNotification(count)
}
}

private fun showRationaleDialog(title: String, message: String) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle(title)
.setMessage(message)
.setPositiveButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
builder.create().show()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.example.assignment

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat

class NotificationHelper(private val context: Context) {
companion object {
private const val CHANNEL_ID = "random_channel_id"
private const val CHANNEL_NAME = "Random Channel"
private const val CHANNEL_DESCRIPTION = "Generate and send random numbers"
const val NOTIFICATION_ID = 1
}

init {
createNotificationChannel()
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = CHANNEL_NAME
val descriptionText = CHANNEL_DESCRIPTION
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}

fun showNotification(count: Int) {
val activityData = context.getSharedPreferences("activityData", Context.MODE_PRIVATE)
activityData.edit().putInt("count", count).apply()

val randomIntent = Intent(context, RandomActivity::class.java).apply {
putExtra("countData", count)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

countData를 상수로 정의해서 사용하면 더 좋을 것 같습니당

}
val randomPendingIntent: PendingIntent =
PendingIntent.getActivity(
context,
0,
randomIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val closeIntent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra("closeNotification", true)
}
val closePendingIntent: PendingIntent =
PendingIntent.getActivity(
context,
1,
closeIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(context.getString(R.string.notification_title))
.setContentText(context.getString((R.string.notification_content)))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(randomPendingIntent)
.setAutoCancel(true)
.addAction(
R.drawable.ic_launcher_foreground,
context.getString(R.string.btn_more),
randomPendingIntent
)
.addAction(
R.drawable.ic_launcher_foreground,
context.getString(R.string.btn_close),
closePendingIntent
)

val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, builder.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.assignment

import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.assignment.databinding.ActivityRandomBinding

class RandomActivity : AppCompatActivity() {
private lateinit var binding: ActivityRandomBinding

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

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(NotificationHelper.NOTIFICATION_ID)

val receivedCount = intent.getIntExtra("countData", 0)
val randomNumberTextView = binding.countTextRandomView
val randomNum = (0..receivedCount).random()
randomNumberTextView.text = randomNum.toString()

val btnBack = binding.backButtonRandomView
btnBack.setOnClickListener() {
val intent = Intent(this, MainActivity::class.java).apply {
putExtra("randomData", randomNum)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(intent)
finish()
}
}
}
6 changes: 6 additions & 0 deletions newProject/app/src/main/res/drawable/btn_custom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/main_button_color" />
<corners android:radius="10dp" />
</shape>
42 changes: 40 additions & 2 deletions newProject/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,53 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_background_color"
tools:context=".MainActivity">

<TextView
android:id="@+id/count_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="0"
android:textColor="@color/text_color"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />

<android.widget.Button
android:id="@+id/toast_button"
style="@style/custom_button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/count_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/count_text" />

<android.widget.Button
android:id="@+id/count_button"
style="@style/custom_button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COUNT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/random_button"
app:layout_constraintStart_toEndOf="@id/toast_button"
app:layout_constraintTop_toBottomOf="@id/count_text" />

<android.widget.Button
android:id="@+id/random_button"
style="@style/custom_button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RANDOM"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/count_button"
app:layout_constraintTop_toBottomOf="@id/count_text" />

</androidx.constraintlayout.widget.ConstraintLayout>
33 changes: 33 additions & 0 deletions newProject/app/src/main/res/layout/activity_random.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_background_color">

<TextView
android:id="@+id/count_text_random_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/text_color"
android:textSize="100sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />

<android.widget.Button
android:id="@+id/back_button_random_view"
style="@style/custom_button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BACK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/count_text_random_view" />


</androidx.constraintlayout.widget.ConstraintLayout>
Loading