summaryrefslogtreecommitdiff
path: root/car-broadcastradio-support/src/com/android/car/broadcastradio/support/platform/ProgramInfoExt.java
blob: c96521a08ab26fe1f6a15e026405bf82a623edba (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
 * Copyright (C) 2018 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.broadcastradio.support.platform;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.Bitmap;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioManager.ProgramInfo;
import android.hardware.radio.RadioMetadata;
import android.media.MediaMetadata;
import android.media.Rating;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Comparator;
import java.util.Objects;

/**
 * Proposed extensions to android.hardware.radio.RadioManager.ProgramInfo.
 *
 * They might eventually get pushed to the framework.
 */
public class ProgramInfoExt {
    private static final String TAG = "BcRadioApp.pinfoext";

    /**
     * If there is no suitable program name, return null instead of doing
     * a fallback to channel display name.
     */
    public static final int NAME_NO_CHANNEL_FALLBACK = 1 << 16;

    /**
     * Flags to control how to fetch program name with {@link #getProgramName}.
     *
     * Lower 16 bits are reserved for {@link ProgramSelectorExt#NameFlag}.
     */
    @IntDef(prefix = { "NAME_" }, flag = true, value = {
        ProgramSelectorExt.NAME_NO_MODULATION,
        ProgramSelectorExt.NAME_MODULATION_ONLY,
        NAME_NO_CHANNEL_FALLBACK,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface NameFlag {}

    private static final char EN_DASH = '\u2013';
    private static final String TITLE_SEPARATOR = " " + EN_DASH + " ";

    private static final String[] PROGRAM_NAME_ORDER = new String[] {
        RadioMetadata.METADATA_KEY_PROGRAM_NAME,
        RadioMetadata.METADATA_KEY_DAB_COMPONENT_NAME,
        RadioMetadata.METADATA_KEY_DAB_SERVICE_NAME,
        RadioMetadata.METADATA_KEY_DAB_ENSEMBLE_NAME,
        RadioMetadata.METADATA_KEY_RDS_PS,
    };

    /**
     * Returns program name suitable to display.
     *
     * <p>If there is no program name, it falls back to channel name. Flags related to
     * the channel name display will be forwarded to the channel name generation method.
     *
     * @param info {@link ProgramInfo} to get name from
     * @param flags Fallback method
     * @param programNameOrder {@link RadioMetadata} metadata keys to pull from {@link ProgramInfo}
     * for the program name
     */
    @NonNull
    public static String getProgramName(@NonNull ProgramInfo info, @NameFlag int flags,
            @NonNull String[] programNameOrder) {
        Objects.requireNonNull(info, "info can not be null.");
        Objects.requireNonNull(programNameOrder, "programNameOrder can not be null");

        RadioMetadata meta = info.getMetadata();
        if (meta != null) {
            for (String key : programNameOrder) {
                String value = meta.getString(key);
                if (value != null) return value;
            }
        }

        if ((flags & NAME_NO_CHANNEL_FALLBACK) != 0) return "";

        ProgramSelector sel = info.getSelector();

        // if it's AM/FM program, prefer to display currently used AF frequency
        if (ProgramSelectorExt.isAmFmProgram(sel)) {
            ProgramSelector.Identifier phy = info.getPhysicallyTunedTo();
            if (phy != null && phy.getType() == ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY) {
                String chName = ProgramSelectorExt.formatAmFmFrequency(phy.getValue(), flags);
                if (chName != null) return chName;
            }
        }

        String selName = ProgramSelectorExt.getDisplayName(sel, flags);
        if (selName != null) return selName;

        Log.w(TAG, "ProgramInfo without a name nor channel name");
        return "";
    }

    /**
     * Returns program name suitable to display.
     *
     * <p>If there is no program name, it falls back to channel name. Flags related to
     * the channel name display will be forwarded to the channel name generation method.
     */
    @NonNull
    public static String getProgramName(@NonNull ProgramInfo info, @NameFlag int flags) {
        return getProgramName(info, flags, PROGRAM_NAME_ORDER);
    }

    /**
     * Proposed reimplementation of {@link RadioManager#ProgramInfo#getMetadata}.
     *
     * As opposed to the original implementation, it never returns null.
     */
    public static @NonNull RadioMetadata getMetadata(@NonNull ProgramInfo info) {
        RadioMetadata meta = info.getMetadata();
        if (meta != null) return meta;

        /* Creating new Metadata object on each get won't be necessary after we
         * push this code to the framework. */
        return (new RadioMetadata.Builder()).build();
    }

    /**
     * Converts {@link ProgramInfo} to {@link MediaMetadata} for displaying.
     *
     * <p>This method is meant to be used for displaying the currently playing station in
     *  {@link MediaSession}, only a subset of keys populated in {@link ProgramInfo#toMediaMetadata}
     *  will be populated in this method.
     *
     * <ul>
     * The following keys will be populated in the {@link MediaMetadata}:
     *  <li>{@link MediaMetadata#METADATA_KEY_DISPLAY_TITLE}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_DISPLAY_SUBTITLE}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_ALBUM_ART}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_USER_RATING}</li>
     * <ul/>
     *
     * @param info {@link ProgramInfo} to convert
     * @param isFavorite {@code true}, if a given program is a favorite
     * @param imageResolver metadata images resolver/cache
     * @param programNameOrder order of keys to look for program name in {@link ProgramInfo}
     * @return {@link MediaMetadata} object
     */
    @NonNull
    public static MediaMetadata toMediaDisplayMetadata(@NonNull ProgramInfo info,
            boolean isFavorite, @NonNull ImageResolver imageResolver,
            @NonNull String[] programNameOrder) {
        Objects.requireNonNull(info, "info can not be null.");
        Objects.requireNonNull(imageResolver, "imageResolver can not be null.");
        Objects.requireNonNull(programNameOrder, "programNameOrder can not be null.");

        MediaMetadata.Builder bld = new MediaMetadata.Builder();

        ProgramSelector selector;
        ProgramSelector.Identifier logicallyTunedTo = info.getLogicallyTunedTo();
        if (logicallyTunedTo != null && logicallyTunedTo.getType()
                == ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY) {
            selector = ProgramSelectorExt.createAmFmSelector(logicallyTunedTo.getValue());
        } else {
            selector = info.getSelector();
        }
        String displayTitle = ProgramSelectorExt.getDisplayName(selector, info.getChannel());
        bld.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, displayTitle);
        String subtitle = getProgramName(info, /* flags= */ 0, programNameOrder);
        bld.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, subtitle);

        Bitmap bm = resolveAlbumArtBitmap(info.getMetadata(), imageResolver);
        if (bm != null) bld.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bm);

        bld.putRating(MediaMetadata.METADATA_KEY_USER_RATING, Rating.newHeartRating(isFavorite));

        return bld.build();
    }

    /**
     * Converts {@link ProgramInfo} to {@link MediaMetadata}.
     *
     * <p>This method is meant to be used for currently playing station in {@link MediaSession}.
     *
     * <ul>
     * The following keys will be populated in the {@link MediaMetadata}:
     *  <li>{@link MediaMetadata#METADATA_KEY_DISPLAY_TITLE}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_TITLE}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_ARTIST}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_ALBUM}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_DISPLAY_SUBTITLE}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_ALBUM_ART}</li>
     *  <li>{@link MediaMetadata#METADATA_KEY_USER_RATING}</li>
     * <ul/>
     *
     * @param info {@link ProgramInfo} to convert
     * @param isFavorite {@code true}, if a given program is a favorite
     * @param imageResolver metadata images resolver/cache
     * @return {@link MediaMetadata} object
     */
    public static @NonNull MediaMetadata toMediaMetadata(@NonNull ProgramInfo info,
            boolean isFavorite, @Nullable ImageResolver imageResolver) {
        MediaMetadata.Builder bld = new MediaMetadata.Builder();

        bld.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, getProgramName(info, 0));

        RadioMetadata meta = info.getMetadata();
        if (meta != null) {
            String title = meta.getString(RadioMetadata.METADATA_KEY_TITLE);
            if (title != null) {
                bld.putString(MediaMetadata.METADATA_KEY_TITLE, title);
            }
            String artist = meta.getString(RadioMetadata.METADATA_KEY_ARTIST);
            if (artist != null) {
                bld.putString(MediaMetadata.METADATA_KEY_ARTIST, artist);
            }
            String album = meta.getString(RadioMetadata.METADATA_KEY_ALBUM);
            if (album != null) {
                bld.putString(MediaMetadata.METADATA_KEY_ALBUM, album);
            }
            if (title != null || artist != null) {
                String subtitle;
                if (title == null) {
                    subtitle = artist;
                } else if (artist == null) {
                    subtitle = title;
                } else {
                    subtitle = title + TITLE_SEPARATOR + artist;
                }
                bld.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, subtitle);
            }

            Bitmap bm = resolveAlbumArtBitmap(meta, imageResolver);
            if (bm != null) bld.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bm);
        }

        bld.putRating(MediaMetadata.METADATA_KEY_USER_RATING, Rating.newHeartRating(isFavorite));

        return bld.build();
    }

    private static Bitmap resolveAlbumArtBitmap(@NonNull RadioMetadata meta,
            @NonNull ImageResolver imageResolver) {
        long albumArtId = RadioMetadataExt.getGlobalBitmapId(meta, RadioMetadata.METADATA_KEY_ART);
        if (albumArtId != 0 && imageResolver != null) {
            return imageResolver.resolve(albumArtId);
        }
        return null;
    }
    public static class ProgramInfoComparator implements Comparator<RadioManager.ProgramInfo> {
        @Override
        public int compare(RadioManager.ProgramInfo info1, RadioManager.ProgramInfo info2) {
            Comparator<ProgramSelector> selectorComparator =
                    new ProgramSelectorExt.ProgramSelectorComparator();
            return selectorComparator.compare(info1.getSelector(), info2.getSelector());
        }
    }
}