summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.ControlGallery.Android/Activity1.cs
blob: bc1772c6ee8d59b2b36a7a44db365466f2a6cbec (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Xamarin.Forms;
using Xamarin.Forms.ControlGallery.Android;
using Xamarin.Forms.Controls;
using Xamarin.Forms.Maps.Android;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppLinks;
using System.IO;
using System.IO.IsolatedStorage;

using Droid = Android;


[assembly: Dependency (typeof (CacheService))]
[assembly: Dependency (typeof (TestCloudService))]
[assembly: Dependency (typeof (StringProvider))]
[assembly: ExportRenderer (typeof (DisposePage), typeof (DisposePageRenderer))]
[assembly: ExportRenderer (typeof (DisposeLabel), typeof (DisposeLabelRenderer))]
[assembly: ExportRenderer (typeof (CustomButton), typeof (CustomButtonRenderer))]
[assembly: ExportEffect (typeof (BorderEffect), "BorderEffect")]

namespace Xamarin.Forms.ControlGallery.Android
{
	public class BorderEffect : PlatformEffect
	{
		protected override void OnAttached ()
		{
			Control.SetBackgroundColor (global::Android.Graphics.Color.Aqua);
		}

		protected override void OnDetached ()
		{
		}

		protected override void OnElementPropertyChanged (PropertyChangedEventArgs args)
		{
			base.OnElementPropertyChanged (args);
		}
	}

	public class CacheService : ICacheService
	{
		public void ClearImageCache ()
		{
			DeleteFilesInDirectory ("ImageLoaderCache");
		}

		static void DeleteFilesInDirectory (string directory)
		{
			using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication ()) {
				if (isolatedStorage.DirectoryExists (directory)) {
					var files = isolatedStorage.GetFileNames (Path.Combine (directory, "*"));
					foreach (string file in files) {
						isolatedStorage.DeleteFile (Path.Combine (directory, file));
					}
				}
			}
		}
	}

	public class DisposePageRenderer : PageRenderer
	{
		protected override void Dispose (bool disposing)
		{
			if (disposing) {
				((DisposePage) Element).SendRendererDisposed ();
			}
			base.Dispose (disposing);

		}
	}

	public class DisposeLabelRenderer : LabelRenderer
	{
		protected override void Dispose (bool disposing)
		{

			if (disposing) {
				((DisposeLabel) Element).SendRendererDisposed ();
			}
			base.Dispose (disposing);
		}
	}

	public class StringProvider : IStringProvider
	{
		public string CoreGalleryTitle
		{
			get
			{
				return "Android CoreGallery";
			}
		}
	}

	public class TestCloudService : ITestCloudService
	{
		public bool IsOnTestCloud ()
		{
			var isInTestCloud = System.Environment.GetEnvironmentVariable ("XAMARIN_TEST_CLOUD");

			return isInTestCloud != null && isInTestCloud.Equals ("1");
		}

		public string GetTestCloudDeviceName ()
		{
			return System.Environment.GetEnvironmentVariable ("XTC_DEVICE_NAME");
		}

		public string GetTestCloudDevice ()
		{
			return System.Environment.GetEnvironmentVariable ("XTC_DEVICE");
		}
	}

#if PRE_APPLICATION_CLASS
	[Activity (Label = "Control Gallery", 
			   Icon = "@drawable/icon",
			   MainLauncher = true, 
			   HardwareAccelerated = true, 
			   ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
	public class Activity1 : AndroidActivity
	{


		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
			
			Forms.Init (this, bundle);
			FormsMaps.Init (this, bundle);

			SetPage (FormsApp.GetFormsApp ());

			MessagingCenter.Subscribe<RootPagesGallery, Type> (this, Messages.ChangeRoot, (sender, pageType) => {
				var page = ((Page)Activator.CreateInstance (pageType));
				SetPage (page);
			});

			MessagingCenter.Subscribe<RootPagesGallery, Type> (this, Messages.ChangeRoot, (sender, pageType) => {
				var page = ((Page)Activator.CreateInstance (pageType));
				SetPage (page);
			});

			MessagingCenter.Subscribe<HomeButton> (this, Messages.GoHome, (sender) => {
				var screen = FormsApp.GetFormsApp ();
				SetPage (screen);
 			});
		}
		
		public override void OnConfigurationChanged (global::Android.Content.Res.Configuration newConfig)
		{
			// we're good
			base.OnConfigurationChanged (newConfig);
		}
		
		protected override void OnDestroy ()
		{
			base.OnDestroy ();
		}
	}

#elif FORMS_APPLICATION_ACTIVITY
	[Activity(Label = "Control Gallery",
		   Icon = "@drawable/icon",
		//   Theme="@style/TestStyle",
		   MainLauncher = true,
		   HardwareAccelerated = true,
		   ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

	public class Activity1 : FormsApplicationActivity
	{
		protected override void OnCreate(Bundle bundle)
		{
			//ToolbarResource = Resource.Layout.Toolbar;
			//TabLayoutResource = Resource.Layout.Tabbar;

			base.OnCreate(bundle);

			if (!Debugger.IsAttached)
				Insights.Initialize(App.Config["InsightsApiKey"], this.ApplicationContext);

			Forms.Init(this, bundle);
			FormsMaps.Init(this, bundle);
			Xamarin.Forms.Forms.ViewInitialized += (object sender, Xamarin.Forms.ViewInitializedEventArgs e) => {
				if (!string.IsNullOrWhiteSpace(e.View.StyleId))
				{
					e.NativeView.ContentDescription = e.View.StyleId;
				}
			};
			// uncomment to verify turning off title bar works. This is not intended to be dynamic really.
			//Forms.SetTitleBarVisibility (AndroidTitleBarVisibility.Never);

			var app = new App ();

			var mdp = app.MainPage as MasterDetailPage;
			var detail = mdp?.Detail as NavigationPage;
			if (detail != null) {
				detail.Pushed += (sender, args) => {
					var nncgPage = args.Page as NestedNativeControlGalleryPage;

					if (nncgPage != null) {
						AddNativeControls (nncgPage);
					}
				};
			} 

			LoadApplication (app);
		}

		private void AddNativeControls (NestedNativeControlGalleryPage page)
		{
			if (page.NativeControlsAdded) {
				return;
			}

			StackLayout sl = page.Layout;

			// Create and add a native TextView
			var textView = new TextView (this) { Text = "I am a native TextView", TextSize = 14 };
			sl?.Children.Add (textView);

			// Create and add a native Button 
			var button = new global::Android.Widget.Button (this) { Text = "Click to change TextView font size" };
			float originalSize = textView.TextSize;
			button.Click += (sender, args) => { textView.TextSize = textView.TextSize == originalSize ? 24 : 14; };

			sl?.Children.Add (button.ToView ());

			// Create a control which we know doesn't behave correctly with regard to measurement
			var difficultControl0 = new BrokenNativeControl (this) {
				Text = "This native control doesn't play nice with sizing, which is why it's all squished to one side."
			};
			var difficultControl1 = new BrokenNativeControl (this) {
				Text = "Same control, but with a custom GetDesiredSize delegate to accomodate it's sizing problems."
			};

			// Add a misbehaving control 
			sl?.Children.Add (difficultControl0);

			// Add a misbehaving control with a custom delegate for GetDesiredSize
			sl?.Children.Add (difficultControl1, SizeBrokenControl);

			page.NativeControlsAdded = true;
		}

		private static SizeRequest? SizeBrokenControl (NativeViewWrapperRenderer renderer,
			int widthConstraint, int heightConstraint)
		{
			global::Android.Views.View nativeView = renderer.Control;

			if ((widthConstraint == 0 && heightConstraint == 0) || nativeView == null) {
				return null;
			}

			int width = global::Android.Views.View.MeasureSpec.GetSize (widthConstraint);
			int widthSpec = global::Android.Views.View.MeasureSpec.MakeMeasureSpec (width * 2,
				global::Android.Views.View.MeasureSpec.GetMode (widthConstraint));
			nativeView.Measure (widthSpec, heightConstraint);
			var size = new Size (nativeView.MeasuredWidth, nativeView.MeasuredHeight);
			return new SizeRequest (size);
		}

		public override void OnConfigurationChanged(global::Android.Content.Res.Configuration newConfig)
		{
			// we're good
			base.OnConfigurationChanged(newConfig);
		}

		protected override void OnDestroy()
		{
			base.OnDestroy();
		}
	}
#else

	[Activity (Label = "Control Gallery",
			   Icon = "@drawable/icon",
			   Theme = "@style/MyTheme",
			   MainLauncher = true,
			   HardwareAccelerated = true,
			   ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
	[IntentFilter (new[] { Intent.ActionView },
		Categories = new[]
		{
			Intent.ActionView,
			Intent.CategoryDefault,
			Intent.CategoryBrowsable
		},
		DataScheme = "http",
		DataHost = App.AppName,
		DataPathPrefix = "/gallery/"
		)
	]
	public class Activity1 : FormsAppCompatActivity
	{
		protected override void OnCreate (Bundle bundle)
		{
			ToolbarResource = Resource.Layout.Toolbar;
			TabLayoutResource = Resource.Layout.Tabbar;

			base.OnCreate (bundle);

			if (!Debugger.IsAttached)
				Insights.Initialize (App.InsightsApiKey, ApplicationContext);

			Forms.Init (this, bundle);
			FormsMaps.Init (this, bundle);
			AndroidAppLinks.Init(this);
			Forms.ViewInitialized += (sender, e) => {
//				if (!string.IsNullOrWhiteSpace(e.View.StyleId)) {
//					e.NativeView.ContentDescription = e.View.StyleId;
//				}
			};
			// uncomment to verify turning off title bar works. This is not intended to be dynamic really.
			//Forms.SetTitleBarVisibility (AndroidTitleBarVisibility.Never);

			var app = new App ();

			var mdp = app.MainPage as MasterDetailPage;
			var detail = mdp?.Detail as NavigationPage;
			if (detail != null) {
				detail.Pushed += (sender, args) => {
					var nncgPage = args.Page as NestedNativeControlGalleryPage;

					if (nncgPage != null) {
						AddNativeControls (nncgPage);
					}
				};
			}

			LoadApplication (app);
		}

		public override void OnConfigurationChanged (global::Android.Content.Res.Configuration newConfig)
		{
			// we're good
			base.OnConfigurationChanged (newConfig);
		}

		protected override void OnDestroy ()
		{
			base.OnDestroy ();
		}

		void AddNativeControls (NestedNativeControlGalleryPage page)
		{
			if (page.NativeControlsAdded) {
				return;
			}

			StackLayout sl = page.Layout;

			// Create and add a native TextView
			var textView = new TextView (this) { Text = "I am a native TextView", TextSize = 14 };
			sl?.Children.Add (textView);

			// Create and add a native Button 
			var button = new global::Android.Widget.Button (this) { Text = "Click to change TextView font size" };
			float originalSize = textView.TextSize;
			button.Click += (sender, args) => {
				textView.TextSize = textView.TextSize == originalSize ? 24 : 14;
			};

			sl?.Children.Add (button.ToView ());

			// Create a control which we know doesn't behave correctly with regard to measurement
			var difficultControl0 = new BrokenNativeControl (this) {
				Text = "This native control doesn't play nice with sizing, which is why it's all squished to one side."
			};
			var difficultControl1 = new BrokenNativeControl (this) {
				Text = "Same control, but with a custom GetDesiredSize delegate to accomodate it's sizing problems."
			};

			// Add a misbehaving control 
			sl?.Children.Add (difficultControl0);

			// Add a misbehaving control with a custom delegate for GetDesiredSize
			sl?.Children.Add (difficultControl1, SizeBrokenControl);

			page.NativeControlsAdded = true;
		}

		static SizeRequest? SizeBrokenControl (NativeViewWrapperRenderer renderer,
			int widthConstraint, int heightConstraint)
		{
			global::Android.Views.View nativeView = renderer.Control;

			if ((widthConstraint == 0 && heightConstraint == 0) || nativeView == null) {
				return null;
			}

			int width = global::Android.Views.View.MeasureSpec.GetSize (widthConstraint);
			int widthSpec = global::Android.Views.View.MeasureSpec.MakeMeasureSpec (width * 2,
				global::Android.Views.View.MeasureSpec.GetMode (widthConstraint));
			nativeView.Measure (widthSpec, heightConstraint);
			var size = new Size (nativeView.MeasuredWidth, nativeView.MeasuredHeight);
			return new SizeRequest (size);
		}
	}
#endif
}