|
@@ -1,21 +1,21 @@
|
|
|
package com.flacksta.chef.journeygpstracker
|
|
|
|
|
|
+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.util.Log
|
|
|
import android.widget.RemoteViews
|
|
|
|
|
|
|
|
|
-/**
|
|
|
- * Implementation of App Widget functionality.
|
|
|
- */
|
|
|
class HomeTempAppWidget : AppWidgetProvider() {
|
|
|
|
|
|
val ACTION_UPDATE = "com.flacksta.chef.journeygpstracker.action.UPDATE"
|
|
|
- var updateCnt: Int = 0
|
|
|
+
|
|
|
+ companion object {
|
|
|
+ var updateCnt: Int = 0
|
|
|
+ }
|
|
|
|
|
|
override fun onUpdate(
|
|
|
context: Context,
|
|
@@ -27,53 +27,43 @@ class HomeTempAppWidget : AppWidgetProvider() {
|
|
|
|
|
|
// There may be multiple widgets active, so update all of them
|
|
|
for (appWidgetId in appWidgetIds) {
|
|
|
- updateAppWidget(context, appWidgetManager, appWidgetId, updateCnt)
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- fun onUpdate( context: Context ) {
|
|
|
- val appWidgetManager = AppWidgetManager.getInstance(context)
|
|
|
+ Log.i("KALLE", "onUpdate no:$appWidgetId")
|
|
|
+
|
|
|
+ val remoteViews = RemoteViews(context.packageName,R.layout.home_temp_app_widget)
|
|
|
+
|
|
|
+ remoteViews.setTextViewText(R.id.appwidget_text, ">$updateCnt<")
|
|
|
|
|
|
- val thisAppWidgetComponentName = ComponentName(context.packageName, javaClass.name)
|
|
|
- val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName)
|
|
|
- onUpdate(context, appWidgetManager, appWidgetIds)
|
|
|
+ val intent = Intent(context, HomeTempAppWidget::class.java)
|
|
|
+ intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
|
|
+ intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
|
|
|
+
|
|
|
+ val pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT )
|
|
|
+ remoteViews.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent)
|
|
|
+
|
|
|
+ appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
override fun onEnabled(context: Context) {
|
|
|
- // Enter relevant functionality for when the first widget is created
|
|
|
+ super.onEnabled(context)
|
|
|
Log.d("KALLE", "onEnabled()");
|
|
|
}
|
|
|
|
|
|
override fun onDisabled(context: Context) {
|
|
|
- // Enter relevant functionality for when the last widget is disabled
|
|
|
+ super.onDisabled(context)
|
|
|
Log.d("KALLE", "onDisabled()");
|
|
|
}
|
|
|
|
|
|
+ override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
|
|
|
+ super.onDeleted(context, appWidgetIds)
|
|
|
+ Log.d("KALLE", "onDeleted()");
|
|
|
+ }
|
|
|
+
|
|
|
override fun onReceive(context: Context?, intent: Intent?) {
|
|
|
super.onReceive(context, intent)
|
|
|
Log.d("KALLE", "onReceive()" + intent?.action);
|
|
|
- if (intent != null) {
|
|
|
- if (ACTION_UPDATE.equals(intent.getAction())) {
|
|
|
- if (context != null) {
|
|
|
- onUpdate(context)
|
|
|
- };
|
|
|
- } else super.onReceive(context, intent)
|
|
|
- };
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-internal fun updateAppWidget(
|
|
|
- context: Context,
|
|
|
- appWidgetManager: AppWidgetManager,
|
|
|
- appWidgetId: Int,
|
|
|
- updateCnt: Int
|
|
|
-) {
|
|
|
- Log.i("KALLE","updateAppWidget")
|
|
|
- //val widgetText = context.getString(R.string.appwidget_text)
|
|
|
- // Construct the RemoteViews object
|
|
|
- val views = RemoteViews(context.packageName, R.layout.home_temp_app_widget)
|
|
|
- views.setTextViewText(R.id.appwidget_text, ":$updateCnt")
|
|
|
-
|
|
|
- // Instruct the widget manager to update the widget
|
|
|
- appWidgetManager.updateAppWidget(appWidgetId, views)
|
|
|
-}
|