feat: Smartspace Torch Provider (#6742)
* feat: Smartspace Torch Signed-off-by: Pun Butrach <pun.butrach@gmail.com> * chore: Add flashlight permission Signed-off-by: Pun Butrach <pun.butrach@gmail.com> * feat: Turn off *all* camera torches Signed-off-by: Pun Butrach <pun.butrach@gmail.com> * chore: Logs Signed-off-by: Pun Butrach <pun.butrach@gmail.com> * chore: Spotlessified Signed-off-by: Pun Butrach <pun.butrach@gmail.com> * refactor: Use SmartspaceAction.onClick instead of receiver Signed-off-by: Pun Butrach <pun.butrach@gmail.com> * refactor: Apply suggestions from reviewer Signed-off-by: Pun Butrach <pun.butrach@gmail.com> --------- Signed-off-by: Pun Butrach <pun.butrach@gmail.com>
This commit is contained in:
1 parent
e917f9c89b
commit
51e56f235d
7 files changed
+152
No files matched your search
@@ -38,6 +38,7 @@
|
||||
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" tools:ignore="ProtectedPermissions"/>
|
||||
<uses-permission android:name="android.permission.STATUS_BAR_SERVICE" tools:ignore="ProtectedPermissions"/>
|
||||
<uses-permission android:name="android.permission.SUSPEND_APPS" tools:ignore="ProtectedPermissions" />
|
||||
<uses-permission android:name="android.permission.FLASHLIGHT" />
|
||||
|
||||
<!--override minSdk declared in it-->
|
||||
<uses-sdk tools:overrideLibrary="com.kieronquinn.app.smartspacer.sdk" />
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<!--
|
||||
~ Copyright (C) 2026 The Android Open Source Project
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">
|
||||
|
||||
<path android:fillColor="@android:color/white" android:pathData="M764,876L640,752L640,800Q640,833 616.5,856.5Q593,880 560,880L400,880Q367,880 343.5,856.5Q320,833 320,800L320,432L84,196Q73,185 73,168Q73,151 84,140Q95,129 112,129Q129,129 140,140L820,820Q831,831 831,848Q831,865 820,876Q809,887 792,887Q775,887 764,876ZM400,800L560,800Q560,800 560,800Q560,800 560,800L560,672L400,512L400,800Q400,800 400,800Q400,800 400,800ZM720,160L720,296Q720,308 717,319Q714,330 707,340L633,453Q622,470 606.5,471Q591,472 578,464Q572,460 567.5,454Q563,448 561,440.5Q559,433 560.5,425Q562,417 567,409L640,296L640,280L410,280Q393,280 382,268.5Q371,257 371,240Q371,223 382.5,211.5Q394,200 411,200L640,200L640,160Q640,160 640,160Q640,160 640,160L289,160Q272,160 261,148.5Q250,137 250,120Q250,103 261.5,91.5Q273,80 290,80L640,80Q673,80 696.5,103.5Q720,127 720,160ZM480,592L480,592Q480,592 480,592Q480,592 480,592L480,592L480,592L480,592Q480,592 480,592Q480,592 480,592ZM410,280L410,280L410,280L410,280L410,280L410,280L410,280L410,280Q410,280 410,280Q410,280 410,280Z"/>
|
||||
|
||||
</vector>
|
||||
@@ -479,9 +479,14 @@
|
||||
<!-- Available targets -->
|
||||
<string name="smartspace_weather">Weather</string>
|
||||
<string name="smartspace_battery_status">Battery status</string>
|
||||
<string name="smartspace_torch">Flashlight status</string>
|
||||
<string name="smartspace_now_playing">Now Playing</string>
|
||||
<string name="smartspace_onboarding">Onboarding</string>
|
||||
|
||||
<!-- Torch strings -->
|
||||
<string name="torch_status_on">Flashlight on</string>
|
||||
<string name="torch_action_off">Tap to turn off</string>
|
||||
|
||||
<!-- Onboarding strings -->
|
||||
<string name="onboarding_open_settings_title">Configure your Lawnchair</string>
|
||||
<string name="onboarding_open_settings_subtitle">Long-press on the Home Screen to open Settings</string>
|
||||
|
||||
@@ -682,6 +682,11 @@ class PreferenceManager2 @Inject constructor(
|
||||
defaultValue = true,
|
||||
)
|
||||
|
||||
val smartspaceTorch = preference(
|
||||
key = booleanPreferencesKey("enable_smartspace_torch"),
|
||||
defaultValue = true,
|
||||
)
|
||||
|
||||
val smartspaceNowPlaying = preference(
|
||||
key = booleanPreferencesKey("enable_smartspace_now_playing"),
|
||||
defaultValue = true,
|
||||
|
||||
@@ -6,5 +6,6 @@ object SmartspaceScores {
|
||||
const val SCORE_MEDIA = 2f
|
||||
const val SCORE_CALENDAR = 3f
|
||||
const val SCORE_LOW_BATTERY = 10f
|
||||
const val SCORE_FLASHLIGHT = 11f
|
||||
const val SCORE_ONBOARDING = 1000f
|
||||
}
|
||||
@@ -28,6 +28,7 @@ class SmartspaceProvider @Inject constructor(
|
||||
val dataSources = listOf(
|
||||
SmartspaceWidgetReader(context),
|
||||
BatteryStatusProvider(context),
|
||||
TorchProvider(context),
|
||||
NowPlayingProvider(context),
|
||||
OnboardingProvider(context),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package app.lawnchair.smartspace.provider
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Icon
|
||||
import android.hardware.camera2.CameraAccessException
|
||||
import android.hardware.camera2.CameraCharacteristics
|
||||
import android.hardware.camera2.CameraManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import androidx.core.content.getSystemService
|
||||
import app.lawnchair.smartspace.model.SmartspaceAction
|
||||
import app.lawnchair.smartspace.model.SmartspaceScores
|
||||
import app.lawnchair.smartspace.model.SmartspaceTarget
|
||||
import com.android.launcher3.R
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class TorchProvider(context: Context) :
|
||||
SmartspaceDataSource(
|
||||
context,
|
||||
R.string.smartspace_torch,
|
||||
{ smartspaceTorch },
|
||||
) {
|
||||
private val cameraManager = context.getSystemService<CameraManager>()
|
||||
|
||||
override val internalTargets = torchFlow()
|
||||
.map { enabled -> listOfNotNull(if (enabled) getSmartspaceTarget() else null) }
|
||||
|
||||
private fun torchFlow(): Flow<Boolean> = callbackFlow {
|
||||
if (cameraManager == null) {
|
||||
trySend(false)
|
||||
close()
|
||||
return@callbackFlow
|
||||
}
|
||||
|
||||
// Collect all flash-capable camera IDs
|
||||
val flashCameraIds = cameraManager.cameraIdList
|
||||
.filter { id ->
|
||||
cameraManager.getCameraCharacteristics(id)
|
||||
.get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true
|
||||
}
|
||||
.toSet()
|
||||
|
||||
if (flashCameraIds.isEmpty()) {
|
||||
trySend(false)
|
||||
close()
|
||||
return@callbackFlow
|
||||
}
|
||||
|
||||
val enabledCameraIds = mutableSetOf<String>()
|
||||
|
||||
val callback = object : CameraManager.TorchCallback() {
|
||||
override fun onTorchModeChanged(id: String, enabled: Boolean) {
|
||||
if (id in flashCameraIds) {
|
||||
if (enabled) {
|
||||
enabledCameraIds.add(id)
|
||||
} else {
|
||||
enabledCameraIds.remove(id)
|
||||
}
|
||||
trySend(enabledCameraIds.intersect(flashCameraIds).isNotEmpty())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onTorchModeUnavailable(id: String) {
|
||||
if (id in flashCameraIds) {
|
||||
enabledCameraIds.remove(id)
|
||||
trySend(enabledCameraIds.intersect(flashCameraIds).isNotEmpty())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cameraManager.registerTorchCallback(callback, Handler(Looper.getMainLooper()))
|
||||
awaitClose { cameraManager.unregisterTorchCallback(callback) }
|
||||
}
|
||||
|
||||
private fun getSmartspaceTarget(): SmartspaceTarget {
|
||||
return SmartspaceTarget(
|
||||
id = "torchStatus",
|
||||
headerAction = SmartspaceAction(
|
||||
id = "torchStatusAction",
|
||||
icon = Icon.createWithResource(context, R.drawable.ic_flashlight_off),
|
||||
title = context.getString(R.string.torch_status_on),
|
||||
subtitle = context.getString(R.string.torch_action_off),
|
||||
onClick = Runnable {
|
||||
val cameraManager = cameraManager ?: return@Runnable
|
||||
try {
|
||||
cameraManager.cameraIdList.forEach { cameraId ->
|
||||
try {
|
||||
val hasFlash = cameraManager
|
||||
.getCameraCharacteristics(cameraId)
|
||||
.get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true
|
||||
if (hasFlash) {
|
||||
cameraManager.setTorchMode(cameraId, false)
|
||||
}
|
||||
} catch (e: CameraAccessException) {
|
||||
Log.e(
|
||||
TAG,
|
||||
"Failed to turn off torch for camera $cameraId: ${e.message}",
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: CameraAccessException) {
|
||||
Log.e(TAG, "Failed to access camera list", e)
|
||||
}
|
||||
},
|
||||
),
|
||||
score = SmartspaceScores.SCORE_FLASHLIGHT,
|
||||
featureType = SmartspaceTarget.FeatureType.FEATURE_FLASHLIGHT,
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "TorchProvider"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user