Introduction
The misuse of official workstations is a common practice among the users of many work environments where proper monitoring mechanisms do not exist. It results in the waste of resources and is 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 that 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,
- Server Application
Server Application will continuously run on each workstation in the 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 the Client/mobile application. This application will interpret and execute commands received from the Client/mobile application.
- 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 it 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 a 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 the 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 by sending commands. The 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
The 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 an 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)
- public class Admin
- {
- public string ID { get; set; }
- public string Username { get; set; }
- public string FullName { get; set; }
- public string Email { get; set; }
- public string Password { get; set; }
- public string Mobile { get; set; }
- public Admin() { }
- public Admin(string Id, string username, string fullName, string email, string password, string mobile) //Constructor with all parameters
- {
- ID = Id;
- Username = username;
- FullName = fullName;
- Email = email;
- Password = password;
- Mobile = mobile;
- }
- public Admin(string Password) //Constructor with one parameter for password verifing
- {
- this.Password = Password;
- }
- }
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)
- public class Helper : SQLiteOpenHelper
- {
- private static string _DatabaseName = "clientDatabase";
- public Helper(Context context) : base(context, _DatabaseName, null, 1) { }
- public override void OnCreate(SQLiteDatabase db)
- {
- db.ExecSQL(Helper.CreateQuery);
- }
- public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- db.ExecSQL(Helper.DeleteQuery);
- OnCreate(db);
- }
- private const string TableName = "adminTable";
- private const string ColumnID = "id";
- private const string ColumnUsername = "username";
- private const string ColumnFullName = "fullname";
- private const string ColumnPassword = "password";
- private const string ColumnEmail = "email";
- private const string ColumnMobile = "mobile";
- public const string CreateQuery = "CREATE TABLE " + TableName +
- " ( "
- + ColumnID + " INTEGER PRIMARY KEY,"
- + ColumnUsername + " TEXT,"
- + ColumnFullName + " TEXT,"
- + ColumnPassword + " TEXT,"
- + ColumnEmail + " TEXT,"
- + ColumnMobile + " TEXT)";
- public const string DeleteQuery = "DROP TABLE IF EXISTS " + TableName;
- public void Register(Context context, Admin admin)
- {
- SQLiteDatabase db = new Helper(context).WritableDatabase;
- ContentValues Values = new ContentValues();
- Values.Put(ColumnUsername, admin.Username);
- Values.Put(ColumnFullName, admin.FullName);
- Values.Put(ColumnPassword, admin.Password);
- Values.Put(ColumnEmail, admin.Email);
- Values.Put(ColumnMobile, admin.Mobile);
- db.Insert(TableName, null, Values);
- db.Close();
- }
- public Admin Authenticate(Context context, Admin admin)
- {
- SQLiteDatabase db = new Helper(context).ReadableDatabase;
- ICursor cursor = db.Query(TableName, new string[]
- { ColumnID, ColumnFullName, ColumnUsername, ColumnPassword, ColumnEmail, ColumnMobile },
- ColumnUsername + "=?", new string[] { admin.Username }, null, null, null);
- if(cursor != null && cursor.MoveToFirst() && cursor.Count > 0)
- {
- Admin admin1 = new Admin(cursor.GetString(3));
- if (admin.Password.Equals(admin1.Password))
- return admin1;
- }
- return null;
- }
- public List<Admin> GetAdmin(Context context)
- {
- List<Admin> admins = new List<Admin>();
- SQLiteDatabase db = new Helper(context).ReadableDatabase;
- string[] columns = new string[] {ColumnID,ColumnUsername,ColumnFullName,ColumnPassword,ColumnEmail,ColumnMobile };
- using(ICursor cursor = db.Query(TableName, columns, null, null, null, null, null))
- {
- while (cursor.MoveToNext())
- {
- admins.Add(new Admin {
- ID = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnID)),
- Username = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnUsername)),
- FullName = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnFullName)),
- Password = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnPassword)),
- Email = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnEmail)),
- Mobile = cursor.GetString(cursor.GetColumnIndexOrThrow(ColumnMobile))
- });
- }
- db.Close();
- return admins;
- }
- }
- }
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)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="20dp">
- <EditText
- android:hint="Full Name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edtfullname"/>
- <EditText
- android:hint="Username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edtusername"/>
- <EditText
- android:hint="Email"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edtEmail"
- />
- <EditText
- android:hint="Mobile"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edtMobile"
- android:inputType="phone" />
- <EditText
- android:hint="Password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edtpassword"
- android:inputType="textPassword" />
- <Button
- android:text="Create"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnCreate" />
- <Button
- android:text="Back to Home"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnBack" />
- </LinearLayout>
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)
- public class SignUp : Activity
- {
- private EditText edtFullname, edtUsername, edtEmail, edtPassword, edtMobile;
- private Button btnCreate, btnBack;
- Helper helper;
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- // Create your application here
- SetContentView(Resource.Layout.SignUp);
- edtFullname = FindViewById<EditText>(Resource.Id.edtfullname);
- edtUsername = FindViewById<EditText>(Resource.Id.edtusername);
- edtPassword = FindViewById<EditText>(Resource.Id.edtpassword);
- edtEmail = FindViewById<EditText>(Resource.Id.edtEmail);
- edtMobile = FindViewById<EditText>(Resource.Id.edtMobile);
- btnCreate = FindViewById<Button>(Resource.Id.btnCreate);
- btnBack = FindViewById<Button>(Resource.Id.btnBack);
- helper = new Helper(this);
- btnBack.Click += delegate { StartActivity(typeof(MainActivity)); };
- btnCreate.Click += delegate
- {
- Admin admin = new Admin()
- {
- FullName = edtFullname.Text,
- Username = edtUsername.Text,
- Password = edtPassword.Text,
- Email = edtEmail.Text,
- Mobile = edtMobile.Text
- };
- string username = edtUsername.Text;
- string password = edtPassword.Text;
- if(string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
- {
- Toast.MakeText(this, "Username and Password should not be empty.", ToastLength.Short).Show();
- }
- else
- {
- helper.Register(this,admin);
- var data = helper.GetAdmin(this);
- admin = data[data.Count - 1];
- Toast.MakeText(this, $"User {admin.FullName} registration successful!", ToastLength.Short).Show();
- Clear();
- Toast.MakeText(this, $"Total {data.Count} Admin founds.", ToastLength.Short).Show();
- }
- };
- }
- void Clear()
- {
- edtFullname.Text = "";
- edtUsername.Text = "";
- edtPassword.Text = "";
- edtMobile.Text = "";
- edtEmail.Text = "";
- }
- }
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)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="20dp">
- <EditText
- android:hint="Username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtusername"/>
- <EditText
- android:hint="Password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtpassword"
- android:inputType="textPassword" />
- <Button
- android:text="Sign In"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnSign" />
- <TextView
- android:text="Or"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textColor="#ffffff"
- android:gravity="center"
- android:id="@+id/txtOr" />
- <Button
- android:text="Create an Account"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnSignUp" />
- </LinearLayout>
Now, go to Solution Explorer-> Project Name-> MainActivity and add the following code with appropriate namespaces.
(FileName: MainActivity)
- public class MainActivity : Activity
- {
- private EditText txtUsername, txtPassword;
- private Button btnSignIn, btnCreate;
- Helper helper;
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- txtUsername = FindViewById<EditText>(Resource.Id.txtusername);
- txtPassword = FindViewById<EditText>(Resource.Id.txtpassword);
- btnCreate = FindViewById<Button>(Resource.Id.btnSignUp);
- btnSignIn = FindViewById<Button>(Resource.Id.btnSign);
- helper = new Helper(this);
- btnCreate.Click += delegate { StartActivity(typeof(SignUp)); };
- btnSignIn.Click += delegate
- {
- try
- {
- string Username = txtUsername.Text.ToString();
- string Password = txtPassword.Text.ToString();
- var user = helper.Authenticate(this,new Admin(null,Username,null,null,Password,null));
- if (user != null)
- {
- Toast.MakeText(this, "Login Successful", ToastLength.Short).Show();
- StartActivity(typeof(Connect));
- }
- else
- {
- Toast.MakeText(this, "Login Unsuccessful! Please verify your Username and Password", ToastLength.Short).Show();
- }
- }
- catch (SQLiteException ex)
- {
- Toast.MakeText(this, ""+ex, ToastLength.Short).Show();
- }
- };
- }
- }
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 log in and registration. For simplicity, I am splitting the article into three parts. In the next part, I will start building the connection between the server and the client. So, please stay tuned for my next article of this series.
0 Comments:
Post a Comment