private async void appBarIconBtnPreview_Click(object sender, EventArgs e) { await Task.Delay(10); ApplicationBar.IsVisible = false; LoadingProgressBar.Visibility = System.Windows.Visibility.Visible; LoadingText.Text = AppResources.MsgCreatingPreview; LockscreenData data = new LockscreenData(false) { BackgroundBitmap = GetCropImage(), DayList = VsCalendar.GetCalendarOfMonth(DateTime.Now, DateTime.Now, true, true), LiveWeather = SettingHelper.Get(Constants.WEATHER_LIVE_RESULT) as LiveWeather, Forecasts = SettingHelper.Get(Constants.WEATHER_FORECAST_RESULT) as Forecasts }; if ((bool)SettingHelper.Get(Constants.CALENDAR_SHOW_APPOINTMENT)) { Appointments appointments = new Appointments(); appointments.SearchCompleted += (s, se) => { VsCalendar.MergeCalendar(data.DayList, se.Results); LockscreenHelper.RenderLayoutToBitmap(data); DisplayPreview(data.BackgroundBitmap); }; appointments.SearchAsync(data.DayList[7].DateTime, data.DayList[data.DayList.Count - 1].DateTime, null); } else { LockscreenHelper.RenderLayoutToBitmap(data); DisplayPreview(data.BackgroundBitmap); } }
private void SetLockscreenImage(ScheduledTask task, LockscreenData data) { try { bool isProvider = LockScreenManager.IsProvidedByCurrentApplication; if (isProvider) { int itemCount = data.Items == null ? 0 : data.Items.Length; //1. 이번에 변경할 파일을 알아야 한다. 그러기 위해서 먼저 현재 락스크린 파일명을 구한다. Uri lockscreenFileUri = null; string lockscreenFileName = null; try { lockscreenFileUri = LockScreen.GetImageUri(); } catch (Exception) { ShellToast toast = new ShellToast(); toast.Title = AppResources.ApplicationTitle; toast.Content = AppResources.MsgFailShortGetLockscreen; toast.Show(); return; } if (lockscreenFileUri != null) { lockscreenFileName = lockscreenFileUri.ToString() .Replace(Constants.PREFIX_APP_DATA_FOLDER, string.Empty) .Replace(Constants.LOCKSCREEN_IMAGE_A_POSTFIX, Constants.LOCKSCREEN_IMAGE_POSTFIX) .Replace(Constants.LOCKSCREEN_IMAGE_B_POSTFIX, Constants.LOCKSCREEN_IMAGE_POSTFIX); } //2. 이번에 변경할 파일명을 구한다. using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { if (!string.IsNullOrEmpty(lockscreenFileName)) { lockscreenFileName = lockscreenFileName.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_READY_POSTFIX); } var imgNames = from element in isoStore.GetFileNames() where element.Contains(Constants.LOCKSCREEN_IMAGE_READY_POSTFIX) && element.CompareTo(lockscreenFileName) > 0 orderby element ascending select element; //이번에 업데이트 할 이미지 이름을 임시 저장 lockscreenFileName = imgNames.Any() ? imgNames.First() : isoStore.GetFileNames(string.Format("*{0}", Constants.LOCKSCREEN_IMAGE_READY_POSTFIX))[0]; using (IsolatedStorageFileStream sourceStream = isoStore.OpenFile(lockscreenFileName, FileMode.Open, FileAccess.Read)) { data.BackgroundBitmap = BitmapFactory.New(0, 0).FromStream(sourceStream); } //3. 표시할 아이템들과 이미지를 합성한다. (이미지 리사이징 포함) LockscreenHelper.RenderLayoutToBitmap(data); GC.Collect(); System.Threading.Thread.Sleep(1500); System.Diagnostics.Debug.WriteLine("이미지 생성 직후" + Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage); //4. 새로운 이름으로 저장하기 위해 전환 논리를 이용하여 새로운 이름을 구한다. if (lockscreenFileUri != null && lockscreenFileUri.ToString().EndsWith(Constants.LOCKSCREEN_IMAGE_A_POSTFIX)) { lockscreenFileName = lockscreenFileName.Replace(Constants.LOCKSCREEN_IMAGE_READY_POSTFIX, Constants.LOCKSCREEN_IMAGE_B_POSTFIX); } else { lockscreenFileName = lockscreenFileName.Replace(Constants.LOCKSCREEN_IMAGE_READY_POSTFIX, Constants.LOCKSCREEN_IMAGE_A_POSTFIX); } //5. 새로운 이름으로 이미지를 저장 using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(lockscreenFileName, FileMode.Create, FileAccess.Write)) { data.BackgroundBitmap.SaveJpeg(targetStream, data.BackgroundBitmap.PixelWidth, data.BackgroundBitmap.PixelHeight, 0, 100); //using (MemoryStream ms = new MemoryStream()) //{ // data.BackgroundBitmap.SaveJpeg(ms, data.BackgroundBitmap.PixelWidth, data.BackgroundBitmap.PixelHeight, 0, 100); // System.Diagnostics.Debug.WriteLine("{0}x{1}", data.BackgroundBitmap.PixelWidth, data.BackgroundBitmap.PixelHeight); // byte[] readBuffer = new byte[4096]; // int bytesRead = -1; // ms.Position = 0; // targetStream.Position = 0; // while ((bytesRead = ms.Read(readBuffer, 0, readBuffer.Length)) > 0) // { // targetStream.Write(readBuffer, 0, bytesRead); // } //} } //6. 새로운 이미지로 락스크린 업데이트 LockScreen.SetImageUri(new Uri(string.Format("{0}{1}", Constants.PREFIX_APP_DATA_FOLDER, lockscreenFileName), UriKind.Absolute)); Uri newLockscreenFileUri = LockScreen.GetImageUri(); //7.변경이 정상적으로 이루어진 경우 기존 파일 삭제 if (newLockscreenFileUri.ToString() != lockscreenFileUri.ToString()) { if (lockscreenFileUri.ToString().Contains(Constants.PREFIX_APP_DATA_FOLDER)) { lockscreenFileName = lockscreenFileUri.ToString().Replace(Constants.PREFIX_APP_DATA_FOLDER, string.Empty); if (isoStore.FileExists(lockscreenFileName)) { isoStore.DeleteFile(lockscreenFileName); } } } } } } catch (Exception e) { LoggingError("error case 3 : \n" + e.StackTrace); } // If debugging is enabled, launch the agent again in one minute. #if DEBUG_AGENT ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(30)); System.Diagnostics.Debug.WriteLine(Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage); /* * ShellToast toast = new ShellToast(); * toast.Title = AppResources.ApplicationTitle; * toast.Content = AppResources.MsgSuccessChangeLockscreen; * toast.Show(); */ #else IsolatedStorageSettings.ApplicationSettings[Constants.LAST_RUN_LOCKSCREEN] = DateTime.Now; IsolatedStorageSettings.ApplicationSettings.Save(); #endif }
private async Task SelectLockscreen(PhonePicture picture) { await Task.Run(async() => { if (picture != null) { try { var isProvider = LockScreenManager.IsProvidedByCurrentApplication; var op = isProvider ? LockScreenRequestResult.Granted : LockScreenRequestResult.Denied; if (!isProvider) { // If you're not the provider, this call will prompt the user for permission. // Calling RequestAccessAsync from a background agent is not allowed. op = await LockScreenManager.RequestAccessAsync(); } if (op == LockScreenRequestResult.Granted) { Dispatcher.BeginInvoke(() => { //로딩 패널 띄움 ShowLoadingPanel(AppResources.MsgApplyingLockscreen); using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream sourceStream = isoStore.OpenFile(picture.Name, FileMode.Open, FileAccess.Read)) { WriteableBitmap wb = BitmapFactory.New(0, 0).FromStream(sourceStream); Size rSize = ResolutionHelper.CurrentResolution; MemoryStream ms = null; LockscreenData data = new LockscreenData(false) { DayList = VsCalendar.GetCalendarOfMonth(DateTime.Now, DateTime.Now, true, true), LiveWeather = SettingHelper.Get(Constants.WEATHER_LIVE_RESULT) as LiveWeather, Forecasts = SettingHelper.Get(Constants.WEATHER_FORECAST_RESULT) as Forecasts, BackgroundBitmap = wb.Crop(new Rect((wb.PixelWidth - rSize.Width) / 2, (wb.PixelHeight - rSize.Height) / 2, rSize.Width, rSize.Height)) }; //편집이 필요한 이미지라면 스트림 생성 및 이미지 복사 if (picture.Warnning != null) { //메모리 스트림 생성 (close 처리는 SetLockscreen에서 한다.) ms = new MemoryStream(); //잘라내기가 된 이미지를 스트림에 저장 data.BackgroundBitmap.SaveJpeg(ms, data.BackgroundBitmap.PixelWidth, data.BackgroundBitmap.PixelHeight, 0, 100); } if ((bool)SettingHelper.Get(Constants.CALENDAR_SHOW_APPOINTMENT)) { Appointments appointments = new Appointments(); appointments.SearchCompleted += (s, se) => { VsCalendar.MergeCalendar(data.DayList, se.Results); LockscreenHelper.RenderLayoutToBitmap(data); SetLockscreen(picture, data.BackgroundBitmap, ms); }; appointments.SearchAsync(data.DayList[7].DateTime, data.DayList[data.DayList.Count - 1].DateTime, null); } else { LockscreenHelper.RenderLayoutToBitmap(data); SetLockscreen(picture, data.BackgroundBitmap, ms); } } } }); } } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } }); }