-
Notifications
You must be signed in to change notification settings - Fork 12
[이건형_Android] 10주차 과제 제출 #76
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
Open
LEE-GEON-HYEONG
wants to merge
2
commits into
BCSDLab-Edu:main
Choose a base branch
from
LEE-GEON-HYEONG:week10
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 182 additions & 5 deletions
187
app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,191 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.enableEdgeToEdge | ||
| import android.Manifest | ||
| import android.content.* | ||
| import android.content.pm.PackageManager | ||
| import android.os.* | ||
| import android.provider.MediaStore | ||
| import android.provider.Settings | ||
| import android.view.View | ||
| import android.widget.* | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import android.net.Uri | ||
| import androidx.core.app.ActivityCompat | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var recyclerView: RecyclerView | ||
| private lateinit var permissionLayout: View | ||
| private lateinit var btnRequestPermission: Button | ||
| private lateinit var playerLayout: View | ||
| private lateinit var currentTitle: TextView | ||
| private lateinit var currentArtist: TextView | ||
| private lateinit var btnPlayPause: ImageButton | ||
| private var permissionDeniedCount = 0 | ||
|
|
||
|
|
||
| private val nowPlayingReceiver = object : BroadcastReceiver() { | ||
| override fun onReceive(context: Context?, intent: Intent?) { | ||
| val title = intent?.getStringExtra("title") ?: return | ||
| val artist = intent.getStringExtra("artist") ?: return | ||
| val isPlaying = intent.getBooleanExtra("isPlaying", false) | ||
|
|
||
| currentTitle.text = title | ||
| currentArtist.text = artist | ||
| btnPlayPause.setImageResource( | ||
| if (isPlaying) android.R.drawable.ic_media_pause | ||
| else android.R.drawable.ic_media_play | ||
| ) | ||
| playerLayout.visibility = View.VISIBLE | ||
| } | ||
| } | ||
|
|
||
| private val permissionLauncher = registerForActivityResult( | ||
| ActivityResultContracts.RequestPermission() | ||
| ) { isGranted -> | ||
| if (isGranted) { | ||
| loadMusicFiles() | ||
| } else { | ||
| permissionDeniedCount++ | ||
| if (permissionDeniedCount < 2) { | ||
| requestPermission() | ||
| } else { | ||
| showPermissionLayout() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
| ActivityCompat.requestPermissions( | ||
| this, | ||
| arrayOf(Manifest.permission.POST_NOTIFICATIONS), | ||
| 100 | ||
| ) | ||
| } | ||
|
|
||
| recyclerView = findViewById(R.id.music_recycler_view) | ||
| permissionLayout = findViewById(R.id.permission_layout) | ||
| btnRequestPermission = findViewById(R.id.btn_request_permission) | ||
| playerLayout = findViewById(R.id.player_layout) | ||
| currentTitle = findViewById(R.id.text_current_title) | ||
| currentArtist = findViewById(R.id.text_current_artist) | ||
| btnPlayPause = findViewById(R.id.button_play_pause) | ||
|
|
||
| btnRequestPermission.setOnClickListener { | ||
| if (permissionDeniedCount < 2) { | ||
| requestPermission() | ||
| } else { | ||
| val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) | ||
| intent.data = Uri.fromParts("package", packageName, null) | ||
| startActivity(intent) | ||
| } | ||
| } | ||
|
|
||
| btnPlayPause.setOnClickListener { | ||
| val intent = Intent("com.example.bcsd_android_2025_1.ACTION_TOGGLE_PLAY") | ||
| sendBroadcast(intent) | ||
| } | ||
|
|
||
| if (isPermissionGranted()) { | ||
| loadMusicFiles() | ||
| } else { | ||
| showPermissionLayout() | ||
| requestPermission() | ||
| } | ||
| } | ||
|
|
||
| override fun onResume() { | ||
| super.onResume() | ||
| registerReceiver( | ||
| nowPlayingReceiver, | ||
| IntentFilter("com.example.bcsd_android_2025_1.NOW_PLAYING"), | ||
| RECEIVER_NOT_EXPORTED | ||
| ) | ||
|
|
||
| sendBroadcast(Intent("com.example.bcsd_android_2025_1.REQUEST_NOW_PLAYING")) | ||
| } | ||
|
|
||
| override fun onPause() { | ||
| super.onPause() | ||
| unregisterReceiver(nowPlayingReceiver) | ||
| } | ||
|
|
||
| private fun isPermissionGranted(): Boolean { | ||
| val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
| Manifest.permission.READ_MEDIA_AUDIO | ||
| } else { | ||
| Manifest.permission.READ_EXTERNAL_STORAGE | ||
| } | ||
| return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED | ||
| } | ||
|
|
||
| private fun requestPermission() { | ||
| val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
| Manifest.permission.READ_MEDIA_AUDIO | ||
| } else { | ||
| Manifest.permission.READ_EXTERNAL_STORAGE | ||
| } | ||
| permissionLauncher.launch(permission) | ||
| } | ||
|
|
||
| private fun showPermissionLayout() { | ||
| recyclerView.visibility = View.GONE | ||
| permissionLayout.visibility = View.VISIBLE | ||
| } | ||
|
|
||
| private fun loadMusicFiles() { | ||
| recyclerView.visibility = View.VISIBLE | ||
| permissionLayout.visibility = View.GONE | ||
|
|
||
| val musicList = mutableListOf<Music>() | ||
| val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | ||
| val projection = arrayOf( | ||
| MediaStore.Audio.Media._ID, | ||
| MediaStore.Audio.Media.TITLE, | ||
| MediaStore.Audio.Media.ARTIST, | ||
| MediaStore.Audio.Media.DURATION | ||
| ) | ||
|
|
||
| val cursor = contentResolver.query( | ||
| uri, | ||
| projection, | ||
| MediaStore.Audio.Media.IS_MUSIC + "!= 0", | ||
| null, | ||
| null | ||
| ) | ||
|
|
||
| cursor?.use { | ||
| val idIdx = it.getColumnIndexOrThrow(MediaStore.Audio.Media._ID) | ||
| val titleIdx = it.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE) | ||
| val artistIdx = it.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST) | ||
| val durationIdx = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION) | ||
|
|
||
| while (it.moveToNext()) { | ||
| val id = it.getLong(idIdx) | ||
| val contentUri = ContentUris.withAppendedId(uri, id) | ||
| val title = it.getString(titleIdx) ?: "Unknown" | ||
| val artist = it.getString(artistIdx) ?: "Unknown" | ||
| val duration = it.getLong(durationIdx) | ||
| musicList.add(Music(title, artist, duration, contentUri)) | ||
| } | ||
| } | ||
|
|
||
| recyclerView.layoutManager = LinearLayoutManager(this) | ||
| recyclerView.adapter = MusicAdapter(musicList) { selectedMusic -> | ||
| val serviceIntent = Intent(this, MusicPlayerService::class.java).apply { | ||
| putExtra(MusicPlayerService.EXTRA_MUSIC_URI, selectedMusic.uri) | ||
| putExtra(MusicPlayerService.EXTRA_TITLE, selectedMusic.title) | ||
| putExtra(MusicPlayerService.EXTRA_ARTIST, selectedMusic.artist) | ||
| } | ||
| startForegroundService(serviceIntent) | ||
| } | ||
| } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/example/bcsd_android_2025_1/Music.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.net.Uri | ||
|
|
||
| data class Music( | ||
| val title: String, | ||
| val artist: String, | ||
| val duration: Long, | ||
| val uri: Uri | ||
| ) |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/example/bcsd_android_2025_1/MusicAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.TextView | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| class MusicAdapter( | ||
| private val musicList: List<Music>, | ||
| private val onItemClick: (Music) -> Unit | ||
| ) : RecyclerView.Adapter<MusicAdapter.MusicViewHolder>() { | ||
|
|
||
| class MusicViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||
| val title: TextView = view.findViewById(R.id.title_text) | ||
| val artist: TextView = view.findViewById(R.id.artist_text) | ||
| val duration: TextView = view.findViewById(R.id.duration_text) | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MusicViewHolder { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.item_music, parent, false) | ||
| return MusicViewHolder(view) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: MusicViewHolder, position: Int) { | ||
| val music = musicList[position] | ||
| holder.title.text = music.title | ||
| holder.artist.text = music.artist | ||
| holder.duration.text = formatDuration(music.duration) | ||
| holder.itemView.setOnClickListener { onItemClick(music) } | ||
| } | ||
|
|
||
| override fun getItemCount(): Int = musicList.size | ||
|
|
||
| private fun formatDuration(durationMs: Long): 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. Long의 확장함수로 작성할 수 있을 것 같습니다 |
||
| val seconds = durationMs / 1000 % 60 | ||
| val minutes = durationMs / (1000 * 60) % 60 | ||
| val hours = durationMs / (1000 * 60 * 60) | ||
| return if (hours > 0) "%d:%02d:%02d".format(hours, minutes, seconds) | ||
| else "%02d:%02d".format(minutes, seconds) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
애플리케이션 재실행 시
permissionDeniedCount가 초기화되기 때문에, 다이얼로그를 띄울 수 없어도permissionDeniedCount가 0이라 띄우려고 시도를 하게 됩니다.