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

[android] Implements exit button in navigation notification #7953

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kavikhalique
Copy link
Contributor

Fixes #7492
Implements Exit button in navigation notification

Use Case -
1 - If user clicks on exit navigation in background of app it simply stops navigation and closes the notification
2 - If user clicks the button while app is in foreground it works similar to pressing the stop button in bottom navigation of navigation screen

What I changed -
I added one button in notification and attached a pending intent with the button. Pending intent runs once the exit button gets clicked. It triggers cancel() method inside RoutingController.java class which basically stops the navigation which is going on. Then it triggers another method stopself() which closes the notification.

Screenshots-
fix2
fix1

Sample Video -

fix4.mp4

Signed-off-by: kavikhalique <kavikhalique3@gmail.com>

Implements Exit button in navigation notification

Signed-off-by: kavikhalique <kavikhalique3@gmail.com>

Implements Exit button in navigation notification 2

Signed-off-by: kavikhalique <kavikhalique3@gmail.com>
Copy link
Member

@biodranik biodranik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

final Intent exitIntent = new Intent(context, NavigationService.class);
exitIntent.setAction(STOP_NAVIGATION);
final PendingIntent exitPendingIntent = PendingIntent.getService(context, 0, exitIntent,
PendingIntent.FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning for selecting these two flags for intent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FLAG_UPDATE_CURRENT insures that there should exist only one pending intent doing same task at any moment. If there is already an existing pendingIntent which is doing same task then rather than creating new pendingIntent it updates the previous one with new one.

Whereas FLAG_IMMUTABLE is used for, if by any reason pendingIntent is cant be overwritten then it creates new one.

Co-authored-by: Alexander Borsuk <170263+biodranik@users.noreply.github.com>
Signed-off-by: kavi khalique <120750626+kavikhalique@users.noreply.github.com>
@RedAuburn
Copy link
Sponsor Member

Tried it out and found an issue:

  1. start routing with vehicle mode
  2. move app to background
  3. press exit button in notification
  4. re-open app

Result:
routing has stopped correctly, but the vehicle map style is still used. (it should be the default clear style)

@kavikhalique
Copy link
Contributor Author

Tried it out and found an issue:

  1. start routing with vehicle mode

  2. move app to background

  3. press exit button in notification

  4. re-open app

Result:

routing has stopped correctly, but the vehicle map style is still used. (it should be the default clear style)

Thanks, I missed that. Will correct it soon.

@Jean-BaptisteC
Copy link
Member

You can use same method used on stop button in navigation bottom bar

@kavikhalique
Copy link
Contributor Author

You can use same method used on stop button in navigation bottom bar

Yes sir, but the issue is that there is a container class which contains all the methods and in background it becomes null because it has been defined inside mwmActivity.java class.

I was thinking to create a shared preference variable which will be set to true if notification will be exited from background and then whenever activity will resume in foreground it will check the value of shared preference and if it will be true it will trigger the stop button method of navigationController.

@biodranik
Copy link
Member

Is it possible to call the same methods that are called by pressing the stop button in the nav panel?

@kavikhalique
Copy link
Contributor Author

Is it possible to call the same methods that are called by pressing the stop button in the nav panel?

Yes sir, I am calling the same method but whenever that method is triggered from background of navigation activity it does not updates the map activity to default.

Trying to figure it out where things are actually changing.

@kavikhalique
Copy link
Contributor Author

kavikhalique commented Apr 19, 2024

Fixed the issue of UI updation after exiting navigation from background of app.

What i did -

Used Shared preferences to check if app is in background or foreground and accordingly called methods after coming in foreground which were not available when app was in background.

Please review it - @Jean-BaptisteC @RedAuburn

@kavikhalique
Copy link
Contributor Author

Please sir review my changes and if there is any changes required please inform me. @rtsisyk @biodranik

Copy link
Member

@biodranik biodranik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the only/cleanest way to fix the issue?

@kavikhalique
Copy link
Contributor Author

Is it the only/cleanest way to fix the issue?

I have gone through OSMAnd's code base for exit button they have created a broadcast receiver and they have a separate class which determines the mode of application and then according to foreground or background mode actions are taken.

Since I didn't found any such class to determine the mode of app i used shared preference.

I feel better and cleaner ways exist.

@kavikhalique
Copy link
Contributor Author

Sir please review it and if there is any suggestions please disscuss so that i can correct it @rtsisyk

@rtsisyk
Copy link
Contributor

rtsisyk commented Apr 24, 2024

Sir please review it and if there is any suggestions please disscuss so that i can correct it @rtsisyk

I will take a look. Sorry for the delay!!

Copy link
Contributor

@rtsisyk rtsisyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution, and sorry for the delay with the code review. 🙏

Could you please evaluate the idea of routing STOP_NAVIGATION intent via MwmActivity to eliminate the need of using IS_CANCELLED_FROM_BACKGROUND flag in Preferences (see a comment inline)? I think it would make the code even much simpler.

@@ -2186,4 +2190,19 @@ public void onTrimMemory(int level)
if (level >= TRIM_MEMORY_RUNNING_LOW)
Framework.nativeMemoryWarning();
}

public void onCancelledFromBackground(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about routing STOP_NAVIGATION intent via MwmActiviy instead of sending it directly to NavigationService? The intent will appear in MwmActiviy.onNewIntent(), where it will be possible to stop the NavigationService and restore the UI of MwmActivity. It should work regardless of the current foreground/background state of MwmActivity. This approach eliminates the need for IS_CANCELLED_FROM_BACKGROUND flag stored in Preferences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the guidance, idea seems perfect will work upon it.

@rtsisyk rtsisyk self-assigned this Apr 27, 2024
@kavikhalique
Copy link
Contributor Author

kavikhalique commented Apr 29, 2024

Could you please evaluate the idea of routing STOP_NAVIGATION intent via MwmActivity to eliminate the need of using IS_CANCELLED_FROM_BACKGROUND flag in Preferences (see a comment inline)? I think it would make the code even much simpler.

I dont know why this idea didn't strike in my mind. Your idea is much better and clean will modify the code accordingly and will update you ASAP.

The only doubt i have is, if we will send intent to activity class it will open up the activity in foreground (means it will open the app)

I will check this out once and then will update you

@kavikhalique
Copy link
Contributor Author

I have tried that but the issue is with UX.

If we will route STOP_NAVIGATION through mwmActivity it opens the app in foreground.

But if the navigation is cancelled from background it should not bring the app in foreground. It should just close the notification.
@rtsisyk

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

Successfully merging this pull request may close these issues.

Exit navigation button in expanded notification widget
5 participants