AndroidX icon indicating copy to clipboard operation
AndroidX copied to clipboard

AndroidX.Media.MediaBrowserServiceCompact.BrowserRoot not working properly.

Open Jack-Paul opened this issue 3 years ago • 10 comments

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

monoandroid 11

Description

Hi Team,

I am trying to implement this, in my xamarin forms application https://developer.android.com/training/cars/media#display_search

public override BrowserRoot OnGetRoot(string clientPackageName, int clientUid, Bundle rootHints)
        {
            Bundle extras = new Bundle();
            extras.PutBoolean(
                MediaConstants.BrowserServiceExtrasKeySearchSupported, true);
            extras.PutInt(
        MediaConstants.DescriptionExtrasKeyContentStyleGroupTitle,
        MediaConstants.DescriptionExtrasValueContentStyleCategoryGridItem);
            extras.PutInt(
                MediaConstants.DescriptionExtrasKeyContentStyleBrowsable,
                MediaConstants.DescriptionExtrasValueContentStyleListItem);
            extras.PutInt(
                MediaConstants.DescriptionExtrasKeyContentStylePlayable,
                MediaConstants.DescriptionExtrasValueContentStyleGridItem);


            return new BrowserRoot(MEDIA_ID_ROOT, extras);
        }

as you can see i have correctly implemented MediaConstants.BrowserServiceExtrasKeySearchSupported

but the search bar doesn't appear on the android auto

I have checked with Andriod targetting version 10, 11 but the issue is not resolved.

image

please share a solution or workaround for this issue.

Thanks.

Steps to Reproduce

follow the same but implement in xamarin c# https://developer.android.com/training/cars/media

Did you find any workaround?

No response

Relevant log output

No response

Jack-Paul avatar Jul 04 '22 02:07 Jack-Paul

latest AndroidX libs require Targetframework 12.0, so target for 12.0 and API 31 and look what happens.

MagicAndre1981 avatar Jul 04 '22 05:07 MagicAndre1981

Hi @MagicAndre1981 Still doesn't work after setting everything to Targetframework 12.0 and API 31 image and every nuget package refer to AndroidX is up to date image Any other solution for this issue?

Thanks, Jack Paul

Jack-Paul avatar Jul 04 '22 08:07 Jack-Paul

Xamarin.AndroidX.AppCompat.Resources has issues. See the yellow warning.

image

Remove it and try again.

MagicAndre1981 avatar Jul 04 '22 11:07 MagicAndre1981

Hi @MagicAndre1981 ,

I have removed this package and rebuilt the project once again and tried again, still facing the same issue.

in my application now there are no deprecated nugets, everything is set to the latest version as well.

any other solution?

Thanks, Jackson Paul T

Jack-Paul avatar Jul 04 '22 12:07 Jack-Paul

Ultimately, all Xamarin.Android does is provide generated wrappers that allow you to call Google’s AndroidX API. We do not have much expertise in how to use those API’s properly.

You will likely need to use the AndroidX API documentation and relevant forums to determine how to write code for your desired outcome.

jpobst avatar Jul 05 '22 13:07 jpobst

Hi @jpobst ,

As I already told I have implemented just as they mentioned in this documentation https://developer.android.com/training/cars/media#display_search In the above link they mentioned code on Java, all I just did is converting to c# code, so as per your suggestion I have used correct api documentation and implementated correctly.

I know xamarin.android is just wrapper code, but I guess xamarin team should have done testing on all the api available, if possible can anyone share the working sample or correct implementation of this, since I couldn't make it working even after implementing correctly.

Jack-Paul avatar Jul 05 '22 14:07 Jack-Paul

does it only happen on emulator? Can you test it on your car? What about removing the other code and only set search. Does this help? Do you also implement OnSearch?

MagicAndre1981 avatar Jul 05 '22 17:07 MagicAndre1981

Hi @MagicAndre1981,

It happens in both emulators and a real car, please refer to the below image, as you can see no search bar on the top right as it should be available. image when i implement only with this extra, still getting the same issue

public override BrowserRoot OnGetRoot(string clientPackageName, int clientUid, Bundle rootHints)
        {
            Bundle extras = new Bundle();
            extras.PutBoolean(
                MediaConstants.BrowserServiceExtrasKeySearchSupported, true);

            return new BrowserRoot(MEDIA_ID_ROOT, extras);
        }

I did implement OnSearch Method here is the code I have used

public override async void OnSearch(string query, Bundle extras, Result result)
        {
            base.OnSearch(query, extras, result);
            result.Detach();
            JavaList<MediaItem> searchResponse = new JavaList<MediaItem>();
            bool succeeded = false;
            Task t = new Task(async () =>
            {
                if (await doSearch(query, extras, searchResponse))
                {
                    succeeded = true;
                }
            });
            t.Start();
            await t.ContinueWith(x =>
            {
                if (succeeded)
                {
                    // Sending an empty List informs the caller that there were no results.
                    result.SendResult(searchResponse);
                }
                else
                {
                    // This invokes onError() on the search callback
                    result.SendResult(null);
                }
            });
        }
        async Task<bool> doSearch(String query, Bundle extras, JavaList<MediaItem> resultsToFill)
        {
            // Implement this method
            SearchService s = new SearchService();
            try
            {
                List<Song> songs = await s.SearchSongAsync(query);

                int order = 0;
                if (songs != null)
                {
                    foreach (var song in songs)
                    {
                        if (song.ImageLocation == null)
                        {
                            song.ImageLocation = StaticResource.DefaultImg;
                        }
                        MediaDescriptionCompat desc = new MediaDescriptionCompat.Builder()
                        .SetDescription(song.Description)
                        .SetMediaId(song.SongName)
                        .SetIconUri(Android.Net.Uri.Parse(song.ImageLocation))

                        .SetTitle(song.SongName).Build();
                        songs[order].SongOrderNumber = order;
                        order += 1;
                        MediaItem item = new MediaItem(desc, (int)Android.Media.Browse.MediaItemFlags.Playable);
                        resultsToFill.Add(item);
                    }
                    CarPlayService.CurrentQueue = songs;
                }

                return true;
            }
            catch
            {

            }
            return false;
        }

but the thing is in debug mode I have set a breakpoint inside OnSearch method when I execute the program, OnSearch method is not invoked at all, breakpoint not hitting.

Is there any other information needed from your end to validate this issue or any other solutions to try?

Regards, Jack Paul

Jack-Paul avatar Jul 07 '22 07:07 Jack-Paul

If there is a sample app from google, open it in Android Studio and try to debug there which functions get called in which order. Maybe this helps to track down which functions are not called in your Xamarin solution.

MagicAndre1981 avatar Jul 07 '22 07:07 MagicAndre1981

@Jack-Paul

Thanks for the feedback and as Jonathan stated we are no experts in API itself.

What I usually do (before porting sample to Xamarin/.NET) is what @MagicAndre1981 suggested - I search for minimal java/kotlin sample and check if it works and then start porting code.

Can you provide minimal repro sample, so I can dive in, please?

moljac avatar Jul 08 '22 06:07 moljac

@Jack-Paul I'm trying to add Android Auto to my Xamarin.Forms application and can't find any sample. Are you able to share a sample of how you implemented Android Auto or maybe the sample that you used to add Android Auto to your app?

Thank you

igorsky78 avatar May 30 '23 18:05 igorsky78

Closing as this seems to an Android API usage issue which we are unable to provide support for.

jpobst avatar Mar 14 '24 20:03 jpobst