/*
 * 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.app.Notification;
import android.app.Notification.BigTextStyle;
import android.app.Notification.Builder;
import android.content.Context;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.offline.DownloadAction;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.Util;

/** Helper class to create notifications for downloads using {@link DownloadManager}. */
public final class DownloadNotificationUtil {

  private DownloadNotificationUtil() {}

  /**
   * Returns a notification for the given {@link DownloadState}, or null if no notification should
   * be displayed.
   *
   * @param downloadState State of the download.
   * @param context Used to access resources.
   * @param smallIcon A small icon for the notifications.
   * @param channelId The id of the notification channel to use. Only required for API level 26 and
   *     above.
   * @param errorMessageProvider An optional {@link ErrorMessageProvider} for translating download
   *     errors into readable error messages.
   * @return A notification for the given {@link DownloadState}, or null if no notification should
   *     be displayed.
   */
  public static @Nullable Notification createNotification(
      DownloadState downloadState,
      Context context,
      int smallIcon,
      String channelId,
      @Nullable ErrorMessageProvider<Throwable> errorMessageProvider) {
    DownloadAction downloadAction = downloadState.downloadAction;
    if (downloadAction.isRemoveAction() || downloadState.state == DownloadState.STATE_CANCELED) {
      return null;
    }

    Builder notificationBuilder = new Builder(context);
    if (Util.SDK_INT >= 26) {
      notificationBuilder.setChannelId(channelId);
    }
    notificationBuilder.setSmallIcon(smallIcon);

    int titleStringId = getTitleStringId(downloadState);
    notificationBuilder.setContentTitle(context.getResources().getString(titleStringId));

    if (downloadState.isRunning()) {
      notificationBuilder.setOngoing(true);
      float percentage = downloadState.downloadPercentage;
      boolean indeterminate = Float.isNaN(percentage);
      notificationBuilder.setProgress(100, indeterminate ? 0 : (int) percentage, indeterminate);
    }

    String message;
    if (downloadState.error != null && errorMessageProvider != null) {
      message = errorMessageProvider.getErrorMessage(downloadState.error).second;
    } else {
      message = downloadAction.getData();
    }

    if (Util.SDK_INT >= 16) {
      notificationBuilder.setStyle(new BigTextStyle().bigText(message));
    } else {
      notificationBuilder.setContentText(message);
    }
    return notificationBuilder.getNotification();
  }

  private static int getTitleStringId(DownloadState downloadState) {
    int titleStringId;
    switch (downloadState.state) {
      case DownloadState.STATE_WAITING:
        titleStringId = R.string.exo_download_queued;
        break;
      case DownloadState.STATE_STARTED:
      case DownloadState.STATE_STOPPING:
      case DownloadState.STATE_CANCELING:
        titleStringId = R.string.exo_downloading;
        break;
      case DownloadState.STATE_ENDED:
        titleStringId = R.string.exo_download_completed;
        break;
      case DownloadState.STATE_ERROR:
        titleStringId = R.string.exo_download_failed;
        break;
      case DownloadState.STATE_CANCELED:
      default:
        // Never happens.
        throw new IllegalStateException();
    }
    return titleStringId;
  }
}

@@ -856,69 +856,8 @@
 le;%0A
-import com.google.android.exoplayer2.offline.DownloadAction;%0A
 impo
@@ -1678,16 +1678,88 @@
  above.%0A
+   * @param message An optional message to display on the notification.%0A
    * @pa
@@ -1894,16 +1894,131 @@
 essages.
+ If not null and there is a download error then the%0A   *     error message is displayed instead of %7B@code message%7D.
 %0A   * @r
@@ -2300,16 +2300,48 @@
 nnelId,%0A
+      @Nullable String message,%0A
       @N
@@ -2412,40 +2412,12 @@
     
-DownloadAction downloadAction = 
+if (
 down
@@ -2444,32 +2444,8 @@
 tion
-;%0A    if (downloadAction
 .isR
@@ -2457,16 +2457,24 @@
 Action()
+%0A       
  %7C%7C down
@@ -3194,28 +3194,8 @@
  %7D%0A%0A
-    String message;%0A
     
@@ -3352,30 +3352,25 @@
    %7D
- else %7B
 %0A    
-  
+if (
 message 
 = do
@@ -3369,43 +3369,21 @@
 age 
+!
 = 
-downloadAction.getData();%0A    %7D%0A%0A
+null) %7B%0A  
     
@@ -3406,32 +3406,34 @@
  %3E= 16) %7B%0A      
+  
 notificationBuil
@@ -3487,16 +3487,18 @@
 ));%0A    
+  
 %7D else %7B
@@ -3498,16 +3498,18 @@
  else %7B%0A
+  
       no
@@ -3547,24 +3547,32 @@
 t(message);%0A
+      %7D%0A
     %7D%0A    re

Add message parameter to DownloadNotificationUtil.createNotification