content: package androidx.core.widget;
  
  import android.content.ClipData;
  import android.content.Context;
  import android.os.Build;
  import android.text.Editable;
  import android.text.Selection;
  import android.text.Spanned;
  import android.util.Log;
  import android.view.View;
  import android.widget.TextView;
  import androidx.core.view.ContentInfoCompat;
  import androidx.core.view.OnReceiveContentListener;
  
  public final class TextViewOnReceiveContentListener implements OnReceiveContentListener {
      private static final String LOG_TAG = "ReceiveContent";
  
      private static final class Api16Impl {
          private Api16Impl() {
          }
  
          static CharSequence coerce(Context context, ClipData.Item item, int flags) {
              if ((flags & 1) == 0) {
                  return item.coerceToStyledText(context);
              }
              CharSequence coerceToText = item.coerceToText(context);
              return coerceToText instanceof Spanned ? coerceToText.toString() : coerceToText;
          }
      }
  
      private static final class ApiImpl {
          private ApiImpl() {
          }
  
          static CharSequence coerce(Context context, ClipData.Item item, int flags) {
              CharSequence coerceToText = item.coerceToText(context);
              return ((flags & 1) == 0 || !(coerceToText instanceof Spanned)) ? coerceToText : coerceToText.toString();
          }
      }
  
      private static CharSequence coerceToText(Context context, ClipData.Item item, int flags) {
          return Build.VERSION.SDK_INT >= 16 ? Api16Impl.coerce(context, item, flags) : ApiImpl.coerce(context, item, flags);
      }
  
      private static void replaceSelection(Editable editable, CharSequence replacement) {
          int selectionStart = Selection.getSelectionStart(editable);
          int selectionEnd = Selection.getSelectionEnd(editable);
          int max = Math.max(0, Math.min(selectionStart, selectionEnd));
          int max2 = Math.max(0, Math.max(selectionStart, selectionEnd));
          Selection.setSelection(editable, max2);
          editable.replace(max, max2, replacement);
      }
  
      public ContentInfoCompat onReceiveContent(View view, ContentInfoCompat payload) {
          if (Log.isLoggable(LOG_TAG, 3)) {
              Log.d(LOG_TAG, "onReceive: " + payload);
          }
          if (payload.getSource() == 2) {
              return payload;
          }
          ClipData clip = payload.getClip();
          int flags = payload.getFlags();
          TextView textView = (TextView) view;
          Editable editable = (Editable) textView.getText();
          Context context = textView.getContext();
          boolean z = false;
          for (int i = 0; i < clip.getItemCount(); i++) {
              CharSequence coerceToText = coerceToText(context, clip.getItemAt(i), flags);
              if (coerceToText != null) {
                  if (!z) {
                      replaceSelection(editable, coerceToText);
                      z = true;
                  } else {
                      editable.insert(Selection.getSelectionEnd(editable), "\n");
                      editable.insert(Selection.getSelectionEnd(editable), coerceToText);
                  }
              }
          }
          return null;
      }
  }
  