// Updates all contacts in the friendlist public static void Update() { lock (friends) { // Go through each friend and check for the pubkey in the PL foreach (Friend friend in friends) { Presence presence = null; try { presence = PresenceList.getPresenceByAddress(friend.walletAddress); } catch (Exception e) { Logging.error("Presence Error {0}", e.Message); presence = null; } if (presence != null) { if (friend.online == false) { friend.online = true; UIHelpers.setContactStatus(friend.walletAddress, friend.online, friend.getUnreadMessageCount(), "", 0); } } else { if (friend.online == true) { friend.online = false; UIHelpers.setContactStatus(friend.walletAddress, friend.online, friend.getUnreadMessageCount(), "", 0); } } } } }
public static string startingScreen = ""; // Which screen to start on private App() { InitializeComponent(); // Fix for issue https://github.com/xamarin/Xamarin.Forms/issues/10712#issuecomment-629394090 Device.SetFlags(new string[] { "anything" }); // check if already started if (Node.Instance == null) { // Prepare the personal folder if (!Directory.Exists(Config.spixiUserFolder)) { Directory.CreateDirectory(Config.spixiUserFolder); } // Init logging Logging.setOptions(5, 1, true); Logging.start(Config.spixiUserFolder); Logging.info(string.Format("Starting Spixi {0} ({1})", Config.version, CoreConfig.version)); // Init fatal exception handlers AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; // Load or generate a device ID. if (Application.Current.Properties.ContainsKey("uid")) { byte[] uid = Application.Current.Properties["uid"] as byte[]; if (uid == null) { // Generate and save the device ID Application.Current.Properties["uid"] = CoreConfig.device_id; } else { CoreConfig.device_id = uid; } } else { // Generate and save the device ID Application.Current.Properties["uid"] = CoreConfig.device_id; } if (Application.Current.Properties.ContainsKey("language")) { if (!SpixiLocalization.loadLanguage(Application.Current.Properties["language"] as string)) { Application.Current.Properties["language"] = SpixiLocalization.getCurrentLanguage(); Application.Current.SavePropertiesAsync(); // Force-save properties for compatibility with WPF } } else { string lang = CultureInfo.CurrentCulture.Name.ToLower(); if (SpixiLocalization.loadLanguage(lang)) { Xamarin.Forms.Application.Current.Properties["language"] = SpixiLocalization.getCurrentLanguage(); Xamarin.Forms.Application.Current.SavePropertiesAsync(); // Force-save properties for compatibility with WPF } } movePersonalFiles(); // Load theme and appearance OSAppTheme currentTheme = Application.Current.RequestedTheme; Current.UserAppTheme = currentTheme; ThemeAppearance themeAppearance = ThemeAppearance.automatic; if (Application.Current.Properties.ContainsKey("appearance")) { themeAppearance = (ThemeAppearance)Current.Properties["appearance"]; } ThemeManager.loadTheme("spixiui", themeAppearance); Current.RequestedThemeChanged += (s, a) => { // Respond to the theme change Current.UserAppTheme = a.RequestedTheme; if (ThemeManager.getActiveAppearance() == ThemeAppearance.automatic) { ThemeManager.changeAppearance(ThemeAppearance.automatic); } UIHelpers.reloadAllPages(); }; // Start Ixian code node = new Node(); // Attempt to load a pre-existing wallet bool wallet_found = Node.checkForExistingWallet(); if (!wallet_found) { // Wallet not found, go to initial launch page MainPage = new NavigationPage(new SPIXI.LaunchPage()); } else { // Wallet found, see if it can be decrypted bool wallet_decrypted = IxianHandler.getWalletList().Count > 0 ? IxianHandler.getWalletStorage().isLoaded() : false; if (!wallet_decrypted) { wallet_decrypted = Node.loadWallet(); } if (wallet_decrypted == false) { MainPage = new NavigationPage(new SPIXI.LaunchRetryPage()); } else { // Wallet found if (isLockEnabled()) { // Show the lock screen MainPage = new NavigationPage(new SPIXI.LockPage()); } else { // Show the home screen MainPage = new NavigationPage(HomePage.Instance()); } } } NavigationPage.SetHasNavigationBar(MainPage, false); } else { // Already started before node = Node.Instance; } }
public static FriendMessage addMessageWithType(byte[] id, FriendMessageType type, byte[] wallet_address, int channel, string message, bool local_sender = false, byte[] sender_address = null, long timestamp = 0, bool fire_local_notification = true, int payable_data_len = 0) { Friend friend = getFriend(wallet_address); if (friend == null) { // No matching contact found in friendlist // Add the contact, then issue the message again? // TODO: need to fetch the stage 1 public key somehow here // Ignoring such messages for now //addFriend(wallet_address, "pubkey", "Unknown"); //addMessage(wallet_address, message); Logging.warn("Received message but contact isn't in our contact list."); return(null); } if (!friend.online) { using (MemoryStream mw = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(mw)) { writer.WriteIxiVarInt(wallet_address.Length); writer.Write(wallet_address); CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M', 'H' }, ProtocolMessageCode.getPresence2, mw.ToArray(), null); } } } bool set_read = false; string sender_nick = ""; if (friend.bot && sender_address != null) { if (IxianHandler.getWalletStorage().isMyAddress(sender_address)) { if (!local_sender) { set_read = true; } local_sender = true; } if (!local_sender) { if (friend.users.hasUser(sender_address) && friend.users.getUser(sender_address).getNick() != "") { sender_nick = friend.users.getUser(sender_address).getNick(); } else { if (!friend.users.hasUser(sender_address) || friend.users.getUser(sender_address).publicKey == null) { StreamProcessor.requestBotUser(friend, sender_address); } } } } else { sender_nick = friend.nickname; } if (timestamp == 0) { timestamp = Clock.getTimestamp(); } FriendMessage friend_message = new FriendMessage(id, message, timestamp, local_sender, type, sender_address, sender_nick); friend_message.payableDataLen = payable_data_len; List <FriendMessage> messages = friend.getMessages(channel); if (messages == null) { Logging.warn("Message with id {0} was sent to invalid channel {1}.", Crypto.hashToString(id), channel); return(null); } lock (messages) { // TODO should be optimized if (id != null) { FriendMessage tmp_msg = messages.Find(x => x.id != null && x.id.SequenceEqual(id)); if (tmp_msg != null) { if (!tmp_msg.localSender) { Logging.warn("Message with id {0} was already in message list.", Crypto.hashToString(id)); } else { friend.setMessageRead(channel, id); } if (messages.Last() == tmp_msg) { friend.metaData.setLastMessage(tmp_msg, channel); friend.metaData.setLastReceivedMessageIds(tmp_msg.id, channel); friend.saveMetaData(); } return(null); } else { friend.metaData.setLastReceivedMessageIds(friend_message.id, channel); } } else if (!local_sender) { Logging.error("Message id sent by {0} is null!", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress)); return(null); } messages.Add(friend_message); } bool old_message = false; // Check if the message was sent before the friend was added to the contact list if (friend.addedTimestamp > friend_message.timestamp) { old_message = true; } if (set_read || old_message) { friend_message.confirmed = true; friend_message.read = true; } friend.metaData.setLastMessage(friend_message, channel); friend.saveMetaData(); // If a chat page is visible, insert the message directly if (friend.chat_page != null) { friend.chat_page.insertMessage(friend_message, channel); } else if (!set_read) { // Increase the unread counter if this is a new message if (!old_message) { friend.metaData.unreadMessageCount++; } friend.saveMetaData(); } UIHelpers.setContactStatus(friend.walletAddress, friend.online, friend.getUnreadMessageCount(), message, timestamp); // Only send alerts if this is a new message if (old_message == false) { // Send a local push notification if Spixi is not in the foreground if (fire_local_notification && !local_sender) { if (App.isInForeground == false || friend.chat_page == null) { // don't fire notification for nickname and avatar if (!friend_message.id.SequenceEqual(new byte[] { 4 }) && !friend_message.id.SequenceEqual(new byte[] { 5 })) { if (friend.bot == false || (friend.metaData.botInfo != null && friend.metaData.botInfo.sendNotification)) { DependencyService.Get <IPushService>().showLocalNotification("Spixi", "New Message", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress)); } } } } ISystemAlert alert = DependencyService.Get <ISystemAlert>(); if (alert != null) { alert.flash(); } } // Write to chat history Node.localStorage.requestWriteMessages(wallet_address, channel); return(friend_message); }