Commit 49e682f3 authored by Sylvain Berfini's avatar Sylvain Berfini

Moved utility static functions to Utils class + fix missing context menu on...

Moved utility static functions to Utils class + fix missing context menu on images sometimes + click on chat image displays it full screen
parent f4b467ed
......@@ -72,6 +72,7 @@
<Image
Grid.Row="1"
Margin="5"
Tap="Image_Tap"
Stretch="UniformToFill"
Visibility="Collapsed"
x:Name="Image" />
......
......@@ -77,7 +77,7 @@ namespace Linphone.Controls
private void Save_Click(object sender, RoutedEventArgs e)
{
Chat.SavePictureInMediaLibrary(ChatMessage.ImageURL);
Utils.SavePictureInMediaLibrary(ChatMessage.ImageURL);
}
/// <summary>
......@@ -94,11 +94,12 @@ namespace Linphone.Controls
{
DownloadImage.Visibility = Visibility.Collapsed;
ProgressBar.Visibility = Visibility.Visible;
BitmapImage image = Chat.GetThumbnailBitmapFromImage(Chat.DownloadImageAndStoreItInIsolatedStorage(ChatMessage.ImageURL, ChatMessage));
BitmapImage image = Utils.GetThumbnailBitmapFromImage(Utils.DownloadImageAndStoreItInIsolatedStorage(ChatMessage.ImageURL, ChatMessage));
if (image != null)
{
Image.Visibility = Visibility.Visible;
Image.Source = image;
Save.Visibility = Visibility.Visible;
}
else
{
......@@ -110,10 +111,15 @@ namespace Linphone.Controls
private void ShowImage_Click(object sender, RoutedEventArgs e)
{
Image.Source = Chat.GetThumbnailBitmapFromImage(Chat.ReadImageFromIsolatedStorage(ChatMessage.ImageURL));
Image.Source = Utils.GetThumbnailBitmapFromImage(Utils.ReadImageFromIsolatedStorage(ChatMessage.ImageURL));
ShowImage.Visibility = Visibility.Collapsed;
Image.Visibility = Visibility.Visible;
Save.Visibility = Visibility.Visible;
}
private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
BaseModel.CurrentPage.NavigationService.Navigate(new Uri("/Views/FullScreenPicture.xaml?uri=" + ChatMessage.ImageURL, UriKind.RelativeOrAbsolute));
}
}
}
......@@ -54,6 +54,7 @@
Grid.Row="0"
Margin="5"
Stretch="UniformToFill"
Tap="Image_Tap"
x:Name="Image" />
<StackPanel
......
......@@ -84,6 +84,7 @@ namespace Linphone.Controls
{
ShowImage.Visibility = Visibility.Collapsed;
Image.Visibility = Visibility.Visible;
Save.Visibility = Visibility.Visible;
}
Image.Source = image;
Timestamp.Text = timestamp;
......@@ -125,7 +126,7 @@ namespace Linphone.Controls
private void Save_Click(object sender, RoutedEventArgs e)
{
Chat.SavePictureInMediaLibrary(ChatMessage.ImageURL);
Utils.SavePictureInMediaLibrary(ChatMessage.ImageURL);
}
/// <summary>
......@@ -140,10 +141,15 @@ namespace Linphone.Controls
private void ShowImage_Click(object sender, RoutedEventArgs e)
{
Image.Source = Chat.GetThumbnailBitmapFromImage(Chat.ReadImageFromIsolatedStorage(ChatMessage.ImageURL));
Image.Source = Utils.GetThumbnailBitmapFromImage(Utils.ReadImageFromIsolatedStorage(ChatMessage.ImageURL));
ShowImage.Visibility = Visibility.Collapsed;
Image.Visibility = Visibility.Visible;
Save.Visibility = Visibility.Visible;
}
private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
BaseModel.CurrentPage.NavigationService.Navigate(new Uri("/Views/FullScreenPicture.xaml?uri=" + ChatMessage.ImageURL, UriKind.RelativeOrAbsolute));
}
}
}
......@@ -132,6 +132,7 @@
<Compile Include="Model\MissedToBrushConverter.cs" />
<Compile Include="Model\SettingsManager.cs" />
<Compile Include="Model\InCallModel.cs" />
<Compile Include="Model\Utils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\AppResources.Designer.cs">
<AutoGen>True</AutoGen>
......@@ -174,6 +175,9 @@
<Compile Include="Views\Dialer.xaml.cs">
<DependentUpon>Dialer.xaml</DependentUpon>
</Compile>
<Compile Include="Views\FullScreenPicture.xaml.cs">
<DependentUpon>FullScreenPicture.xaml</DependentUpon>
</Compile>
<Compile Include="Views\History.xaml.cs">
<DependentUpon>History.xaml</DependentUpon>
</Compile>
......@@ -356,6 +360,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\FullScreenPicture.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\History.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
......
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace Linphone.Model
{
/// <summary>
/// Static utiliy methods, mostly image management
/// </summary>
public class Utils
{
private const int LOCAL_IMAGES_QUALITY = 100;
private const int THUMBNAIL_WIDTH = 420;
/// <summary>
/// Saves an image sent or received in the media library of the device.
/// </summary>
/// <param name="fileName">File's name in the isolated storage</param>
public static void SavePictureInMediaLibrary(string fileName)
{
MediaLibrary library = new MediaLibrary();
byte[] data;
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream file = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
data = new byte[file.Length];
file.Read(data, 0, data.Length);
file.Close();
}
}
library.SavePicture(fileName, data);
}
catch { }
}
/// <summary>
/// Get a thumbnail of a picture (used for chat images)
/// </summary>
/// <param name="image">The image to size down</param>
/// <returns>The thumbnail picture</returns>
public static BitmapImage GetThumbnailBitmapFromImage(BitmapImage image)
{
if (image == null)
return null;
if (image.PixelWidth <= THUMBNAIL_WIDTH)
return image;
MemoryStream ms = new MemoryStream();
WriteableBitmap bitmap = new WriteableBitmap(image);
int w, h;
w = THUMBNAIL_WIDTH;
h = (image.PixelHeight * w) / image.PixelWidth;
bitmap.SaveJpeg(ms, w, h, 0, LOCAL_IMAGES_QUALITY);
BitmapImage thumbnail = new BitmapImage();
thumbnail.SetSource(ms);
return thumbnail;
}
/// <summary>
/// Returns a BitmapImage of a file stored in isolated storage
/// </summary>
/// <param name="fileName">Name of the file to open</param>
/// <returns>a BitmapImage or null</returns>
public static BitmapImage ReadImageFromIsolatedStorage(string fileName)
{
byte[] data;
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream file = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
data = new byte[file.Length];
file.Read(data, 0, data.Length);
file.Close();
}
}
MemoryStream ms = new MemoryStream(data);
BitmapImage image = new BitmapImage();
image.SetSource(ms);
ms.Close();
return image;
}
catch { }
return null;
}
/// <summary>
/// Download an image received using the given url and store it locally
/// </summary>
/// <param name="url">Url to download the image</param>
/// <param name="message">ChatMessage linked to the image</param>
/// <returns>The Bitmap Image for display</returns>
public static BitmapImage DownloadImageAndStoreItInIsolatedStorage(string url, ChatMessage message)
{
//Download image
BitmapImage image = new BitmapImage(new Uri(url));
image.ImageOpened += (sender, e) =>
{
//Store it in isolated storage
string fileName = url.Substring(url.LastIndexOf("/") + 1);
SaveImageInLocalFolder(image, fileName);
//Update image url in database to point on local image
message.ImageURL = fileName;
DatabaseManager.Instance.SubmitChanges();
};
return image;
}
public static void SaveImageInLocalFolder(BitmapImage image, string fileName)
{
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(fileName))
{
store.DeleteFile(fileName);
}
using (IsolatedStorageFileStream file = store.CreateFile(fileName))
{
WriteableBitmap bitmap = new WriteableBitmap(image);
Extensions.SaveJpeg(bitmap, file, bitmap.PixelWidth, bitmap.PixelHeight, 0, LOCAL_IMAGES_QUALITY);
file.Flush();
file.Close();
bitmap = null;
}
}
}
catch { }
}
}
}
......@@ -45,9 +45,7 @@ namespace Linphone.Views
/// </summary>
public partial class Chat : BasePage, LinphoneChatMessageListener, MessageReceivedListener
{
private const int LOCAL_IMAGES_QUALITY = 100;
private const int SENT_IMAGES_QUALITY = 50;
private const int THUMBNAIL_WIDTH = 420;
private HttpClient _httpPostClient { get; set; }
......@@ -103,11 +101,14 @@ namespace Linphone.Views
chatRoom = LinphoneManager.Instance.LinphoneCore.CreateChatRoom(sipAddress);
// Define the query to gather all of the messages linked to the current contact.
var messagesInDB = from message in DatabaseManager.Instance.Messages where (message.LocalContact.Contains(sipAddress) || message.RemoteContact.Contains(sipAddress)) select message;
// Execute the query and place the results into a collection.
List<ChatMessage> messages = messagesInDB.ToList();
DisplayPastMessages(messages);
if (e.NavigationMode != NavigationMode.Back)
{
// Define the query to gather all of the messages linked to the current contact.
var messagesInDB = from message in DatabaseManager.Instance.Messages where (message.LocalContact.Contains(sipAddress) || message.RemoteContact.Contains(sipAddress)) select message;
// Execute the query and place the results into a collection.
List<ChatMessage> messages = messagesInDB.ToList();
DisplayPastMessages(messages);
}
}
else if (e.NavigationMode != NavigationMode.Back)
{
......@@ -165,90 +166,6 @@ namespace Linphone.Views
LinphoneManager.Instance.UpdateLiveTile();
}
/// <summary>
/// Saves an image sent or received in the media library of the device.
/// </summary>
/// <param name="fileName">File's name in the isolated storage</param>
public static void SavePictureInMediaLibrary(string fileName)
{
MediaLibrary library = new MediaLibrary();
byte[] data;
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream file = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
data = new byte[file.Length];
file.Read(data, 0, data.Length);
file.Close();
}
}
library.SavePicture(fileName, data);
}
catch { }
}
/// <summary>
/// Get a thumbnail of a picture (used for chat images)
/// </summary>
/// <param name="image">The image to size down</param>
/// <returns>The thumbnail picture</returns>
public static BitmapImage GetThumbnailBitmapFromImage(BitmapImage image)
{
if (image == null)
return null;
if (image.PixelWidth <= THUMBNAIL_WIDTH)
return image;
MemoryStream ms = new MemoryStream();
WriteableBitmap bitmap = new WriteableBitmap(image);
int w, h;
w = THUMBNAIL_WIDTH;
h = (image.PixelHeight * w) / image.PixelWidth;
bitmap.SaveJpeg(ms, w, h, 0, LOCAL_IMAGES_QUALITY);
BitmapImage thumbnail = new BitmapImage();
thumbnail.SetSource(ms);
return thumbnail;
}
/// <summary>
/// Returns a BitmapImage of a file stored in isolated storage
/// </summary>
/// <param name="fileName">Name of the file to open</param>
/// <returns>a BitmapImage or null</returns>
public static BitmapImage ReadImageFromIsolatedStorage(string fileName)
{
byte[] data;
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream file = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
data = new byte[file.Length];
file.Read(data, 0, data.Length);
file.Close();
}
}
MemoryStream ms = new MemoryStream(data);
BitmapImage image = new BitmapImage();
image.SetSource(ms);
ms.Close();
return image;
}
catch { }
return null;
}
/// <summary>
/// Method called when this page isn't displayed anymore.
/// </summary>
......@@ -302,60 +219,12 @@ namespace Linphone.Views
scrollToBottom();
}
/// <summary>
/// Download an image received using the given url and store it locally
/// </summary>
/// <param name="url">Url to download the image</param>
/// <param name="message">ChatMessage linked to the image</param>
/// <returns>The Bitmap Image for display</returns>
public static BitmapImage DownloadImageAndStoreItInIsolatedStorage(string url, ChatMessage message)
{
//Download image
BitmapImage image = new BitmapImage(new Uri(url));
image.ImageOpened += (sender, e) =>
{
//Store it in isolated storage
string fileName = url.Substring(url.LastIndexOf("/") + 1);
SaveImageInLocalFolder(image, fileName);
//Update image url in database to point on local image
message.ImageURL = fileName;
DatabaseManager.Instance.SubmitChanges();
};
return image;
}
private static void SaveImageInLocalFolder(BitmapImage image, string fileName)
{
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(fileName))
{
store.DeleteFile(fileName);
}
using (IsolatedStorageFileStream file = store.CreateFile(fileName))
{
WriteableBitmap bitmap = new WriteableBitmap(image);
Extensions.SaveJpeg(bitmap, file, bitmap.PixelWidth, bitmap.PixelHeight, 0, LOCAL_IMAGES_QUALITY);
file.Flush();
file.Close();
bitmap = null;
}
}
}
catch { }
}
private async Task<string> UploadImageMessage(BitmapImage image, string filePath)
{
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
//Copy image in local folder
SaveImageInLocalFolder(image, fileName);
Utils.SaveImageInLocalFolder(image, fileName);
//Upload the image
string boundary = "----------" + DateTime.Now.Ticks.ToString();
......
<phone:PhoneApplicationPage
x:Class="Linphone.Views.FullScreenPicture"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image
x:Name="Image"
Stretch="Uniform" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Linphone.Model;
using System.Windows.Media.Imaging;
namespace Linphone.Views
{
/// <summary>
/// Displays on full screen received or sent pictures
/// </summary>
public partial class FullScreenPicture : PhoneApplicationPage
{
/// <summary>
/// Public constructor
/// </summary>
public FullScreenPicture()
{
InitializeComponent();
}
/// <summary>
/// Method called when the page is displayed.
/// Check if the uri contains a sip address, if yes, it displays the matching chat history.
/// </summary>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("uri"))
{
string uri = NavigationContext.QueryString["uri"];
BitmapImage image = Utils.ReadImageFromIsolatedStorage(uri);
if (image != null)
{
Image.Source = image;
}
else
{
//TODO ?
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment