Browse Source

Testing adding a widget

Thomas Chef 2 years ago
parent
commit
9c01127ff5

+ 3 - 0
.idea/misc.xml

@@ -3,12 +3,15 @@
   <component name="DesignSurface">
     <option name="filePathToZoomLevelMap">
       <map>
+        <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/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/menu/bottom_nav_menu.xml" value="0.25" />
       </map>
     </option>

+ 15 - 6
app/src/main/AndroidManifest.xml

@@ -5,11 +5,9 @@
 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
-
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <uses-permission android:name="android.permission.INTERNET" />
 
-
     <application
         android:name=".JourneyGpsTrackerApplication"
         android:allowBackup="true"
@@ -22,13 +20,24 @@
         android:theme="@style/Theme.JourneyGPSTracker"
         android:usesCleartextTraffic="true"
         tools:targetApi="31">
+        <receiver
+            android:name=".HomeTempAppWidget"
+            android:exported="false">
+            <intent-filter>
+                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+            </intent-filter>
+
+            <meta-data
+                android:name="android.appwidget.provider"
+                android:resource="@xml/home_temp_app_widget_info" />
+        </receiver>
+
         <service
             android:name=".TrackingService"
+            android:description="@string/tracking_service_desc"
             android:enabled="true"
             android:exported="false"
-            android:description="@string/tracking_service_desc"
-            android:foregroundServiceType="location">
-        </service>
+            android:foregroundServiceType="location"></service>
 
         <activity
             android:name=".MainActivity"

+ 59 - 0
app/src/main/java/com/flacksta/chef/journeygpstracker/HomeTempAppWidget.kt

@@ -0,0 +1,59 @@
+package com.flacksta.chef.journeygpstracker
+
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.Context
+import android.content.Intent
+import android.util.Log
+import android.widget.RemoteViews
+
+/**
+ * Implementation of App Widget functionality.
+ */
+class HomeTempAppWidget : AppWidgetProvider() {
+
+    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) {
+            updateAppWidget(context, appWidgetManager, appWidgetId, updateCnt)
+        }
+    }
+
+    override fun onEnabled(context: Context) {
+        // Enter relevant functionality for when the first widget is created
+    }
+
+    override fun onDisabled(context: Context) {
+        // Enter relevant functionality for when the last widget is disabled
+    }
+
+    override fun onReceive(context: Context?, intent: Intent?) {
+        super.onReceive(context, intent)
+        Log.d("KALLE", "onReceive()" + intent?.action);
+    }
+}
+
+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)
+}

BIN
app/src/main/res/drawable-nodpi/example_appwidget_preview.png


+ 10 - 0
app/src/main/res/drawable-v21/app_widget_background.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+Background for widgets to make the rounded corners based on the
+appWidgetRadius attribute value
+-->
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+
+    <corners android:radius="?attr/appWidgetRadius" />
+    <solid android:color="?android:attr/colorBackground" />
+</shape>

+ 10 - 0
app/src/main/res/drawable-v21/app_widget_inner_view_background.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+Background for views inside widgets to make the rounded corners based on the
+appWidgetInnerRadius attribute value
+-->
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+
+    <corners android:radius="?attr/appWidgetInnerRadius" />
+    <solid android:color="?android:attr/colorBackground" />
+</shape>

+ 20 - 0
app/src/main/res/layout/home_temp_app_widget.xml

@@ -0,0 +1,20 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    style="@style/Widget.JourneyGPSTracker.AppWidget.Container"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:theme="@style/Theme.JourneyGPSTracker.AppWidgetContainer">
+
+    <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>

+ 10 - 0
app/src/main/res/values-night-v31/themes.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <!--
+    Having themes.xml for night-v31 because of the priority order of the resource qualifiers.
+    -->
+    <style name="Theme.JourneyGPSTracker.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault.DayNight">
+        <item name="appWidgetRadius">@android:dimen/system_app_widget_background_radius</item>
+        <item name="appWidgetInnerRadius">@android:dimen/system_app_widget_inner_radius</item>
+    </style>
+</resources>

+ 14 - 0
app/src/main/res/values-v21/styles.xml

@@ -0,0 +1,14 @@
+<resources>
+
+    <style name="Widget.JourneyGPSTracker.AppWidget.Container" parent="android:Widget">
+        <item name="android:id">@android:id/background</item>
+        <item name="android:padding">?attr/appWidgetPadding</item>
+        <item name="android:background">@drawable/app_widget_background</item>
+    </style>
+
+    <style name="Widget.JourneyGPSTracker.AppWidget.InnerView" parent="android:Widget">
+        <item name="android:padding">?attr/appWidgetPadding</item>
+        <item name="android:background">@drawable/app_widget_inner_view_background</item>
+        <item name="android:textColor">?android:attr/textColorPrimary</item>
+    </style>
+</resources>

+ 16 - 0
app/src/main/res/values-v31/styles.xml

@@ -0,0 +1,16 @@
+<resources>
+
+    <style name="Widget.JourneyGPSTracker.AppWidget.Container" parent="android:Widget">
+        <item name="android:id">@android:id/background</item>
+        <item name="android:padding">?attr/appWidgetPadding</item>
+        <item name="android:background">@drawable/app_widget_background</item>
+        <item name="android:clipToOutline">true</item>
+    </style>
+
+    <style name="Widget.JourneyGPSTracker.AppWidget.InnerView" parent="android:Widget">
+        <item name="android:padding">?attr/appWidgetPadding</item>
+        <item name="android:background">@drawable/app_widget_inner_view_background</item>
+        <item name="android:textColor">?android:attr/textColorPrimary</item>
+        <item name="android:clipToOutline">true</item>
+    </style>
+</resources>

+ 11 - 0
app/src/main/res/values-v31/themes.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <!--
+    Having themes.xml for v31 variant because @android:dimen/system_app_widget_background_radius
+     and @android:dimen/system_app_widget_internal_padding requires API level 31
+    -->
+    <style name="Theme.JourneyGPSTracker.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault.DayNight">
+        <item name="appWidgetRadius">@android:dimen/system_app_widget_background_radius</item>
+        <item name="appWidgetInnerRadius">@android:dimen/system_app_widget_inner_radius</item>
+    </style>
+</resources>

+ 7 - 0
app/src/main/res/values/attrs.xml

@@ -0,0 +1,7 @@
+<resources>
+    <declare-styleable name="AppWidgetAttrs">
+        <attr name="appWidgetPadding" format="dimension" />
+        <attr name="appWidgetInnerRadius" format="dimension" />
+        <attr name="appWidgetRadius" format="dimension" />
+    </declare-styleable>
+</resources>

+ 4 - 0
app/src/main/res/values/colors.xml

@@ -7,4 +7,8 @@
     <color name="teal_700">#FF018786</color>
     <color name="black">#FF000000</color>
     <color name="white">#FFFFFFFF</color>
+    <color name="light_blue_50">#FFE1F5FE</color>
+    <color name="light_blue_200">#FF81D4FA</color>
+    <color name="light_blue_600">#FF039BE5</color>
+    <color name="light_blue_900">#FF01579B</color>
 </resources>

+ 10 - 0
app/src/main/res/values/dimens.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <!--
+Refer to App Widget Documentation for margin information
+http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
+    -->
+    <dimen name="widget_margin">0dp</dimen>
+
+</resources>

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -6,4 +6,7 @@
     <string name="tracking_service_desc">GPS Journey tracking service.</string>
 
     <string name="notification_desc">Touch to open the App. Dist:</string>
+    <string name="appwidget_text">GHI</string>
+    <string name="add_widget">Add widget</string>
+    <string name="app_widget_description">Widget that shows outside temp at your home</string>
 </resources>

+ 12 - 0
app/src/main/res/values/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <style name="Widget.JourneyGPSTracker.AppWidget.Container" parent="android:Widget">
+        <item name="android:id">@android:id/background</item>
+        <item name="android:background">?android:attr/colorBackground</item>
+    </style>
+
+    <style name="Widget.JourneyGPSTracker.AppWidget.InnerView" parent="android:Widget">
+        <item name="android:background">?android:attr/colorBackground</item>
+        <item name="android:textColor">?android:attr/textColorPrimary</item>
+    </style>
+</resources>

+ 15 - 0
app/src/main/res/values/themes.xml

@@ -13,4 +13,19 @@
         <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
         <!-- Customize your theme here. -->
     </style>
+
+    <style name="Theme.JourneyGPSTracker.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault">
+        <!-- Radius of the outer bound of widgets to make the rounded corners -->
+        <item name="appWidgetRadius">16dp</item>
+        <!--
+        Radius of the inner view's bound of widgets to make the rounded corners.
+        It needs to be 8dp or less than the value of appWidgetRadius
+        -->
+        <item name="appWidgetInnerRadius">8dp</item>
+    </style>
+
+    <style name="Theme.JourneyGPSTracker.AppWidgetContainer" parent="Theme.JourneyGPSTracker.AppWidgetContainerParent">
+        <!-- Apply padding to avoid the content of the widget colliding with the rounded corners -->
+        <item name="appWidgetPadding">16dp</item>
+    </style>
 </resources>

+ 14 - 0
app/src/main/res/xml/home_temp_app_widget_info.xml

@@ -0,0 +1,14 @@
+<?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="80dp"
+    android:minHeight="80dp"
+    android:previewImage="@drawable/example_appwidget_preview"
+
+    android:previewLayout="@layout/home_temp_app_widget"
+    android:targetCellWidth="2"
+    android:targetCellHeight="2"
+    android:updatePeriodMillis="5000"
+    android:widgetCategory="home_screen" />