Example #1
0
        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();
        }