Xamarin SDK for Malwarelytics

Introduction

Xamarin SDK for Malwarelytics is just a Binding Library that is bridging all the classes and methods from the Android Kotlin SDK for you to use in C#.

This documentation contains just a sample of how to integrate the SDK to your Xamarin project. The full documentation of all the features and capabilities can be found in the native android plugin.

How To Use The SDK

1. Get Malwarelytics native SDK and credentials

The SDK and credentials can be found under the “Get The SDK” section in the Malwarelytics Console.

2. Make the Nuget package available

Download the .nupkg package from the Releases page. If you have your local Nuget Package repository, put it there. If not, copy the package anywhere in your system and add this folder as a Nuget source in Visual Studio (Preferences > Nuget > Sources).

3. Add the package to your project

  1. Double click on “Packages” in your .Android project.
  2. In the top left corner, select your previously set NuGet folder as the source.
  3. Add Wultra.Android.Xamarin.Malwarelytics package to your project

4. Use the SDK

The following sample code is integration into a blank app with just one button.

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using System.Threading;
using Xamarin.Essentials;
using Com.Wultra.Android.Antimalware;

namespace DemoApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, Antivirus.IInitializationObserver
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            var config = (new AntivirusConfig.Builder(ApplicationContext))
                .InvokeSmartProtectionConfig((new AntivirusConfig.SmartProtectionConfig.Builder())
                    .InvokeSmartProtectionEnabled(true)
                    .InvokeSilentModeEnabled(false)
                    .InvokeSmartProtectionIntervalHours(48)
                    .Build()
                )
                .InvokeApiUsername("username")
                .InvokeApiPassword("password");

            config.InvokeClientAppUserId("myuserid");

            Antivirus.Instance.InitializeAsync(config.Build(), this);

            FindViewById<Button>(Resource.Id.button1).Click += (sender, e) =>
            {
                Toast.MakeText(this, "Smart protection running", ToastLength.Short).Show();
                new Thread(() =>
                {
                    var result = Antivirus.Instance.PerformSingleSmartProtectionUpdate();
                    MainThread.BeginInvokeOnMainThread(() =>
                    {
                        Toast.MakeText(this, $"Smart protection finished: {result}", ToastLength.Short).Show();
                    });
                }).Start();
            };
        }
        
        public void OnInitialized()
        {
            // Antivirus is initialized
        }
        
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

Styling Threat Screen (Of Smart Protection)

Threat screen can by styled by setting custom theme for the activity and by changing icons for the image buttons on the screen.

There are two icons. Delete icon and settings icon. Delete icon serves for button requesting app uninstall (for apps that can be uninstalled). Settings icon serves for button opening app’s detail in system settings (for apps that cannot be uninstalled) where user can disable the app.

The theme for the screen should contain standard Android attributes and can contain couple of extra attributes defined by the AV SDK.

We recommend to derive the theme from AppCompat themes. The usage of ActionBar in the screen is derived from the theme. Use NoActionBar version of the theme for design without the ActionBar (contains the app name). Alternatively you can hide the ActionBar by adding

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

to your theme.

Following is an example of a theme using Wultra-defined attributes that you can put into the Resources/values/malwarelytics.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="CustomAppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">#FF0000</item>
        <item name="colorPrimaryDark">#222222</item>
        <item name="colorAccent">#FF0000</item>

        <item name="titleTextColor">@android:color/black</item>

        <!-- <item name="android:windowBackground">@drawable/img_bg_app</item> -->

        <!-- <item name="android:fontFamily">@font/font_family_roboto</item>
        <item name="fontFamily">@font/font_family_roboto</item> -->

        <!-- <item name="buttonStyle">@style/CustomAppTheme.Button.Filled</item> -->
        <item name="imageButtonStyle">@style/CustomAppTheme.ImageButton.Borderless</item>

        <item name="wultraThreatTitleTextAppearance">@style/CustomAppTheme.TextAppearance.Title</item>
        <item name="wultraThreatTitleStyle">@style/CustomAppTheme.TextView.Title</item>
        <item name="wultraThreatDescriptionTextAppearance">@style/CustomAppTheme.TextAppearance.Description</item>
        <item name="wultraThreatDescriptionStyle">@style/CustomAppTheme.TextView.Description</item>
        <item name="wultraThreatProtipTextAppearance">@style/CustomAppTheme.TextAppearance.Description</item>
        <item name="wultraThreatProtipStyle">@style/CustomAppTheme.TextView.Description</item>
    </style>

    <style name="CustomAppTheme.TextAppearance.Button" parent="TextAppearance.AppCompat.Button">
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textSize">19.2sp</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="CustomAppTheme.Button.Filled" parent="Widget.AppCompat.Button">
        <item name="android:textAppearance">@style/CustomAppTheme.TextAppearance.Button</item>
        <item name="android:textAllCaps">false</item>
        <!-- <item name="android:background">@drawable/bg_button_filled</item> -->
    </style>

    <style name="CustomAppTheme.ImageButton.Borderless" parent="Widget.AppCompat.ImageButton">
        <!-- <item name="android:background">@drawable/bg_button_borderless</item> -->
    </style>

    <style name="CustomAppTheme.TextView"/>

    <style name="CustomAppTheme.TextView.Title">
        <item name="android:gravity">center</item>
    </style>

    <style name="CustomAppTheme.TextAppearance.Title" parent="TextAppearance.AppCompat.Title">
        <item name="android:textColor">#FF0000</item>
    </style>

    <style name="CustomAppTheme.TextView.Description">
        <item name="android:gravity">center</item>
    </style>

    <style name="CustomAppTheme.TextAppearance.Description" parent="TextAppearance.AppCompat.Medium">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">16.8sp</item>
    </style>
</resources>

Such custom theme can be set as part of the SDK configuration.

var tmc = new AntivirusConfig.ThreatMitigationUIConfig.Builder();
// set the custom theme
tmc.InvokeScreenTheme(Resource.Style.CustomAppTheme);
// set the custom notification icon
tmc.InvokeNotificationSmallIcon(Resource.Mipmap.ic_launcher);

// add the Mitigation UI configuration to AntivirusConfig builder
var config = new AntivirusConfig.Builder(ApplicationContext);
config.InvokeThreatMitigationUIConfig(tmc.Build());

Support and Compatibility

Xamarin Library Native Library
0.11.x 0.11.x
Last updated on Mar 29, 2021 (01:35) View product
Search

develop

Malwarelytics for Xamarin