Skip to content

Commit

Permalink
[nunu/#33] 7주차 구현
Browse files Browse the repository at this point in the history
- 앨범 fragment 바인딩
- foreground 설정
  • Loading branch information
Ssamssamukja committed Jun 1, 2024
1 parent 727ccb7 commit 65f61fb
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 12 deletions.
23 changes: 17 additions & 6 deletions UMC_6th/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.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -12,24 +17,30 @@
android:supportsRtl="true"
android:theme="@style/Theme.UMC_6th"
tools:targetApi="31">
<activity android:name=".SplashActivity"
<service
android:name=".ForegroundService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaPlayback"></service>

<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
android:exported="true"></activity>
<activity
android:name=".SongActivity"
android:exported="true"
android:hardwareAccelerated="true"
android:label="@string/title_activity_song"
android:theme="@style/Theme.UMC_6th"
android:hardwareAccelerated="true">
</activity>
android:theme="@style/Theme.UMC_6th"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class AlbumFragment: Fragment(R.layout.fragment_album) {
savedInstanceState: Bundle?
): View {
_binding = FragmentAlbumBinding.inflate(inflater, container, false)

//앨범 데이터
val albumToJson = arguments?.getString("album")
val album = gson.fromJson(albumToJson, Album::class.java)
setInit(album)
return binding.root
}

Expand Down Expand Up @@ -63,7 +68,7 @@ class AlbumFragment: Fragment(R.layout.fragment_album) {

private fun setInit(album : Album) {
Log.d("AlbumFragment", "Album parsed: ${album.title}, ${album.artist}, ${album.coverImg}")
// binding.imgAlbumAlbumCov.setImageResource(album.coverImage!!)
binding.imgAlbumAlbumCov.setImageResource(album.coverImg!!)
binding.txAlbumAlbumTitle.text = album.title
binding.txAlbumAlbumArtist.text = album.artist
}
Expand Down
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Constant.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.umc_6th

object Constant {
const val CHANNEL_ID = "ch123"
const val MUSIC_NOTIFICATION_ID = 123
}
62 changes: 62 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/ForegroundService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.umc_6th

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi

class ForegroundService : Service() {

override fun onBind(intent: Intent): IBinder? {
return null
}

override fun onCreate() {
super.onCreate()
createNotificationChannel()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 버전 확인
showNotification()
}

return START_STICKY
}
@RequiresApi(Build.VERSION_CODES.O)
private fun showNotification() {
val notificationIntent = Intent(this, SongActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE
)
val notification = Notification
.Builder(this, Constant.CHANNEL_ID)
.setContentText("현재 음악이 재생 중입니다.")
.setSmallIcon(R.drawable.ic_flo_logo)
.setContentIntent(pendingIntent)
.build()

startForeground(Constant.MUSIC_NOTIFICATION_ID, notification)
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
Constant.CHANNEL_ID, "Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)

val manager = getSystemService(
NotificationManager::class.java
)

manager.createNotificationChannel(serviceChannel)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HomeFragment : Fragment() {
add(Album(4, "Drama", "에스파 (aespa)", R.drawable.img_album_drama))
add(Album(5, "Weekend", "태연 (Tae Yeon)", R.drawable.img_album_exp6))
}

val albumRecyclerAdapter = AlbumRecyclerAdapter(albumDatas)
binding.homeTodayMusicAlbum.adapter = albumRecyclerAdapter
binding.homeTodayMusicAlbum.layoutManager = LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)
Expand All @@ -58,9 +59,6 @@ class HomeFragment : Fragment() {
albumRecyclerAdapter.setItemClickListener(object : AlbumRecyclerAdapter.OnItemClickListener {
override fun onItemClick(album : Album) {
changeToAlbumFragment(album)
(context as MainActivity).supportFragmentManager.beginTransaction()
.replace(R.id.main_container, AlbumFragment())
.commitAllowingStateLoss()
}
})
return binding.root
Expand Down
42 changes: 42 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.umc_6th.adapter.LockerPagerAdapter
import com.example.umc_6th.databinding.FragmentHomeBinding
import com.example.umc_6th.databinding.FragmentLockerBinding
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.tabs.TabLayoutMediator

class LockerFragment : Fragment() {
private var _binding: FragmentLockerBinding? = null
private val binding get() = _binding!!
private val information = arrayListOf("저장한곡", "음악파일")

private lateinit var bottomSheetDialog: BottomSheetDialog
lateinit var songDB: SongDatabase

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -32,8 +40,42 @@ class LockerFragment : Fragment() {
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.txLockerSelectAll.setOnClickListener{
showBottomSheetDialog()
}
}

private fun showBottomSheetDialog() {
bottomSheetDialog = BottomSheetDialog(requireContext())
bottomSheetDialog.setContentView(R.layout.bottom_sheet_dialog)

val btnDislike = bottomSheetDialog.findViewById<ImageView>(R.id.imgDialogDelete)
btnDislike?.setOnClickListener {
bottomSheetDialog.dismiss() // Dialog 숨기기
}

bottomSheetDialog.show()
}


override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

class SharedViewModel : ViewModel() {
private val _dislikeAllEvent = MutableLiveData<Boolean>()
val dislikeAllEvent: LiveData<Boolean> = _dislikeAllEvent

fun triggerDislikeAll() {
_dislikeAllEvent.value = true
}

fun eventHandled() {
_dislikeAllEvent.value = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import com.example.umc_6th.adapter.LockerAlbumRecyclerAdapter
import com.example.umc_6th.databinding.FragmentLockerSavedSongBinding
import com.google.gson.Gson

class LockerSavedSongFragment : Fragment() {
class LockerSavedSongFragment : Fragment(){
private var songDatas = ArrayList<Song>()
lateinit var binding : FragmentLockerSavedSongBinding
lateinit var songDB: SongDatabase
val lockerAlbumRecyclerAdapter = LockerAlbumRecyclerAdapter(songDatas)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -39,7 +41,6 @@ class LockerSavedSongFragment : Fragment() {
if (songDB != null) {
songDatas = ArrayList(songDB.songDao().getLikedSongs(true))
}
val lockerAlbumRecyclerAdapter = LockerAlbumRecyclerAdapter(songDatas)
binding.rvLockerSavedSong.adapter = lockerAlbumRecyclerAdapter
binding.rvLockerSavedSong.layoutManager = LinearLayoutManager(requireActivity())

Expand Down Expand Up @@ -73,4 +74,6 @@ class LockerSavedSongFragment : Fragment() {
.commitAllowingStateLoss()
}



}
28 changes: 28 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.umc_6th

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.graphics.PorterDuff
Expand Down Expand Up @@ -103,12 +104,14 @@ class SongActivity : AppCompatActivity() {
binding.imgSongPlayPauseBtn.visibility = View.VISIBLE
binding.imgSongPlayBtn.visibility = View.GONE
startOrResumeTimer()
startStopService()
}

binding.imgSongPlayPauseBtn.setOnClickListener {
isTimerRunning = false
binding.imgSongPlayBtn.visibility = View.VISIBLE
binding.imgSongPlayPauseBtn.visibility = View.GONE
startStopService()
}

binding.imgSongRepeat.setOnClickListener {
Expand Down Expand Up @@ -293,4 +296,29 @@ class SongActivity : AppCompatActivity() {
apply()
}
}

private fun startStopService() {
if (isServiceRunning(ForegroundService::class.java)) {
Toast.makeText(this, "Foreground Service Stopped", Toast.LENGTH_SHORT).show()
stopService(Intent(this, ForegroundService::class.java))
}
else {
Toast.makeText(this, "Foreground Service Started", Toast.LENGTH_SHORT).show()
startService(Intent(this, ForegroundService::class.java))
}
}

private fun isServiceRunning(inputClass : Class<ForegroundService>) : Boolean {
val manager : ActivityManager = getSystemService(
Context.ACTIVITY_SERVICE
) as ActivityManager

for (service : ActivityManager.RunningServiceInfo in manager.getRunningServices(Integer.MAX_VALUE)) {
if (inputClass.name.equals(service.service.className)) {
return true
}

}
return false
}
}
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/res/layout/fragment_album.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
android:id="@+id/tlAlbum"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@color/white"
app:tabTextColor="@color/grey"
app:tabSelectedTextColor="@color/flo"
app:tabIndicatorColor="@color/flo"
app:tabIndicatorFullWidth="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgAlbumAlbumCov">
Expand Down

0 comments on commit 65f61fb

Please sign in to comment.