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

Merged
merged 2 commits into from
May 25, 2024

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

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!

@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

@kavikhalique kavikhalique requested a review from rtsisyk May 24, 2024 21:50
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 reworking this PR. Simplicity the key to brilliance.

I am ready to merge this PR. Could you please fix two minor issues before merging?

  1. Remove unused constants and imports from NavigationService,java
  2. Please squash all commits into one:

git rebase -i HEAD~6 # six commits
change "pick" to "squash" to everything except the first commit
reword the first commit
push it back to the repo (git push origin exit-button-navigation:exit-button-navigation -f)

private static final String STOP_NAVIGATION = "STOP_NAVIGATION";
private static final String NAVIGATION = "NAVIGATION";
private static final String IS_APP_IN_BACKGROUND = "isAppInBackground";
private static final String IS_CANCELLED_FROM_BACKGROUND = "isCancelledFromBackground";
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please remove unused constants?

@kavikhalique kavikhalique force-pushed the exit-button-navigation branch 3 times, most recently from 0db2c53 to 71271da Compare May 25, 2024 17:21
{
RoutingController.get().cancel();
stopSelf();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the late comment, but it looks like that we forgot return START_NOT_STICKY; // The service will be stopped by stopSelf(). here. Please 6 lines below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -184,6 +191,14 @@ public void onLowMemory()
@Override
public int onStartCommand(@NonNull Intent intent, int flags, int startId)
{
final String action = intent.getAction();
if (action != null && action.equals(STOP_NAVIGATION))
{
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: should be on the previous line..

@rtsisyk
Copy link
Contributor

rtsisyk commented May 25, 2024

Thanks for your patience!!

@rtsisyk rtsisyk merged commit b3cccaf into organicmaps:master May 25, 2024
6 checks passed
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