public void Initialize(BackgroundTaskDeferral deferral) { if (_connection == null & _protoService == null) { VoIPCallTask.Log("Mediator initialized", "Creating proto service"); var deviceInfoService = new DeviceInfoService(); var eventAggregator = new TelegramEventAggregator(); var cacheService = new InMemoryCacheService(eventAggregator); var updatesService = new UpdatesService(cacheService, eventAggregator); var transportService = new TransportService(); var connectionService = new ConnectionService(deviceInfoService); var statsService = new StatsService(); var protoService = new MTProtoService(deviceInfoService, updatesService, cacheService, transportService, connectionService, statsService); protoService.Initialized += (s, args) => { VoIPCallTask.Log("ProtoService initialized", "waiting for updates"); updatesService.LoadStateAndUpdate(() => { VoIPCallTask.Log("Difference processed", "Difference processed"); if (_phoneCall == null) { VoIPCallTask.Log("Difference processed", "No call found in difference"); if (_systemCall != null) { _systemCall.NotifyCallEnded(); } } }); }; eventAggregator.Subscribe(this); protoService.Initialize(); _protoService = protoService; _transportService = transportService; } else { VoIPCallTask.Log("Mediator initialized", "_connection is null: " + (_connection == null)); } _deferral = deferral; _initialized = true; ProcessUpdates(); }
public void Run(IBackgroundTaskInstance taskInstance) { var deferral = taskInstance.GetDeferral(); var data = Toast.GetData(taskInstance); if (data != null) { if (data.ContainsKey("QuickMessage")) { var deviceInfoService = new DeviceInfoService(); var eventAggregator = new TelegramEventAggregator(); var cacheService = new InMemoryCacheService(eventAggregator); var updatesService = new UpdatesService(cacheService, eventAggregator); var transportService = new TransportService(); var connectionService = new ConnectionService(deviceInfoService); var statsService = new StatsService(); var manualResetEvent = new ManualResetEvent(false); var protoService = new MTProtoService(deviceInfoService, updatesService, cacheService, transportService, connectionService, statsService); protoService.Initialized += (s, args) => { var text = data["QuickMessage"]; var messageText = text.Replace("\r\n", "\n").Replace('\v', '\n').Replace('\r', '\n'); var entitiesBase = Utils.GetEntities(ref messageText); var replyToMsgId = 0; var inputPeer = default(TLInputPeerBase); if (data.ContainsKey("from_id")) { inputPeer = new TLInputPeerUser { UserId = int.Parse(data["from_id"]), AccessHash = long.Parse(data["access_hash"]) }; } else if (data.ContainsKey("channel_id")) { inputPeer = new TLInputPeerChannel { ChannelId = int.Parse(data["channel_id"]), AccessHash = long.Parse(data["access_hash"]) }; replyToMsgId = data.ContainsKey("msg_id") ? int.Parse(data["msg_id"]) : 0; } else if (data.ContainsKey("chat_id")) { inputPeer = new TLInputPeerChat { ChatId = int.Parse(data["chat_id"]) }; replyToMsgId = data.ContainsKey("msg_id") ? int.Parse(data["msg_id"]) : 0; } TLVector <TLMessageEntityBase> entities = null; if (entitiesBase != null) { entities = new TLVector <TLMessageEntityBase>(entitiesBase); } var obj = new TLMessagesSendMessage { Peer = inputPeer, ReplyToMsgId = replyToMsgId, Message = messageText, Entities = entities, IsBackground = true, RandomId = TLLong.Random() }; protoService.SendInformativeMessageInternal <TLUpdatesBase>("messages.sendMessage", obj, result => { manualResetEvent.Set(); }, faultCallback: fault => { // TODO: alert user? manualResetEvent.Set(); }, fastCallback: () => { manualResetEvent.Set(); }); //var date = TLUtils.DateToUniversalTimeTLInt(protoService.ClientTicksDelta, DateTime.Now); //var message = TLUtils.GetMessage(SettingsHelper.UserId, inputPeer, TLMessageState.Sending, true, true, date, text, new TLMessageMediaEmpty(), TLLong.Random(), replyToMsgId); //var history = cacheService.GetHistory(inputPeer, 1); //cacheService.SyncSendingMessage(message, null, async (m) => //{ // await protoService.SendMessageAsync(message, () => // { // // TODO: fast callback // }); // manualResetEvent.Set(); //}); }; protoService.InitializationFailed += (s, args) => { manualResetEvent.Set(); }; //cacheService.Init(); protoService.Initialize(); manualResetEvent.WaitOne(15000); } } deferral.Complete(); }