Esempio n. 1
0
        // Use this for initialization
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <exception cref="Exception">Motive URL not set in WebServices</exception>
        public void Initialize()
        {
            SpaceName = !string.IsNullOrEmpty(AppId) ? AppId.Substring(0, AppId.LastIndexOf('.')) : null;

#if !WINDOWS_UWP
            System.Net.ServicePointManager.ServerCertificateValidationCallback =
                new System.Net.Security.RemoteCertificateValidationCallback(
                    (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                     System.Security.Cryptography.X509Certificates.X509Chain chain,
                     System.Net.Security.SslPolicyErrors sslPolicyErrors) =>
            {
                return(true);
            });
#endif
            var mediaFolder = StorageManager.EnsureDownloadsFolder("media");

            MediaDownloadManager = new MediaDownloadManager(mediaFolder);

            // catch MotiveUrl not set error
            if (string.IsNullOrEmpty(MotiveUrl))
            {
                m_logger.Error("Motive URL not set in WebServices");
                throw new Exception("Motive URL not set in WebServices");
            }

            MotiveAuthenticator.Instance.Initialize(
                MotiveUrl.Replace(" ", string.Empty),
                AppId.Replace(" ", string.Empty),
                ApiKey.Replace(" ", string.Empty),
                new FileStorageAgent(StorageManager.GetFilePath("authenticator", "authenticationState.json")));

            //m_catalogLoader = new CatalogLoader(MotiveUrl, SpaceName, UseDevCatalogs);
        }
Esempio n. 2
0
        void DownloadMedia()
        {
            SetDownloadState(WebServicesDownloadState.Resolving);

            // Catalogs ready! Download media if required
            MediaDownloadManager.Resolve((resSuccess) =>
            {
                ThreadHelper.Instance.CallOnMainThread(() =>
                {
                    if (resSuccess)
                    {
                        if (InternetReachability == NetworkReachability.ReachableViaCarrierDataNetwork &&
                            MediaDownloadManager.OutstandingFileSize > CellDownloadThreshold * 1000000)
                        {
                            SetDownloadState(WebServicesDownloadState.WaitWifi);
                        }
                        else
                        {
                            StartDownload();
                        }
                    }
                    else
                    {
                        SetDownloadState(WebServicesDownloadState.Error);

                        m_logger.Error("Failed to resolve media.");
                    }
                });
            });
        }
Esempio n. 3
0
        public override void DidPush()
        {
            m_checkLocationServices        = false;
            m_locationServiceCheckComplete = false;
            m_downloadManager = WebServices.Instance.MediaDownloadManager;

            SetState(WebServices.Instance.DownloadState);

            base.DidPush();
        }
Esempio n. 4
0
        public MainWindowViewModel()
        {
            app        = Locator.Current.GetService <AthameApp>();
            downloader = new MediaDownloadManager();
            source     = new MediaDownloadSource();

            Router = new RoutingState();

            MediaSearch    = new MediaSearchViewModel();
            ProgressStatus = new ProgressStatusViewModel();
            MediaItems     = new MediaItemsViewModel(source);

            DownloadMediaCommand = ReactiveCommand.CreateFromTask(
                DownloadMedia,
                MediaItems.CanDownload);
            CancelDownloadCommand = ReactiveCommand.Create(RemoveMedia);
            CanRestoreCommand     = ReactiveCommand.Create(CanRestore);
            SaveSettingsCommand   = ReactiveCommand.Create(SaveSettings);
            ViewSettingsCommand   = ReactiveCommand.CreateFromObservable(
                Router.Navigate <SettingsViewModel>);
            ViewAboutAppCommand = ReactiveCommand.CreateFromObservable(
                Router.Navigate <AboutMeViewModel>);

            Activator = new ViewModelActivator();
            this.WhenActivated((CompositeDisposable disposables) =>
            {
                this.WhenAnyValue(x => x.MediaSearch.SearchResult)
                .Where(media => media != null)
                .Subscribe(AddMedia)
                .DisposeWith(disposables);

                Observable
                .FromEventPattern <MediaDownloadEventArgs>(downloader, nameof(downloader.MediaDownloadStarted))
                .Subscribe(e => MediaServiceDequeued(e.EventArgs))
                .DisposeWith(disposables);
                Observable
                .FromEventPattern <TrackDownloadEventArgs>(downloader, nameof(downloader.TrackDownloadStarted))
                .Subscribe()
                .DisposeWith(disposables);
                Observable
                .FromEventPattern <TrackDownloadEventArgs>(
                    downloader.TrackDownloader, nameof(downloader.TrackDownloader.TrackDownloadStarted), RxApp.MainThreadScheduler)
                .Subscribe(e => TrackDownloadStarted(e.EventArgs));
                Observable
                .FromEventPattern <TrackDownloadEventArgs>(downloader, nameof(downloader.TrackDownloadProgressChanged))
                .Subscribe(e => TrackDownloadProgressed(e.EventArgs))
                .DisposeWith(disposables);
                Observable
                .FromEventPattern <TrackDownloadEventArgs>(downloader, nameof(downloader.TrackDownloadCompleted))
                .Subscribe(e => TrackDownloadCompleted(e.EventArgs))
                .DisposeWith(disposables);
            });
        }
Esempio n. 5
0
        public void StartDownload()
        {
            if (MediaDownloadManager.OutstandingFileSize > 0)
            {
                SetDownloadState(WebServicesDownloadState.Downloading);

                //m_totalFiles = MediaDownloadManager.OutstandingFileCount;
                //m_totalSize = MediaDownloadManager.OutstandingFileSize;

                MediaDownloadManager.DownloadAll((dlSuccess) =>
                {
                    ThreadHelper.Instance.CallOnMainThread(() =>
                    {
                        if (!dlSuccess)
                        {
                            SetDownloadState(WebServicesDownloadState.Error);

                            if (m_completeFailHandler != null)
                            {
                                m_completeFailHandler("Error downloading media. Try again.");
                            }
                        }
                        else
                        {
                            SetDownloadState(WebServicesDownloadState.Ready);

                            if (m_completeSuccessHandler != null)
                            {
                                m_completeSuccessHandler();
                            }
                        }
                    });
                });
            }
            else
            {
                SetDownloadState(WebServicesDownloadState.Ready);

                if (m_completeSuccessHandler != null)
                {
                    m_completeSuccessHandler();
                }
            }
        }
Esempio n. 6
0
        public void ReloadFromServer(Action onSuccess, Action <string> onFail)
        {
            MediaDownloadManager.Reset();

            m_completeSuccessHandler = onSuccess;
            m_completeFailHandler    = onFail;

            foreach (var call in m_preloadActions.ToArray())
            {
                call();
            }

            SetDownloadState(WebServicesDownloadState.Resolving);

            Action configLoadComplete = () =>
            {
                ThreadHelper.Instance.CallOnMainThread(() =>
                {
                    foreach (var obj in ScriptObjectDirectory.Instance.AllObjects)
                    {
                        if (obj is IMediaItemProvider)
                        {
                            MediaDownloadManager.AddMediaItemProvider((IMediaItemProvider)obj);
                        }
                    }

                    if (m_catalogLoadActions.Count > 0)
                    {
                        BatchProcessor iter = new BatchProcessor(m_catalogLoadActions.Count, DownloadMedia);

                        foreach (var call in m_catalogLoadActions)
                        {
                            call(() => { iter++; });
                        }
                    }
                    else
                    {
                        DownloadMedia();
                    }
                });
            };

            if (!string.IsNullOrEmpty(ConfigName))
            {
                var cfgManager = StorageManager.GetAppStorageManager().GetManager("downloads", SpaceName, "config");

                if (InternetReachability == NetworkReachability.NotReachable)
                {
                    bool result = false;

                    if (!RequireNetworkConnection)
                    {
                        result = ProjectConfigService.Instance.LoadFromCache(cfgManager, ConfigName, ShouldUseDevCatalogs);
                    }

                    if (result)
                    {
                        configLoadComplete();
                    }
                    else
                    {
                        SetDownloadError(ServiceCallLoadStatus.ServiceCallFail);
                    }
                }
                else
                {
                    ProjectConfigService.Instance.Load(cfgManager, MotiveUrl, SpaceName, ConfigName, ShouldUseDevCatalogs, (status) =>
                    {
                        if (status == ServiceCallLoadStatus.Success)
                        {
                            configLoadComplete();
                        }
                        else
                        {
                            SetDownloadError(status);
                        }
                    });
                }
            }
            else
            {
                configLoadComplete();
            }
        }
Esempio n. 7
0
        public void LoadCatalog <T>(string spaceName, string catalogName, Action <Catalog <T> > onLoad, bool?useDevCatalogs = null)
        {
            if (string.IsNullOrEmpty(catalogName))
            {
                return;
            }

            var fileName = StorageManager.GetCatalogFileName(spaceName, catalogName + ".json");

            // If the caller has specified a preference for using dev catalogs, use that,
            // otherwise defer to the setting here | the debug setting
            bool useDev = useDevCatalogs ??
                          (ShouldUseDevCatalogs || SettingsHelper.IsDebugSet("Debug_UseDevCatalogs"));

            if (InternetReachability == NetworkReachability.NotReachable)
            {
                if (RequireNetworkConnection)
                {
                    SetDownloadError(WebServicesDownloadErrorCode.NoNetworkConnection);
                }
                else
                {
                    var catalog = CatalogLoader.LoadCatalogFromCache <T>(fileName);

                    if (catalog == null)
                    {
                        if (m_completeFailHandler != null)
                        {
                            m_completeFailHandler("Please connect to the network and re-try");
                        }
                    }
                    else
                    {
                        MediaDownloadManager.AddMediaItemProvider(catalog);

                        onLoad(catalog);
                    }
                }
            }
            else
            {
                CatalogLoader.LoadCatalog <T>(fileName, spaceName, catalogName, useDev,
                                              (status, catalog) =>
                {
                    if (status == ServiceCallLoadStatus.Success)
                    {
                        m_logger.Debug("Loaded catalog {0} with {1} item(s)",
                                       catalogName, catalog.Items == null ? 0 : catalog.Items.Length);

                        // This callback happens outside of the Unity thread,
                        // use the Thread Helper to move into the Unity context
                        ThreadHelper.Instance.CallOnMainThread(() =>
                        {
                            MediaDownloadManager.AddMediaItemProvider(catalog);

                            onLoad(catalog);

                            // Since we're in the Unity thread here we don't need
                            // to protect this in a critical section
                        });
                    }
                    else
                    {
                        m_logger.Error("Error loading catalog {0}", catalogName);

                        SetDownloadError(status);

                        if (m_completeFailHandler != null)
                        {
                            ThreadHelper.Instance.CallOnMainThread(() =>
                            {
                                m_completeFailHandler("Error loading catalog " + catalogName);
                            });
                        }
                    }
                });
            }
        }