add profiler instrumentation (#7134)
authorkingces95 <kingces95@users.noreply.github.com>
Thu, 15 Aug 2019 18:34:27 +0000 (08:34 -1000)
committerGitHub <noreply@github.com>
Thu, 15 Aug 2019 18:34:27 +0000 (08:34 -1000)
Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs
Xamarin.Forms.Platform.Android/Forms.cs
Xamarin.Forms.Platform.Android/Platform.cs
Xamarin.Forms.Platform.Android/Renderers/ShellFlyoutRenderer.cs
Xamarin.Forms.Platform.Android/Renderers/ShellFlyoutTemplatedContentRenderer.cs
Xamarin.Forms.Platform.Android/Renderers/ShellRenderer.cs

index d50c0b8..72b5006 100644 (file)
@@ -102,11 +102,18 @@ namespace Xamarin.Forms.Platform.Android
 
                static void RegisterHandler(Type target, Type handler, Type filter)
                {
+                       Profile.FrameBegin();
+
+                       Profile.FramePartition(target.Name);
                        Type current = Registrar.Registered.GetHandlerType(target);
-                       if (current != filter)
-                               return;
 
-                       Registrar.Registered.Register(target, handler);
+                       if (current == filter)
+                       {
+                               Profile.FramePartition("Register");
+                               Registrar.Registered.Register(target, handler);
+                       }
+
+                       Profile.FrameEnd();
                }
 
                // This is currently being used by the previewer please do not change or remove this
@@ -144,6 +151,7 @@ namespace Xamarin.Forms.Platform.Android
 
                        if (!_renderersAdded)
                        {
+                               Profile.FramePartition("RegisterHandlers");
                                RegisterHandlers();
                                _renderersAdded = true;
                        }
@@ -151,13 +159,18 @@ namespace Xamarin.Forms.Platform.Android
                        if (_application != null)
                                _application.PropertyChanged -= AppOnPropertyChanged;
 
+                       Profile.FramePartition("SetAppIndexingProvider");
                        _application = application ?? throw new ArgumentNullException(nameof(application));
                        ((IApplicationController)application).SetAppIndexingProvider(new AndroidAppIndexProvider(this));
+
+                       Profile.FramePartition("SetCurrentApplication");
                        Xamarin.Forms.Application.SetCurrentApplication(application);
 
+                       Profile.FramePartition("SetSoftInputMode");
                        if (Xamarin.Forms.Application.Current.OnThisPlatform().GetWindowSoftInputModeAdjust() != WindowSoftInputModeAdjust.Unspecified)
                                SetSoftInputMode();
 
+                       Profile.FramePartition("CheckForAppLink");
                        CheckForAppLink(Intent);
 
                        application.PropertyChanged += AppOnPropertyChanged;
@@ -192,6 +205,7 @@ namespace Xamarin.Forms.Platform.Android
                        Bundle savedInstanceState, 
                        ActivationFlags flags)
                {
+                       Profile.FrameBegin();
                        _activityCreated = true;
                        if (!AllowFragmentRestore)
                        {
@@ -201,8 +215,10 @@ namespace Xamarin.Forms.Platform.Android
                                savedInstanceState?.Remove("android:support:fragments");
                        }
 
+                       Profile.FramePartition("Xamarin.Android.OnCreate");
                        base.OnCreate(savedInstanceState);
 
+                       Profile.FramePartition("SetSupportActionBar");
                        AToolbar bar;
                        if (ToolbarResource != 0)
                        {
@@ -217,9 +233,11 @@ namespace Xamarin.Forms.Platform.Android
 
                        SetSupportActionBar(bar);
 
+                       Profile.FramePartition("SetContentView");
                        _layout = new ARelativeLayout(BaseContext);
                        SetContentView(_layout);
 
+                       Profile.FramePartition("OnStateChanged");
                        Xamarin.Forms.Application.ClearCurrent();
 
                        _previousState = _currentState;
@@ -227,19 +245,24 @@ namespace Xamarin.Forms.Platform.Android
 
                        OnStateChanged();
 
+                       Profile.FramePartition("Forms.IsLollipopOrNewer");
                        if (Forms.IsLollipopOrNewer)
                        {
                                // Allow for the status bar color to be changed
                                if ((flags & ActivationFlags.DisableSetStatusBarColor) == 0)
                                {
+                                       Profile.FramePartition("Set DrawsSysBarBkgrnds");
                                        Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                                }
                        }
                        if (Forms.IsLollipopOrNewer)
                        {
                                // Listen for the device going into power save mode so we can handle animations being disabled
+                               Profile.FramePartition("Allocate PowerSaveModeReceiver");
                                _powerSaveModeBroadcastReceiver = new PowerSaveModeBroadcastReceiver();
                        }
+
+                       Profile.FrameEnd();
                }
 
                protected override void OnDestroy()
@@ -301,6 +324,8 @@ namespace Xamarin.Forms.Platform.Android
 
                protected override void OnResume()
                {
+                       Profile.FrameBegin();
+
                        // counterpart to OnPause
                        base.OnResume();
 
@@ -321,16 +346,24 @@ namespace Xamarin.Forms.Platform.Android
                        }
 
                        OnStateChanged();
+
+                       Profile.FrameEnd();
                }
 
                protected override void OnStart()
                {
+                       Profile.FrameBegin();
+
+                       Profile.FramePartition("Android OnStart");
                        base.OnStart();
 
                        _previousState = _currentState;
                        _currentState = AndroidApplicationLifecycleState.OnStart;
 
+                       Profile.FramePartition("OnStateChanged");
                        OnStateChanged();
+
+                       Profile.FrameEnd();
                }
 
                // Scenarios that stop and restart your app
index 3e2e2f7..20ca905 100644 (file)
@@ -260,8 +260,10 @@ namespace Xamarin.Forms
 
                        // 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("new AndroidDeviceInfo(activity)");
+                       Profile.FramePartition("create AndroidDeviceInfo");
                        Device.Info = new AndroidDeviceInfo(activity);
+
+                       Profile.FramePartition("setFlags");
                        Device.SetFlags(s_flags);
 
                        Profile.FramePartition("AndroidTicker");
index 2ecd07b..a042137 100644 (file)
@@ -338,12 +338,10 @@ namespace Xamarin.Forms.Platform.Android
 
                internal static IVisualElementRenderer CreateRenderer(VisualElement element, Context context)
                {
-                       Profile.FrameBegin(nameof(CreateRenderer));
                        IVisualElementRenderer renderer = Registrar.Registered.GetHandlerForObject<IVisualElementRenderer>(element, context)
                                ?? new DefaultRenderer(context);
-                       Profile.FramePartition(element.GetType().Name);
+
                        renderer.SetElement(element);
-                       Profile.FrameEnd();
 
                        return renderer;
                }
index a77cd56..40f73b1 100644 (file)
@@ -4,6 +4,7 @@ using Android.Util;
 using Android.Views;
 using System;
 using System.ComponentModel;
+using Xamarin.Forms.Internals;
 using AView = Android.Views.View;
 using LP = Android.Views.ViewGroup.LayoutParams;
 
@@ -81,8 +82,11 @@ namespace Xamarin.Forms.Platform.Android
 
                protected virtual void AttachFlyout(IShellContext context, AView content)
                {
+                       Profile.FrameBegin();
+
                        _content = content;
 
+                       Profile.FramePartition("Create ContentRenderer");
                        _flyoutContent = context.CreateShellFlyoutContentRenderer();
 
                        // Depending on what you read the right edge of the drawer should be Max(56dp, actionBarSize)
@@ -97,6 +101,7 @@ namespace Xamarin.Forms.Platform.Android
                        // this is about landscape devices and google does not perfectly follow these
                        // rules... so we'll kind of just... do our best.
 
+                       Profile.FramePartition("Fudge Width");
                        var metrics = Context.Resources.DisplayMetrics;
                        var width = Math.Min(metrics.WidthPixels, metrics.HeightPixels);
 
@@ -119,12 +124,19 @@ namespace Xamarin.Forms.Platform.Android
                        _flyoutContent.AndroidView.LayoutParameters =
                                new LayoutParams(width, LP.MatchParent) { Gravity = (int)GravityFlags.Start };
 
+                       Profile.FramePartition("AddView Content");
                        AddView(content);
+
+                       Profile.FramePartition("AddView Flyout");
                        AddView(_flyoutContent.AndroidView);
 
+                       Profile.FramePartition("Add DrawerListener");
                        AddDrawerListener(this);
 
+                       Profile.FramePartition("Add BehaviorObserver");
                        ((IShellController)context.Shell).AddFlyoutBehaviorObserver(this);
+
+                       Profile.FrameEnd();
                }
 
                protected virtual void OnShellPropertyChanged(object sender, PropertyChangedEventArgs e)
index 8f72d8b..f7fab97 100644 (file)
@@ -10,7 +10,7 @@ using System;
 using System.ComponentModel;
 using AView = Android.Views.View;
 using LP = Android.Views.ViewGroup.LayoutParams;
-using Android.Graphics;
+using Xamarin.Forms.Internals;
 
 namespace Xamarin.Forms.Platform.Android
 {
@@ -23,24 +23,26 @@ namespace Xamarin.Forms.Platform.Android
 
                #endregion IShellFlyoutContentRenderer
 
-        IShellContext _shellContext;
-        bool _disposed;
-        HeaderContainer _headerView;
-        ViewGroup _rootView;
-        Drawable _defaultBackgroundColor;
+               IShellContext _shellContext;
+               bool _disposed;
+               HeaderContainer _headerView;
+               ViewGroup _rootView;
+               Drawable _defaultBackgroundColor;
                ImageView _bgImage;
-        View _flyoutHeader;
-        int _actionBarHeight;
+               View _flyoutHeader;
+               int _actionBarHeight;
 
-        public ShellFlyoutTemplatedContentRenderer(IShellContext shellContext)
-        {
-            _shellContext = shellContext;
+               public ShellFlyoutTemplatedContentRenderer(IShellContext shellContext)
+               {
+                       _shellContext = shellContext;
 
                        LoadView(shellContext);
                }
 
                protected virtual void LoadView(IShellContext shellContext)
                {
+                       Profile.FrameBegin();
+
                        var context = shellContext.AndroidContext;
 
                        // Android designer can't load fragments or resources from layouts
@@ -51,13 +53,19 @@ namespace Xamarin.Forms.Platform.Android
                        }
 
                        var coordinator = LayoutInflater.FromContext(context).Inflate(Resource.Layout.FlyoutContent, null);
+
+                       Profile.FramePartition("Find Recycler");
                        var recycler = coordinator.FindViewById<RecyclerView>(Resource.Id.flyoutcontent_recycler);
+
+                       Profile.FramePartition("Find AppBar");
                        var appBar = coordinator.FindViewById<AppBarLayout>(Resource.Id.flyoutcontent_appbar);
 
                        _rootView = coordinator as ViewGroup;
 
+                       Profile.FramePartition("Add Listener");
                        appBar.AddOnOffsetChangedListener(this);
 
+                       Profile.FramePartition("Add HeaderView");
                        _actionBarHeight = (int)context.ToPixels(56);
 
                        _flyoutHeader = ((IShellController)shellContext.Shell).FlyoutHeader;
@@ -75,12 +83,14 @@ namespace Xamarin.Forms.Platform.Android
                        };
                        appBar.AddView(_headerView);
 
+                       Profile.FramePartition("Recycler.SetAdapter");
                        var adapter = new ShellFlyoutRecyclerAdapter(shellContext, OnElementSelected);
                        recycler.SetPadding(0, (int)context.ToPixels(20), 0, 0);
                        recycler.SetClipToPadding(false);
                        recycler.SetLayoutManager(new LinearLayoutManager(context, (int)Orientation.Vertical, false));
                        recycler.SetAdapter(adapter);
 
+                       Profile.FramePartition("Initialize BgImage");
                        var metrics = context.Resources.DisplayMetrics;
                        var width = Math.Min(metrics.WidthPixels, metrics.HeightPixels);
 
@@ -101,11 +111,15 @@ namespace Xamarin.Forms.Platform.Android
                                LayoutParameters = new LP(coordinator.LayoutParameters)
                        };
 
+                       Profile.FramePartition("UpdateFlyoutHeaderBehavior");
                        UpdateFlyoutHeaderBehavior();
-            _shellContext.Shell.PropertyChanged += OnShellPropertyChanged;
+                       _shellContext.Shell.PropertyChanged += OnShellPropertyChanged;
+
+                       Profile.FramePartition("UpdateFlyoutBackground");
+                       UpdateFlyoutBackground();
 
-            UpdateFlyoutBackground();
-        }
+                       Profile.FrameEnd();
+               }
 
                void OnFlyoutHeaderMeasureInvalidated(object sender, EventArgs e)
                {
@@ -118,8 +132,8 @@ namespace Xamarin.Forms.Platform.Android
                        ((IShellController)_shellContext.Shell).OnFlyoutItemSelected(element);
                }
 
-        protected virtual void OnShellPropertyChanged(object sender, PropertyChangedEventArgs e)
-        {
+               protected virtual void OnShellPropertyChanged(object sender, PropertyChangedEventArgs e)
+               {
                        if (e.PropertyName == Shell.FlyoutHeaderBehaviorProperty.PropertyName)
                                UpdateFlyoutHeaderBehavior();
                        else if (e.IsOneOf(
@@ -127,7 +141,7 @@ namespace Xamarin.Forms.Platform.Android
                                Shell.FlyoutBackgroundImageProperty,
                                Shell.FlyoutBackgroundImageAspectProperty))
                                UpdateFlyoutBackground();
-        }
+               }
 
                protected virtual void UpdateFlyoutBackground()
                {
@@ -245,31 +259,31 @@ namespace Xamarin.Forms.Platform.Android
                        _headerView.SetPadding(0, -verticalOffset, 0, 0);
                }
 
-        protected override void Dispose(bool disposing)
-        {
-            if (!_disposed)
-            {
-                if (disposing)
-                {
-                    _shellContext.Shell.PropertyChanged -= OnShellPropertyChanged;
+               protected override void Dispose(bool disposing)
+               {
+                       if (!_disposed)
+                       {
+                               if (disposing)
+                               {
+                                       _shellContext.Shell.PropertyChanged -= OnShellPropertyChanged;
 
-                    if (_flyoutHeader != null)
-                        _flyoutHeader.MeasureInvalidated += OnFlyoutHeaderMeasureInvalidated;
+                                       if (_flyoutHeader != null)
+                                               _flyoutHeader.MeasureInvalidated += OnFlyoutHeaderMeasureInvalidated;
 
-                    _headerView.Dispose();
-                    _rootView.Dispose();
-                    _defaultBackgroundColor?.Dispose();
+                                       _headerView.Dispose();
+                                       _rootView.Dispose();
+                                       _defaultBackgroundColor?.Dispose();
                                        _bgImage?.Dispose();
                                }
 
-                _flyoutHeader = null;
-                _defaultBackgroundColor = null;
+                               _flyoutHeader = null;
+                               _defaultBackgroundColor = null;
                                _bgImage = null;
                                _rootView = null;
-                _headerView = null;
-                _shellContext = null;
-                _disposed = true;
-            }
+                               _headerView = null;
+                               _shellContext = null;
+                               _disposed = true;
+                       }
 
                        base.Dispose(disposing);
                }
index 1b4b0c6..1933185 100644 (file)
@@ -8,6 +8,7 @@ using Android.Widget;
 using System;
 using System.ComponentModel;
 using System.Threading.Tasks;
+using Xamarin.Forms.Internals;
 using AColor = Android.Graphics.Color;
 using AView = Android.Views.View;
 using LP = Android.Views.ViewGroup.LayoutParams;
@@ -148,7 +149,7 @@ namespace Xamarin.Forms.Platform.Android
                event EventHandler<PropertyChangedEventArgs> _elementPropertyChanged;
 
                public ShellRenderer(Context context)
-               {
+               {       
                        AndroidContext = context;
                }
 
@@ -206,43 +207,64 @@ namespace Xamarin.Forms.Platform.Android
 
                protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
                {
+                       Profile.FrameBegin();
+
                        if (e.PropertyName == Shell.CurrentItemProperty.PropertyName)
                                SwitchFragment(FragmentManager, _frameLayout, Element.CurrentItem);
 
                        _elementPropertyChanged?.Invoke(sender, e);
+
+                       Profile.FrameEnd();
                }
 
                protected virtual void OnElementSet(Shell shell)
                {
+                       Profile.FrameBegin();
+
+                       Profile.FramePartition("Flyout");
                        _flyoutRenderer = CreateShellFlyoutRenderer();
+
+                       Profile.FramePartition("Frame");
                        _frameLayout = new CustomFrameLayout(AndroidContext)
                        {
                                LayoutParameters = new LP(LP.MatchParent, LP.MatchParent),
                                Id = Platform.GenerateViewId(),
                        };
+
+                       Profile.FramePartition("SetFitsSystemWindows");
                        _frameLayout.SetFitsSystemWindows(true);
 
+                       Profile.FramePartition("AttachFlyout");
                        _flyoutRenderer.AttachFlyout(this, _frameLayout);
 
+                       Profile.FramePartition("AddAppearanceObserver");
                        ((IShellController)shell).AddAppearanceObserver(this, shell);
 
                        // Previewer Hack
-                       if(AndroidContext.GetActivity() != null)
+                       Profile.FramePartition("Previewer Hack");
+                       if (AndroidContext.GetActivity() != null)
                                SwitchFragment(FragmentManager, _frameLayout, shell.CurrentItem, false);
+
+                       Profile.FrameEnd();
                }
 
                IShellItemRenderer _currentRenderer;
 
                protected virtual void SwitchFragment(FragmentManager manager, AView targetView, ShellItem newItem, bool animate = true)
                {
+                       Profile.FrameBegin();
+
+                       Profile.FramePartition("IsDesignerContext");
                        if (AndroidContext.IsDesignerContext())
                                return; 
 
+                       Profile.FramePartition("CreateShellItemRenderer");
                        var previousRenderer = _currentRenderer;
                        _currentRenderer = CreateShellItemRenderer(newItem);
                        _currentRenderer.ShellItem = newItem;
                        var fragment = _currentRenderer.Fragment;
 
+                       Profile.FramePartition("Transaction");
                        FragmentTransaction transaction = manager.BeginTransaction();
 
                        if (animate)
@@ -251,6 +273,7 @@ namespace Xamarin.Forms.Platform.Android
                        transaction.Replace(_frameLayout.Id, fragment);
                        transaction.CommitAllowingStateLoss();
 
+                       Profile.FramePartition("OnDestroyed");
                        void OnDestroyed (object sender, EventArgs args)
                        {
                                previousRenderer.Destroyed -= OnDestroyed;
@@ -261,19 +284,32 @@ namespace Xamarin.Forms.Platform.Android
 
                        if (previousRenderer != null)
                                previousRenderer.Destroyed += OnDestroyed;
+
+                       Profile.FrameEnd();
                }
 
                void OnElementSizeChanged(object sender, EventArgs e)
                {
+                       Profile.FrameBegin();
+
+                       Profile.FramePartition("ToPixels");
                        int width = (int)AndroidContext.ToPixels(Element.Width);
                        int height = (int)AndroidContext.ToPixels(Element.Height);
+
+                       Profile.FramePartition("Measure");
                        _flyoutRenderer.AndroidView.Measure(MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly), 
                                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.Exactly));
+
+                       Profile.FramePartition("Layout");
                        _flyoutRenderer.AndroidView.Layout(0, 0, width, height);
+
+                       Profile.FrameEnd();
                }
 
                void UpdateStatusBarColor(ShellAppearance appearance)
                {
+                       Profile.FrameBegin("UpdtStatBarClr");
+
                        var activity = AndroidContext.GetActivity();
                        var window = activity?.Window;
                        var decorView = window?.DecorView;
@@ -300,17 +336,24 @@ namespace Xamarin.Forms.Platform.Android
                                // All it really is is a drawable that only draws under the statusbar/bottom bar to make sure
                                // we dont draw over areas we dont need to. This has very limited benefits considering its
                                // only saving us a flat color fill BUT it helps people not freak out about overdraw.
+                               AColor color;
                                if (appearance != null)
                                {
-                                       var color = appearance.BackgroundColor.ToAndroid(Color.FromHex("#03A9F4"));
-                                       decorView.SetBackground(new SplitDrawable(color, statusBarHeight, navigationBarHeight));
+                                       color = appearance.BackgroundColor.ToAndroid(Color.FromHex("#03A9F4"));
                                }
                                else
                                {
-                                       var color = Color.FromHex("#03A9F4").ToAndroid();
-                                       decorView.SetBackground(new SplitDrawable(color, statusBarHeight, navigationBarHeight));
+                                       color = Color.FromHex("#03A9F4").ToAndroid();
                                }
+
+                               Profile.FramePartition("Create SplitDrawable");
+                               var split = new SplitDrawable(color, statusBarHeight, navigationBarHeight);
+
+                               Profile.FramePartition("SetBackground");
+                               decorView.SetBackground(split);
                        }
+
+                       Profile.FrameEnd();
                }
 
                class SplitDrawable : Drawable