Browse Source

Works with http fetch and click.

Thomas Chef 2 years ago
parent
commit
7b5239bb1e

+ 4 - 0
app/build.gradle

@@ -82,4 +82,8 @@ dependencies {
 
     // Location
     implementation 'com.google.android.gms:play-services-location:18.0.0'
+
+    // HTTP Get requests
+    implementation 'com.android.volley:volley:1.2.1'
+
 }

+ 5 - 1
app/src/main/AndroidManifest.xml

@@ -20,6 +20,10 @@
         android:theme="@style/Theme.JourneyGPSTracker"
         android:usesCleartextTraffic="true"
         tools:targetApi="31">
+        <service
+            android:name=".UpdateWidgetService"
+            android:enabled="true"
+            android:exported="true"></service>
 
         <receiver
             android:name=".HomeTempAppWidget"
@@ -38,7 +42,7 @@
             android:description="@string/tracking_service_desc"
             android:enabled="true"
             android:exported="false"
-            android:foregroundServiceType="location"></service>
+            android:foregroundServiceType="location" />
 
         <activity
             android:name=".MainActivity"

+ 9 - 24
app/src/main/java/com/flacksta/chef/journeygpstracker/HomeTempAppWidget.kt

@@ -1,48 +1,33 @@
 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
 
 
 class HomeTempAppWidget : AppWidgetProvider() {
 
     val ACTION_UPDATE = "com.flacksta.chef.journeygpstracker.action.UPDATE"
 
-    companion object {
-        var updateCnt: Int = 0
-    }
-
     override fun onUpdate(
         context: Context,
         appWidgetManager: AppWidgetManager,
         appWidgetIds: IntArray
     ) {
-        updateCnt++
-        Log.i("KALLE", "onUpdate cnt:$updateCnt")
-
-        // There may be multiple widgets active, so update all of them
-        for (appWidgetId in appWidgetIds) {
-
-            Log.i("KALLE", "onUpdate no:$appWidgetId")
-
-            val remoteViews = RemoteViews(context.packageName,R.layout.home_temp_app_widget)
-
-            remoteViews.setTextViewText(R.id.appwidget_text, ">$updateCnt<")
+        Log.d("KALLE","onUpdate()")
 
-            val intent = Intent(context, HomeTempAppWidget::class.java)
-            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
-            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
+        val thisWidget = ComponentName(context,HomeTempAppWidget::class.java)
+        val allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
 
-            val pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT )
-            remoteViews.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent)
+        // Build the intent to call the service
+        val intent = Intent(context.applicationContext,UpdateWidgetService::class.java)
+        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
 
-            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
-        }
+        // Update the widgets via the service
+        context.startService(intent);
     }
 
 

+ 70 - 0
app/src/main/java/com/flacksta/chef/journeygpstracker/UpdateWidgetService.kt

@@ -0,0 +1,70 @@
+package com.flacksta.chef.journeygpstracker
+
+
+import android.app.PendingIntent
+import android.app.Service
+import android.appwidget.AppWidgetManager
+import android.content.Intent
+import android.os.IBinder
+import android.util.Log
+import android.widget.RemoteViews
+import android.widget.TextView
+import com.android.volley.Request
+import com.android.volley.Response
+import com.android.volley.toolbox.StringRequest
+import com.android.volley.toolbox.Volley
+import java.util.*
+
+
+class UpdateWidgetService : Service() {
+
+    override fun onStart(intent: Intent, startId: Int) {
+        val appWidgetManager = AppWidgetManager.getInstance(this.applicationContext)
+        val allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)
+
+        val remoteViews = RemoteViews(this.applicationContext.packageName,  R.layout.home_temp_app_widget )
+
+        val queue = Volley.newRequestQueue(this)
+        val url = "http://chef.maya.se/info/curr_temp.php"
+
+        // Request a string response from the provided URL.
+        val stringRequest = StringRequest(
+            Request.Method.GET, url,
+            Response.Listener<String> { response ->
+                Log.i("SERVICE", "Response is: $response")
+                for (widgetId in allWidgetIds!!) {
+                    remoteViews.setTextViewText(R.id.appwidget_text,"$response")
+                    appWidgetManager.updateAppWidget(widgetId, remoteViews)
+                }
+            },
+            Response.ErrorListener {
+                Log.i("SERVICE", "That didn't work!" )
+                for (widgetId in allWidgetIds!!) {
+                    remoteViews.setTextViewText(R.id.appwidget_text,"ERR")
+                    appWidgetManager.updateAppWidget(widgetId, remoteViews)
+                }
+            }
+        )
+
+        queue.add(stringRequest)
+
+        for (widgetId in allWidgetIds!!) {
+
+            val clickIntent = Intent(this.applicationContext,HomeTempAppWidget::class.java )
+            clickIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
+            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds )
+            val pendingIntent = PendingIntent.getBroadcast(
+                applicationContext, 0, clickIntent,
+                PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+            )
+            remoteViews.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent)
+        }
+        stopSelf()
+        super.onStart(intent, startId)
+    }
+
+    override fun onBind(intent: Intent): IBinder? {
+        return null
+    }
+}
+

+ 12 - 13
app/src/main/res/layout/home_temp_app_widget.xml

@@ -4,19 +4,18 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_margin="8dip"
-    android:background="@drawable/my_backgrnd_shape" >
+    android:background="@drawable/my_backgrnd_shape">
 
-<TextView
-    android:id="@+id/appwidget_text"
-    style="@android:style/TextAppearance.Medium"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:layout_gravity="center"
-    android:gravity="center_horizontal|center_vertical"
-    android:layout_margin="4dip"
-    android:text="Text"
-    android:textSize="36dp"
-    android:textStyle="bold">
-</TextView>
+    <TextView
+        android:id="@+id/appwidget_text"
+        style="@android:style/TextAppearance.Medium"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_gravity="center"
+        android:layout_margin="4dip"
+        android:gravity="center_horizontal|center_vertical"
+        android:text="Text"
+        android:textSize="36dp"
+        android:textStyle="bold"></TextView>
 
 </LinearLayout>

+ 2 - 2
app/src/main/res/xml/home_temp_app_widget_info.xml

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
     android:initialLayout="@layout/home_temp_app_widget"
-    android:minWidth="72dp"
-    android:minHeight="72dp"
+    android:minWidth="144dp"
+    android:minHeight="50dp"
     android:updatePeriodMillis="1800000"