Bug 59421 - AccessibilityManager.AddAccessibilityStateChangeListener throws invalid cast exception
Summary: AccessibilityManager.AddAccessibilityStateChangeListener throws invalid cast ...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 7.4 (15.3)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2017-09-12 14:33 UTC by marcin.klimczak
Modified: 2017-09-14 08:19 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description marcin.klimczak 2017-09-12 14:33:25 UTC
Android documentation about "AccessibilityStateChangeListenerCompat" abstract class says

This class was deprecated in API level 24.2.0.
Use AccessibilityManager.AccessibilityStateChangeListener directly instead of this listener.

AddAccessibilityStateChangeListener is expecting object which implements IAccessibilityStateChangeListener.
When I provided such object method AddAccessibilityStateChangeListener throws invalid cast exception.

After some fiddling I have found that when my listener inherits from AccessibilityStateChangeListenerCompat the method AddAccessibilityStateChangeListener start to work as expected. This behaviour happens on Android M.


In summary:
providing that kind of listener gives error:
    public class MyCustomListener: IAccessibilityStateChangeListener

providing that kind of listener is working ok
#pragma warning disable 618
    public class MyCustomListener: AccessibilityManagerCompat.AccessibilityStateChangeListenerCompat, IAccessibilityStateChangeListener
#pragma warning restore 618

So either this is a bug in Android itself or there is something missing in xamarin.android.
This happens both on API 23 and API 25
Comment 1 Jon Douglas [MSFT] 2017-09-12 16:18:21 UTC
Hi Marcin,

Can you please provide a sample project demonstrating this behavior? I believe what you need is the following Interface:

AccessibilityManager.IAccessibilityStateChangeListener

Thus it should look something like this:

    public class MyListener : Java.Lang.Object, AccessibilityManager.IAccessibilityStateChangeListener
    {
        public void OnAccessibilityStateChanged(bool enabled)
        {
            throw new NotImplementedException();
        }
    }

Please also provide any error messages/exceptions so we can see what's going on. Feel free to upload these directly to the bug report and I will investigate further once we have a sample project. Thank you in advance for the report!
Comment 2 marcin.klimczak 2017-09-13 07:45:39 UTC
Doing it your way works perfectly. On the other hand documentation and compiler gives 0 clues what can be the problem.
Comment 3 Jon Douglas [MSFT] 2017-09-13 14:55:21 UTC
(In reply to marcin.klimczak from comment #2)
> Doing it your way works perfectly. On the other hand documentation and
> compiler gives 0 clues what can be the problem.

We have added a helpful "fail faster" approach to this problem that you can view here: https://bugzilla.xamarin.com/show_bug.cgi?id=56819

Unfortunately it will not be included until 15.5. Hopefully this will help out your specific issue to let you know in the future when this build becomes available.
Comment 4 marcin.klimczak 2017-09-14 08:19:06 UTC
It's great that you have fixed it. Googling the issue wasn't giving any meaningfull results. Those small issues are having big impact on how developers perceive xamarin as a whole. People might think that xamarin is broken, where in fact it's only misunderstanding.