App Passcode > AirWatch SDK (Xamarin Android)

Download file:
Sample Code

Overview

The steps in this tutorial are done with the assumption that you have gone through the steps in General Setup tutorial as well as the SDK Setup section.

Important: You will need to download the SDK binary separately via https://components.xamarin.com/view/awsdk.

Requirements

  • Android 4.0+ / Ice Cream Sandwich / API Level 14+
  • Xamarin Studio – if you have Visual Studio with the Xamarin plugin, this should also work, but this document is based on Xamarin Studio.
  • Android Test Device
  • AirWatch Agent v5.3+ for Android (Requirement be lower depending on features used)
  • The AirWatch Xamarin Android SDK from the Xamarin Component Store AWClient-SDK.dll.

Tutorial

This document describes how to integrate the AirWatch Xamarin Android SDK into an existing Xamarin Android application in order to add a PIN-code locking screen to the app. To follow along with this document, you will need:

  • The AirWatch Xamarin Android SDK, AWClient-SDK.dll.
  • The AirWatch debug Java keystore, android_airwatch_debug.keystore, or another keystore that contains a key that can be used to sign applications for the AirWatch Console.
  • Xamarin Studio – if you have Visual Studio with the Xamarin plugin, this should also work, but this document is based on Xamarin Studio.
  • A Xamarin Android app into which you want to integrate the Airwatch SDK. If you do not have a suitable application, you can create a new application in Xamarin Studio and integrate the SDK into that.

Initialize the SDK

Basic SDK Integration is very simple. At the most basic level, the developer needs to interact with one class, the SDKManager. A common use would be to call SDKManager.Init (activity) and then SsoSessionReturnCode code = SDKManager.ValidateSSOSession (activity) within a background thread in the OnResume () method of your activity. The AWSDKHelper class below shows how to perform these calls.

using System;
using Com.Airwatch.Sdk;
using Android.App;
using Android.Util;
using System.Threading.Tasks;

namespace XamarinPasscodeSample
{
    public interface AWSDKCallback
    {
        void onSSOValidateStart ();

        void onSSOValidateSuccess ();

        void onSSOValidateFailure (SsoSessionReturnCode status);

        void onSSOValidateComplete ();
    }

    public static class AWSDKHelper
    {
        const String TAG = "AWSDKHelper";

        public static void checkSSOSession (Activity activity, AWSDKCallback callback)
        {
            if (callback != null) {
                Log.Debug (TAG, "onSSOValidateStart");
                callback.onSSOValidateStart ();
            }

            // SDKManager.ValidateSSOSession should be executed in a background thread
            Task.Run (() => {
                SDKManager.Init (activity);
                SsoSessionReturnCode code = SDKManager.ValidateSSOSession (activity); 
                if (callback != null) {
                    if (code == SsoSessionReturnCode.Success ||
                        code == SsoSessionReturnCode.AuthInProgress) {
                        Log.Debug (TAG, "onSSOValidateSuccess");
                        callback.onSSOValidateSuccess ();
                    } else {
                        Log.Debug (TAG, "onSSOValidateFailure");
                        callback.onSSOValidateFailure (code);
                    }
                    Log.Debug (TAG, "onSSOValidateComplete");
                    callback.onSSOValidateComplete ();
                }
            });
        }
    }
}

Because each Activity is responsible for checking its own SSO status, in a typical application with multiple Activity classes, it usually proves helpful to move the SSO calls into a custom subclass of Activity and have your individual Activities inherit from this subclass. You can customize the AWSDKCallback implementation if desired to add additional behavior such as a progress dialog. Here is a simple example of a subclass of Activity:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using Com.Airwatch.Sdk;

namespace XamarinPasscodeSample
{
    public class BaseSDKActivity : Activity
    {
        private AWSDKCallbackImpl callback;

        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            callback = new AWSDKCallbackImpl (this);
        }

        protected override void OnResume ()
        {
            base.OnResume ();
            AWSDKHelper.checkSSOSession (this, callback);
        }
    }

    // This callback manages hiding and showing the AWSSOLoginDialog during authentication
    class AWSDKCallbackImpl: AWSDKCallback
    {
        private Activity parent;
        private AWSSOLoginDialog loginDialog;

        public AWSDKCallbackImpl (Activity parent)
        {
            this.parent = parent;    
        }

        public void onSSOValidateStart ()
        {
            this.loginDialog = new AWSSOLoginDialog ();
            parent.FragmentManager.BeginTransaction ().Add (this.loginDialog, "Checking").Commit ();
        }

        public void onSSOValidateSuccess ()
        {
            parent.FragmentManager.BeginTransaction ().Remove (this.loginDialog).CommitAllowingStateLoss ();
        }

        public void onSSOValidateFailure (SsoSessionReturnCode status)
        {
            if (status == SsoSessionReturnCode.SsoModeDisabled) {
                loginDialog.SetError (parent.GetString (Resource.String.sdk_no_sso_error));
            } else {
                loginDialog.SetError (parent.GetString (Resource.String.sdk_generic_error));
            }
        }

        public void onSSOValidateComplete ()
        {
        }
    }

    // Simple DialogFragment for Data Loss Prevention during SDK initialization
    public class AWSSOLoginDialog : DialogFragment
    {
        public TextView dialogMessage;
        public ProgressBar progressBar;

        public override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            this.SetStyle (DialogFragmentStyle.NoFrame, Android.Resource.Style.ThemeHoloLight);
        }

        public void SetError (string message)
        {
            dialogMessage.Text = message;
            progressBar.Visibility = ViewStates.Invisible;
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate (Resource.Layout.FullscreenProgressDialog, container, false);

            this.progressBar = view.FindViewById<ProgressBar> (Resource.Id.progressBar);
            dialogMessage = view.FindViewById<TextView> (Resource.Id.dialogMessage);
            return view;
        }
    }
}

As you can see, this adds a DialogFragment that acts as a splash screen during SDK initialization. Having an Activity base class with all the SSO capability built-in makes using the SDK very easy afterward. You simply have your Activities extend BaseSDKActivity:

public class MainActivity : BaseSDKActivity
{

In this example, the only customization required is to change the base class from Activity or AppCompatActivity to BaseSDKActivity. Once SSO is enabled in the AirWatch console, this app is now enabled with a PIN.

Final Notes

If you find that you are not seeing an SSO passcode, ensure that the Organization Group has Single Sign On enabled and that an Authentication Type is set as shown below.

 

+

Questions? Comments? Leave them for us here.