private IObservable <ConnectionResultContainer> ConnectAsync(string localAddress, int port) { if (localAddress == null) { return(Observable.Return(new ConnectionResultContainer(ConnectionResult.WifiDisabled))); } bool hasCustomIpAddress = !string.IsNullOrWhiteSpace(this.userSettings.ServerAddress); return(Observable.If(() => hasCustomIpAddress, Observable.Return(this.userSettings.ServerAddress), NetworkMessenger.Instance.DiscoverServerAsync(localAddress, port)) .SelectMany(address => { string password = null; // We don't want users that aren't premium or in the trial period to send any // existing password if (this.userSettings.IsPremium || TrialHelpers.IsInTrialPeriod(AppConstants.TrialTime, this.clock, this.installationDateFetcher)) { password = string.IsNullOrWhiteSpace(this.userSettings.AdministratorPassword) ? null : this.userSettings.AdministratorPassword; } return NetworkMessenger.Instance.ConnectAsync(address, port, this.userSettings.UniqueIdentifier, password); })); }
public void OneTickBeforeExpirationIsInTrialPeriod() { var clock = Substitute.For <IClock>(); clock.Now.Returns(DateTimeOffset.MinValue + AppConstants.TrialTime - TimeSpan.FromTicks(1)); var installationDateFetcher = Substitute.For <IInstallationDateFetcher>(); installationDateFetcher.GetInstallationDate().Returns(DateTimeOffset.MinValue); Assert.True(TrialHelpers.IsInTrialPeriod(AppConstants.TrialTime, clock, installationDateFetcher)); }
public void ExpirationIsOnExactSameTicks() { var clock = Substitute.For <IClock>(); clock.Now.Returns(DateTimeOffset.MinValue + AppConstants.TrialTime); var installationDateFetcher = Substitute.For <IInstallationDateFetcher>(); installationDateFetcher.GetInstallationDate().Returns(DateTimeOffset.MinValue); Assert.False(TrialHelpers.IsInTrialPeriod(AppConstants.TrialTime, clock, installationDateFetcher)); }
private NetworkMessenger() { this.gate = new SemaphoreSlim(1, 1); this.messagePipeline = new Subject <NetworkMessage>(); this.disconnected = new Subject <Unit>(); this.connectionEstablished = new Subject <Unit>(); this.connectionInfoReceived = new Subject <ConnectionInfo>(); this.client = new Subject <ITcpClient>(); this.isConnected = this.Disconnected.Select(_ => false) .Merge(this.connectionEstablished.Select(_ => true)) .StartWith(false) .Do(x => this.Log().Info("Connection status: {0}", x ? "Connected" : "Disconnected")) .ToProperty(this, x => x.IsConnected); var connectConn = this.IsConnected; var pipeline = this.client.Select(x => Observable.Defer(() => x.GetStream().ReadNextMessageAsync() .ToObservable()) .Repeat() .LoggedCatch(this, null, "Error while reading the next network message") .TakeWhile(m => m != null) .TakeUntil(this.Disconnected)) .Switch() .Publish(); this.messagePipeline = pipeline.ObserveOn(RxApp.TaskpoolScheduler); this.messagePipelineConnection = pipeline.Connect(); var pushMessages = this.messagePipeline.Where(x => x.MessageType == NetworkMessageType.Push) .Select(x => x.Payload.ToObject <PushInfo>()); this.PlaylistChanged = pushMessages.Where(x => x.PushAction == PushAction.UpdateCurrentPlaylist) .Select(x => x.Content.ToObject <NetworkPlaylist>()); this.PlaybackStateChanged = pushMessages.Where(x => x.PushAction == PushAction.UpdatePlaybackState) .Select(x => x.Content["state"].ToObject <NetworkPlaybackState>()); this.PlaybackTimeChanged = pushMessages.Where(x => x.PushAction == PushAction.UpdateCurrentPlaybackTime) .Select(x => x.Content["currentPlaybackTime"].ToObject <TimeSpan>()); var settings = Locator.Current.GetService <UserSettings>(); if (settings == null) { throw new InvalidOperationException("No user settings registered!"); } this.accessPermission = pushMessages.Where(x => x.PushAction == PushAction.UpdateAccessPermission) .Select(x => x.Content["accessPermission"].ToObject <NetworkAccessPermission>()) .Merge(this.connectionInfoReceived.Select(x => x.AccessPermission)) .Select(x => TrialHelpers.GetAccessPermissionForPremiumState(x, settings.IsPremium || TrialHelpers.IsInTrialPeriod(AppConstants.TrialTime))) .ToProperty(this, x => x.AccessPermission); var connectAccessPermission = this.AccessPermission; this.guestSystemInfo = pushMessages.Where(x => x.PushAction == PushAction.UpdateGuestSystemInfo) .Select(x => x.Content.ToObject <GuestSystemInfo>()) .Merge(this.connectionInfoReceived.Select(x => x.GuestSystemInfo)) .ToProperty(this, x => x.GuestSystemInfo); var connectGuestSystemInfo = this.GuestSystemInfo; }
public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); this.Activity.ActionBar.SetTitle(Resource.String.settings); this.userSettings = Locator.Current.GetService <UserSettings>(); this.androidSettings = Locator.Current.GetService <AndroidSettings>(); this.AddPreferencesFromResource(Resource.Layout.Settings); var portPref = (EditTextPreference)this.FindPreference(this.GetString(Resource.String.preference_port)); portPref.EditText.InputType = InputTypes.ClassNumber; portPref.EditText.Events().TextChanged .Select(x => Int32.Parse(new string(x.Text.ToArray()))) .Where(x => !NetworkHelpers.IsPortValid(x)) .Subscribe(x => { portPref.EditText.Error = this.GetString(Resource.String.preference_port_validation_error); }); portPref.BindToSetting(this.userSettings, x => x.Port, x => x.Text, x => int.Parse(x.ToString()), x => x.ToString(), NetworkHelpers.IsPortValid); var ipAddressPref = (EditTextPreference)this.FindPreference(this.GetString(Resource.String.preference_ipaddress)); ipAddressPref.EditText.InputType = InputTypes.ClassPhone; ipAddressPref.EditText.Events().TextChanged .Select(x => new string(x.Text.ToArray())) .Where(x => !IsValidIpAddress(x)) .Subscribe(x => { ipAddressPref.EditText.Error = this.GetString(Resource.String.preference_ipaddress_validation_error); }); ipAddressPref.BindToSetting(this.userSettings, x => x.ServerAddress, x => x.Text, x => (string)x, x => x, IsValidIpAddress); var saveEnergyPref = (SwitchPreference)this.FindPreference(this.GetString(Resource.String.preference_save_energy)); saveEnergyPref.BindToSetting(this.androidSettings, x => x.SaveEnergy, x => x.Checked, x => bool.Parse(x.ToString())); var passwordPreference = (EditTextPreference)this.FindPreference(this.GetString(Resource.String.preference_administrator_password)); passwordPreference.BindToSetting(this.userSettings, x => x.AdministratorPassword, x => x.Text, x => (string)x); this.userSettings.WhenAnyValue(x => x.IsPremium, x => x || TrialHelpers.IsInTrialPeriod(AppConstants.TrialTime)) .BindTo(passwordPreference, x => x.Enabled); Preference premiumButton = this.FindPreference(this.GetString(Resource.String.preference_purchase_premium)); premiumButton.Events().PreferenceClick.Select(_ => this.PurchasePremium().ToObservable() .Catch <Unit, Exception>(ex => Observable.Start(() => this.TrackInAppPurchaseException(ex)))) .Concat() .Subscribe(); Preference restorePremiumButton = this.FindPreference(this.GetString(Resource.String.preference_restore_premium)); restorePremiumButton.Events().PreferenceClick.Select(_ => this.RestorePremium().ToObservable() .Catch <Unit, Exception>(ex => Observable.Start(() => this.TrackInAppPurchaseException(ex)))) .Concat() .Subscribe(); Preference versionPreference = this.FindPreference(this.GetString(Resource.String.preference_version)); versionPreference.Summary = this.Activity.PackageManager.GetPackageInfo(this.Activity.PackageName, 0).VersionName; var virtualServerPreference = (SwitchPreference)this.FindPreference(this.GetString(Resource.String.preference_virtual_server)); virtualServerPreference.Persistent = false; virtualServerPreference.Events().PreferenceChange .Subscribe(x => { if ((bool)x.NewValue) { NetworkMessenger.Override(new VirtualNetworkMessenger()); } else { NetworkMessenger.ResetOverride(); } }); #if !DEBUG && !DEVELOPMENT var developmentCategory = (PreferenceCategory)this.FindPreference(this.GetString(Resource.String.preference_development)); var screen = (PreferenceScreen)this.FindPreference(this.GetString(Resource.String.preference_main_screen)); screen.RemovePreference(developmentCategory); #endif }