<commit_before>﻿using System;
using System.Threading.Tasks;
using Android.Content;
using Android.Media;
using Android.Runtime;
using Android.Support.V4.Media.Session;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;
using Com.Google.Android.Exoplayer2.Source;
using Com.Google.Android.Exoplayer2.Source.Dash;
using Com.Google.Android.Exoplayer2.Source.Smoothstreaming;
using Com.Google.Android.Exoplayer2.Trackselection;
using Com.Google.Android.Exoplayer2.Upstream;
using Com.Google.Android.Exoplayer2.Util;
using MediaManager.Audio;
using MediaManager.Media;
using MediaManager.Platforms.Android.Audio;
using MediaManager.Platforms.Android.Playback;
using MediaManager.Playback;
using MediaManager.Queue;
using MediaManager.Video;

namespace MediaManager.Platforms.Android.Media
{
    public abstract class MediaPlayer : Java.Lang.Object, IAudioPlayer, IVideoPlayer, IExoPlayerImplementation
    {
        public MediaPlayer()
        {
        }

        public MediaPlayer(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {
        }

        protected virtual int AudioAttributesContentType => (int)AudioContentType.Music;
        protected virtual int AudioAttributesUsage => (int)AudioUsageKind.Media;

        protected INotifyMediaManager MediaManager = CrossMediaManager.Current as INotifyMediaManager;

        protected Context Context { get; set; } = CrossMediaManager.Current.GetContext();

        protected string UserAgent { get; set; }
        protected DefaultHttpDataSourceFactory HttpDataSourceFactory { get; set; }
        public static DefaultDataSourceFactory DefaultDataSourceFactory { get; set; }
        public static DefaultDashChunkSource.Factory DefaultDashChunkSource { get; set; }
        public static DefaultSsChunkSource.Factory DefaultSsChunkSource { get; set; }

        protected DefaultBandwidthMeter BandwidthMeter { get; set; }
        protected AdaptiveTrackSelection.Factory TrackSelectionFactory { get; set; }
        protected DefaultTrackSelector TrackSelector { get; set; }
        protected PlaybackController PlaybackController { get; set; }

        protected MediaSessionConnector MediaSessionConnector { get; set; }
        protected QueueNavigator QueueNavigator { get; set; }
        protected ConcatenatingMediaSource MediaSource { get; set; }
        protected QueueDataAdapter QueueDataAdapter { get; set; }
        protected MediaSourceFactory MediaSourceFactory { get; set; }
        protected TimelineQueueEditor TimelineQueueEditor { get; set; }
        protected MediaSessionConnectorPlaybackPreparer PlaybackPreparer { get; set; }
        protected PlayerEventListener PlayerEventListener { get; set; }
        protected RatingCallback RatingCallback { get; set; }

        //TODO: Remove with Exoplayer 2.9.0
        internal AudioFocusManager AudioFocusManager { get; set; }

        public SimpleExoPlayer Player { get; set; }
        public MediaSessionCompat MediaSession { get; set; }

        public MediaPlayerState State
        {
            get
            {
                if (!MediaSession.Active)
                    return MediaPlayerState.Stopped;

                return MediaSession.Controller.PlaybackState.ToMediaPlayerState();
            }
        }

        public TimeSpan Position => TimeSpan.FromTicks(Player.CurrentPosition);

        public TimeSpan Duration => TimeSpan.FromTicks(Player.Duration);

        public TimeSpan Buffered => TimeSpan.FromTicks(Player.BufferedPosition);

        public event BeforePlayingEventHandler BeforePlaying;
        public event AfterPlayingEventHandler AfterPlaying;

        public virtual void Initialize()
        {
            if (Player != null)
                return;

            if (MediaSession == null)
                throw new ArgumentNullException(nameof(MediaSession));

            UserAgent = Util.GetUserAgent(Context, "MediaManager");
            HttpDataSourceFactory = new DefaultHttpDataSourceFactory(UserAgent);

            var requestHeaders = CrossMediaManager.Current.RequestHeaders;
            if (requestHeaders?.Count > 0)
            {
                foreach (var item in requestHeaders)
                {
                    HttpDataSourceFactory.DefaultRequestProperties.Set(item.Key, item.Value);
                }
            }

            DefaultDataSourceFactory = new DefaultDataSourceFactory(Context, null, HttpDataSourceFactory);
            DefaultDashChunkSource = new DefaultDashChunkSource.Factory(DefaultDataSourceFactory);
            DefaultSsChunkSource = new DefaultSsChunkSource.Factory(DefaultDataSourceFactory);<commit_msg>Set user agent from request headers<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.Content;
using Android.Media;
using Android.Runtime;
using Android.Support.V4.Media.Session;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;
using Com.Google.Android.Exoplayer2.Source;
using Com.Google.Android.Exoplayer2.Source.Dash;
using Com.Google.Android.Exoplayer2.Source.Smoothstreaming;
using Com.Google.Android.Exoplayer2.Trackselection;
using Com.Google.Android.Exoplayer2.Upstream;
using Com.Google.Android.Exoplayer2.Util;
using MediaManager.Audio;
using MediaManager.Media;
using MediaManager.Platforms.Android.Audio;
using MediaManager.Platforms.Android.Playback;
using MediaManager.Playback;
using MediaManager.Queue;
using MediaManager.Video;

namespace MediaManager.Platforms.Android.Media
{
    public abstract class MediaPlayer : Java.Lang.Object, IAudioPlayer, IVideoPlayer, IExoPlayerImplementation
    {
        public MediaPlayer()
        {
        }

        public MediaPlayer(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {
        }

        protected virtual int AudioAttributesContentType => (int)AudioContentType.Music;
        protected virtual int AudioAttributesUsage => (int)AudioUsageKind.Media;

        protected INotifyMediaManager MediaManager = CrossMediaManager.Current as INotifyMediaManager;
        protected Dictionary<string, string> RequestHeaders => MediaManager.RequestHeaders;

        protected Context Context { get; set; } = CrossMediaManager.Current.GetContext();

        protected string UserAgent { get; set; }
        protected DefaultHttpDataSourceFactory HttpDataSourceFactory { get; set; }
        public static DefaultDataSourceFactory DefaultDataSourceFactory { get; set; }
        public static DefaultDashChunkSource.Factory DefaultDashChunkSource { get; set; }
        public static DefaultSsChunkSource.Factory DefaultSsChunkSource { get; set; }

        protected DefaultBandwidthMeter BandwidthMeter { get; set; }
        protected AdaptiveTrackSelection.Factory TrackSelectionFactory { get; set; }
        protected DefaultTrackSelector TrackSelector { get; set; }
        protected PlaybackController PlaybackController { get; set; }

        protected MediaSessionConnector MediaSessionConnector { get; set; }
        protected QueueNavigator QueueNavigator { get; set; }
        protected ConcatenatingMediaSource MediaSource { get; set; }
        protected QueueDataAdapter QueueDataAdapter { get; set; }
        protected MediaSourceFactory MediaSourceFactory { get; set; }
        protected TimelineQueueEditor TimelineQueueEditor { get; set; }
        protected MediaSessionConnectorPlaybackPreparer PlaybackPreparer { get; set; }
        protected PlayerEventListener PlayerEventListener { get; set; }
        protected RatingCallback RatingCallback { get; set; }

        //TODO: Remove with Exoplayer 2.9.0
        internal AudioFocusManager AudioFocusManager { get; set; }

        public SimpleExoPlayer Player { get; set; }
        public MediaSessionCompat MediaSession { get; set; }

        public MediaPlayerState State
        {
            get
            {
                if (!MediaSession.Active)
                    return MediaPlayerState.Stopped;

                return MediaSession.Controller.PlaybackState.ToMediaPlayerState();
            }
        }

        public TimeSpan Position => TimeSpan.FromTicks(Player.CurrentPosition);

        public TimeSpan Duration => TimeSpan.FromTicks(Player.Duration);

        public TimeSpan Buffered => TimeSpan.FromTicks(Player.BufferedPosition);

        public event BeforePlayingEventHandler BeforePlaying;
        public event AfterPlayingEventHandler AfterPlaying;

        public virtual void Initialize()
        {
            if (Player != null)
                return;

            if (MediaSession == null)
                throw new ArgumentNullException(nameof(MediaSession));

            if (RequestHeaders?.Count > 0 && RequestHeaders.TryGetValue("User-Agent", out string userAgent))
                UserAgent = userAgent;
            else
                UserAgent = Util.GetUserAgent(Context, "MediaManager");

            HttpDataSourceFactory = new DefaultHttpDataSourceFactory(UserAgent);

            if (RequestHeaders?.Count > 0)
            {
                foreach (var item in RequestHeaders)
                {
                    HttpDataSourceFactory.DefaultRequestProperties.Set(item.Key, item.Value);
                }
            }

            DefaultDataSourceFactory = new DefaultDataSourceFactory(Context, null, HttpDataSourceFactory);
            DefaultDashChunkSource = new DefaultDashChunkSource.Factory(DefaultDataSourceFactory);
            DefaultSsChunkSource = new DefaultSsChunkSource.Factory(DefaultDataSourceFactory);
{'fraction_non_alphanumeric': 0.07043867875377723, 'fraction_numerical': 0.002813379180994061, 'mean_word_length': 3.1477960242005185, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1210788', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2286, 'n_words': 558}