Skip to content

Commit

Permalink
Sleep action on actionbar
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Mar 15, 2019
1 parent a9c406a commit 52a493e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
41 changes: 31 additions & 10 deletions app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.view.View
import android.widget.TextView
import android.widget.Toast
import eu.zderadicka.audioserve.fragments.*
import eu.zderadicka.audioserve.utils.ifStoppedOrDead
import kotlinx.android.synthetic.main.content_main.*
import java.io.File
import android.app.SearchManager
Expand All @@ -35,9 +34,7 @@ import eu.zderadicka.audioserve.data.*
import eu.zderadicka.audioserve.net.DOWNLOAD_ACTION
import eu.zderadicka.audioserve.net.DownloadService
import eu.zderadicka.audioserve.ui.ExpandableFrameLayout
import eu.zderadicka.audioserve.utils.SleepService
import eu.zderadicka.audioserve.utils.cancelSleepTimer
import eu.zderadicka.audioserve.utils.extendSleepTimer
import eu.zderadicka.audioserve.utils.*
import kotlinx.android.synthetic.main.sleep_overlay.*


Expand Down Expand Up @@ -87,16 +84,25 @@ class MainActivity : AppCompatActivity(),

}


private var playerControlsVisible = false
set(value) {
if (field!= value) {
field = value
invalidateOptionsMenu()
}
}
private val mCallback = object : MediaControllerCompat.Callback() {
override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
if (state == null) return
super.onPlaybackStateChanged(state)
ifStoppedOrDead(state,
{
playerControlsContainer.visibility = View.GONE
playerControlsVisible = false
},
{
playerControlsContainer.visibility = View.VISIBLE
playerControlsVisible = true
//playerControlsContainer.layoutParams.height = resources.getDimension(R.dimen.control_view_height_max).toInt();
})

Expand Down Expand Up @@ -392,8 +398,16 @@ class MainActivity : AppCompatActivity(),
mCallback.onPlaybackStateChanged(mediaController?.playbackState)
}


private var timerOn = false
set(value) {
if (field!= value) {
invalidateOptionsMenu()
field = value
}
}
private fun onTimerChange(isRunning:Boolean, remains_mins: Int) {

timerOn = isRunning
val menu = nav_view.menu
val startTimer = menu.findItem(R.id.nav_sleep)
val stopTimer = menu.findItem(R.id.nav_cancel_sleep)
Expand Down Expand Up @@ -452,7 +466,7 @@ class MainActivity : AppCompatActivity(),
super.onBackPressed()
} else {
backDoublePressed = true
Toast.makeText(this, "Press Back again to exit", Toast.LENGTH_SHORT).show()
Toast.makeText(this, getString(R.string.press_back_toast), Toast.LENGTH_SHORT).show()
Handler().postDelayed({ backDoublePressed = false }, 2000)
}

Expand All @@ -466,6 +480,8 @@ class MainActivity : AppCompatActivity(),
// Inflate the menu; this adds items to the action bar if it is present.

menuInflater.inflate(R.menu.main, menu)
val timerItem = menu.findItem(R.id.action_sleep)
timerItem.isVisible = ! timerOn && playerControlsVisible
val searchItem = menu.findItem(R.id.action_search)
searchItem.isVisible = searchPrefix != null
val infoItem = menu.findItem(R.id.action_info)
Expand All @@ -483,10 +499,10 @@ class MainActivity : AppCompatActivity(),
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
Log.d(LOG_TAG, "Clicked menu item id ${item.itemId} which is resource ${resources.getResourceName(item.itemId)}")
when (item.itemId) {
return when (item.itemId) {
R.id.action_reload -> {
folderFragment?.reload()
return true
true
}

R.id.action_info -> {
Expand All @@ -495,7 +511,12 @@ class MainActivity : AppCompatActivity(),
intent.putExtra(ARG_FOLDER_NAME, folderFragment?.folderName)
intent.putExtra(ARG_FOLDER_DETAILS, folderDetails)
startActivity(intent)
return true
true
}

R.id.action_sleep -> {
startSleepTimer(this)
true
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.widget.Switch
import eu.zderadicka.audioserve.R
import eu.zderadicka.audioserve.utils.SLEEP_START_ACTION
import eu.zderadicka.audioserve.utils.SleepService
import eu.zderadicka.audioserve.utils.startSleepTimer


// All four must be dividable by 5
Expand Down Expand Up @@ -78,11 +79,7 @@ class SleepDialogFragment : DialogFragment() {
.setIcon(R.drawable.ic_timer)
.setView(view)
.setPositiveButton("Start", { _, _ ->

val intent = Intent(context, SleepService::class.java)
intent.action = SLEEP_START_ACTION
activity?.startService(intent)

activity?.let { startSleepTimer(it) }
})
.setNegativeButton("Cancel", { _, _ ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ fun cancelSleepTimer(context: Context) {
context.startService(intent)
}

fun startSleepTimer(context: Context) {
val intent = Intent(context, SleepService::class.java)
intent.action = SLEEP_START_ACTION
context.startService(intent)
}

fun extendSleepTimer(context: Context) {
val intent = Intent(context, SleepService::class.java)
intent.action = SLEEP_EXTEND_ACTION
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
android:icon="@drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />

<item android:id="@+id/action_sleep"
android:title="@string/start_sleep_timer"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_timer_white"
/>

<item android:id="@+id/action_info"
android:orderInCategory="90"
android:title="@string/action_info"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_info"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<string name="transcoding">TRANSCODING</string>
<string name="pref_downloads_summary">When dowloading folder %1$s files will download in parallell.
Assure you have enough transcoding slots on the server</string>
<string name="action_sleep_timer">Sleep Timer</string>
<string name="action_sleep_timer">Sleep Timer Details</string>
<string name="minutes">Minutes</string>
<string name="extend_by">Extend By</string>
<string name="pause_after">Pause After</string>
Expand Down Expand Up @@ -112,4 +112,6 @@
<string name="pitch">Pitch:</string>
<string name="action_modified">Recently Modified</string>
<string name="recently_modified">Recently Modified Folders</string>
<string name="start_sleep_timer">Start Sleep Timer</string>
<string name="press_back_toast">Press Back again to exit</string>
</resources>

0 comments on commit 52a493e

Please sign in to comment.