Skip to content

Commit

Permalink
Sleep overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Mar 14, 2019
1 parent 72cdc2f commit 13bb86b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
26 changes: 23 additions & 3 deletions app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import android.os.IBinder
import android.preference.PreferenceManager
import android.support.v7.widget.SearchView
import android.support.v7.widget.SwitchCompat
import android.view.MotionEvent
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 kotlinx.android.synthetic.main.fragment_controller.*
import eu.zderadicka.audioserve.utils.extendSleepTimer
import kotlinx.android.synthetic.main.sleep_overlay.*


private const val LOG_TAG = "Main"
Expand Down Expand Up @@ -279,6 +279,20 @@ class MainActivity : AppCompatActivity(),
val haveServer = !prefs.getString("pref_server_url", null).isNullOrBlank()
val haveSecret = !prefs.getString(("pref_shared_secret"), null).isNullOrBlank()

//mask clicks on the sleep overlay
sleepOverlayView.setOnClickListener {
}

cancelSleepBtn.setOnClickListener {
cancelSleepTimer(this)
}

extendSleepBtn.setOnClickListener{
extendSleepTimer(this)
}



if (!haveSecret || !haveServer) startActivity(Intent(this, SettingsActivity::class.java))

}
Expand Down Expand Up @@ -379,12 +393,18 @@ class MainActivity : AppCompatActivity(),
}


private fun onTimerChange(isRunning:Boolean) {
private fun onTimerChange(isRunning:Boolean, remains_mins: Int) {
val menu = nav_view.menu
val startTimer = menu.findItem(R.id.nav_sleep)
val stopTimer = menu.findItem(R.id.nav_cancel_sleep)
startTimer.isVisible = !isRunning
stopTimer.isVisible = isRunning
sleepOverlayView.visibility = if (isRunning) View.VISIBLE else View.GONE
val hours = (remains_mins / 60).toString().padStart(2,'0')
val mins = (remains_mins % 60).toString().padStart(2,'0')
sleepCounterView.text = "${hours}:${mins}"


}

private val timerServiceConnection = object: ServiceConnection {
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/eu/zderadicka/audioserve/utils/SleepService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@ fun cancelSleepTimer(context: Context) {
context.startService(intent)
}

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

class SleepService() : Service() {

private val remains = -1
var statusListener: ((Boolean) -> Unit)? = null
var statusListener: ((Boolean, Int) -> Unit)? = null
set(v) {
field = v
v?.invoke(isRunning)
v?.invoke(isRunning, 0)
}

private var timer: MyTimer? by Delegates.observable<MyTimer?>(null) {
_, _, newValue ->
statusListener?.invoke(newValue!= null)
statusListener?.invoke(newValue!= null, 0)
}
private lateinit var prefs: SharedPreferences

Expand Down Expand Up @@ -86,6 +92,7 @@ class SleepService() : Service() {
if (time > 0) {

timer = MyTimer(time )
statusListener?.invoke(isRunning, time)
timer?.startWithNotification()


Expand Down Expand Up @@ -304,6 +311,7 @@ class SleepService() : Service() {

override fun onTick(millisUntilFinished: Long) {
remains = (millisUntilFinished.toDouble() / minuteMillis).roundToInt()
statusListener?.invoke(isRunning, remains)
if (remains == 1) registerWithSensor()
Log.d(LOG_TAG, "Timer tick $millisUntilFinished $remains")
notificationManager.notify(NOTIFICATION_ID, createNotification(remains, startedExtended))
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
android:id="@+id/mainContentContainer">

Expand Down Expand Up @@ -36,4 +42,8 @@



</LinearLayout>
</LinearLayout>

<include layout="@layout/sleep_overlay" />

</FrameLayout>
56 changes: 56 additions & 0 deletions app/src/main/res/layout/sleep_overlay.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sleepOverlayView"
android:layout_width="64dp"
android:layout_height="172dp"
android:layout_gravity="end"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:background="@drawable/sleep_overlay_background"
android:visibility="gone">


<ImageView
android:id="@+id/cancelSleepBtn"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:tint="@color/colorAccent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_timer_cancel" />

<ImageView
android:id="@+id/extendSleepBtn"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"

android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:tint="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_timer" />


<TextView
android:id="@+id/sleepCounterView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/extendSleepBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cancelSleepBtn"
tools:text="00:00" />
</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<color name="colorAccent">#ff4081</color>
<color name="colorAccentLight">#f8bbd0</color>
<color name="colorListBackground">#fafafa</color>
<color name="colorBackgroundOverlay">#80000000</color>

<color name="list_separator">#bcaaa4</color>

Expand Down

0 comments on commit 13bb86b

Please sign in to comment.