Browse Source

It works with click !

Thomas Chef 2 years ago
parent
commit
9fb4667db9

+ 2 - 0
.idea/misc.xml

@@ -6,12 +6,14 @@
         <entry key="app/src/main/res/drawable-v21/app_widget_background.xml" value="0.1865" />
         <entry key="app/src/main/res/drawable-v21/app_widget_inner_view_background.xml" value="0.1865" />
         <entry key="app/src/main/res/drawable/ic_baseline_map_24.xml" value="0.2195" />
+        <entry key="app/src/main/res/drawable/my_backgrnd_shape.xml" value="0.187" />
         <entry key="app/src/main/res/layout/activity_main.xml" value="0.375" />
         <entry key="app/src/main/res/layout/activity_main_screen.xml" value="0.25" />
         <entry key="app/src/main/res/layout/fragment_home.xml" value="0.3098958333333333" />
         <entry key="app/src/main/res/layout/fragment_map.xml" value="0.3098958333333333" />
         <entry key="app/src/main/res/layout/fragment_settings.xml" value="0.29583333333333334" />
         <entry key="app/src/main/res/layout/home_temp_app_widget.xml" value="0.25" />
+        <entry key="app/src/main/res/layout/widget_layout.xml" value="0.1890625" />
         <entry key="app/src/main/res/menu/bottom_nav_menu.xml" value="0.25" />
       </map>
     </option>

+ 27 - 37
app/src/main/java/com/flacksta/chef/journeygpstracker/HomeTempAppWidget.kt

@@ -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)
-}

+ 0 - 31
app/src/main/java/com/flacksta/chef/journeygpstracker/WidUtils.kt

@@ -1,31 +0,0 @@
-package com.flacksta.chef.journeygpstracker
-
-import android.app.AlarmManager
-import android.app.PendingIntent
-import android.content.Context
-import android.content.Intent
-import android.preference.PreferenceManager
-
-
-class WidUtils {/*
-    fun scheduleUpdate(context: Context) {
-        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager
-        val pendingIntent = getAlarmIntent(context) //PendingIntent.getService(context, requestId, intent, PendingIntent.FLAG_NO_CREATE)
-
-        val intervalMillis = (interval!!.toInt() * 60 * 1000).toLong()
-        val pi = getAlarmIntent(context)
-        am.cancel(pi)
-        am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalMillis, pi)
-    }
-
-    private fun getAlarmIntent(context: Context): PendingIntent {
-        val intent = Intent(context, HomeTempAppWidget::class.java)
-        intent.action = HomeTempAppWidget.ACTION_UPDATE
-        return PendingIntent.getBroadcast(context, 0, intent, 0)
-    }
-
-    fun clearUpdate(context: Context) {
-        val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
-        am.cancel(getAlarmIntent(context))
-    }*/
-}

+ 20 - 0
app/src/main/res/drawable/my_backgrnd_shape.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle" >
+
+    <stroke
+        android:width="4dp"
+        android:color="#FF88FFFF" />
+
+    <gradient
+        android:angle="225"
+        android:endColor="#DD2ECCFA"
+        android:startColor="#DD000000" />
+
+    <corners
+        android:bottomLeftRadius="7dp"
+        android:bottomRightRadius="8dp"
+        android:topLeftRadius="8dp"
+        android:topRightRadius="7dp" />
+
+</shape>

+ 19 - 17
app/src/main/res/layout/home_temp_app_widget.xml

@@ -1,20 +1,22 @@
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    style="@style/Widget.JourneyGPSTracker.AppWidget.Container"
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:theme="@style/Theme.JourneyGPSTracker.AppWidgetContainer">
+    android:layout_margin="8dip"
+    android:background="@drawable/my_backgrnd_shape" >
 
-    <TextView
-        android:id="@+id/appwidget_text"
-        style="@style/Widget.JourneyGPSTracker.AppWidget.InnerView"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_centerHorizontal="true"
-        android:layout_centerVertical="true"
-        android:layout_margin="0dp"
-        android:gravity="center"
-        android:contentDescription="W"
-        android:text="123XYZ"
-        android:textSize="36dp"
-        android:textStyle="bold" />
-</RelativeLayout>
+<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>
+
+</LinearLayout>

+ 5 - 9
app/src/main/res/xml/home_temp_app_widget_info.xml

@@ -1,14 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
-    android:description="@string/app_widget_description"
-    android:initialKeyguardLayout="@layout/home_temp_app_widget"
     android:initialLayout="@layout/home_temp_app_widget"
-    android:minWidth="146dp"
+    android:minWidth="72dp"
     android:minHeight="72dp"
-    android:previewImage="@drawable/example_appwidget_preview"
-
-    android:previewLayout="@layout/home_temp_app_widget"
-    android:targetCellWidth="2"
-    android:targetCellHeight="2"
     android:updatePeriodMillis="1800000"
-    android:widgetCategory="home_screen" />
+
+
+    android:widgetCategory="home_screen"
+/>