Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status Widget #429 #430

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@
</intent-filter>
</receiver>

<receiver
android:name=".StatusWidget"
android:label="@string/status_widget_label"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/status_widget_info" />
</receiver>

</application>

</manifest>
83 changes: 83 additions & 0 deletions app/src/main/java/org/traccar/client/StatusWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2016 - 2022 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.client

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.*
import android.widget.RemoteViews

import androidx.preference.PreferenceManager

/**
* Implementation of Status Widget functionality, which can display the status of the service -
* whether it is enabled or not.
*/
class StatusWidget : AppWidgetProvider() {

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
update(context, appWidgetManager, appWidgetIds)
}

override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
val filter = IntentFilter()
filter.addAction(TrackingService.ACTION_STARTED)
filter.addAction(TrackingService.ACTION_STOPPED)
// Note: we must register through the app context as a workaround
// for 'BroadcastReceiver components are not allowed to register to receive intents'
context.applicationContext.registerReceiver(Companion, filter)
}

override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
context.applicationContext.unregisterReceiver(Companion)
}

companion object: BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
if (TrackingService.ACTION_STARTED == intent.action
|| TrackingService.ACTION_STOPPED == intent.action) {
updateWidgets(context)
}
}

// Performs update for all widgets of this class.
fun updateWidgets(context: Context) {
val manager = AppWidgetManager.getInstance(context)
val appWidgetIds = manager.getAppWidgetIds(ComponentName(context, StatusWidget::class.java.name))
update(context, manager, appWidgetIds)
}

}

}

// Performs update for the widgets with given identifiers.
internal fun update(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
Anton-V-K marked this conversation as resolved.
Show resolved Hide resolved
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val enabled = prefs.getBoolean(MainFragment.KEY_STATUS, false)
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.status_widget)
views.setImageViewResource(R.id.ivEnabled, if (enabled) R.mipmap.ic_start else R.mipmap.ic_stop)

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-v21/app_widget_background.xml
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions app/src/main/res/drawable-v21/app_widget_inner_view_background.xml
Original file line number Diff line number Diff line change
@@ -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/colorAccent" />
</shape>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions app/src/main/res/layout/status_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.Traccarclientandroid.AppWidget.Container"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme.AppWidgetContainer">
Anton-V-K marked this conversation as resolved.
Show resolved Hide resolved

<ImageView
android:id="@+id/ivEnabled"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/settings_status_title"
android:src="@android:drawable/ic_menu_help"
app:srcCompat="@mipmap/ic_launcher" />
</RelativeLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/values-night-v31/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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="AppTheme.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault.DayNight">
<item name="appWidgetRadius">@android:dimen/system_app_widget_background_radius</item>
<item name="appWidgetPadding">16dp</item>
<item name="appWidgetInnerRadius">@android:dimen/system_app_widget_inner_radius</item>
</style>
</resources>
13 changes: 13 additions & 0 deletions app/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<resources>

<style name="Widget.Traccarclientandroid.AppWidget.Container" parent="android:Widget">
<item name="android:padding">?attr/appWidgetPadding</item>
<item name="android:background">@drawable/app_widget_background</item>
</style>

<style name="Widget.Traccarclientandroid.AppWidget.InnerView" parent="android:Widget">
<item name="android:padding">?attr/appWidgetPadding</item>
<item name="android:background">@drawable/app_widget_inner_view_background</item>
</style>

</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values-v31/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<resources>

<style name="Widget.Traccarclientandroid.AppWidget.Container" parent="android:Widget">
<item name="android:clipToOutline">true</item>
</style>

</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-v31/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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="AppTheme.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault.DayNight">
<item name="appWidgetRadius">@android:dimen/system_app_widget_background_radius</item>
<item name="appWidgetPadding">16dp</item>
<item name="appWidgetInnerRadius">@android:dimen/system_app_widget_inner_radius</item>
</style>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -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>
6 changes: 6 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="spacing_normal">16dp</dimen>

<!--
Refer to App Widget Documentation for margin information
https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">0dp</dimen>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<string name="status_location_update">Location update</string>
<string name="status_network_online">Network online</string>
<string name="status_network_offline">Network offline</string>
<string name="status_widget_description">The widget shows the service status</string>
<string name="status_widget_label">Status widget</string>
<string name="hidden_app_name">Device Settings</string>
<string name="hidden_alert">The app has been hidden. To open it again please dial 8722227 (TRACCAR).</string>
<string name="error_msg_invalid_url">Please enter a valid http:// or https:// URL</string>
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<resources>

<style name="Widget.Traccarclientandroid.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.Traccarclientandroid.AppWidget.InnerView" parent="android:Widget">
<item name="android:background">?android:attr/colorBackground</item>
<item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@
<item name="colorAccent">@color/accent</item>
</style>

<style name="AppTheme.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault">
<item name="appWidgetRadius">8dp</item>
<item name="appWidgetPadding">4dp</item>
<item name="appWidgetInnerRadius">4dp</item>
</style>

<style name="AppTheme.AppWidgetContainer" parent="AppTheme.AppWidgetContainerParent" />

</resources>
13 changes: 13 additions & 0 deletions app/src/main/res/xml/status_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/status_widget_description"
android:initialKeyguardLayout="@layout/status_widget"
android:initialLayout="@layout/status_widget"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/status_widget_preview"
Anton-V-K marked this conversation as resolved.
Show resolved Hide resolved
android:previewLayout="@layout/status_widget"
android:resizeMode="none"
android:targetCellWidth="1"
android:targetCellHeight="1"
android:updatePeriodMillis="0" />