/*
 * 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.google.android.exoplayer2.ui;

import android.content.res.Resources;
import android.text.TextUtils;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;

import java.util.Locale;

/** A default {@link TrackNameProvider}. */
public class DefaultTrackNameProvider implements TrackNameProvider {

  private final Resources resources;

  /** @param resources Resources from which to obtain strings. */
  public DefaultTrackNameProvider(Resources resources) {
    this.resources = Assertions.checkNotNull(resources);
  }

  @Override
  public String getTrackName(Format format) {
    String trackName;
    if (MimeTypes.isVideo(format.sampleMimeType)) {
      trackName = joinWithSeparator(buildResolutionString(format), buildBitrateString(format));
    } else if (MimeTypes.isAudio(format.sampleMimeType)) {
      trackName =
          joinWithSeparator(
              buildLanguageString(format),
              buildAudioChannelString(format),
              buildBitrateString(format));
    } else {
      trackName = buildLanguageString(format);
    }
    return trackName.length() == 0 ? resources.getString(R.string.exo_track_unknown) : trackName;
  }

  private String buildResolutionString(Format format) {
    int width = format.width;
    int height = format.height;
    return width == Format.NO_VALUE || height == Format.NO_VALUE
        ? ""
        : resources.getString(R.string.exo_track_resolution, width, height);
  }

  private String buildBitrateString(Format format) {
    int bitrate = format.bitrate;
    return bitrate == Format.NO_VALUE
        ? ""
        : resources.getString(R.string.exo_track_bitrate, bitrate / 1000000f);
  }

  private String buildAudioChannelString(Format format) {
    int channelCount = format.channelCount;
    if (channelCount == Format.NO_VALUE || channelCount < 1) {
      return "";
    }
    switch (channelCount) {
      case 1:
        return resources.getString(R.string.exo_track_mono);
      case 2:
        return resources.getString(R.string.exo_track_stereo);
      case 6:
      case 7:
        return resources.getString(R.string.exo_track_surround_5_point_1);
      case 8:
        return resources.getString(R.string.exo_track_surround_7_point_1);
      default:
        return resources.getString(R.string.exo_track_surround);
    }
  }

  private String buildLanguageString(Format format) {
    String language = format.language;
    return TextUtils.isEmpty(language) || C.LANGUAGE_UNDETERMINED.equals(language)
        ? ""
        : buildLanguageString(language);
  }

  private String buildLanguageString(String language) {
    Locale locale = Util.SDK_INT >= 21 ? Locale.forLanguageTag(language) : new Locale(language);
    return locale.getDisplayLanguage();
  }

  private String joinWithSeparator(String... items) {
    String itemList = "";
    for (String item : items) {
      if (item.length() > 0) {
        if (TextUtils.isEmpty(itemList)) {
          itemList = item;
        } else {
          itemList = resources.getString(R.string.exo_item_list, itemList, item);
        }
      }
    }
    return itemList;
  }
}

@@ -965,17 +965,16 @@
 l.Util;%0A
-%0A
 import j
@@ -1417,51 +1417,93 @@
    i
-f (MimeTypes.isVideo(format.sampleMimeType)
+nt trackType = inferPrimaryTrackType(format);%0A    if (trackType == C.TRACK_TYPE_VIDEO
 ) %7B%0A
@@ -1617,48 +1617,39 @@
 if (
-MimeTypes.isAudio(format.sampleMimeType)
+trackType == C.TRACK_TYPE_AUDIO
 ) %7B%0A
@@ -3941,11 +3941,708 @@
 ist;%0A  %7D
+%0A%0A  private static int inferPrimaryTrackType(Format format) %7B%0A    int trackType = MimeTypes.getTrackType(format.sampleMimeType);%0A    if (trackType != C.TRACK_TYPE_UNKNOWN) %7B%0A      return trackType;%0A    %7D%0A    if (MimeTypes.getVideoMediaMimeType(format.codecs) != null) %7B%0A      return C.TRACK_TYPE_VIDEO;%0A    %7D%0A    if (MimeTypes.getAudioMediaMimeType(format.codecs) != null) %7B%0A      return C.TRACK_TYPE_AUDIO;%0A    %7D%0A    if (format.width != Format.NO_VALUE %7C%7C format.height != Format.NO_VALUE) %7B%0A      return C.TRACK_TYPE_VIDEO;%0A    %7D%0A    if (format.channelCount != Format.NO_VALUE %7C%7C format.sampleRate != Format.NO_VALUE) %7B%0A      return C.TRACK_TYPE_AUDIO;%0A    %7D%0A    return C.TRACK_TYPE_UNKNOWN;%0A  %7D
 %0A%7D%0A

Improve track naming when sampleMimeType unset