Skip to content

Commit

Permalink
[android] stop using Build.VERSION.SdkInt
Browse files Browse the repository at this point in the history
We have a beautiful .NET 6 API we can use instead, that doesn't call
into Java (no JNI!):

https://docs.microsoft.com/dotnet/api/system.operatingsystem.isandroidversionatleast

Usage of `Build.VERSION.SdkInt` came from both Xamarin.Forms and
Xamarin.Essentials. We were caching the result of the value, but still
doing that work twice.

We can also use the pattern:

    OperatingSystem.IsAndroidVersionAtLeast((int)BuildVersionCodes.JellyBean)

`BuildVersionCodes` is a regular C# enum, and so the C# compiler will
strip this information away leaving just an integer in the final IL.

I used either an integer or the enum, depending on what the existing
code did.

~~ Results ~~

Testing a `Release` build of `Maui.Controls.Sample.SingleProject.csproj`
on a Pixel 5 device:

    Before:
    12-15 13:24:54.256  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s795ms
    12-15 13:24:59.457  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s770ms
    12-15 13:25:04.799  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s880ms
    12-15 13:25:09.892  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s761ms
    12-15 13:25:15.111  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s741ms
    12-15 13:25:20.449  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s889ms
    12-15 13:25:25.547  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s769ms
    12-15 13:25:30.720  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s742ms
    12-15 13:25:35.972  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s728ms
    12-15 13:25:41.291  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s823ms
    Average(ms): 1789.8
    Std Err(ms): 18.0264003925107
    Std Dev(ms): 57.0044832544872

    After:
    12-16 11:50:30.209  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s734ms
    12-16 11:50:35.371  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s696ms
    12-16 11:50:40.688  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s785ms
    12-16 11:50:45.867  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s740ms
    12-16 11:50:51.083  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s749ms
    12-16 11:50:56.274  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s740ms
    12-16 11:51:01.465  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s697ms
    12-16 11:51:06.772  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s754ms
    12-16 11:51:11.925  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s699ms
    12-16 11:51:17.111  1741  1935 I ActivityTaskManager: Displayed com.microsoft.maui.sample/crc64dac46b470c4e9200.MainActivity: +1s700ms
    Average(ms): 1729.4
    Std Err(ms): 9.59189472650969
    Std Dev(ms): 30.3322344123285

This appears to maybe save ~60ms on startup for this app?
  • Loading branch information
jonathanpeppers committed Dec 16, 2021
1 parent 1cd8f03 commit 2eb36ad
Show file tree
Hide file tree
Showing 55 changed files with 193 additions and 600 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ void UpdateTextColor()
[PortHandler]
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
{
NativeButton.LetterSpacing = Element.CharacterSpacing.ToEm();
}

NativeButton.LetterSpacing = Element.CharacterSpacing.ToEm();
}

void IOnClickListener.OnClick(AView v) => ButtonElementManager.OnClick(Element, Element, v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ public override bool OnOptionsItemSelected(IMenuItem item)

public void SetStatusBarColor(AColor color)
{
if (Forms.IsLollipopOrNewer)
{
Window.SetStatusBarColor(color);
}
Window.SetStatusBarColor(color);
}

static void RegisterHandler(Type target, Type handler, Type filter)
Expand Down Expand Up @@ -247,15 +244,11 @@ protected override void OnCreate(Bundle savedInstanceState)

OnStateChanged();

Profile.FramePartition("Forms.IsLollipopOrNewer");
if (Forms.IsLollipopOrNewer)
// Allow for the status bar color to be changed
if ((flags & ActivationFlags.DisableSetStatusBarColor) == 0)
{
// Allow for the status bar color to be changed
if ((flags & ActivationFlags.DisableSetStatusBarColor) == 0)
{
Profile.FramePartition("Set DrawsSysBarBkgrnds");
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
}
Profile.FramePartition("Set DrawsSysBarBkgrnds");
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
}

Profile.FrameEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public override void Draw(Canvas canvas)

if (Drawable != null)
{
if ((int)Forms.SdkInt >= 18 && backgroundDrawable != null)
if (backgroundDrawable != null)
{
var outlineBounds = backgroundDrawable.GetPaddingBounds(canvas.Width, canvas.Height);
var width = (float)canvas.Width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ void UpdateFont()
[PortHandler]
protected void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
{
EditText.LetterSpacing = Element.CharacterSpacing.ToEm();
}
EditText.LetterSpacing = Element.CharacterSpacing.ToEm();
}

[PortHandler("Partially ported, still missing code related to TitleColor, etc.")]
Expand Down
18 changes: 1 addition & 17 deletions src/Compatibility/Core/src/Android/AppCompat/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,23 +695,7 @@ void UpdateBackgroundColor()
}
}

internal static int GenerateViewId()
{
// getting unique Id's is an art, and I consider myself the Jackson Pollock of the field
if ((int)Forms.SdkInt >= 17)
return global::Android.Views.View.GenerateViewId();

// Numbers higher than this range reserved for xml
// If we roll over, it can be exceptionally problematic for the user if they are still retaining things, android's internal implementation is
// basically identical to this except they do a lot of locking we don't have to because we know we only do this
// from the UI thread
if (s_id >= 0x00ffffff)
s_id = 0x00000400;

return s_id++;
}

static int s_id = 0x00000400;
internal static int GenerateViewId() => global::Android.Views.View.GenerateViewId();

#region Statics

Expand Down
38 changes: 5 additions & 33 deletions src/Compatibility/Core/src/Android/AppCompat/TabbedPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class TabbedPageRenderer : VisualElementRenderer<TabbedPage>,
#pragma warning restore CS0618 // Type or member is obsolete
ViewPager.IOnPageChangeListener, IManageFragments, NavigationBarView.IOnItemSelectedListener
{
Drawable _backgroundDrawable;
Drawable _wrappedBackgroundDrawable;
ColorStateList _originalTabTextColors;
ColorStateList _orignalTabIconColors;

Expand Down Expand Up @@ -416,11 +414,7 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)

if (tabs.Visibility != ViewStates.Gone)
{
//MinimumHeight is only available on API 16+
if ((int)Forms.SdkInt >= 16)
tabsHeight = Math.Min(height, Math.Max(tabs.MeasuredHeight, tabs.MinimumHeight));
else
tabsHeight = Math.Min(height, tabs.MeasuredHeight);
tabsHeight = Math.Min(height, Math.Max(tabs.MeasuredHeight, tabs.MinimumHeight));
}

pager.Measure(MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.AtMost), MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.AtMost));
Expand Down Expand Up @@ -728,36 +722,14 @@ void UpdateBarBackgroundColor()
{
Color tintColor = Element.BarBackgroundColor;

if (Forms.IsLollipopOrNewer)
if (tintColor == null)
{
if (tintColor == null)
_tabLayout.BackgroundTintMode = null;
else
{
_tabLayout.BackgroundTintMode = PorterDuff.Mode.Src;
_tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid());
}
_tabLayout.BackgroundTintMode = null;
}
else
{
if (tintColor == null && _backgroundDrawable != null)
_tabLayout.SetBackground(_backgroundDrawable);
else if (tintColor != null)
{
// if you don't create a new drawable then SetBackgroundColor
// just sets the color on the background drawable that's saved
// it doesn't create a new one
if (_backgroundDrawable == null && _tabLayout.Background != null)
{
_backgroundDrawable = _tabLayout.Background;
_wrappedBackgroundDrawable = ADrawableCompat.Wrap(_tabLayout.Background).Mutate();
}

if (_wrappedBackgroundDrawable != null)
_tabLayout.Background = _wrappedBackgroundDrawable;

_tabLayout.SetBackgroundColor(tintColor.ToAndroid());
}
_tabLayout.BackgroundTintMode = PorterDuff.Mode.Src;
_tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid());
}
}
}
Expand Down
17 changes: 5 additions & 12 deletions src/Compatibility/Core/src/Android/BorderBackgroundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public void UpdateDrawable()
shadowDx = 0;
shadowColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();
}
// Otherwise get values from the control (but only for supported APIs)
else if ((int)Forms.SdkInt >= 16)
// Otherwise get values from the control
else
{
shadowRadius = _renderer.ShadowRadius;
shadowDy = _renderer.ShadowDy;
Expand All @@ -151,16 +151,9 @@ public void UpdateDrawable()

if (!backgroundColorIsDefault || _drawOutlineWithBackground)
{
if (Forms.IsLollipopOrNewer)
{
var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();
_rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null);
Control.SetBackground(_rippleDrawable);
}
else
{
Control.SetBackground(_backgroundDrawable);
}
var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();
_rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null);
Control.SetBackground(_rippleDrawable);
}

_drawableEnabled = true;
Expand Down
8 changes: 2 additions & 6 deletions src/Compatibility/Core/src/Android/Cells/BaseCellView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,8 @@ public BaseCellView(Context context, Cell cell) : base(context)

SetMinimumHeight((int)context.ToPixels(DefaultMinHeight));
_androidDefaultTextColor = Color.FromUint((uint)_mainText.CurrentTextColor);

if ((int)Forms.SdkInt > 16)
{
_mainText.TextAlignment = global::Android.Views.TextAlignment.ViewStart;
_detailText.TextAlignment = global::Android.Views.TextAlignment.ViewStart;
}
_mainText.TextAlignment = global::Android.Views.TextAlignment.ViewStart;
_detailText.TextAlignment = global::Android.Views.TextAlignment.ViewStart;
}

public AView AccessoryView { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ void UpdateOnColor(SwitchCellView cell, SwitchCell switchCell)
}
else
{
if (Forms.SdkInt >= BuildVersionCodes.JellyBean)
{
aSwitch.TrackDrawable.SetColorFilter(switchCell.OnColor, FilterMode.Multiply);
}
aSwitch.TrackDrawable.SetColorFilter(switchCell.OnColor, FilterMode.Multiply);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class FlowDirectionExtensions
{
internal static void UpdateFlowDirection(this AView view, IVisualElementController controller)
{
if (view == null || controller == null || (int)Forms.SdkInt < 17)
if (view == null || controller == null)
return;

if (controller is IView v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ internal static class ScrollViewExtensions
{
internal static void HandleScrollBarVisibilityChange(this IScrollView scrollView)
{
if (Forms.SdkInt <= BuildVersionCodes.Kitkat)
return;

// According to the Android Documentation
// * <p>AwakenScrollBars method should be invoked every time a subclass directly updates
// *the scroll parameters.</ p >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class TextAlignmentExtensions
{
internal static void UpdateHorizontalAlignment(this EditText view, TextAlignment alignment, bool hasRtlSupport, AGravityFlags orMask = AGravityFlags.NoGravity)
{
if ((int)Build.VERSION.SdkInt < 17 || !hasRtlSupport)
if (!hasRtlSupport)
view.Gravity = alignment.ToHorizontalGravityFlags() | orMask;
else
view.TextAlignment = alignment.ToTextAlignment();
Expand All @@ -22,7 +22,7 @@ internal static void UpdateVerticalAlignment(this EditText view, TextAlignment a

internal static void UpdateTextAlignment(this EditText view, TextAlignment horizontal, TextAlignment vertical)
{
if ((int)Build.VERSION.SdkInt < 17 || !view.Context.HasRtlSupport())
if (!view.Context.HasRtlSupport())
{
view.Gravity = vertical.ToVerticalGravityFlags() | horizontal.ToHorizontalGravityFlags();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,7 @@ void UpdateTextColor()
[PortHandler]
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
{
LetterSpacing = Button.CharacterSpacing.ToEm();
}
LetterSpacing = Button.CharacterSpacing.ToEm();
}

float IBorderVisualElementRenderer.ShadowRadius => ShadowRadius;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@ void UpdateGravity()
[PortHandler]
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
{
LetterSpacing = Element.CharacterSpacing.ToEm();
}
LetterSpacing = Element.CharacterSpacing.ToEm();
}

[PortHandler]
Expand Down

0 comments on commit 2eb36ad

Please sign in to comment.