aboutsummaryrefslogtreecommitdiff
path: root/TestMediaApp/src/com/android/car/media/testmediaapp/TmaBrowser.java
blob: f8d4b534d31df750eb22522e909b07e042318eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright (c) 2019, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.media.testmediaapp;

import static com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaBrowseNodeType.LEAF_CHILDREN;
import static com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaBrowseNodeType.QUEUE_ONLY;
import static com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaLoginEventOrder.PLAYBACK_STATE_UPDATE_FIRST;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.media.MediaBrowserCompat.MediaItem;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media.MediaBrowserServiceCompat;

import com.android.car.media.testmediaapp.loader.TmaLoader;
import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaAccountType;
import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaReplyDelay;
import com.android.car.media.testmediaapp.prefs.TmaPrefs;

import java.util.ArrayList;
import java.util.List;


/**
 * Implementation of {@link MediaBrowserServiceCompat} that delivers {@link MediaItem}s based on
 * json configuration files stored in the application's assets. Those assets combined with a few
 * preferences (see: {@link TmaPrefs}), allow to create a variety of use cases (including error
 * states) to stress test the Car Media application. <p/>
 * The media items are cached in the {@link TmaLibrary}, and can be virtually played with
 * {@link TmaPlayer}.
 */
public class TmaBrowser extends MediaBrowserServiceCompat {

    private static final String MEDIA_SESSION_TAG = "TEST_MEDIA_SESSION";
    private static final String ROOT_ID = "_ROOT_ID_";
    private static final String SEARCH_SUPPORTED = "android.media.browse.SEARCH_SUPPORTED";
    /**
     * Extras key to allow Android Auto to identify the browse service from the media session.
     */
    private static final String BROWSE_SERVICE_FOR_SESSION_KEY =
        "android.media.session.BROWSE_SERVICE";

    private TmaPrefs mPrefs;
    private Handler mHandler;
    private MediaSessionCompat mSession;
    private TmaLibrary mLibrary;
    private TmaPlayer mPlayer;

    private BrowserRoot mRoot;
    private String mLastLoadedNodeId;

    @Override
    public void onCreate() {
        super.onCreate();
        mPrefs = TmaPrefs.getInstance(this);
        mHandler = new Handler();
        mSession = new MediaSessionCompat(this, MEDIA_SESSION_TAG);
        setSessionToken(mSession.getSessionToken());

        mLibrary = new TmaLibrary(new TmaLoader(this));
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mPlayer = new TmaPlayer(this, mLibrary, audioManager, mHandler, mSession);

        mSession.setCallback(mPlayer);
        mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        Bundle mediaSessionExtras = new Bundle();
        mediaSessionExtras.putString(BROWSE_SERVICE_FOR_SESSION_KEY, TmaBrowser.class.getName());
        mSession.setExtras(mediaSessionExtras);

        mPrefs.mAccountType.registerChangeListener(
                (oldValue, newValue) -> onAccountChanged(newValue));

        mPrefs.mRootNodeType.registerChangeListener(
                (oldValue, newValue) -> invalidateRoot());

        mPrefs.mRootReplyDelay.registerChangeListener(
                (oldValue, newValue) -> invalidateRoot());

        Bundle browserRootExtras = new Bundle();
        browserRootExtras.putBoolean(SEARCH_SUPPORTED, true);
        mRoot = new BrowserRoot(ROOT_ID, browserRootExtras);

        updatePlaybackState(mPrefs.mAccountType.getValue());
    }

    @Override
    public void onDestroy() {
        mSession.release();
        mHandler = null;
        mPrefs = null;
        super.onDestroy();
    }

    private void onAccountChanged(TmaAccountType accountType) {
        if (PLAYBACK_STATE_UPDATE_FIRST.equals(mPrefs.mLoginEventOrder.getValue())) {
            updatePlaybackState(accountType);
            invalidateRoot();
        } else {
            invalidateRoot();
            (new Handler()).postDelayed(() -> {
                updatePlaybackState(accountType);
            }, 3000);
        }
    }

    private void updatePlaybackState(TmaAccountType accountType) {
        if (accountType == TmaAccountType.NONE) {
            mSession.setMetadata(null);
            mPlayer.onStop();
            mPlayer.setPlaybackState(
                    new TmaMediaEvent(TmaMediaEvent.EventState.ERROR,
                            TmaMediaEvent.StateErrorCode.AUTHENTICATION_EXPIRED,
                            getResources().getString(R.string.no_account),
                            getResources().getString(R.string.select_account),
                            TmaMediaEvent.ResolutionIntent.PREFS,
                            TmaMediaEvent.Action.NONE, 0, null));
        } else {
            // TODO don't reset error in all cases...
            PlaybackStateCompat.Builder playbackState = new PlaybackStateCompat.Builder();
            playbackState.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0);
            playbackState.setActions(PlaybackStateCompat.ACTION_PREPARE);
            mSession.setPlaybackState(playbackState.build());
        }
    }

    private void invalidateRoot() {
        notifyChildrenChanged(ROOT_ID);
    }

    @Override
    public BrowserRoot onGetRoot(
            @NonNull String clientPackageName, int clientUid, Bundle rootHints) {
        return mRoot;
    }

    @Override
    public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaItem>> result) {
        mLastLoadedNodeId = parentId;
        getMediaItemsWithDelay(parentId, result, null);

        if (QUEUE_ONLY.equals(mPrefs.mRootNodeType.getValue()) && ROOT_ID.equals(parentId)) {
            TmaMediaItem queue = mLibrary.getRoot(LEAF_CHILDREN);
            if (queue != null) {
                mSession.setQueue(queue.buildQueue());
                mPlayer.prepareMediaItem(queue.getPlayableByIndex(0));
            }
        }
    }

    @Override
    public void onSearch(final String query, final Bundle extras, Result<List<MediaItem>> result) {
        getMediaItemsWithDelay(mLastLoadedNodeId, result, query);
    }

    private void getMediaItemsWithDelay(@NonNull String parentId,
            @NonNull Result<List<MediaItem>> result, @Nullable String filter) {
        // TODO: allow per item override of the delay ?
        TmaReplyDelay delay = mPrefs.mRootReplyDelay.getValue();
        Runnable task = () -> {
            TmaMediaItem node;
            if (TmaAccountType.NONE.equals(mPrefs.mAccountType.getValue())) {
                node = null;
            } else if (ROOT_ID.equals(parentId)) {
                node = mLibrary.getRoot(mPrefs.mRootNodeType.getValue());
            } else {
                node = mLibrary.getMediaItemById(parentId);
            }

            if (node == null) {
                result.sendResult(null);
            } else {
                List<MediaItem> items = new ArrayList<>(node.mChildren.size());
                for (TmaMediaItem child : node.mChildren) {
                    MediaItem item = child.toMediaItem();
                    CharSequence title = item.getDescription().getTitle();
                    if (filter == null || (title != null && title.toString().contains(filter))) {
                        items.add(item);
                    }
                }
                result.sendResult(items);
            }
        };
        if (delay == TmaReplyDelay.NONE) {
            task.run();
        } else {
            result.detach();
            mHandler.postDelayed(task, delay.mReplyDelayMs);
        }
    }
}