-
-
Notifications
You must be signed in to change notification settings - Fork 672
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
Implementation of App.beep() for Android #2068
Conversation
@freakboy3742 Please review this PR |
FYI: rather than waiting for GitHub to run the pre-commit checks, you can set it up on your own machine as shown here. |
@mhsmith Thanks for the hint :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I run this on the Briefcase default emulator, I get:
D/Ringtone: Successfully created local player
E/FileSource: Failed to open file 'content://settings/system/notification_sound'. (No such file or directory)
E/AudioManager: hasHapticChannels failure:java.io.IOException: Failed to instantiate extractor.
This looks like it might be a limitation of the emulator - can you confirm whether you're seeing this as well?
From poking around looking for a quick fix for this, I also saw some suggestions that you should use MediaPlayer, rather than RingtoneManager to play sounds like this, because MediaPlayer will play over any existing sounds (e.g., music playing in the background), rather than interrupting audio.
examples/window/window/app.py
Outdated
self.scroller = toga.ScrollContainer( | ||
horizontal=False, | ||
vertical=True, | ||
style=Pack(flex=1, padding=10), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This puts the padding on the outside of the scroll container, so there's a border around the scrolling content. We don't need any padding there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removed padding.
examples/window/window/app.py
Outdated
style=Pack(flex=1, padding=10), | ||
) | ||
self.scroller.content = self.inner_box | ||
self.main_box.add(self.scroller) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The widget layout you've got here is: content -> main_box -> scroller -> inner_box -> *buttons
AFAICT, the main box isn't adding anything. You should be able to add the scroller directly as the main content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removed main_box and renamed scroller to main_scroller, so it becomes obvious that the scroller represents the main content.
This is strange. It works on my emulator and I had the code running on my phone as well (in pyPlayground). |
FWIW: It does beep on my physical Android device; it's only my emulator where it raises an error. Even there, the app doesn't crash - it's just a bit noisy in the logs. If we can establish that this is a quirk of (some) emulators, I can live with that; if we can catch the error and log something a little more helpful, that's even better. |
I think that interrupting existing sounds is what we want in this case: the user should get notified. If we play the beep over the running sound, it might go unnoticed by the user. |
@freakboy3742 Please re-review this PR. |
I have a few devices of various ages, so I can test this one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got the same "Failed to open file" error on the Briefcase default emulator with API level 31, and a Pixel 7 with API level 33. But in both cases, the sound still worked, so I think the message is harmless. It's not throwing an exception to us, so there's no way to suppress it.
It also works on an older device and emulator. However, I have noticed that emulators sometimes stop producing any sound at all. You can usually get it back by rebooting the emulator:
- Close the emulator.
- Open Android Studio and go to Tools > Device Manager.
- Open the ellipsis menu next to the emulator in question, and choose "Cold boot now".
The interaction with background music seems reasonable: the notification is clearly audible, but it doesn't interrupt the music any more than necessary.
So I'm fine with this, except for one thing:
android/src/toga_android/app.py
Outdated
uri_obj = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | ||
uri = java.cast(Uri, uri_obj) | ||
ring_obj = RingtoneManager.getRingtone(self.native.getApplicationContext(), uri) | ||
ring = java.cast(Ringtone, ring_obj) | ||
ring.play() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these cast
calls are necessary. Why did you add them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right: we already get the correct class instances. Somehow, I thought we get Objects back... I'll fix this.
Do you also have the error message on real devices? |
I got the error message on a real Pixel 7 with API level 33, but not on a Nexus 5X with API level 27. The sound worked on both devices. |
Yes, the problem with the error message seems to depend on the Android version. With API level 30, there is no error message. Starting with API level 31, the error message appears. The peep sound works on both API levels. |
@mhsmith |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good; after restarting the emulator as suggested by @mhsmith, I'm now getting a beep (or a twinkle, or whatever you want to call that sound :-).
Interestingly, I'm not getting any file errors any more - I get a warning about offset/length being adjusted:
W/FileSource: offset/length adjusted from 0/576460752303423487 to 0/16905
but the audio plays fine, so I'll call that a win.
I've renamed the changefile to a misc
- since the beep()
API was added in this release, it will already have a feature indicator (#2018); we don't need a separate feature for Android.
This PR implements App.beep() for Android
The "window" example has been extended to demo this feature.
PR Checklist: