-
Notifications
You must be signed in to change notification settings - Fork 1
[김예란_Android] 4주차 과제 제출 #4
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,81 @@ | ||
| package com.example.android_25_2 | ||
|
|
||
| import android.Manifest | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import android.net.Uri | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
| import androidx.activity.enableEdgeToEdge | ||
| import android.os.WorkDuration | ||
| import android.provider.MediaStore | ||
| import android.provider.MediaStore.Audio.Artists | ||
| import android.provider.Settings | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.constraintlayout.widget.ConstraintLayout | ||
| import androidx.core.app.ActivityCompat | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
| 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 | ||
| } | ||
| permissionCheck() | ||
|
|
||
| val musicList = mutableListOf<MusicItem>() | ||
|
|
||
| val projection = arrayOf( | ||
| MediaStore.Audio.Media.DISPLAY_NAME, | ||
| MediaStore.Audio.Media.DURATION, | ||
| MediaStore.Audio.Media.ARTIST | ||
| ) | ||
|
|
||
| applicationContext.contentResolver.query( | ||
| MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, | ||
| projection, | ||
| "", | ||
| arrayOf(), | ||
| "" | ||
| )?.use { cursor -> | ||
| val displayNameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME) | ||
| val durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION) | ||
| val artistsColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST) | ||
| while (cursor.moveToNext()){ | ||
| val displayName = cursor.getString(displayNameColumn) | ||
| val duration = cursor.getInt(durationColumn) | ||
| val artists = cursor.getString(artistsColumn) | ||
|
Collaborator
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. 얻어온 정보를 통해 list 에 넣고있지 않습니다. |
||
| } | ||
| } | ||
|
|
||
| val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) | ||
| val musicAdapter = MusicAdapter(musicList) | ||
| recyclerView.layoutManager = LinearLayoutManager(this) | ||
| recyclerView.adapter = musicAdapter | ||
| } | ||
|
|
||
| private fun permissionCheck() { | ||
| val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
| Manifest.permission.READ_MEDIA_AUDIO | ||
| } else { | ||
| Manifest.permission.READ_EXTERNAL_STORAGE | ||
| } | ||
| if (ContextCompat.checkSelfPermission(this, permission) | ||
| != PackageManager.PERMISSION_GRANTED) { | ||
| ActivityCompat.requestPermissions( | ||
| this, arrayOf(permission), 1000 | ||
| ) | ||
| if(!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { | ||
| val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:${packageName}")) | ||
| startActivity(intent) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.example.android_25_2 | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.TextView | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import androidx.recyclerview.widget.RecyclerView.ViewHolder | ||
|
|
||
| class MusicAdapter(private val musicList: List<MusicItem>): RecyclerView.Adapter<ViewHolder>(){ | ||
|
|
||
| class MusicViewHolder(view: View) : RecyclerView.ViewHolder(view){ | ||
| val displayName: TextView = itemView.findViewById(R.id.text_display_name) | ||
| val artists: TextView = itemView.findViewById(R.id.text_artist) | ||
| val duration: TextView = itemView.findViewById(R.id.text_duration) | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.item, parent, false) | ||
|
|
||
| return MusicViewHolder(view) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
| val music = musicList[position] | ||
|
Collaborator
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. 각 position 의 textView 의 text 를 바꾸고 있지 않습니다. |
||
| holder.itemView | ||
| } | ||
|
|
||
| override fun getItemCount() = musicList.size | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.example.android_25_2 | ||
|
|
||
| data class MusicItem ( | ||
| val displayName: String, | ||
| val duration: Int, | ||
| val artist: String | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?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:background="@color/gray" | ||
| android:id="@+id/root_view" | ||
| android:padding="16dp"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/text_display_name" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:textSize="20sp" | ||
| android:textColor="@color/black" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/text_artist" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:textSize="15sp" | ||
| android:textColor="@color/black" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/text_display_name" | ||
| android:layout_marginTop="7dp"/> | ||
|
|
||
| <TextView | ||
| android:id="@+id/text_duration" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:textSize="15sp" | ||
| android:textColor="@color/black" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| app:layout_constraintBottom_toBottomOf="parent" /> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,7 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <resources> | ||
| <color name="purple_200">#FFBB86FC</color> | ||
| <color name="purple_500">#FF6200EE</color> | ||
| <color name="purple_700">#FF3700B3</color> | ||
| <color name="teal_200">#FF03DAC5</color> | ||
| <color name="teal_700">#FF018786</color> | ||
| <color name="purple">#FF6200EE</color> | ||
| <color name="black">#FF000000</color> | ||
| <color name="white">#FFFFFFFF</color> | ||
| <color name="gray">#F1F3F5</color> | ||
| </resources> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| <resources> | ||
| <string name="app_name">Android_25-2</string> | ||
| <string name="dialog_title">Delet</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.
없는편이 좋습니다.