Commit 26a43747 authored by rajneesh's avatar rajneesh

Android comments added to xml and java file

parent 0bbabcf2
package com.example.wireless_x;
/*! \file
\brief This is where the main code of the Wireless-X android application is written.
*/
package com.example.wireless_x;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompatSideChannelService;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
......@@ -42,7 +43,6 @@ import android.widget.Space;
import android.widget.TextView;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
......@@ -63,9 +63,15 @@ import java.io.PrintWriter;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Arrays;
import javax.security.auth.login.LoginException;
/*! \brief This is where the main code of the Wireless-X android application is written.
*
* The MainActivity consists of the methods that initialize all the required variables and fields
* when the app starts, methods which keep listening to the mouse and keyboard events such as a mouse
* click event or a key press event, screen touch events, methods which send the camera frames to
* the virtual camera device running on the laptop and so on.
*/
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
private TextView textView;
......@@ -94,7 +100,6 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
private SharedPreferences preferences;
private String MY_PREFS_NAME="ip_store";
private Button wave_key, l_shift, r_shift, forward_slash_key, period_key, comma_key, semicolon_key, apostrophe_key, left_box_brac_key, capslock_key;
private Button right_box_brac_key, back_slash_key, one_key, two_key, three_key, four_key, five_key, six_key, seven_key, eight_key, nine_key, zero_key, minus_key, equal_key;
......@@ -110,10 +115,13 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
private int mouse_space_height, mouse_space_width;
private int desktop_width=1, desktop_height=1;
private TextView title_wirelessx;
/*! \brief OpenCV Initialization
*
* Wireless-X works with OpenCV manager in an asynchronous manner, the onManagerConnected callback
* will be called in the UI thread once the initialization finishes.
*/
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
......@@ -130,9 +138,11 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
};
/*! \brief Sets up the app layout and contains the methods to handle various touch-related events.
*
* Initializes all the app components and contains an listener for those events which can occur when
* the user interacts with the screen by single tap, double tap, scrolling or some gesture on the screen.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -174,29 +184,41 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
minus_key=findViewById(R.id.minus_key);
equal_key=findViewById(R.id.equal_key);
capslock_key=findViewById(R.id.capslock_key);
mouse_space=findViewById(R.id.mouse_space);
/*! \brief Method which listens for screen-touch related events.
*
* When the user performs a double tap, it is translated to the double left-click on a physical mouse.
* Similarly, when the user performs a single tap, it's effect is same as a single click on any physical mouse.
* There is also an onScroll event which corresponds to the mouse scrolling event.
* This listener uses the GestureDetector class to handle such events.
*/
mouse_space.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
/*! \brief Method to handle the double-tap event.
*
* When the user performs a double tap, it is translated to the double left-click on a physical mouse.
* This is done by starting two threads simultaneously, which product the effect of two single-clicks
* without much delay, thus corresponding to a double-click.
*/
public boolean onDoubleTap(MotionEvent e) {
//Log.e("TEST", "onDoubleTap");
Thread thread1 = new Thread(new SendMouseClicks("LEFT \nCLICK"));
thread1.start();
Thread thread2 = new Thread(new SendMouseClicks("LEFT \nCLICK"));
thread2.start();
return super.onDoubleTap(e);
}
@Override
/*! \brief Method to handle the single-tap event.
*
* When the user performs a single tap, it is translated to the single left-click on a physical mouse.
* This is done by starting a thread, which sends the event information to the server running on laptop
* and then the server acts accordingly.
*/
public boolean onSingleTapConfirmed(MotionEvent e) {
//Log.e("TEST", "onSingleTapConfirmed");
......@@ -208,10 +230,12 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
@Override
/*! \brief Method to handle the mouse scrolling event.
*
* When the user performs a scroll event, the coordinates are transferred to the server, which
* translates those coordinates to the position with respect to the laptop screen.
*/
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
x=e2.getX()/mouse_space_width;
y=e2.getY()/mouse_space_height;
......@@ -223,10 +247,14 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
return super.onScroll(e1, e2, distanceX, distanceY);
}
});
@Override
/*! \brief Method to handle the screen-touch event.
*
* This method calls the GestureDetector object to handle the screen-touch event which can be
* any one of the single-tap, double-tap or scroll events.
*/
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
......@@ -234,19 +262,8 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_MOVE:
// x=motionEvent.getX()/mouse_space_width;
// y=motionEvent.getY()/mouse_space_height;
//
// if(x>=0 && x<=1 && y>=0 && y<=1)
// {
// Thread thread1 = new Thread(new SendMouseCoordinatesThread(x, y));
// thread1.start();
// }
//
break;
case MotionEvent.ACTION_UP:
Log.i("TAG", "touched up");
break;
......@@ -255,18 +272,20 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
break;
}
return true;
}
});
cameraRadioButton=findViewById(R.id.camera_radio);
mouseRadioButton=findViewById(R.id.mouse_radio);
cameraSwitchRadioButton=findViewById(R.id.camera_switch_radio);
}
/*! \brief Displays the keys which correspond to special characters.
*
* When the shift-key is pressed on the keyboard in Wireless-X app, this method changes the layout
* of some keys to those keys which correspond to special characters such as brackets, '@', etc.
*/
public void shiftPress(View view) {
shift_enabled=!shift_enabled;
......@@ -370,14 +389,14 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
zero_key.getBackground().clearColorFilter();
minus_key.getBackground().clearColorFilter();
equal_key.getBackground().clearColorFilter();
}
}
/*! \brief Describes the action to be performed when Test IP is clicked on the app.
*
* This method tries to set-up a connection with the IP address entered in the textfield to check
* if the IP address entered by the user is valid or not.
*/
public void test_IP(View view) {
SERVER_IP = String.valueOf(ip_address_editText.getText());
......@@ -396,7 +415,11 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
/*! \brief Tests whether the server's IP address is valid or not.
*
* This method tries to set-up a connection with the server's IP address and if the address is
* invalid then it displays the appropriate error message.
*/
class TestIP_Thread implements Runnable {
public void run() {
......@@ -439,14 +462,10 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
if (response.contains("Success"))
{
enter_wireless_x_button.setVisibility(View.VISIBLE);
test_textView.setText("Connection Success!! "+getEmojiByUnicode(0x1F47D)+"\nEnter Wireless-X");
enter_wireless_x_button.setEnabled(true);
enter_wireless_x_button.setClickable(true);
}
else{
test_textView.setText("Connection Failed!! "+getEmojiByUnicode(0x1F615)+
......@@ -463,11 +482,16 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
}
/*! \brief Returns the emoji corresponding to an unicode
*/
public String getEmojiByUnicode(int unicode){
return new String(Character.toChars(unicode));
}
/*! \brief Performs the action when the "Enter Wireless-X" button is clicked
*
* This method displays the mouse layout once the user clicks on "Enter Wireless-X" button.
*/
public void enter_wireless_x(View view){
ip_address_linear_layout.setVisibility(View.GONE);
mouse_layout.setVisibility(View.VISIBLE);
......@@ -479,15 +503,17 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
mouse_space.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mouse_space_height = mouse_space.getHeight();
mouse_space_width=mouse_space.getWidth();
//textView.setText( String.valueOf(mouse_space.getHeight()) + "\t Width: "+ mouse_space.getWidth() );
}
});
}
@Override
/*! \brief Performs the action when the back button is pressed
*
* It checks whether the back button is pressed twice within 2 seconds, if it is, then it exits the app.
* It also saves the IP address of the server so that the user doesn't need to re-enter it the next time he/she
* opens the app.
*/
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
......@@ -507,14 +533,13 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
ip_address_editText.setText(SERVER_IP);
mouse_enabled=false;
if (mOpenCvCameraView != null && cameraRadioButton.isChecked()){
if (mOpenCvCameraView != null && cameraRadioButton.isChecked()) {
mOpenCvCameraView.disableView();
mOpenCvCameraView.setVisibility(SurfaceView.INVISIBLE);
cameraRadioButton.setChecked(false);
camera_switch_radio_layout.setVisibility(View.GONE);
}
else if(mOpenCvCameraView != null){
else if(mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
......@@ -530,14 +555,15 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}, 2000);
}
/*! \brief Enables or disables the visibility of Mouse UI
*
*/
public void mouse_on_off(View view) {
if(mouse_enabled) {
if(mouse_enabled)
{
textView.setText(R.string.wireless_x_mode_keyboard);
mouse_layout.setVisibility(View.GONE);
keyboard_layout.setVisibility(View.VISIBLE);
}
else
{
......@@ -550,9 +576,9 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
mouse_enabled=!mouse_enabled;
}
/*! \brief Enables or disables the camera layout
*
*/
public void camera_on_off(View view) {
if(camera_enabled) {
......@@ -571,9 +597,11 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
camera_enabled=!camera_enabled;
}
/*! \brief Implementation of the camera switch button functionality
*
* This method changes the main camera to the front or rear camera of the smartphone depending
* upon what the user has selected.
*/
public void camera_switch(View view){
if(mOpenCvCameraView != null)
......@@ -595,12 +623,11 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
mOpenCvCameraView.enableView();
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
}
}
/*! \brief Checks whether all the required permissions are granted by the user or not
*
*/
private boolean allPermissionsGranted(){
for(String permission : REQUIRED_PERMISSIONS){
......@@ -613,6 +640,11 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
@Override
/*! \brief Sets up the camera view if all the permissions are granted
*
* This method initializes all the camera parameters subject to the condition that all the required
* permissions are granted by the user. If this is not the case, then an error message is displayed.
*/
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
......@@ -643,6 +675,11 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
@Override
/*! \brief Handles the onResume state of the app
*
* If the app reaches an "onResume" state in the lifecycle, then this method checks if all the permissions
* are granted or not, if they are, then it sets up camera parameters otherwise it requests the permissions.
*/
public void onResume() {
super.onResume();
......@@ -671,12 +708,16 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
String ip = preferences.getString("ip_addr", "");//"No name defined" is the default value.
ip_address_editText.setText(ip);
SERVER_IP=ip;
}
@Override
/*! \brief Handles the onPause state of the app
*
* If the app reaches the "onPause" state in the lifecycle, then this method disables the camera
* view. It also saves the server's IP address so that the next time the app is opened, the user
* doesn't require to enter the same address again.
*/
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
......@@ -689,31 +730,40 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
/*! \brief Handles the onDestroy state of the app
*
* If the app reaches the "onDestroy" state in the lifecycle, then this method disables the camera
* view.
*/
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
/// @cond DO_NOT_DOCUMENT
public void onCameraViewStarted(int width, int height) {
// mRGBA= new Mat(height, width, CvType.CV_8UC4);
}
/// @endcond
@Override
/// @cond DO_NOT_DOCUMENT
public void onCameraViewStopped() {
// mRGBA.release();
}
/// @endcond
Socket soc;
PrintWriter outToServer;
Mat mat_t, mat;
@Override
/*! \brief Transmits the camera frames to the server
*
* On receiving a camera frame, this method encodes that frame and transmits it to the server.
*/
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mat= inputFrame.rgba();
......@@ -725,7 +775,6 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
try {
soc = new Socket(SERVER_IP,9998);
outToServer = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));
//here it sends the data
outToServer.print(yourString + "#$#$#$");
outToServer.flush();
soc.close();
......@@ -738,15 +787,18 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
/*! \brief Sets up the layout as defined in the "activity_main.xml" file
*
*/
public void layout_switch(View view) {
setContentView(R.layout.activity_main);
}
/*! \brief Sends the mouse coordinates to the server
*
* This method encodes the mouse coordinates such that the server is able to interpret that
* the coordinates are that of mouse.
*/
class SendMouseCoordinatesThread implements Runnable {
float x = 0;
......@@ -777,19 +829,20 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
/*! \brief Sends the mouse clicks
*
*/
public void mouse_click(View view) {
String Key = (String) ((Button) view).getText();
Thread thread1 = new Thread(new SendMouseClicks(Key));
thread1.start();
}
/*! \brief Used to send the mouse click events to the server
*
* This class implements the runnable interface, which encodes the mouse click events and sends it
* to the server.
*/
class SendMouseClicks implements Runnable {
String click_message="";
......@@ -818,7 +871,10 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
/*! \brief Handles the key press event
*
* This method handles the key press event and also handles the scroll button available on mouse layout.
*/
public void keyPress(View view) {
String Key = (String) ((Button) view).getText();
......@@ -880,10 +936,13 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
};
});
}
/*! \brief Used to send the keyboard events to the server
*
* This class implements the runnable interface, which encodes the keyboard events and sends it
* to the server.
*/
class SendKeyboardPressesThread implements Runnable {
String key, device;
......@@ -921,6 +980,4 @@ public class MainActivity extends AppCompatActivity implements CameraBridgeViewB
}
}
}
}
\ No newline at end of file
......@@ -10,6 +10,9 @@
<!-- First screen after starting app -->
<!-- Testing server connection layout-->
<LinearLayout
android:id="@+id/ip_address_linear_layout"
android:layout_width="match_parent"
......@@ -114,6 +117,7 @@
android:layout_weight="0.1"/>
</LinearLayout>
</LinearLayout>
......@@ -121,10 +125,7 @@
</LinearLayout>
<!-- Upper Bar-->
<!-- Upper Bar which gives the mode in which wirelessX is and also radio buttons as switches-->
<LinearLayout
android:layout_width="match_parent"
......@@ -238,10 +239,13 @@
</LinearLayout>
</LinearLayout>
<!-- Green border of 2dp-->
<LinearLayout
......@@ -254,7 +258,12 @@
<!-- Mouse Layout will go in this-->
<!-- Mouse Layout Interface which includes buttons and track pad area-->
<!-- android:visibility="gone" -->
<LinearLayout
......@@ -389,13 +398,16 @@
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- Keyboard Layout will go in this-->
<!-- Keyboard Layout Interface which includes all the keys UI elements related to keyboard-->
<LinearLayout
android:background="#1A1A1A"
......@@ -1248,13 +1260,8 @@
android:text="→"
android:onClick="keyPress"/>
</LinearLayout>
</LinearLayout>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment