-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
304 lines (281 loc) · 10.9 KB
/
MainPage.xaml.cs
File metadata and controls
304 lines (281 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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 LockScreenApp.Resources;
using Microsoft.Phone.Scheduler;
using Windows.Phone.System.UserProfile;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.IO;
namespace LockScreenApp
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
ObservableCollection<ImgList> imglists;
List<string> actpho = new List<string>();
public MainPage()
{
InitializeComponent();
imglists = new ObservableCollection<ImgList>();
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("photolen"))
{
settings.Add("photolen", "0");
}
if (!settings.Contains("navchk"))
{
settings.Add("navchk", "0");
}
else
{
if(settings["navchk"] as string != "0")
{
LoadActivePhotos();
}
}
imglist.ItemsSource = imglists;
//GetLockScreenPermission();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
public async void GetLockScreenPermission()
{
if (!LockScreenManager.IsProvidedByCurrentApplication)
{
var result = await LockScreenManager.RequestAccessAsync();
if (result == LockScreenRequestResult.Granted)
{
MessageBox.Show("Now You Can Change Your LockScreen for every 20 minutes.");
MessageBox.Show("Choose the photos from your library by clicking Add Photos button");
MessageBox.Show("Click the Change Lockscreen button to change it for every 20 minutes");
}
}
}
public async void SaveActivePhotos(List<string> actlist)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (actlist.Count > 0)
{
string save = "";
foreach (string s in actlist)
{
save += s + ",";
}
settings["navchk"] = save;
settings.Save();
}
else
{
settings["navchk"] = "0";
settings.Save();
}
}
public async void LoadActivePhotos()
{
string[] stringSeparators = new string[] { "," };
string[] result;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
var ap = settings["navchk"] as string;
ap = ap.EndsWith(",") ? ap.Substring(0, ap.Length - 1) : ap;
result = ap.Split(stringSeparators, StringSplitOptions.None);
foreach(string s in result)
{
actpho.Add(s);
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(s+".jpg", System.IO.FileMode.Open, file))
{
BitmapImage image = new BitmapImage();
image.SetSource(stream);
imglists.Add(new ImgList() { ImgSrc = image, IS = s, Photos = stream });
}
}
}
public async void StartLockScreenImageChange()
{
var photolen = IsolatedStorageSettings.ApplicationSettings["navchk"] as string;
if (photolen != "0")
{
var taskName = "my task";
if (ScheduledActionService.Find(taskName) != null)
{
ScheduledActionService.Remove(taskName);
}
var periodicTask = new PeriodicTask(taskName) { Description = "some desc" };
try
{
ScheduledActionService.Add(periodicTask);
ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(10));
}
catch
{
//handle
}
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (!LockScreenManager.IsProvidedByCurrentApplication)
{
var result = await LockScreenManager.RequestAccessAsync();
if (result == LockScreenRequestResult.Granted)
{
var photolen = IsolatedStorageSettings.ApplicationSettings["navchk"] as string;
if (photolen == "0")
{
MessageBox.Show("Please choose some images");
}
else
{
StartLockScreenImageChange();
}
}
}
else
{
var photolen = IsolatedStorageSettings.ApplicationSettings["navchk"] as string;
if (photolen == "0")
{
MessageBox.Show("Please choose some images");
}
else
{
StartLockScreenImageChange();
}
}
}
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
PhotoChooserTask choose = new PhotoChooserTask();
choose.Completed += choose_Completed;
choose.Show();
}
void choose_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto != null)
{
string photolen = "";
ImgList immm = new ImgList();
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
photolen = IsolatedStorageSettings.ApplicationSettings["photolen"] as string;
int pl = int.Parse(photolen);
pl = pl + 1;
immm.IS = pl.ToString();
immm.Photos = e.ChosenPhoto;
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(pl.ToString() + ".jpg", System.IO.FileMode.Create, file))
{
byte[] buffer = new byte[1024];
while (e.ChosenPhoto.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
settings["photolen"] = pl.ToString();
settings.Save();
}
BitmapImage image = new BitmapImage();
image.SetSource(stream);
immm.ImgSrc = image;
imglists.Add(immm);
actpho.Add(immm.IS);
SaveActivePhotos(actpho);
}
}
}
private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
{
PhotoChooserTask choose = new PhotoChooserTask();
choose.Completed += choose_Completed;
choose.Show();
}
private void LongListMultiSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
{
if(imglist.SelectedItems.Count > 0)
{
if (LockScreenManager.IsProvidedByCurrentApplication)
{
LockScreen.SetImageUri(
new Uri("ms-appx:///mankatha.png",
UriKind.Absolute));
}
var taskName = "my task";
if (ScheduledActionService.Find(taskName) != null)
{
ScheduledActionService.Remove(taskName);
}
ObservableCollection<ImgList> delcol = new ObservableCollection<ImgList>();
foreach(ImgList toberemoved in imglist.SelectedItems)
{
delcol.Add(toberemoved);
actpho.Remove(toberemoved.IS);
}
foreach(ImgList rem in delcol)
{
imglists.Remove(rem);
}
SaveActivePhotos(actpho);
if (LockScreenManager.IsProvidedByCurrentApplication)
{
StartLockScreenImageChange();
}
}
}
// Sample code for building a localized ApplicationBar
//private void BuildLocalizedApplicationBar()
//{
// // Set the page's ApplicationBar to a new instance of ApplicationBar.
// ApplicationBar = new ApplicationBar();
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
class ImgList : INotifyPropertyChanged
{
private BitmapImage imgSrc;
private string is1;
private Stream photos;
public BitmapImage ImgSrc
{
get { return imgSrc; }
set { imgSrc = value; OnPropertyChanged("ImgSrc"); }
}
public Stream Photos
{
get { return photos; }
set { photos = value; OnPropertyChanged("Photos"); }
}
public string IS
{
get { return is1; }
set { is1 = value; OnPropertyChanged("IS"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}