void SetUpForceRestartTest() { // Listen for messages from the app restart test MessagingCenter.Subscribe <RestartAppTest>(this, RestartAppTest.ForceRestart, (e) => { // We can force a restart by making a configuration change; in this case, we'll enter // Car Mode. (The easy way to do this is to change the orientation, but ControlGallery // handles orientation changes so they don't cause a restart.) var uiModeManager = UiModeManager.FromContext(this); if (uiModeManager.CurrentModeType == UiMode.TypeCar) { // If for some reason we're already in car mode, disable it uiModeManager.DisableCarMode(DisableCarModeFlags.None); } uiModeManager.EnableCarMode(EnableCarModeFlags.None); // And put things back to normal so we can keep running tests uiModeManager.DisableCarMode(DisableCarModeFlags.None); ((App)Xamarin.Forms.Application.Current).Reset(); }); }
static DeviceIdiom GetIdiom() { var currentIdiom = DeviceIdiom.Unknown; // first try UIModeManager using (var uiModeManager = UiModeManager.FromContext(Essentials.Platform.AppContext)) { try { var uiMode = uiModeManager?.CurrentModeType ?? UiMode.TypeUndefined; currentIdiom = DetectIdiom(uiMode); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to detect using UiModeManager: {ex.Message}"); } } // then try Configuration if (currentIdiom == DeviceIdiom.Unknown) { var configuration = Essentials.Platform.AppContext.Resources?.Configuration; if (configuration != null) { var uiMode = configuration.UiMode; currentIdiom = DetectIdiom(uiMode); // now just guess if (currentIdiom == DeviceIdiom.Unknown) { var minWidth = configuration.SmallestScreenWidthDp; var isWide = minWidth >= tabletCrossover; currentIdiom = isWide ? DeviceIdiom.Tablet : DeviceIdiom.Phone; } } } // start clutching at straws if (currentIdiom == DeviceIdiom.Unknown) { var metrics = Essentials.Platform.AppContext.Resources?.DisplayMetrics; if (metrics != null) { var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels); var isWide = minSize * metrics.Density >= tabletCrossover; currentIdiom = isWide ? DeviceIdiom.Tablet : DeviceIdiom.Phone; } } // hope we got it somewhere return(currentIdiom); }
static string GetIdiom() { var currentIdiom = Idioms.Unsupported; // first try UiModeManager using (var uiModeManager = UiModeManager.FromContext(Essentials.Platform.CurrentContext)) { var uiMode = uiModeManager?.CurrentModeType ?? UiMode.TypeUndefined; currentIdiom = DetectIdiom(uiMode); } // then try Configuration if (currentIdiom == Idioms.Unsupported) { var configuration = Essentials.Platform.CurrentContext.Resources?.Configuration; if (configuration != null) { var uiMode = configuration.UiMode; currentIdiom = DetectIdiom(uiMode); // now just guess if (currentIdiom == Idioms.Unsupported) { var minWidth = configuration.SmallestScreenWidthDp; var isWide = minWidth >= tabletCrossover; currentIdiom = isWide ? Idioms.Tablet : Idioms.Phone; } } } // start clutching at straws if (currentIdiom == Idioms.Unsupported) { var metrics = Essentials.Platform.CurrentContext.Resources?.DisplayMetrics; if (metrics != null) { var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels); var isWide = minSize * metrics.Density >= tabletCrossover; currentIdiom = isWide ? Idioms.Tablet : Idioms.Phone; } } // hope we got it somewhere return(currentIdiom); }
static void SetupInit( Context activity, Assembly resourceAssembly, InitializationOptions?maybeOptions = null ) { Profile.FrameBegin(); Registrar.RegisterRendererToHandlerShim(RendererToHandlerShim.CreateShim); if (!IsInitialized) { // Only need to get this once; it won't change ApplicationContext = activity.ApplicationContext; } #pragma warning disable 618 // Still have to set this up so obsolete code can function Context = activity; #pragma warning restore 618 if (!IsInitialized) { // Only need to do this once Profile.FramePartition("ResourceManager.Init"); ResourceManager.Init(resourceAssembly); } Profile.FramePartition("Color.SetAccent()"); // We want this to be updated when we have a new activity (e.g. on a configuration change) // This could change if the UI mode changes (e.g., if night mode is enabled) Color.SetAccent(GetAccentColor(activity)); _ColorButtonNormalSet = false; if (!IsInitialized) { // Only need to do this once Profile.FramePartition("Log.Listeners"); Internals.Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c))); } // We want this to be updated when we have a new activity (e.g. on a configuration change) // because AndroidPlatformServices needs a current activity to launch URIs from Profile.FramePartition("Device.PlatformServices"); var androidServices = new AndroidPlatformServices(activity); Device.PlatformServices = androidServices; Device.PlatformInvalidator = androidServices; // use field and not property to avoid exception in getter if (Device.info != null) { ((AndroidDeviceInfo)Device.info).Dispose(); Device.info = null; } // We want this to be updated when we have a new activity (e.g. on a configuration change) // because Device.Info watches for orientation changes and we need a current activity for that Profile.FramePartition("create AndroidDeviceInfo"); Device.Info = new AndroidDeviceInfo(activity); Profile.FramePartition("setFlags"); Device.SetFlags(s_flags); Profile.FramePartition("AndroidTicker"); Ticker.SetDefault(null); Profile.FramePartition("RegisterAll"); if (!IsInitialized) { if (maybeOptions.HasValue) { var options = maybeOptions.Value; var handlers = options.Handlers; var flags = options.Flags; var effectScopes = options.EffectScopes; //TODO: ExportCell? //TODO: ExportFont // renderers Registrar.RegisterRenderers(handlers); // effects if (effectScopes != null) { for (var i = 0; i < effectScopes.Length; i++) { var effectScope = effectScopes[0]; Registrar.RegisterEffects(effectScope.Name, effectScope.Effects); } } // css Registrar.RegisterStylesheets(flags); } else { // Only need to do this once Registrar.RegisterAll(new[] { typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute), typeof(ExportFontAttribute) }); } } Profile.FramePartition("Epilog"); var currentIdiom = TargetIdiom.Unsupported; // First try UIModeManager using (var uiModeManager = UiModeManager.FromContext(ApplicationContext)) { try { var uiMode = uiModeManager?.CurrentModeType ?? UiMode.TypeUndefined; currentIdiom = DetectIdiom(uiMode); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to detect using UiModeManager: {ex.Message}"); } } // Then try Configuration if (TargetIdiom.Unsupported == currentIdiom) { var configuration = activity.Resources.Configuration; if (configuration != null) { var minWidth = configuration.SmallestScreenWidthDp; var isWide = minWidth >= TabletCrossover; currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone; } else { // Start clutching at straws var metrics = activity.Resources?.DisplayMetrics; if (metrics != null) { var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels); var isWide = minSize * metrics.Density >= TabletCrossover; currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone; } } } Device.SetIdiom(currentIdiom); if (SdkInt >= BuildVersionCodes.JellyBeanMr1) { Device.SetFlowDirection(activity.Resources.Configuration.LayoutDirection.ToFlowDirection()); } if (ExpressionSearch.Default == null) { ExpressionSearch.Default = new AndroidExpressionSearch(); } IsInitialized = true; Profile.FrameEnd(); }
static void SetupInit( IMauiContext context, Assembly resourceAssembly, InitializationOptions?maybeOptions = null ) { var activity = context.Context; Profile.FrameBegin(); Registrar.RegisterRendererToHandlerShim(RendererToHandlerShim.CreateShim); // Allow this multiple times to support the app and then the activity ApplicationContext = activity.ApplicationContext; MauiContext = context; if (!IsInitialized) { // Only need to do this once Profile.FramePartition("ResourceManager.Init"); ResourceManager.Init(resourceAssembly); } Profile.FramePartition("Color.SetAccent()"); // We want this to be updated when we have a new activity (e.g. on a configuration change) // This could change if the UI mode changes (e.g., if night mode is enabled) Application.AccentColor = GetAccentColor(activity); _ColorButtonNormalSet = false; if (!IsInitialized) { // Only need to do this once Profile.FramePartition("Log.Listeners"); Internals.Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c))); } // We want this to be updated when we have a new activity (e.g. on a configuration change) // because AndroidPlatformServices needs a current activity to launch URIs from Profile.FramePartition("Device.PlatformServices"); var androidServices = new AndroidPlatformServices(activity); Device.PlatformServices = androidServices; Device.PlatformInvalidator = androidServices; // use field and not property to avoid exception in getter if (Device.info != null) { ((AndroidDeviceInfo)Device.info).Dispose(); Device.info = null; } // We want this to be updated when we have a new activity (e.g. on a configuration change) // because Device.Info watches for orientation changes and we need a current activity for that Profile.FramePartition("create AndroidDeviceInfo"); Device.Info = new AndroidDeviceInfo(activity); Profile.FramePartition("setFlags"); Device.SetFlags(s_flags); Profile.FramePartition("AndroidTicker"); Ticker.SetDefault(null); Profile.FramePartition("RegisterAll"); if (maybeOptions?.Flags.HasFlag(InitializationFlags.SkipRenderers) != true) { RegisterCompatRenderers(maybeOptions); } Profile.FramePartition("Epilog"); var currentIdiom = TargetIdiom.Unsupported; // First try UIModeManager using (var uiModeManager = UiModeManager.FromContext(ApplicationContext)) { try { var uiMode = uiModeManager?.CurrentModeType ?? UiMode.TypeUndefined; currentIdiom = DetectIdiom(uiMode); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to detect using UiModeManager: {ex.Message}"); } } // Then try Configuration if (TargetIdiom.Unsupported == currentIdiom) { var configuration = activity.Resources.Configuration; if (configuration != null) { var minWidth = configuration.SmallestScreenWidthDp; var isWide = minWidth >= TabletCrossover; currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone; } else { // Start clutching at straws var metrics = activity.Resources?.DisplayMetrics; if (metrics != null) { var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels); var isWide = minSize * metrics.Density >= TabletCrossover; currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone; } } } Device.SetIdiom(currentIdiom); if (SdkInt >= BuildVersionCodes.JellyBeanMr1) { Device.SetFlowDirection(activity.Resources.Configuration.LayoutDirection.ToFlowDirection()); } if (ExpressionSearch.Default == null) { ExpressionSearch.Default = new AndroidExpressionSearch(); } IsInitialized = true; Profile.FrameEnd(); }