Skip to content
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

Refactor to jetpack compose #43

Open
wants to merge 22 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0d2aa23
added baseViewmodel and refactored it
gouri-panda May 24, 2021
6109d6e
added ImageFragmentState and refactored it
gouri-panda May 27, 2021
985e441
added compose dependencies and runs!!
gouri-panda May 27, 2021
7985e38
It works!!!
gouri-panda Oct 9, 2021
ecd92bb
beginning of jetpack compose
gouri-panda Apr 10, 2022
e1443a1
Merge branch 'dev' into Refactor_to_Jetpack_Compose
gouri-panda Apr 10, 2022
8707e48
dependency updated and removed view binding code from project
gouri-panda Apr 10, 2022
a64a860
removed view binding from image fragement
gouri-panda Apr 10, 2022
037e84d
added MediaList and MediaItem composables
gouri-panda Apr 11, 2022
1e33f4b
small improvement
gouri-panda Apr 11, 2022
6c25771
Merge remote-tracking branch 'origin/Refactor_to_Jetpack_Compose' int…
gouri-panda Apr 11, 2022
a8352d5
added onclick funcanality in both media item and menu media item
gouri-panda Apr 11, 2022
caa0333
added folder list
gouri-panda Apr 11, 2022
6776464
some how it works now
gouri-panda Apr 21, 2022
15c8d76
bug fix,use hilt module to dependecy injection, code clean up, some u…
gouri-panda Apr 22, 2022
33817d6
refactor Event Listener to CustomEventListener for clean up code and …
gouri-panda Apr 22, 2022
d8510c0
code format
gouri-panda Apr 22, 2022
ed68c9f
FoldersList icon and it's size updated
gouri-panda Apr 22, 2022
2c63193
added some test
gouri-panda Apr 22, 2022
a1fa976
added more time test
gouri-panda Apr 22, 2022
c197386
added input validation check in path title finding
gouri-panda Apr 22, 2022
5bd7a01
updated dependencies, gradle version, supported linear progress bar i…
gouri-panda Apr 22, 2022
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
Prev Previous commit
Next Next commit
bug fix,use hilt module to dependecy injection, code clean up, some u…
…tilty function for string created
  • Loading branch information
gouri-panda committed Apr 22, 2022
commit 15c8d760de9135b0e597e7cb09926ad4d61e1240
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 27 additions & 46 deletions app/src/main/java/com/one4ll/xplayer/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -27,9 +27,9 @@ import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import com.one4ll.xplayer.databinding.ActivityMainBinding
import com.one4ll.xplayer.helpers.*
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

private const val TAG = "MainActivity"

@@ -44,15 +44,17 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
private lateinit var videoTitle: TextView
private var screenWidth: Int? = null
private var screenHeight: Int? = null
private val audioManager by lazy { getSystemService(Context.AUDIO_SERVICE) as AudioManager }
private val brightness by lazy {
Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_BRIGHTNESS
)
}

private var simpleExoPlayer: SimpleExoPlayer? = null
@Inject
lateinit var audioManager: AudioManager
@Inject
lateinit var simpleExoPlayer: SimpleExoPlayer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@@ -66,7 +68,6 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
screenWidth = displayMetrics.widthPixels
screenHeight = displayMetrics.heightPixels
Log.d(TAG, "onCreate: screen width $screenWidth ,screen height $screenHeight")
simpleExoPlayer = SimpleExoPlayer.Builder(this).build()
gestureDetector = GestureDetector(this, this)
playerView.player = simpleExoPlayer
if (intent.action != null && intent.action == Intent.ACTION_VIEW) {
@@ -84,25 +85,15 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
this,
Util.getUserAgent(this@MainActivity, getString(R.string.app_name))
)
Log.d(TAG, "onCreate: video path $videoUriPath")
Log.d(TAG, "onCreate: Video Path $videoUriPath")
lateinit var mediaSource: MediaSource
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
val decode = BitmapFactory.decodeFile(videoUriPath, options)
val width = decode?.width
val height = decode?.height
options.inJustDecodeBounds = false
Log.d(
TAG,
"onCreate: video uri decode file width $width video uri decode file height $height"
)
mediaSource =
ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(videoUriPath))
simpleExoPlayer!!.prepare(mediaSource)
simpleExoPlayer.prepare(mediaSource)

simpleExoPlayer!!.playWhenReady = true
simpleExoPlayer!!.addListener(eventListener)
simpleExoPlayer.playWhenReady = true
simpleExoPlayer.addListener(eventListener)

}

@@ -112,8 +103,7 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
}

private fun setVideoTitle() {
val title = videoUriPath.substringAfterLast("/").substringBefore(".", "")
videoTitle.text = title
videoTitle.text = videoUriPath.getTitleFromVideoPath()
}

private val eventListener: Player.EventListener = object : Player.EventListener {
@@ -140,19 +130,20 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
when (playbackState) {
Player.STATE_READY -> {
goToTextView.visibility = View.INVISIBLE
audioManager.requestAudioFocus(
this@MainActivity,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
// TODO: is the requestAudioFocus necessary here??
// audioManager.requestAudioFocus(
// this@MainActivity,
// AudioManager.STREAM_MUSIC,
// AudioManager.AUDIOFOCUS_GAIN
// )
Log.d(TAG, "onPlayerStateChanged: state ready")
}
Player.STATE_BUFFERING -> {
Log.d(TAG, "onPlayerStateChanged: buffering")
}
Player.STATE_ENDED -> {
playerView.keepScreenOn = false
simpleExoPlayer?.playWhenReady = false
simpleExoPlayer.playWhenReady = false
Log.d(TAG, "onPlayerStateChanged: state ended")
}
Player.STATE_IDLE -> {
@@ -182,24 +173,18 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
override fun onAudioFocusChange(focusChange: Int) {
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK -> {
simpleExoPlayer?.playWhenReady = true
simpleExoPlayer.playWhenReady = true
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
simpleExoPlayer?.playWhenReady = false
simpleExoPlayer.playWhenReady = false
}
AudioManager.AUDIOFOCUS_LOSS -> {
simpleExoPlayer?.playWhenReady = false
simpleExoPlayer.playWhenReady = false
}
}
}


override fun onDestroy() {
simpleExoPlayer!!.release()
super.onDestroy()
}


override fun onTouch(view: View?, event: MotionEvent?): Boolean {
view?.performClick()
gestureDetector.onTouchEvent(event)
@@ -220,16 +205,12 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
val middle = screenWidth!! / 2
if (rawX < middle) {
Log.d(TAG, "forwardAndBackWardVideoOnDoubleClick: left side")
val currentPosition = simpleExoPlayer?.currentPosition
if (currentPosition != null) {
simpleExoPlayer?.seekTo(currentPosition - SKIP_DURATION)
}
val currentPosition = simpleExoPlayer.currentPosition
simpleExoPlayer.seekTo(currentPosition - SKIP_DURATION)
} else {
Log.d(TAG, "forwardAndBackWardVideoOnDoubleClick: right side")
val currentPosition = simpleExoPlayer?.currentPosition
if (currentPosition != null) {
simpleExoPlayer?.seekTo(currentPosition + SKIP_DURATION)
}
val currentPosition = simpleExoPlayer.currentPosition
simpleExoPlayer.seekTo(currentPosition + SKIP_DURATION)

}
}
@@ -283,13 +264,13 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.
//forward
goToTextView.text =
"Forward +20.00s\n\t\t ${convertDuration(currentPosition)}s"
simpleExoPlayer?.seekTo(currentPosition + 20000)
simpleExoPlayer.seekTo(currentPosition + 20000)

} else if (motionEvent1.x - motionEvent2.x >= 100) {
//backward
goToTextView.text =
"BackWard -20.00s\n\t\t ${convertDuration(currentPosition)}"
simpleExoPlayer?.seekTo(currentPosition - 20000)
simpleExoPlayer.seekTo(currentPosition - 20000)
} else {
val rawX = motionEvent1.rawX
val middle = screenWidth!! / 2
@@ -350,6 +331,6 @@ class MainActivity : AppCompatActivity(), View.OnTouchListener, GestureDetector.

override fun onPause() {
super.onPause()
simpleExoPlayer?.playWhenReady = false
simpleExoPlayer.playWhenReady = false
}
}
9 changes: 5 additions & 4 deletions app/src/main/java/com/one4ll/xplayer/compose/MediaList.kt
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ fun MediaItem(
) {

val localContext = LocalContext.current
val coroutineScope = rememberCoroutineScope()
// val coroutineScope = rememberCoroutineScope()
val thumbnail: MutableState<Bitmap?> = remember {
mutableStateOf(null)
}
@@ -88,9 +88,10 @@ fun MediaItem(
.error(R.drawable.exo_controls_pause)
.build()
)
coroutineScope.launch {
thumbnail.value = localContext.getBitmapFromVideoFile(mediaItem.path)
}
// TODO: Create thumbnail for each files
// coroutineScope.launch {
// thumbnail.value = localContext.getBitmapFromVideoFile(mediaItem.path)
// }

if (painter.state is AsyncImagePainter.State.Loading) {
Image(
13 changes: 13 additions & 0 deletions app/src/main/java/com/one4ll/xplayer/di/Module.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.one4ll.xplayer.di

import android.app.Application
import android.content.Context
import android.media.AudioManager
import androidx.room.Room
import com.google.android.exoplayer2.SimpleExoPlayer
import com.one4ll.xplayer.database.MediaDatabase
import dagger.Module
import dagger.Provides
@@ -22,4 +25,14 @@ class Module {
MediaDatabase.DATABASE_NAME
).build()
}

@Provides
@Singleton
fun provideSimpleExoPlayer(app: Application): SimpleExoPlayer =
SimpleExoPlayer.Builder(app).build()

@Provides
@Singleton
fun provideAudioManager(app: Application) =
app.getSystemService(Context.AUDIO_SERVICE) as AudioManager
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/one4ll/xplayer/helpers/utils.kt
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.media.ThumbnailUtils
import android.net.Uri
import android.os.Build
import android.os.CancellationSignal
import android.provider.MediaStore
@@ -583,3 +584,12 @@ fun AppCompatActivity.showSystemUi(toggleActionVisibility: Boolean) {
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}

/**
* Returns the title of file name from string path
*/
fun String.getTitleFromVideoPath() = this.substringAfterLast("/").substringBefore(".", "")

/**
* Returns the title of file name from string Uri
*/
fun Uri.getTitleFromURI() = this.toString().getTitleFromVideoPath()
30 changes: 1 addition & 29 deletions app/src/main/java/com/one4ll/xplayer/ui/video/VideoFragment.kt
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.rememberNavController
import com.one4ll.xplayer.MainActivity
import com.one4ll.xplayer.Media
import com.one4ll.xplayer.compose.MediaList
@@ -49,7 +48,7 @@ class VideoFragment : Fragment() {
askPermissionForVideoList()
}

//we will ask once for videos ,images and audios
// We will ask once for videos ,images and audios
/**
* Asks permission about read and write if The device is below marshmallow then no need to ask
* we already have permission
@@ -103,31 +102,4 @@ class VideoFragment : Fragment() {
}
}

/**
* Sets the adapter
*/
//todo add grid layout or linear layout in compose
// private suspend fun setAdapter(videoList: List<Media>) {
// withContext(Main) {
// d(TAG, "setAdapter: thread ${Thread.currentThread().name}")
// val sharedPreferences = binding.root.context.getSharedPreferences(
// SHARED_PREF_SETTINGS,
// Context.MODE_PRIVATE
// )
// if (sharedPreferences.getBoolean(IS_GRID_LAYOUT, false)) {
// val adapter =
// activity?.let { VideoRecyclerViewAdapter(it, videoList, lifecycleScope) }
// binding.root.video_list_recycler_view.adapter = adapter
// val gridLayoutManager = GridLayoutManager(binding.root.context, 2)
// binding.root.video_list_recycler_view.layoutManager = gridLayoutManager
// } else {
// setc
// LazyColumn {
//
// }
// MediaCard(media = videoList)
// }
// }
// }

}