Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Android10.0 API deprecation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Dec 27, 2019
1 parent 1de59c5 commit f478243
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 43 deletions.
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<!-- This is used by the libraries -->
<PropertyGroup Condition="'$(AndroidTargetFrameworks)' == ''">
<AndroidTargetFrameworks>MonoAndroid90;</AndroidTargetFrameworks>
<AndroidTargetFrameworks Condition="'$(MSBuildAssemblyVersion)' != '16.0'">MonoAndroid90;</AndroidTargetFrameworks>
<AndroidTargetFrameworks Condition="'$(MSBuildAssemblyVersion)' == '16.0'">MonoAndroid90;MonoAndroid10.0;</AndroidTargetFrameworks>
</PropertyGroup>

</Project>
</Project>
2 changes: 1 addition & 1 deletion PagesGallery/PagesGallery.Droid/PagesGallery.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFrameworkVersion)' == 'v9.0' ">
<ItemGroup Condition=" '$(TargetFrameworkVersion)' != 'v8.1' ">
<ProjectReference Include="..\..\Xamarin.Forms.Material.Android\Xamarin.Forms.Material.Android.csproj">
<Project>{e1586ce6-8eac-4388-a15a-1aabf108b5f8}</Project>
<Name>Xamarin.Forms.Material.Android</Name>
Expand Down
120 changes: 120 additions & 0 deletions Xamarin.Forms.ControlGallery.Android/DrawableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using ADrawable = Android.Graphics.Drawables.Drawable;
using AColorFilter = Android.Graphics.ColorFilter;
using AColor = Android.Graphics.Color;
using ADrawableCompat = Android.Support.V4.Graphics.Drawable.DrawableCompat;
using Android.Graphics;

namespace Xamarin.Forms.Platform.Android
{
enum FilterMode
{
SrcIn,
Multiply,
SrcAtop
}

internal static class DrawableExtensions
{

#if __ANDROID_29__
public static BlendMode GetFilterMode(FilterMode mode)
{
switch (mode)
{
case FilterMode.SrcIn:
return BlendMode.SrcIn;
case FilterMode.Multiply:
return BlendMode.Multiply;
case FilterMode.SrcAtop:
return BlendMode.SrcAtop;
}

throw new Exception("Invalid Mode");
}

#else
[Obsolete]
static PorterDuff.Mode GetFilterMode(FilterMode mode)
{
return GetFilterModePre29(mode);
}
#endif

[Obsolete]
static PorterDuff.Mode GetFilterModePre29(FilterMode mode)
{
switch (mode)
{
case FilterMode.SrcIn:
return PorterDuff.Mode.SrcIn;
case FilterMode.Multiply:
return PorterDuff.Mode.Multiply;
case FilterMode.SrcAtop:
return PorterDuff.Mode.SrcAtop;
}

throw new Exception("Invalid Mode");
}

public static AColorFilter GetColorFilter(this ADrawable drawable)
{
if (drawable == null)
return null;

return ADrawableCompat.GetColorFilter(drawable);
}

public static void SetColorFilter(this ADrawable drawable, AColorFilter colorFilter)
{
if (drawable == null)
return;

if (colorFilter == null)
ADrawableCompat.ClearColorFilter(drawable);

drawable.SetColorFilter(colorFilter);
}


public static void SetColorFilter(this ADrawable drawable, Color color, AColorFilter defaultColorFilter, FilterMode mode)
{
if (drawable == null)
return;

if (color == Color.Default)
{
SetColorFilter(drawable, defaultColorFilter);
return;
}

drawable.SetColorFilter(color.ToAndroid(), mode);
}

public static void SetColorFilter(this ADrawable drawable, Color color, FilterMode mode)
{
drawable.SetColorFilter(color.ToAndroid(), mode);
}

public static void SetColorFilter(this ADrawable drawable, AColor color, FilterMode mode)
{
#if __ANDROID_29__
if((int)global::Android.OS.Build.VERSION.SdkInt >= 29)
drawable.SetColorFilter(new BlendModeColorFilter(color, GetFilterMode(mode)));
else
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
drawable.SetColorFilter(color, GetFilterModePre29(mode));
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0612 // Type or member is obsolete
#else
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
drawable.SetColorFilter(color, GetFilterMode(mode));
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0612 // Type or member is obsolete
#endif
}

}
}
16 changes: 12 additions & 4 deletions Xamarin.Forms.ControlGallery.Android/Issue7249SwitchRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
if (Control.Checked)
{
Control.TrackDrawable.SetColorFilter(_view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
Control.TrackDrawable.SetColorFilter(_view.SwitchOnColor, FilterMode.SrcAtop);
}
else
{
Control.TrackDrawable.SetColorFilter(_view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
Control.TrackDrawable.SetColorFilter(_view.SwitchOffColor, FilterMode.SrcAtop);
}

Control.ThumbDrawable.SetColorFilter(_view.SwitchThumbColor.ToAndroid(), PorterDuff.Mode.Multiply);

Control.TrackDrawable.SetColorFilter(_view.SwitchThumbColor, FilterMode.Multiply);
Control.CheckedChange += OnCheckedChange;
}
}
Expand All @@ -55,11 +54,20 @@ void OnCheckedChange(object sender, CheckedChangeEventArgs e)
{
if (Control.Checked)
{

#if __ANDROID_29__
Control.TrackDrawable.SetColorFilter(new BlendModeColorFilter(_view.SwitchOffColor.ToAndroid(), BlendMode.SrcAtop));
#else
Control.TrackDrawable.SetColorFilter(_view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
#endif
}
else
{
#if __ANDROID_29__
Control.TrackDrawable.SetColorFilter(new BlendModeColorFilter(_view.SwitchOffColor.ToAndroid(), BlendMode.SrcAtop));
#else
Control.TrackDrawable.SetColorFilter(_view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="AndroidControlGallery.AndroidControlGallery" android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="28" />
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="AndroidControlGallery.AndroidControlGallery" android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application android:label="AndroidControlGallery" android:supportsRtl="true">
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
<AndroidApplication>true</AndroidApplication>
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidTargetFrameworkVersion Condition="$(AndroidTargetFrameworkVersion) == ''">v9.0</AndroidTargetFrameworkVersion>
<AndroidTargetFrameworkVersion Condition="$(AndroidTargetFrameworkVersion) == '' AND '$(MSBuildAssemblyVersion)' != '16.0'">v9.0</AndroidTargetFrameworkVersion>
<AndroidTargetFrameworkVersion Condition="$(AndroidTargetFrameworkVersion) == ''">v10.0</AndroidTargetFrameworkVersion>
<TargetFrameworkVersion>$(AndroidTargetFrameworkVersion)</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<AndroidManifest Condition="$(AndroidTargetFrameworkVersion) != 'v9.0'">Properties\AndroidManifest.xml</AndroidManifest>
<AndroidManifest Condition="$(AndroidTargetFrameworkVersion) == 'v9.0'">Properties\AndroidManifest28.xml</AndroidManifest>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<NuGetPackageImportStamp>
Expand Down Expand Up @@ -84,6 +86,7 @@
<Compile Include="BorderEffect.cs" />
<Compile Include="BrokenNativeControl.cs" />
<Compile Include="CacheService.cs" />
<Compile Include="DrawableExtensions.cs" />
<Compile Include="FormsApplicationActivity.cs" />
<Compile Include="DisposeLabelRenderer.cs" />
<Compile Include="DisposePageRenderer.cs" />
Expand Down Expand Up @@ -220,7 +223,8 @@
</AndroidResource>
</ItemGroup>
<ItemGroup>
<None Include="Properties\AndroidManifest.xml" />
<None Condition="$(AndroidTargetFrameworkVersion) != 'v9.0'" Include="Properties\AndroidManifest.xml" />
<None Condition="$(AndroidTargetFrameworkVersion) == 'v9.0'" Include="Properties\AndroidManifest28.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable\error.xml" />
Expand Down Expand Up @@ -380,4 +384,4 @@
</CreateItem>
<Copy SourceFiles="@(MapsKey)" DestinationFiles="Properties\MapsKey.cs" Condition="!Exists('Properties\MapsKey.cs')" />
</Target>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
if (Control != null)
{
Drawable drawable = Control.Background;
drawable.SetColorFilter(global::Android.Graphics.Color.Blue, PorterDuff.Mode.SrcAtop);
drawable.SetColorFilter(global::Android.Graphics.Color.Blue, FilterMode.SrcAtop);
Control.Background = drawable;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Xamarin.Forms.Material.Android/MaterialColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ internal static void ApplySeekBarColors(this ASeekBar seekBar, Color progressCol
}
else
{
seekBar.Thumb.SetColorFilter(thumbColor.ToAndroid(), PorterDuff.Mode.SrcIn);
seekBar.Thumb.SetColorFilter(thumbColor.ToAndroid(), FilterMode.SrcIn);
}
}

Expand All @@ -216,7 +216,7 @@ internal static void ApplyProgressBarColors(this AProgressBar progressBar, Platf
else
{
(progressBar.Indeterminate ? progressBar.IndeterminateDrawable :
progressBar.ProgressDrawable).SetColorFilter(progressColor, PorterDuff.Mode.SrcIn);
progressBar.ProgressDrawable).SetColorFilter(progressColor, FilterMode.SrcIn);

}
}
Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Platform.Android/AppCompat/PageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public EmbeddedFragment(ViewGroup content, Platform platform)
_platform = platform;
}

#if __ANDROID_29__
[Obsolete]
#endif
public override global::Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return _content;
Expand Down
6 changes: 3 additions & 3 deletions Xamarin.Forms.Platform.Android/AppCompat/SwitchRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void UpdateOnColor()
}
else
{
Control.TrackDrawable?.SetColorFilter(Element.OnColor.ToAndroid(), PorterDuff.Mode.Multiply);
Control.TrackDrawable?.SetColorFilter(Element.OnColor, FilterMode.Multiply);
}
}
else
Expand All @@ -141,7 +141,7 @@ void UpdateThumbColor()

if (Element.ThumbColor != Color.Default)
{
Control.ThumbDrawable?.SetColorFilter(Element.ThumbColor.ToAndroid(), PorterDuff.Mode.Multiply);
Control.ThumbDrawable?.SetColorFilter(Element.ThumbColor, FilterMode.Multiply);
_changedThumbColor = true;
}
else
Expand All @@ -164,4 +164,4 @@ void UpdateEnabled()
Control.Enabled = Element.IsEnabled;
}
}
}
}
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.Android/Cells/SwitchCellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void UpdateOnColor(SwitchCellView cell, SwitchCell switchCell)
{
if (Forms.SdkInt >= BuildVersionCodes.JellyBean)
{
aSwitch.TrackDrawable.SetColorFilter(switchCell.OnColor.ToAndroid(), PorterDuff.Mode.Multiply);
aSwitch.TrackDrawable.SetColorFilter(switchCell.OnColor, FilterMode.Multiply);
}
}
}
Expand Down
Loading

0 comments on commit f478243

Please sign in to comment.