Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0e35886c authored by William Escande's avatar William Escande Committed by Automerger Merge Worker
Browse files

Merge "AutoOnFeature: Address sleep mode" into main am: 3142e0ba

parents b6aea495 3142e0ba
Loading
Loading
Loading
Loading
+22 −12
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

package com.android.server.bluetooth

import android.app.AlarmManager
import android.bluetooth.BluetoothAdapter.ACTION_AUTO_ON_STATE_CHANGED
import android.bluetooth.BluetoothAdapter.AUTO_ON_STATE_DISABLED
import android.bluetooth.BluetoothAdapter.AUTO_ON_STATE_ENABLED
@@ -157,24 +158,23 @@ private constructor(
    looper: Looper,
    private val context: Context,
    private val receiver: BroadcastReceiver,
    callback_on: () -> Unit,
    private val callback_on: () -> Unit,
    private val now: LocalDateTime,
    private val target: LocalDateTime,
    private val timeToSleep: Duration
) {
) : AlarmManager.OnAlarmListener {
    private val alarmManager: AlarmManager = context.getSystemService(AlarmManager::class.java)!!

    private val handler = Handler(looper)

    init {
        writeDateToStorage(target, context.contentResolver)
        handler.postDelayed(
            {
                Log.i(TAG, "[${this}]: Bluetooth restarting now")
                callback_on()
                cancel()
                // Set global instance to null to prevent further action. Job is done here
                timer = null
            },
            timeToSleep.inWholeMilliseconds
        alarmManager.set(
            AlarmManager.ELAPSED_REALTIME,
            timeToSleep.inWholeMilliseconds,
            "Bluetooth AutoOnFeature",
            this,
            handler
        )
        Log.i(TAG, "[${this}]: Scheduling next Bluetooth restart")

@@ -190,6 +190,13 @@ private constructor(
        )
    }

    override fun onAlarm() {
        Log.i(TAG, "[${this}]: Bluetooth restarting now")
        callback_on()
        cancel()
        timer = null
    }

    companion object {
        @VisibleForTesting internal val STORAGE_KEY = "bluetooth_internal_automatic_turn_on_timer"

@@ -236,6 +243,7 @@ private constructor(
    internal fun pause() {
        Log.i(TAG, "[${this}]: Pausing timer")
        context.unregisterReceiver(receiver)
        alarmManager.cancel(this)
        handler.removeCallbacksAndMessages(null)
    }

@@ -244,11 +252,13 @@ private constructor(
    internal fun cancel() {
        Log.i(TAG, "[${this}]: Cancelling timer")
        context.unregisterReceiver(receiver)
        alarmManager.cancel(this)
        handler.removeCallbacksAndMessages(null)
        resetStorage(context.contentResolver)
    }

    override fun toString() = "Timer scheduled ${now} for target=${target} (=${timeToSleep} delay)."
    override fun toString() =
        "Timer was scheduled at ${now} and should expire at ${target}. (sleep for ${timeToSleep})."
}

@VisibleForTesting internal val USER_SETTINGS_KEY = "bluetooth_automatic_turn_on"
+11 −2
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.server.bluetooth.test

import android.app.AlarmManager
import android.app.Application
import android.bluetooth.BluetoothAdapter
import android.content.Context
@@ -165,7 +166,11 @@ class AutoOnFeatureTest {
    fun setupTimer_whenBtOffAndUserEnabled_triggerCallback() {
        setupTimer()

        shadowOf(looper).runToEndOfTasks()
        val shadowAlarmManager = shadowOf(context.getSystemService(AlarmManager::class.java))
        shadowAlarmManager.fireAlarm(shadowAlarmManager.peekNextScheduledAlarm())

        shadowOf(looper).runOneTask()

        expect.that(callback_count).isEqualTo(1)
        expect.that(timer).isNull()
    }
@@ -176,7 +181,11 @@ class AutoOnFeatureTest {
        setupTimer()
        setupTimer()

        shadowOf(looper).runToEndOfTasks()
        val shadowAlarmManager = shadowOf(context.getSystemService(AlarmManager::class.java))
        shadowAlarmManager.fireAlarm(shadowAlarmManager.peekNextScheduledAlarm())

        shadowOf(looper).runOneTask()

        expect.that(callback_count).isEqualTo(1)
        expect.that(timer).isNull()
    }