-
Notifications
You must be signed in to change notification settings - Fork 12
[김은서_Android] 9주차 과제 제출 #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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>) : | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
|
|
||
|
|
||
| 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) | ||
| } | ||
| } | ||
| } |
| 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 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
| 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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
| 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> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment.
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에 선언 하지 않아야 합니다