In Focus

Xamarin.Android - Build Real-life Application Using TCP/IP - Part One

The purpose of this project is to develop an Android-based application which will serve the administrator with on-demand remote surveillance and controlling of workstations of a workplace. The project will involve the development of two applications, one will run on workstations and the other one will run on the mobile phone of the Administrator. Both of these applications will be connected through Wi-Fi.

Introduction

Tshe misuse of official workstations is a common practice among the users of many work environments where proper monitoring mechanism do not exist. It results in the waste of resources and in not meeting the overall objectives of an organization. So, to achieve the maximum and desired results in a work environment, an effective mechanism through which the Administrator can monitor and control the workstations under the use of employees is needed.

Purpose

The purpose of this project is to develop an Android-based application which will serve the administrator with on-demand remote surveillance and controlling of workstations of the workplace. The project will involve the development of two applications, one will run on workstations and the other one will run on the mobile phone of the administrator. Both the applications will be connected through Wi-Fi. The implementation of such a mechanism will allow the administrator, to not only have an eye on the activities of users but also, will make him/her able to remotely control the specific workstation(s).

Scope of Project

There will be two applications in this project,

  1. Server Application
    Server Application will continuously run on each workstation in workplace. The server application will be connected with the Client Application through a Wi-Fi connection. It will take a screenshot of user activity on the demand of the administrator and will transfer the same to Client/mobile application. This application will interpret and execute commands received from the Client/mobile application.
  1. Client / Android Application
    Android application will run on Administrator’s smart mobile phone and will provide an effective interface through which the Administrator will be able to connect it with the Server Application. Through this application, the Administrator will be able to instruct the Server Application to take the screenshot(s) and send to this application. Through this application, the Administrator will also be able to control any workstation and perform the activities such as sending a message, turning the workstation into sleep mode and shutting down any workstation etc.
    • Sign in and User Registration
      Client / mobile Application will provide User Registration interface through which the user will register as Admin. The application will provide a proper interface for Sign in through which Admin will log in to the application by entering the username and password.

    • Screenshot taking and sending to Client / Mobile Application
      Server Application will take the screenshot(s) of user activity on any workstation on demand of the Administrator through Client/mobile Application. Screenshot(s) will be transferred to the client/mobile application.

    • Controlling Work Stations
      The administrator will be able to control any workstation through sending commands. Administrator will be able to perform sending a message to the workstation turning that into sleep mode, locking the workstation, and shutting down the workstation

    • Logout
      Administrator will be able to terminate the connection between the client/mobile application and Server application by logging out from the application.

Client Application

In the requirements, the first task is to provide the functionality of login and registration for the client. Therefore, first, I will develop a login and registration module in Xamarin.Android.

The steps given below are required to be followed in order to create a SQLite operations app in Xamarin.Android, using Visual Studio.

Step 1 - Create a Project

Open Visual Studio and go to New Project-> Templates-> Visual C#-> Android-> Blank app. give it a name, like Client.

Step 2 - Writing Admin Class

Before you go further, you need to write your Admin class with all the getter and setter methods to maintain a single admin as an object. Also, write two constructors in the admin class. Go to Solution Explorer-> Project Name and right-click. Select Add -> New Item-> Class. Give it a name as Admin.cs and write the following code.

(File Name: Admin.cs)

  1. public class Admin  
  2.     {  
  3.         public string ID { get; set; }  
  4.         public string Username { get; set; }  
  5.         public string FullName { get; set; }  
  6.         public string Email { get; set; }  
  7.         public string Password { get; set; }  
  8.         public string Mobile { get; set; }  
  9.         public Admin() { }  
  10.         public Admin(string Id, string username, string fullName, string email, string password, string mobile) //Constructor with all parameters  
  11.         {  
  12.             ID = Id;  
  13.             Username = username;  
  14.             FullName = fullName;  
  15.             Email = email;  
  16.             Password = password;  
  17.             Mobile = mobile;  
  18.         }  
  19.         public Admin(string Password) //Constructor with one parameter for password verifing  
  20.         {  
  21.             this.Password = Password;  
  22.         }         
  23.     }  

Step 3 - Writing SQLite Helper Class

We need to write our own helper class to create a new database with operations. Go to Solution Explorer-> Project Name and right-click. Select Add -> New Item-> Class. Give it a name as Helper.cs. Inherit with SQLiteOpenHelper class and write the following code with appropriate namespaces.

(File Name: Helper.cs)

  1. public class Helper : SQLiteOpenHelper  
  2.     {  
  3.         private static string _DatabaseName = "clientDatabase";  
  4.   
  5.         public Helper(Context context) : base(context, _DatabaseName, null, 1) { }  
  6.         public override void OnCreate(SQLiteDatabase db)  
  7.         {  
  8.             db.ExecSQL(Helper.CreateQuery);  
  9.         }  
  10.   
  11.         public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)  
  12.         {  
  13.             db.ExecSQL(Helper.DeleteQuery);  
  14.             OnCreate(db);  
  15.         }  
  16.           
  17.         private const string TableName = "adminTable";  
  18.         private const string ColumnID = "id";  
  19.         private const string ColumnUsername = "username";  
  20.         private const string ColumnFullName = "fullname";  
  21.         private const string ColumnPassword = "password";  
  22.         private const string ColumnEmail = "email";  
  23.         private const string ColumnMobile = "mobile";  
  24.   
  25.         public const string CreateQuery = "CREATE TABLE " + TableName +  
  26.             " ( "  
  27.             + ColumnID + " INTEGER PRIMARY KEY,"  
  28.             + ColumnUsername + " TEXT,"  
  29.             + ColumnFullName + " TEXT,"  
  30.             + ColumnPassword + " TEXT,"  
  31.             + ColumnEmail + " TEXT,"  
  32.             + ColumnMobile + " TEXT)";  
  33.   
  34.         public const string DeleteQuery = "DROP TABLE IF EXISTS " + TableName;  
  35.   
  36.         public void Register(Context context, Admin admin)  
  37.         {  
  38.             SQLiteDatabase db = new Helper(context).WritableDatabase;  
  39.             ContentValues Values = new ContentValues();  
  40.             Values.Put(ColumnUsername, admin.Username);  
  41.             Values.Put(ColumnFullName, admin.FullName);  
  42.             Values.Put(ColumnPassword, admin.Password);  
  43.             Values.Put(ColumnEmail, admin.Email);  
  44.             Values.Put(ColumnMobile, admin.Mobile);  
  45.             db.Insert(TableName, null, Values);  
  46.             db.Close();  
  47.         }  
  48.         public Admin Authenticate(Context context, Admin admin)  
  49.         {  
  50.             SQLiteDatabase db = new Helper(context).ReadableDatabase;  
  51.             ICursor cursor = db.Query(TableName, new string[]   
  52.             { ColumnID, ColumnFullName, ColumnUsername, ColumnPassword, ColumnEmail, ColumnMobile },  
  53.             ColumnUsername + "=?"new string[] { admin.Username }, nullnullnull);  
  54.             if(cursor != null && cursor.MoveToFirst() && cursor.Count > 0)  
  55.             {  
  56.                 Admin admin1 = new Admin(cursor.GetString(3));  
  57.                 if (admin.Password.Equals(admin1.Password))  
  58.                 return admin1;  
  59.             }  
  60.             return null;  
  61.         }  
  62.   
  63.         public List<Admin> GetAdmin(Context context)  
  64.         {  
  65.             List<Admin> admins = new List<Admin>();  
  66.             SQLiteDatabase db = new Helper(context).ReadableDatabase;  
  67.             string[] columns = new string[] {ColumnID,ColumnUsername,ColumnFullName,ColumnPassword,ColumnEmail,ColumnMobile };  
  68.             using(ICursor cursor = db.Query(TableName, columns, nullnullnullnullnull))  
  69.             {  
  70.                 while (cursor.MoveToNext())  
  71.                 {  
  72.                     admins.Add(new Admin {  
  73.                         ID = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnID)),  
  74.                         Username = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnUsername)),  
  75.                         FullName = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnFullName)),  
  76.                         Password = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnPassword)),  
  77.                         Email = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnEmail)),  
  78.                         Mobile = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnMobile))  
  79.   
  80.                     });  
  81.                 }  
  82.                 db.Close();  
  83.                 return admins;  
  84.             }  
  85.         }  
  86.     }  

Step 4 - Add SignUp Layout

Next, add a new layout by going to Solution Explorer-> Project Name-> Resources-> Layout. Right-click to add a new item, select Layout, and give it a name, such as SignUp.axml. Open this layout file and add the following code.

(Folder Name: Layout , File Name: SignUp.axml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:padding="20dp">  
  7.     <EditText  
  8.         android:hint="Full Name"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/edtfullname"/>  
  12.     <EditText  
  13.         android:hint="Username"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:id="@+id/edtusername"/>  
  17.     <EditText  
  18.         android:hint="Email"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/edtEmail"  
  22.          />  
  23.     <EditText  
  24.         android:hint="Mobile"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:id="@+id/edtMobile"  
  28.         android:inputType="phone" />  
  29.     <EditText  
  30.         android:hint="Password"  
  31.         android:layout_width="match_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:id="@+id/edtpassword"  
  34.         android:inputType="textPassword" />  
  35.     <Button  
  36.         android:text="Create"  
  37.         android:layout_width="match_parent"  
  38.         android:layout_height="wrap_content"  
  39.         android:id="@+id/btnCreate" />  
  40.     <Button  
  41.         android:text="Back to Home"  
  42.         android:layout_width="match_parent"  
  43.         android:layout_height="wrap_content"  
  44.         android:id="@+id/btnBack" />  
  45. </LinearLayout>  

Build Real Life Application Using TCP/IP 

Step 5 - Create SignUp Activity

Add a new Activity. For this, open Solution Explorer-> Project Name-> right click to add a new item and select Activity. Give it a name like SignUp.cs and add the following code using the appropriate namespaces.

(FileName: SignUp)

  1. public class SignUp : Activity  
  2.     {  
  3.         private EditText edtFullname, edtUsername, edtEmail, edtPassword, edtMobile;  
  4.         private Button btnCreate, btnBack;  
  5.         Helper helper;  
  6.         protected override void OnCreate(Bundle savedInstanceState)  
  7.         {  
  8.             base.OnCreate(savedInstanceState);  
  9.   
  10.             // Create your application here  
  11.             SetContentView(Resource.Layout.SignUp);  
  12.               
  13.             edtFullname = FindViewById<EditText>(Resource.Id.edtfullname);  
  14.             edtUsername = FindViewById<EditText>(Resource.Id.edtusername);  
  15.             edtPassword = FindViewById<EditText>(Resource.Id.edtpassword);  
  16.             edtEmail = FindViewById<EditText>(Resource.Id.edtEmail);  
  17.             edtMobile = FindViewById<EditText>(Resource.Id.edtMobile);  
  18.             btnCreate = FindViewById<Button>(Resource.Id.btnCreate);  
  19.             btnBack = FindViewById<Button>(Resource.Id.btnBack);  
  20.             helper = new Helper(this);  
  21.   
  22.             btnBack.Click += delegate { StartActivity(typeof(MainActivity)); };  
  23.   
  24.             btnCreate.Click += delegate   
  25.             {  
  26.                 Admin admin = new Admin()  
  27.                 {  
  28.                     FullName = edtFullname.Text,  
  29.                     Username = edtUsername.Text,  
  30.                     Password = edtPassword.Text,  
  31.                     Email = edtEmail.Text,  
  32.                     Mobile = edtMobile.Text  
  33.                 };  
  34.                 string username = edtUsername.Text;  
  35.                 string password = edtPassword.Text;  
  36.                 if(string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))  
  37.                 {  
  38.                     Toast.MakeText(this"Username and Password should not be empty.", ToastLength.Short).Show();  
  39.                 }  
  40.                 else  
  41.                 {  
  42.                     helper.Register(this,admin);  
  43.                     var data = helper.GetAdmin(this);  
  44.                     admin = data[data.Count - 1];  
  45.                     Toast.MakeText(this, $"User {admin.FullName} registration successful!", ToastLength.Short).Show();  
  46.                     Clear();  
  47.                     Toast.MakeText(this, $"Total {data.Count} Admin founds.", ToastLength.Short).Show();  
  48.                 }  
  49.             };  
  50.              
  51.         }  
  52.         void Clear()  
  53.         {  
  54.             edtFullname.Text = "";  
  55.             edtUsername.Text = "";  
  56.             edtPassword.Text = "";  
  57.             edtMobile.Text = "";  
  58.             edtEmail.Text = "";  
  59.         }  
  60.     }  

Step 6 - Main Layout

Open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml. Open this main layout file and add the following code.

(File Name: Main.axml , Folder Name: Layout)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:padding="20dp">  
  7.     <EditText  
  8.         android:hint="Username"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/txtusername"/>  
  12.     <EditText  
  13.         android:hint="Password"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:id="@+id/txtpassword"  
  17.         android:inputType="textPassword" />  
  18.     <Button  
  19.         android:text="Sign In"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:id="@+id/btnSign" />  
  23.     <TextView  
  24.         android:text="Or"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:textColor="#ffffff"  
  28.         android:gravity="center"  
  29.         android:id="@+id/txtOr" />  
  30.     <Button  
  31.         android:text="Create an Account"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:id="@+id/btnSignUp" />  
  35. </LinearLayout>  

Build Real Life Application Using TCP/IP 

Step 7 - Main Activity Class

Now, go to Solution Explorer-> Project Name-> MainActivity and add the following code with appropriate namespaces.

(FileName: MainActivity)

  1. public class MainActivity : Activity  
  2.     {  
  3.         private EditText txtUsername, txtPassword;  
  4.         private Button btnSignIn, btnCreate;  
  5.         Helper helper;  
  6.         protected override void OnCreate(Bundle savedInstanceState)  
  7.         {  
  8.             base.OnCreate(savedInstanceState);  
  9.   
  10.             // Set our view from the "main" layout resource  
  11.             SetContentView(Resource.Layout.Main);  
  12.               
  13.             txtUsername = FindViewById<EditText>(Resource.Id.txtusername);  
  14.             txtPassword = FindViewById<EditText>(Resource.Id.txtpassword);  
  15.             btnCreate = FindViewById<Button>(Resource.Id.btnSignUp);  
  16.             btnSignIn = FindViewById<Button>(Resource.Id.btnSign);  
  17.             helper = new Helper(this);  
  18.   
  19.             btnCreate.Click += delegate { StartActivity(typeof(SignUp)); };  
  20.   
  21.             btnSignIn.Click += delegate  
  22.             {  
  23.                 try  
  24.                 {  
  25.                     string Username = txtUsername.Text.ToString();  
  26.                     string Password = txtPassword.Text.ToString();  
  27.                     var user = helper.Authenticate(this,new Admin(null,Username,null,null,Password,null));  
  28.                     if (user != null)  
  29.                     {  
  30.                         Toast.MakeText(this"Login Successful", ToastLength.Short).Show();  
  31.                         StartActivity(typeof(Connect));  
  32.                     }  
  33.                     else  
  34.                     {  
  35.                         Toast.MakeText(this"Login Unsuccessful! Please verify your Username and Password", ToastLength.Short).Show();  
  36.                     }  
  37.                 }  
  38.                 catch (SQLiteException ex)  
  39.                 {  
  40.                     Toast.MakeText(this""+ex, ToastLength.Short).Show();  
  41.                 }  
  42.                   
  43.             };  
  44.         }  
  45.     }  

This was the process of creating an app for Login and Registration with SQLite database in Xamarin.Android.

 

We have completed our first module of the client that was login and registration. For simplicity, I am splitting the article into three parts. In the next part, I will start building the connection with server and client. So, please stay tuned for my next article of this series.