public Server() { CurrentSecret = null; receiveClient.CachePolicy = new RequestCachePolicy (RequestCacheLevel.BypassCache); receiveClient.DownloadStringCompleted += (sender, e) => { if (e.Cancelled) return; if (e.Error != null) { Console.Out.WriteLine ( "Error fetching data: {0}", e.Error.Message ); Listen (); return; } JsonValue items = JsonArray.Parse (e.Result); foreach (JsonValue i in items) { DataItem item = new DataItem (i, DataItemDirection.In, DateTime.Now); TransferEvent (item); } Listen (); }; }
public void Paste(DataItem item) { CrossCopyApp.Srv.CurrentSecret.DataItems.Insert (0, item); /*var history = FindViewById<TextView> (Resource.Id.history); RunOnUiThread (() => { history.Text = item.Data + "\n" + history.Text; });*/ }
/// <summary> /// Called when there is something new in the server for the current secret /// Here we have two options: /// - If the item starts with '/api/CURRENT_SECRET/' it means that this is /// really a file that should be downloaded, then we start the async download /// - If the item doesn't start with the mentioned text, then is just some text to display. /// /// In both cases we add the filename or the string to the history. /// </summary> /// <param name='item'> /// Item: The new item, that contains the information of the new staff. /// </param> public void Paste(DataItem item) { if (item.Direction == DataItemDirection.Out && !string.IsNullOrEmpty (_uploadingFileName)) item.Data = _localUri; CrossCopyApp.Srv.CurrentSecret.DataItems.Insert (0, item); Task.Factory.StartNew (() => { CrossCopyApp.Save (Application.Context); }); if (item.Direction == DataItemDirection.In) AddIncomingItemToHistory (item, false); else if (!string.IsNullOrEmpty (_uploadingFileName)) { _uploadingFileName = string.Empty; _localUri = string.Empty; } else AddOutgoingItemToHistory (item); }
HistoryItem CreateOutgoingItem(DataItem item) { try { var input = GetRealPathFromURI (Android.Net.Uri.Parse (item.Data)); if (!string.IsNullOrEmpty (input) && File.Exists (input)) return new HistoryItem { Outgoing = Path.GetFileName (input), LocalPath = item.Data, Downloading = false}; } catch (Exception) { } return new HistoryItem { Outgoing = item.Data, Downloading = false }; }
HistoryItem CreateIncomingItem(DataItem item, bool alreadyDownloaded, ProgressBarX progress, View view) { if (!item.Data.StartsWith (CrossCopyApp.Srv.CurrentPath)) return new HistoryItem { Incoming = item.Data, Downloading = false }; var hItem = new HistoryItem { Incoming = Path.GetFileName (item.Data), LocalPath = BaseDir + item.Data.Substring (4, item.Data.Length - 4), Downloading = !alreadyDownloaded }; if (hItem.Downloading) { progress.Visibility = ViewStates.Visible; progress.Indeterminate = true; view.Visibility = ViewStates.Invisible; StartDownload (item.Data, hItem, progress, view); } return hItem; }
void AddOutgoingItemToHistory(DataItem item) { // This happens when we are starting and we are // adding the old items to the history var view = _inflater.Inflate (Resource.Layout.HistoryItemView, _mainLayout, false); var textView = view.FindViewById<TextView> (Resource.Id.tvText); textView.Gravity = GravityFlags.Right; textView.Text = GetDisplayNameFromURI (Android.Net.Uri.Parse (item.Data)); _historyItems [view] = CreateOutgoingItem (item); AddView (view); }
private void AddOldItemToHistory(DataItem item) { if (item.Direction == DataItemDirection.In) AddIncomingItemToHistory (item, true); else AddOutgoingItemToHistory (item); }
void AddIncomingItemToHistory(DataItem item, bool isOldItem) { RunOnUiThread (() => { var view = _inflater.Inflate (Resource.Layout.HistoryItemView, _mainLayout, false); var progress = view.FindViewById<ProgressBarX> (Resource.Id.transferProgress); var textView = view.FindViewById<TextView> (Resource.Id.tvText); textView.Gravity = GravityFlags.Left; var theNewItem = CreateIncomingItem (item, isOldItem, progress, textView); textView.Text = progress.Text = theNewItem.Incoming; _historyItems [view] = theNewItem; AddView (view); }); }
private void ShowSecretsView() { var secret = currentSecret; RootElement root = CreateRootElement (); rootDVC = new StyledDialogViewController (root, null, backgroundColor) { Autorotate = true, HidesBottomBarWhenPushed = true }; rootDVC.ViewAppearing += (sender, e) => { server.Abort (); currentSecret = null; NSError err; Analytics.SharedTracker.TrackPageView ("/secrets", out err); ReOrderSecrets (); }; var aboutButton = UIHelper.CreateInfoButton(40f, 60f); aboutButton.TouchDown += (sender, e) => { ShowAboutView(); }; rootDVC.View.AddSubview(aboutButton); navigation = new UINavigationController (); Flurry.LogAllPageViews(navigation); navigation.SetNavigationBarHidden (true, false); navigation.PushViewController (rootDVC, false); window.RootViewController = navigation; if (ResharedItem != null) { var sbounds = UIScreen.MainScreen.Bounds; var btnCancel = UIButton.FromType (UIButtonType.RoundedRect); btnCancel.Tag = CANCEL_BUTTON_TAG; btnCancel.Frame = new RectangleF ( 10, sbounds.Height - 60f, 100, 30 ); btnCancel.SetTitle ("Cancel", UIControlState.Normal); btnCancel.SetTitleColor (UIColor.Black, UIControlState.Normal); btnCancel.TouchDown += delegate { ResharedItem = null; if (rootDVC.View.ViewWithTag(CANCEL_BUTTON_TAG) != null) { rootDVC.View.ViewWithTag(CANCEL_BUTTON_TAG).RemoveFromSuperview(); } UpdateSecretsViewLabel(WELCOME_LABEL_TEXT); DisplaySecretDetail(secret); }; rootDVC.View.AddSubview (btnCancel); UpdateSecretsViewLabel(String.Format(SHARE_LABEL_TEXT, UrlHelper.GetFileName(ResharedItem.ItemPath))); } }
private void ReshareData() { if (rootDVC.View.ViewWithTag(CANCEL_BUTTON_TAG) != null) { rootDVC.View.ViewWithTag(CANCEL_BUTTON_TAG).RemoveFromSuperview(); } UpdateSecretsViewLabel(WELCOME_LABEL_TEXT); switch (ResharedItemType) { case FileType.Photo: ALAssetsLibrary library = new ALAssetsLibrary (); library.AssetForUrl (new NSUrl (ResharedItem.Data), (asset) => { if (asset != null) { UIImage image = UIImage.FromImage (asset.DefaultRepresentation.GetImage()); UploadMedia (image, NSUrl.FromFilename(ResharedItem.ItemPath), null); ResharedItem = null; } else { Console.Out.WriteLine ("Asset is null."); } }, (failureError) => { if (failureError != null) { Console.Out.WriteLine ("Error: " + failureError.LocalizedDescription); } } ); break; case FileType.Video: case FileType.Other: var fileUrl = NSUrl.FromFilename (ResharedItem.Data); UploadMedia(null, fileUrl, fileUrl); ResharedItem = null; break; default: Console.Out.WriteLine ("Error: Resharing file type is not set"); break; } }
private void PasteData(string data, DataItemDirection direction) { DataItem item = new DataItem (data, direction, DateTime.Now); Paste (item); }
private void Paste(DataItem item) { UIApplication.SharedApplication.InvokeOnMainThread (delegate { currentSecret.DataItems.Insert (0, item); entriesSection.Insert (0, CreateDataItemElement (item)); } ); }
private void OpenFile(string filePath, DataItem item) { var sbounds = UIScreen.MainScreen.Bounds; string ext = UrlHelper.GetExtension(filePath); if (ext.ToUpper () == ".MOV" || ext.ToUpper () == ".M4V") { var movieController = new AdvancedUIViewController (); moviePlayer = new MPMoviePlayerController (NSUrl.FromFilename (filePath)); moviePlayer.View.Frame = new RectangleF ( sbounds.X, sbounds.Y - 20, sbounds.Width, sbounds.Height - 30 ); moviePlayer.ControlStyle = MPMovieControlStyle.Fullscreen; moviePlayer.View.AutoresizingMask = (UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight); moviePlayer.ShouldAutoplay = true; moviePlayer.PrepareToPlay (); moviePlayer.Play (); var btnClose = UIButton.FromType (UIButtonType.RoundedRect); btnClose.Frame = new RectangleF (3, 7, 60, 30); btnClose.SetTitle ("Close", UIControlState.Normal); btnClose.SetTitleColor (UIColor.Black, UIControlState.Normal); btnClose.TouchDown += delegate { movieController.DismissModalViewControllerAnimated (true); }; var btnShare = UIButton.FromType (UIButtonType.RoundedRect); btnShare.Frame = new RectangleF ( (sbounds.Width / 2) - 50, sbounds.Height - 50, 100, 30 ); btnShare.SetTitle ("Share", UIControlState.Normal); btnShare.SetTitleColor (UIColor.Black, UIControlState.Normal); btnShare.TouchDown += delegate { ResharedItem = item; ResharedItemType = FileType.Video; ShowSecretsView(); }; movieController.View.AddSubview (moviePlayer.View); movieController.View.AddSubview (btnClose); movieController.View.AddSubview (btnShare); navigation.PresentModalViewController (movieController, true); } else if (ext.ToUpper () == ".JPEG" || ext.ToUpper () == ".JPG" || ext.ToUpper () == ".PNG") { ALAssetsLibrary library = new ALAssetsLibrary (); library.AssetForUrl (new NSUrl (filePath), (asset) => { if (asset != null) { var imageController = new AdvancedUIViewController (); var image = UIImage.FromImage (asset.DefaultRepresentation.GetFullScreenImage ()); var imageView = new UIImageView (image); imageView.Frame = sbounds; imageView.UserInteractionEnabled = true; imageView.ClipsToBounds = true; imageView.ContentMode = UIViewContentMode.ScaleAspectFit; var btnClose = UIButton.FromType (UIButtonType.RoundedRect); btnClose.Frame = new RectangleF ( (sbounds.Width / 2) - 50, 20, 100, 30 ); btnClose.SetTitle ("Close", UIControlState.Normal); btnClose.SetTitleColor (UIColor.Black, UIControlState.Normal); btnClose.TouchDown += delegate { imageController.DismissModalViewControllerAnimated (true); }; var btnShare = UIButton.FromType (UIButtonType.RoundedRect); btnShare.Frame = new RectangleF ( (sbounds.Width / 2) - 50, sbounds.Height - 60, 100, 30 ); btnShare.SetTitle ("Share", UIControlState.Normal); btnShare.SetTitleColor (UIColor.Black, UIControlState.Normal); btnShare.TouchDown += delegate { ResharedItem = item; ResharedItemType = FileType.Photo; ShowSecretsView(); }; var scrollView = new UIScrollView (sbounds); scrollView.ClipsToBounds = true; scrollView.ContentSize = sbounds.Size; scrollView.BackgroundColor = UIColor.Gray; scrollView.MinimumZoomScale = 1.0f; scrollView.MaximumZoomScale = 3.0f; scrollView.MultipleTouchEnabled = true; scrollView.AutoresizingMask = (UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight); scrollView.ViewForZoomingInScrollView = delegate(UIScrollView sv) { return imageView; }; scrollView.AddSubview (imageView); imageController.View.AddSubview (scrollView); imageController.View.AddSubview (btnClose); imageController.View.AddSubview (btnShare); navigation.PresentModalViewController (imageController, true); } else { Console.Out.WriteLine ("Asset is null."); } }, (error) => { if (error != null) { Console.Out.WriteLine ("Error: " + error.LocalizedDescription); } } ); } else { var btnShare = UIButton.FromType (UIButtonType.RoundedRect); btnShare.Frame = new RectangleF ( (sbounds.Width / 2) - 50, sbounds.Height - 50, 100, 30 ); btnShare.SetTitle ("Share", UIControlState.Normal); btnShare.SetTitleColor (UIColor.Black, UIControlState.Normal); btnShare.Tag = SHARE_BUTTON_TAG; btnShare.TouchDown += delegate { ResharedItem = item; ResharedItemType = FileType.Other; ShowSecretsView(); }; navigation.Add(btnShare); interactionControllerDelegate = new UIDocumentInteractionControllerDelegateClass(navigation); interactionController = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath)); interactionController.Delegate = interactionControllerDelegate; InvokeOnMainThread(delegate { interactionController.PresentPreview(true); }); } }
private Element CreateDataItemElement(DataItem item) { Element element; if ((item.Data.StartsWith (server.CurrentPath)) || (item.Data.StartsWith (ASSETS_LIBRARY)) || (item.Data.StartsWith (BaseDir))) { var dataElement = new DataImageStringElement ( Path.GetFileName (String.IsNullOrEmpty(item.ItemPath) ? item.Data : item.ItemPath), (item.Direction == DataItemDirection.In) ? imgDownload : imgUpload, item.Data ); dataElement.Tapped += delegate { if (!dataElement.Downloading) { OpenFile (dataElement.Data, item); } }; dataElement.Alignment = (item.Direction == DataItemDirection.In) ? UITextAlignment.Right : UITextAlignment.Left; if (item.Data.StartsWith (server.CurrentPath)) { dataElement.Animating = true; dataElement.Downloading = true; var localFilePath = Path.Combine ( BaseDir, dataElement.Caption); Server.DownloadFileAsync (dataElement.Data, (s, e) => { var bytes = e.Result; if (bytes == null) throw e.Error; var mediaHelper = new MediaHelper (); mediaHelper.FileSavedToPhotosAlbum += (sender, args) => { dataElement.Data = args.ReferenceUrl; item.ItemPath = args.FilePath; item.Data = dataElement.Data; dataElement.Animating = false; dataElement.Downloading = false; }; mediaHelper.SaveFileToPhotosAlbum (localFilePath, bytes); } ); } else { dataElement.Animating = false; } element = (Element)dataElement; } else { UITextAlignment alignment = (item.Direction == DataItemDirection.In) ? UITextAlignment.Right : UITextAlignment.Left; var htmlElement = UIHelper.CreateHtmlViewElement ( null, item.Data, alignment ); element = (Element)htmlElement; } return element; }
public void Send(string message) { if (CurrentSecret == null) return; Thread share = new Thread (() => { WebClient shareClient = new WebClient (); try { string result = shareClient.UploadString ( new Uri (String.Format ("{0}/api/{1}.json{2}", SERVER, CurrentSecret, DeviceID) ), "PUT", message); if (String.IsNullOrWhiteSpace (result)) return; DataItem item = new DataItem (JsonObject.Parse (result), DataItemDirection.Out, DateTime.Now); TransferEvent (item); } catch (Exception e) { Console.Out.WriteLine ( "Error sharing data: {0}", e.Message ); return; } } ); share.Start (); }