Skip to content

shitty widget that doesnt even work like it should #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@
</intent-filter>
</receiver>

<receiver android:name=".widget.EngineToggleWidgetProvider"
android:exported="false"> <intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="me.timschneeberger.rootlessjamesdsp.SET_POWER_STATE" />
<action android:name="me.timschneeberger.rootlessjamesdsp.ACTION_ENGINE_STATE_CHANGED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_engine_toggle_info" />
</receiver>
<receiver android:name=".widget.WidgetActionReceiver"
android:exported="false"
android:process="${applicationId}">
<intent-filter>
<action android:name="me.timschneeberger.rootlessjamesdsp.ACTION_WIDGET_CLICK" />
</intent-filter>
</receiver>
<!-- Providers -->
<provider
android:name="rikka.shizuku.ShizukuProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class RootlessAudioProcessorService : BaseAudioProcessorService() {
ServiceNotificationHelper.createServiceNotification(this, arrayOf()),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
)

val widgetFilter = IntentFilter(Constants.ACTION_ENGINE_STATE_CHANGED)
registerReceiver(widgetReceiver, widgetFilter)
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Expand Down Expand Up @@ -191,27 +194,36 @@ class RootlessAudioProcessorService : BaseAudioProcessorService() {
// Setup media projection
mediaProjectionStartIntent = intent.extras?.getParcelableAs(EXTRA_MEDIA_PROJECTION_DATA)

mediaProjection = try {
mediaProjectionManager.getMediaProjection(
Activity.RESULT_OK,
mediaProjectionStartIntent!!
)
}
catch (ex: Exception) {
Timber.e("Failed to acquire media projection")
sendLocalBroadcast(Intent(Constants.ACTION_DISCARD_AUTHORIZATION))
Timber.e(ex)
null
}
if (mediaProjectionStartIntent != null) { // Add null check here
mediaProjection = try {
mediaProjectionManager.getMediaProjection(
Activity.RESULT_OK,
mediaProjectionStartIntent!! // Safe to use !! here as it's been checked
)
} catch (ex: Exception) {
Timber.e("Failed to acquire media projection")
sendLocalBroadcast(Intent(Constants.ACTION_DISCARD_AUTHORIZATION))
Timber.e(ex)
null
}

mediaProjection?.registerCallback(projectionCallback, Handler(Looper.getMainLooper()))
mediaProjection?.registerCallback(projectionCallback, Handler(Looper.getMainLooper()))

if (mediaProjection != null) {
startRecording()
sendLocalBroadcast(Intent(Constants.ACTION_SERVICE_STARTED))
if (mediaProjection != null) {
startRecording()
sendLocalBroadcast(Intent(Constants.ACTION_SERVICE_STARTED))
} else {
Timber.w("Failed to capture audio")
stopSelf()
}
} else {
Timber.w("Failed to capture audio")
Timber.e("onStartCommand: mediaProjectionStartIntent is null")
// Handle the case where mediaProjectionStartIntent is null, e.g.:
// - Stop the service
// - Log an error
// - Notify the user
stopSelf()
return START_NOT_STICKY
}

return START_REDELIVER_INTENT
Expand All @@ -235,6 +247,7 @@ class RootlessAudioProcessorService : BaseAudioProcessorService() {

// Unregister receivers and release resources
unregisterLocalReceiver(broadcastReceiver)
unregisterReceiver(widgetReceiver)
mediaProjection?.unregisterCallback(projectionCallback)
mediaProjection = null

Expand Down Expand Up @@ -695,4 +708,15 @@ class RootlessAudioProcessorService : BaseAudioProcessorService() {
}
}
}
}

private val widgetReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Constants.ACTION_ENGINE_STATE_CHANGED -> {
// Reiniciar la grabación para aplicar el nuevo estado del motor
restartRecording()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ object Constants {
const val PREF_APP = "application"
const val PREF_VAR = "variable"



const val ACTION_WIDGET_CLICK = "me.timschneeberger.rootlessjamesdsp.ACTION_WIDGET_CLICK"



// DSP-relevant preference namespaces
const val PREF_BASS = "dsp_bass"
const val PREF_COMPANDER = "dsp_compander"
Expand Down Expand Up @@ -45,4 +51,6 @@ object Constants {

// Intent extras
const val EXTRA_SAMPLE_RATE = BuildConfig.APPLICATION_ID + ".extra.service.SAMPLE_RATE"

const val ACTION_ENGINE_STATE_CHANGED = "me.timschneeberger.rootlessjamesdsp.ACTION_ENGINE_STATE_CHANGED"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ object EngineUtils : KoinComponent {
}
preferences.set(R.string.key_powered_on, isOn)
return

}

sdkAbove(Build.VERSION_CODES.Q) {
// Rootless
if (!isOn)
if (!isOn) {
RootlessAudioProcessorService.stop(this)
else
launchService(activityStarter)
} else {
RootlessAudioProcessorService.start(this, null)
}
}

// Guardar el estado del motor en las preferencias SIEMPRE
preferences.set(R.string.key_powered_on, isOn)
}
fun isEngineEnabled(context: Context): Boolean {
return preferences.get(R.string.key_powered_on, false, Boolean::class)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.timschneeberger.rootlessjamesdsp.widget

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import me.timschneeberger.rootlessjamesdsp.R
import me.timschneeberger.rootlessjamesdsp.utils.Constants
import me.timschneeberger.rootlessjamesdsp.utils.EngineUtils
import timber.log.Timber

class EngineToggleWidgetProvider : AppWidgetProvider() {

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
for (appWidgetId in appWidgetIds) {
updateWidget(context, appWidgetManager, appWidgetId)
}
}

override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)

when (intent.action) {
Constants.ACTION_ENGINE_STATE_CHANGED,
Constants.ACTION_SERVICE_STARTED,
Constants.ACTION_SERVICE_STOPPED -> {
val appWidgetManager = AppWidgetManager.getInstance(context)
val thisWidget = ComponentName(context, EngineToggleWidgetProvider::class.java)
val appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
onUpdate(context, appWidgetManager, appWidgetIds)
}
}
}

private fun updateWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
Timber.d("Updating widget with ID: $appWidgetId")

val views = RemoteViews(context.packageName, R.layout.widget_engine_toggle)

val iconResource = if (EngineUtils.isEngineEnabled(context)) {
R.drawable.ic_engine_on
} else {
R.drawable.ic_engine_off
}
views.setImageViewResource(R.id.widget_engine_icon, iconResource)

val intent = Intent(context, WidgetActionReceiver::class.java)
intent.action = Constants.ACTION_WIDGET_CLICK
val pendingIntent = PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
views.setOnClickPendingIntent(R.id.widget_engine_icon, pendingIntent)

appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.timschneeberger.rootlessjamesdsp.widget

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import me.timschneeberger.rootlessjamesdsp.utils.Constants
import me.timschneeberger.rootlessjamesdsp.utils.EngineUtils
import me.timschneeberger.rootlessjamesdsp.utils.EngineUtils.toggleEnginePower // <-- Correct import
import timber.log.Timber

class WidgetActionReceiver : BroadcastReceiver() {


override fun onReceive(context: Context, intent: Intent) {
Timber.d("Widget action received")
Timber.d("WidgetActionReceiver: onReceive() triggered") // Add this log
when (intent.action) {
Constants.ACTION_WIDGET_CLICK -> { // Maneja la acción del widget
Timber.d("Alternando el estado del motor: ${!EngineUtils.isEngineEnabled(context)}")
context.toggleEnginePower(!EngineUtils.isEngineEnabled(context))

// Notificar al servicio sobre el cambio de estado
val updateIntent = Intent(Constants.ACTION_ENGINE_STATE_CHANGED)
context.sendBroadcast(updateIntent) // Utilizar sendBroadcast()
}
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_engine_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,3L2,15h20z"
android:fillColor="#F44336"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_engine_on.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,3L2,15h20z"
android:fillColor="#4CAF50"/>
</vector>
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/widget_preview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFEEEEEE" />
<corners android:radius="8dp" />
<padding
android:left="16dp"
android:top="16dp"
android:right="16dp"
android:bottom="16dp" />

<size
android:width="40dp"
android:height="40dp" />
</shape>
9 changes: 6 additions & 3 deletions app/src/main/res/layout/activity_dsp_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
android:id="@+id/power_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bar"
app:srcCompat="@drawable/ic_twotone_power_settings_24dp"
android:contentDescription="@string/power_button_alt"
android:hapticFeedbackEnabled="true"
android:contentDescription="@string/power_button_alt" />
android:rotation="-25"
android:rotationX="37"
android:rotationY="33"
app:layout_anchor="@id/bar"
app:srcCompat="@drawable/ic_twotone_power_settings_24dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
16 changes: 16 additions & 0 deletions app/src/main/res/layout/widget_engine_toggle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="8dp">

<ImageView
android:id="@+id/widget_engine_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_engine_off"
android:contentDescription="@string/widget_engine_toggle_description" />

</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,6 @@
<string name="version_mismatch_root_description">The JamesDSP magisk package installed on your device is outdated and not supported by this app anymore.\n\nPlease retrieve the latest JamesDSP magisk package ZIP and install it via Magisk.\nNote that the magisk package may replace this app with the official JamesDSP app.\n\nDo you want to visit the download website for the Magisk packages now?</string>
<string name="version_mismatch_root_toast">Failed to start. The installed JamesDSP magisk package is too old and not supported by this app anymore.</string>

<!-- widget engine toggle -->
<string name="widget_engine_toggle_description">Toggle audio processing engine</string>
</resources>
11 changes: 11 additions & 0 deletions app/src/main/res/xml/widget_engine_toggle_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget_engine_toggle"
android:initialLayout="@layout/widget_engine_toggle"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/widget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen">
</appwidget-provider>