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

Open
Anton-V-K opened this issue Jan 15, 2022 · 7 comments
Open

Status Widget #429

Anton-V-K opened this issue Jan 15, 2022 · 7 comments

Comments

@Anton-V-K
Copy link
Contributor

It would be handy to have a widget which could show the application status - whether its service running or not.
When the widget is clicked, the main window may be opened.

Is your feature request related to a problem? Please describe.
On some smartphones with too restrictive built-in task managers (Oukitel Y4800 with Android 9 is a typical example), the application can be closed (killed) by the system despite all optimization settings (ignoring battery optimization, white-listing, etc.).

Describe the solution you'd like
A possible solution (a kind of workaround) to keep the app alive is to add an updatable widget (to the home screen), which will let system know that the app shouldn't be killed (for power optimization reasons).

Additional context
Refer to the forum thread for more details.

@Anton-V-K
Copy link
Contributor Author

@tananaev , I had some free time to look into this feature implementation, and it doesn't seem to be sophisticated.
I've managed to generate new widget in Android Studio, and with minor code changes it works quite good.
The main concern is the place where I can update widget's state when the service is started/stopped.
I've added one line to onSharedPreferenceChanged (in MainFragment):

    override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
        if (key == KEY_STATUS) {
            if (sharedPreferences.getBoolean(KEY_STATUS, false)) {
                startTrackingService(checkPermission = true, initialPermission = false)
            } else {
                stopTrackingService()
            }
            (requireActivity().application as MainApplication).handleRatingFlow(requireActivity())
            StatusWidget.updateWidgets(requireContext()) // <<<
        } else if (key == KEY_DEVICE) {
            findPreference<Preference>(KEY_DEVICE)?.summary = sharedPreferences.getString(KEY_DEVICE, null)
        }
    }

Is this the right place?

I'll test my build on the device where the system kills the application to check whether the new status widget solves the problem.

@tananaev
Copy link
Member

Probably makes sense to update it somewhere inside startTrackingService.

@Anton-V-K
Copy link
Contributor Author

OK. Same update should also be performed from stopTrackingService.
I'm ready to commit my changes.
I'll create a new branch statius_widget, so my changes will be isolated until they are ready for merging into master.
The widget really solves the problem with keeping app alive, so the feature may be useful for others as well.

@Anton-V-K
Copy link
Contributor Author

while testing my build I've noticed that the widget isn't updated if the service started/stoped via the shortcut.
Probably the update should also be performed from ShortcutActivity.executeAction ?
I was also thinking about adding service-related broadcasts to the widget's intents filter, so the widget could be decoupled from the rest:

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

Unfortunately this approach doesn't seem to work on modern Androids (8.0+), probably because of Broadcast Limitations.

@Anton-V-K
Copy link
Contributor Author

I've managed to register for service-related intents in the code of StatusWidget:

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

Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Jan 23, 2022
@Anton-V-K Anton-V-K mentioned this issue Jan 23, 2022
11 tasks
@Anton-V-K Anton-V-K mentioned this issue Jan 30, 2022
13 tasks
Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Jan 31, 2022
Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Jan 31, 2022
@Anton-V-K
Copy link
Contributor Author

If anyone is willing to test the functionality until it is officially available, here is the test build TraccarClient-app-regular-debug-81.apk.zip
It has different package name and some icons are recolored, so the app can be installed and used side-by-side with the original one (the settings aren't shared between them as well).
This build had been under test on Android 9 (Oukitel Y4800) for a month, and I can confirm the app keeps running while you have the status widget on a home screen.

@buhao75
Copy link

buhao75 commented Jul 4, 2022

I have tested the widget and it seems to work. It would be cool if this feature would be available in the main release. I would suggest two optimizations:

  1. The status widget icon is quite similar to the Widget-Buttons for starting / stopping the service.
  2. When clicking on the status widget the app should open

Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Aug 1, 2023
Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Aug 23, 2023
Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Aug 24, 2023
Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Aug 24, 2023
…et with minor changes for traccar#429

# Resolved conflicts:
#	app/src/main/java/org/traccar/client/TrackingService.kt
Anton-V-K added a commit to Anton-V-K/traccar-client-android that referenced this issue Aug 24, 2023
tananaev pushed a commit that referenced this issue Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants