Skip to content

Commit

Permalink
Export control builders
Browse files Browse the repository at this point in the history
  • Loading branch information
Domi04151309 committed Jan 22, 2024
1 parent a51edbb commit 5d97ebc
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.github.domi04151309.home.services

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.service.controls.Control
import android.service.controls.templates.ControlButton
import android.service.controls.templates.StatelessTemplate
import android.service.controls.templates.ToggleTemplate
import androidx.annotation.RequiresApi
import androidx.preference.PreferenceManager
import io.github.domi04151309.home.R
import io.github.domi04151309.home.activities.MainActivity
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.P

@RequiresApi(Build.VERSION_CODES.R)
object ControlBuilders {
private fun getPendingIntent(context: Context): PendingIntent =
PendingIntent.getActivity(
context,
0,
Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
},
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)

fun buildUnreachableControl(
context: Context,
id: String,
device: DeviceItem,
): Control =
Control.StatefulBuilder(id, getPendingIntent(context))
.setTitle(device.name)
.setZone(device.name)
.setStructure(context.resources.getString(R.string.app_name))
.setDeviceType(Global.getDeviceType(device.iconName))
.setStatus(Control.STATUS_DISABLED)
.setStatusText(context.resources.getString(R.string.str_unreachable))
.build()

fun buildGenericControl(
context: Context,
listItem: ListViewItem,
device: DeviceItem,
): Control =
Control.StatelessBuilder(
device.id + '@' + listItem.hidden,
getPendingIntent(context),
)
.setTitle(listItem.title)
.setSubtitle(device.name)
.setZone(device.name)
.setStructure(context.resources.getString(R.string.app_name))
.setDeviceType(Global.getDeviceType(device.iconName))
.build()

fun buildStatefulControl(
context: Context,
id: String,
listItem: ListViewItem,
device: DeviceItem,
): Control {
val controlBuilder =
Control.StatefulBuilder(id, getPendingIntent(context))
.setTitle(listItem.title)
.setSubtitle(device.name)
.setZone(device.name)
.setStructure(context.resources.getString(R.string.app_name))
.setDeviceType(Global.getDeviceType(device.iconName))
.setStatus(Control.STATUS_OK)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
controlBuilder.setAuthRequired(
PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(
P.PREF_CONTROLS_AUTH,
P.PREF_CONTROLS_AUTH_DEFAULT,
),
)
}
if (listItem.state != null) {
controlBuilder.setControlTemplate(
ToggleTemplate(
id,
ControlButton(
listItem.state ?: false,
listItem.state.toString(),
),
),
)
controlBuilder.setStatusText(
context.resources.getString(
if (listItem.state == true) {
R.string.str_on
} else {
R.string.str_off
},
),
)
}
if (device.mode == Global.TASMOTA) {
controlBuilder.setControlTemplate(
StatelessTemplate(id),
)
}
return controlBuilder.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.domi04151309.home.services

import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.os.Looper
Expand All @@ -10,20 +8,13 @@ import android.service.controls.ControlsProviderService
import android.service.controls.actions.BooleanAction
import android.service.controls.actions.CommandAction
import android.service.controls.actions.ControlAction
import android.service.controls.templates.ControlButton
import android.service.controls.templates.StatelessTemplate
import android.service.controls.templates.ToggleTemplate
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.preference.PreferenceManager
import io.github.domi04151309.home.R
import io.github.domi04151309.home.activities.MainActivity
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.P
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import java.util.concurrent.Flow
import java.util.function.Consumer
Expand All @@ -33,16 +24,6 @@ class ControlService : ControlsProviderService() {
private var updateSubscriber: Flow.Subscriber<in Control>? = null
private var finishedRequests = 0

internal fun getPendingIntent(): PendingIntent =
PendingIntent.getActivity(
this,
0,
Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
},
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)

override fun createPublisherForAllAvailable(): Flow.Publisher<Control> =
Flow.Publisher { subscriber ->
updateSubscriber = subscriber
Expand Down Expand Up @@ -87,16 +68,7 @@ class ControlService : ControlsProviderService() {
) {
for (it in holder.response ?: emptyList()) {
subscriber.onNext(
Control.StatelessBuilder(
relevantDevices[index].id + '@' + it.hidden,
getPendingIntent(),
)
.setTitle(it.title)
.setSubtitle(relevantDevices[index].name)
.setZone(relevantDevices[index].name)
.setStructure(resources.getString(R.string.app_name))
.setDeviceType(Global.getDeviceType(relevantDevices[index].iconName))
.build(),
ControlBuilders.buildGenericControl(this@ControlService, it, relevantDevices[index]),
)
}
finishedRequests++
Expand All @@ -111,19 +83,6 @@ class ControlService : ControlsProviderService() {
}
}

internal fun getUnreachableControl(
id: String,
device: DeviceItem,
): Control =
Control.StatefulBuilder(id, getPendingIntent())
.setTitle(device.name)
.setZone(device.name)
.setStructure(resources.getString(R.string.app_name))
.setDeviceType(Global.getDeviceType(device.iconName))
.setStatus(Control.STATUS_DISABLED)
.setStatusText(resources.getString(R.string.str_unreachable))
.build()

private fun loadStatefulControl(
subscriber: Flow.Subscriber<in Control>?,
id: String,
Expand All @@ -134,7 +93,7 @@ class ControlService : ControlsProviderService() {
.getCorrectAPI(this, device.mode, device.id)
.loadList(getStatefulControlsCallback(device, id, subscriber))
} else {
subscriber?.onNext(getUnreachableControl(id, device))
subscriber?.onNext(ControlBuilders.buildUnreachableControl(this, id, device))
}
}

Expand All @@ -148,54 +107,12 @@ class ControlService : ControlsProviderService() {
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
if (holder.response == null) {
subscriber?.onNext(getUnreachableControl(id, device))
subscriber?.onNext(ControlBuilders.buildUnreachableControl(this@ControlService, id, device))
return
}
for (it in holder.response) {
if (device.id + '@' + it.hidden != id) continue
val controlBuilder =
Control.StatefulBuilder(id, getPendingIntent())
.setTitle(it.title)
.setSubtitle(device.name)
.setZone(device.name)
.setStructure(resources.getString(R.string.app_name))
.setDeviceType(Global.getDeviceType(device.iconName))
.setStatus(Control.STATUS_OK)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
controlBuilder.setAuthRequired(
PreferenceManager.getDefaultSharedPreferences(this@ControlService)
.getBoolean(
P.PREF_CONTROLS_AUTH,
P.PREF_CONTROLS_AUTH_DEFAULT,
),
)
}
if (it.state != null) {
controlBuilder.setControlTemplate(
ToggleTemplate(
id,
ControlButton(
it.state ?: false,
it.state.toString(),
),
),
)
controlBuilder.setStatusText(
resources.getString(
if (it.state == true) {
R.string.str_on
} else {
R.string.str_off
},
),
)
}
if (device.mode == Global.TASMOTA) {
controlBuilder.setControlTemplate(
StatelessTemplate(id),
)
}
subscriber?.onNext(controlBuilder.build())
subscriber?.onNext(ControlBuilders.buildStatefulControl(this@ControlService, id, it, device))
}
}

Expand Down

0 comments on commit 5d97ebc

Please sign in to comment.