-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
227 additions
and
222 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/artemchep/pocketmode/sensors/FlowOfKeyguard.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.artemchep.pocketmode.sensors | ||
|
||
import android.content.Context | ||
import com.artemchep.pocketmode.ext.isKeyguardLocked | ||
import com.artemchep.pocketmode.models.Keyguard | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
|
||
private const val KEYGUARD_CHECK_INTERVAL = 300L | ||
|
||
/** | ||
* @author Artem Chepurnoy | ||
*/ | ||
fun flowOfKeyguard( | ||
context: Context | ||
): Flow<Keyguard> = flow<Keyguard> { | ||
while (true) { | ||
val keyguard = when (context.isKeyguardLocked()) { | ||
true -> Keyguard.Locked | ||
false -> Keyguard.Unlocked | ||
} | ||
emit(keyguard) | ||
delay(KEYGUARD_CHECK_INTERVAL) | ||
} | ||
} | ||
.flowOn(Dispatchers.Main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/artemchep/pocketmode/sensors/FlowOfProximity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.artemchep.pocketmode.sensors | ||
|
||
import android.content.Context | ||
import android.hardware.Sensor | ||
import android.hardware.SensorEvent | ||
import android.hardware.SensorEventListener | ||
import android.hardware.SensorManager | ||
import androidx.core.content.getSystemService | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.asLiveData | ||
import com.artemchep.pocketmode.util.observerFlow | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flatMap | ||
|
||
fun flowOfProximity( | ||
context: Context, | ||
): Flow<Float> = observerFlow<Float> { callback -> | ||
val sensorManager = context.getSystemService<SensorManager>() | ||
?: return@observerFlow {} | ||
|
||
val sensorProximity = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) | ||
val sensorListener = object : SensorEventListener { | ||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { | ||
// Unused. | ||
} | ||
|
||
override fun onSensorChanged(event: SensorEvent) { | ||
val distance = event.values[0] | ||
callback(distance) | ||
} | ||
} | ||
|
||
val delay = SensorManager.SENSOR_DELAY_NORMAL | ||
sensorManager.registerListener(sensorListener, sensorProximity, delay) | ||
|
||
return@observerFlow { | ||
sensorManager.unregisterListener(sensorListener) | ||
} | ||
} | ||
|
||
@Deprecated( | ||
message = "Flow analogue is 'flowOfProximity'", | ||
replaceWith = ReplaceWith("flowOfProximity(context)"), | ||
) | ||
@Suppress("FunctionName") | ||
fun ProximityLiveData( | ||
context: Context | ||
): LiveData<Float> = flowOfProximity(context).asLiveData() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/artemchep/pocketmode/sensors/FlowOfScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.artemchep.pocketmode.sensors | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import com.artemchep.pocketmode.ext.isScreenOn | ||
import com.artemchep.pocketmode.models.Screen | ||
import com.artemchep.pocketmode.util.observerFlow | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
|
||
private const val SCREEN_ON_CHECK_INTERVAL = 600L | ||
|
||
fun flowOfScreen( | ||
context: Context, | ||
): Flow<Screen> = observerFlow<Screen> { callback -> | ||
val broadcastReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent) { | ||
val screen = isScreenOn(context) | ||
callback(screen) | ||
} | ||
} | ||
val intentFilter = IntentFilter() | ||
.apply { | ||
addAction(Intent.ACTION_SCREEN_OFF) | ||
addAction(Intent.ACTION_SCREEN_ON) | ||
} | ||
context.registerReceiver(broadcastReceiver, intentFilter) | ||
|
||
val screen = isScreenOn(context) | ||
callback(screen) | ||
return@observerFlow { | ||
context.unregisterReceiver(broadcastReceiver) | ||
} | ||
} | ||
.flatMapLatest { screen -> | ||
when (screen) { | ||
is Screen.On -> flow { | ||
emit(screen) | ||
|
||
// While the screen is on, send the update every | ||
// few seconds. This is needed because of the | ||
// Always On mode which may not send the 'screen is off' | ||
// broadcast. | ||
while (true) { | ||
delay(SCREEN_ON_CHECK_INTERVAL) | ||
val newScreen = isScreenOn(context) | ||
emit(newScreen) | ||
} | ||
} | ||
else -> flowOf(screen) | ||
} | ||
} | ||
.flowOn(Dispatchers.Main) | ||
.distinctUntilChanged() | ||
|
||
/** | ||
* It does not store the screen state, it | ||
* retrieves it every time. | ||
*/ | ||
private fun isScreenOn(context: Context): Screen = | ||
when (context.isScreenOn()) { | ||
true -> Screen.On | ||
false -> Screen.Off | ||
} |
46 changes: 0 additions & 46 deletions
46
app/src/main/java/com/artemchep/pocketmode/sensors/KeyguardLiveData.kt
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
app/src/main/java/com/artemchep/pocketmode/sensors/ProximityLiveData.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.