Skip to content

Commit

Permalink
perform simple widget rendering
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/org/blitzortung/android/alert/handler/AlertHandler.kt
#	app/src/main/java/org/blitzortung/android/app/WidgetProvider.kt
  • Loading branch information
wuan committed Oct 13, 2024
1 parent 4a845fe commit 5f65828
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!--<receiver android:name=".WidgetProvider">
<receiver android:name=".WidgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>-->
</receiver>
</application>
<queries>
<intent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AlertHandler @Inject constructor(

private var signalingLastTimestamp: Long = 0

private val locationEventConsumer: (LocationEvent) -> Unit = { event ->
internal val locationEventConsumer: (LocationEvent) -> Unit = { event ->
Log.v(Main.LOG_TAG, "AlertHandler.locationEventConsumer ${event.location}")

checkStrikes(lastStrikes, event.location)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
private var backgroundAlertEnabled: Boolean = false
private lateinit var statusComponent: StatusComponent

private lateinit var strikeColorHandler: StrikeColorHandler
@set:Inject
internal lateinit var strikeColorHandler: StrikeColorHandler

private lateinit var strikeListOverlay: StrikeListOverlay
private lateinit var ownLocationOverlay: OwnLocationOverlay
private lateinit var fadeOverlay: FadeOverlay
Expand Down Expand Up @@ -243,8 +245,6 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
preferences.registerOnSharedPreferenceChangeListener(this)

strikeColorHandler = StrikeColorHandler(preferences)

statusComponent = StatusComponent(
findViewById(R.id.warning),
findViewById(R.id.status),
Expand Down
114 changes: 103 additions & 11 deletions app/src/main/java/org/blitzortung/android/app/WidgetProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,125 @@
package org.blitzortung.android.app

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.RemoteViews
import dagger.android.AndroidInjection
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.blitzortung.android.alert.AlertResult
import org.blitzortung.android.alert.event.AlertEvent
import org.blitzortung.android.alert.event.AlertResultEvent
import org.blitzortung.android.alert.handler.AlertDataHandler
import org.blitzortung.android.alert.handler.AlertHandler
import org.blitzortung.android.app.view.AlertView
import org.blitzortung.android.data.Flags
import org.blitzortung.android.data.Parameters
import org.blitzortung.android.data.provider.result.ResultEvent
import org.blitzortung.android.data.provider.standard.JsonRpcDataProvider
import org.blitzortung.android.location.LocationHandler
import org.blitzortung.android.map.overlay.color.StrikeColorHandler
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject


class WidgetProvider : AppWidgetProvider() {

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
for (element in appWidgetIds) {
val appWidgetId = element
updateAppWidget(context)
@set:Inject
internal lateinit var colorHandler: StrikeColorHandler

@set:Inject
internal lateinit var alertHandler: AlertHandler

@set:Inject
internal lateinit var alertDataHandler: AlertDataHandler

@set:Inject
internal lateinit var locationHandler: LocationHandler

@set:Inject
internal lateinit var dataProvider: JsonRpcDataProvider

var df: DateFormat = SimpleDateFormat("hh:mm:ss")


override fun onAppWidgetOptionsChanged(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetId: Int, newOptions: Bundle?) {
AndroidInjection.inject(this, context)
Log.v(Main.LOG_TAG, "WidgetProvider.onAppWidgetOptionsChanged()")
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions)
}

override fun onReceive(context: Context?, intent: Intent) {
AndroidInjection.inject(this, context)
val action = intent.action
Log.v(Main.LOG_TAG, "WidgetProvider.onReceive() $action")
if (ACTION_APPWIDGET_UPDATE == action) {
locationHandler.start()
val views = getUpdatedViews(context!!)
AppWidgetManager.getInstance(context).updateAppWidget(
ComponentName(context, WidgetProvider::class.java)
, views)
locationHandler.shutdown()
}
}

private fun updateAppWidget(context: Context) {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
AndroidInjection.inject(this, context)
Log.v(Main.LOG_TAG, "WidgetProvider.onUpdate()")

locationHandler.start()

val views = getUpdatedViews(context)
for (appWidgetId in appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, views)
}

locationHandler.shutdown()
}

private fun getUpdatedViews(context: Context): RemoteViews {
val alertView = AlertView(context)
alertView.setColorHandler(colorHandler, 60)

val parameters = Parameters(region = 0, rasterBaselength = 5000, intervalDuration = 60)
val result = runBlocking {
withContext(Dispatchers.Default) {
async {
Log.v(Main.LOG_TAG, "Widget.getUpdatedViews() retrieve running in ${Thread.currentThread().name}")
var result = ResultEvent(referenceTime = System.currentTimeMillis(), parameters = parameters, flags = Flags())
dataProvider.retrieveData {
result = getStrikesGrid(parameters, result)
}
Log.v(Main.LOG_TAG, "Widget.getUpdatedViews() check running in ${Thread.currentThread().name}")
alertDataHandler.checkStrikes(result.strikes!!, locationHandler.location!!, alertHandler.alertParameters, result.referenceTime)
}.await()
}
}

Log.v(Main.LOG_TAG, "Widget.getUpdatedViews() result in ${Thread.currentThread().name} $result")
alertView.alertEventConsumer.invoke(AlertResultEvent(result))

alertView.measure(150, 150)
alertView.layout(0, 0, 150, 150)
alertView.isDrawingCacheEnabled = true
val bitmap = alertView.drawingCache

val remoteViews = RemoteViews(
context.packageName,
R.layout.widget
)
remoteViews.setImageViewBitmap(R.layout.widget, bitmap)
val bitmap = alertView.drawingCache

val views = RemoteViews(context.packageName, R.layout.widget)
views.setImageViewBitmap(R.id.alarm_diagram, bitmap)
val format = df.format(Date())
Log.v(Main.LOG_TAG, "WidgetProvider.getUpdatedViews() $format")
views.setTextViewText(R.id.widget_update_time, format)
return views
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.android.ContributesAndroidInjector
import org.blitzortung.android.app.AppService
import org.blitzortung.android.app.Main
import org.blitzortung.android.app.Preferences
import org.blitzortung.android.app.WidgetProvider

@Module
abstract class ActivityBindingModule {
Expand All @@ -17,4 +18,7 @@ abstract class ActivityBindingModule {

@ContributesAndroidInjector
abstract fun contributesAppServiceInjector(): AppService

@ContributesAndroidInjector
abstract fun contributesWidgetProvider(): WidgetProvider
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
package org.blitzortung.android.map.overlay.color

import android.content.SharedPreferences
import javax.inject.Inject
import javax.inject.Singleton

open class StrikeColorHandler(preferences: SharedPreferences) : ColorHandler(preferences) {
@Singleton
open class StrikeColorHandler @Inject constructor(preferences: SharedPreferences) : ColorHandler(preferences) {

private val streetmapColors = mutableMapOf<IntArray, IntArray>()

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

<TextView
android:id="@+id/widget_update_time"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/TextAppearance.AppCompat.Headline"
android:text="foobar"
/>

</LinearLayout>
9 changes: 6 additions & 3 deletions app/src/main/res/xml/widget_provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="120dp"
android:minWidth="150dp"
android:updatePeriodMillis="4320000"/>
android:previewImage="@drawable/abc_vector_test"
android:minHeight="20dp"
android:minWidth="20dp"
android:updatePeriodMillis="10000"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"/>

0 comments on commit 5f65828

Please sign in to comment.