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
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -12,6 +17,12 @@
android:supportsRtl="true"
android:theme="@style/Theme.BCSD_Android_20251"
tools:targetApi="31">
<activity
android:name=".Adapter"
Copy link
Contributor

Choose a reason for hiding this comment

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

adapter와 data class는 activity가 아니고, androidmanifest에 선언 하지 않아야 합니다

android:exported="false" />
<activity
android:name=".Music"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/Adapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.bcsd_android_2025_1

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class MusicAdapter(private val musicList: List<Music>) :
Copy link
Contributor

Choose a reason for hiding this comment

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

클래스 이름과 파일 이름을 동일하게 수정해주세요

RecyclerView.Adapter<MusicAdapter.MusicViewHolder>() {

class MusicViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val titleText: TextView = view.findViewById(R.id.text_list_Title)
val artistText: TextView = view.findViewById(R.id.text_list_Artist)
val durationText: TextView = view.findViewById(R.id.text_list_time)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MusicViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.music, parent, false)
return MusicViewHolder(view)
}

override fun onBindViewHolder(holder: MusicViewHolder, position: Int) {
val music = musicList[position]
holder.titleText.text = music.title
holder.artistText.text = music.artist

val seconds = (music.duration / 1000) % 60
val minutes = (music.duration / (1000 * 60)) % 60
holder.durationText.text = String.format("%02d:%02d", minutes, seconds)

holder.view.setOnClickListener {
Toast.makeText(holder.view.context, "재생: ${music.title}", Toast.LENGTH_SHORT).show()
}
}

override fun getItemCount(): Int = musicList.size
}


103 changes: 99 additions & 4 deletions app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,109 @@
package com.example.bcsd_android_2025_1

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.provider.Settings
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 androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.LinearLayoutManager

class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView

private val requestPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
val permission = getAudioPermission()

if (isGranted) {
getAudioFile()
} else {
if (shouldShowRequestPermissionRationale(permission)) {
showPermissionRationaleDialog()
} else {
showSettingsDialog()
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

recyclerView = findViewById(R.id.recyclerView)

requestPermission.launch(getAudioPermission())
}

private fun getAudioPermission(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Manifest.permission.READ_MEDIA_AUDIO
} else {
Manifest.permission.READ_EXTERNAL_STORAGE
}
}

private fun showPermissionRationaleDialog() {
AlertDialog.Builder(this)
.setTitle(getString(R.string.permission_required))
.setMessage(getString(R.string.permission_required_music))
.setPositiveButton(getString(R.string.permission_request)) { _, _ ->
requestPermission.launch(getAudioPermission())
}
.setNegativeButton(getString(R.string.cancel), null)
.show()
}

private fun showSettingsDialog() {
AlertDialog.Builder(this)
.setTitle(getString(R.string.permission_denied))
.setMessage(getString(R.string.permission_setup_request))
.setPositiveButton(getString(R.string.permission_move_settings)) { _, _ ->
val intent = Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", packageName, null)
)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
.setNegativeButton(getString(R.string.cancel), null)
.show()
}

private fun getAudioFile() {
val musicList = mutableListOf<Music>()
val projection = arrayOf(
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION
)

val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"

contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER
)?.use { cursor ->
val titleCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)
val artistCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)
val durationCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)

while (cursor.moveToNext()) {
val title = cursor.getString(titleCol)
val artist = cursor.getString(artistCol)
val duration = cursor.getLong(durationCol)

musicList.add(Music(title, artist, duration))
}
}

recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = MusicAdapter(musicList)
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/Music.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.bcsd_android_2025_1

data class Music(
val title: String,
val artist: String,
val duration: Long
)
12 changes: 4 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Android!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
Copy link
Contributor

Choose a reason for hiding this comment

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

xml naming convention은 snake_case입니다

android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
48 changes: 48 additions & 0 deletions app/src/main/res/layout/music.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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="wrap_content"
android:padding="12dp">

<TextView
android:id="@+id/text_list_Title"
Copy link
Contributor

Choose a reason for hiding this comment

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

snake_case에서는 대문자를 사용하지 않습니다

android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<TextView
android:id="@+id/text_list_Artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="@id/text_list_Title"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/text_list_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="@id/text_list_Title"
app:layout_constraintEnd_toEndOf="parent" />

<View
android:id="@+id/text_list_line"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/gray"
app:layout_constraintTop_toBottomOf="@id/text_list_time"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="8sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="gray">#BDBDBD</color>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<resources>
<string name="app_name">BCSD_Android_2025-1</string>
<string name="play">재생</string>
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

<string name="permission_required">권한 필요</string>
<string name="permission_required_music">음악을 가져오기 위해 권한이 필요합니다.</string>
<string name="permission_request">권한 요청</string>
<string name="cancel">취소</string>
<string name="permission_denied">권한이 거부됨.</string>
<string name="permission_setup_request">설정에서 권한을 직접 허용해주세요.</string>
<string name="permission_move_settings">설정으로 이동</string>
</resources>