From 24dc3f6a9ba9c227e29b97cb7da758735d5b28eb Mon Sep 17 00:00:00 2001 From: Apalak Dutta Date: Mon, 26 Jun 2023 21:45:35 +0530 Subject: [PATCH] On pressing the logout button ,the app is checking whether location service is running or not . On pressing the Check in button it disappears and reappears when the check out button is pressed . On pressing the back button on the login activity the app exits . Service starting only when the Check in button is pressed . Check out button cannot be pressed outside range . --- .../java/ru/Service/MyLocationService.java | 16 ++-- .../ru/visionlab/femdemo/CheckInActivity.java | 74 +++++++++++++++++-- .../femdemo/login/LoginActivity.java | 10 ++- 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/examples/example_bestshot/example/app/src/main/java/ru/Service/MyLocationService.java b/examples/example_bestshot/example/app/src/main/java/ru/Service/MyLocationService.java index bb1148d..b2b90ed 100644 --- a/examples/example_bestshot/example/app/src/main/java/ru/Service/MyLocationService.java +++ b/examples/example_bestshot/example/app/src/main/java/ru/Service/MyLocationService.java @@ -65,11 +65,13 @@ public class MyLocationService extends Service { /*Intent intent = new Intent(MainActivity.this,MyLocationService.class); startService(intent);*/ - if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + /*if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { OnGPS(); } else { getLocation(); - } + }*/ + + Toast.makeText(getApplicationContext(),"You are currently near location",Toast.LENGTH_LONG).show(); } }, delay); @@ -220,11 +222,11 @@ public class MyLocationService extends Service { { double lat=LocationGps.getLatitude(); double longi=LocationGps.getLongitude(); - /*double newLat = 22.5747; - double newLong = 88.4338;*/ + double newLat = 22.5747; + double newLong = 88.4338; - double newLat = 22.5796; - double newLong = 88.4383; + /*double newLat = 22.5796; + double newLong = 88.4383;*/ float[] results = new float[1]; Location.distanceBetween(lat,longi,newLat,newLong,results); @@ -261,6 +263,8 @@ public class MyLocationService extends Service { double newLat = 22.5747; double newLong = 88.4338; + /*double newLat = 22.5135; + double newLong = 88.4029;*/ float[] results = new float[1]; Location.distanceBetween(lat,longi,newLat,newLong,results); diff --git a/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/CheckInActivity.java b/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/CheckInActivity.java index 4a29017..d29476f 100644 --- a/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/CheckInActivity.java +++ b/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/CheckInActivity.java @@ -3,6 +3,7 @@ package ru.visionlab.femdemo; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; +import android.app.ActivityManager; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; @@ -52,6 +53,8 @@ public class CheckInActivity extends AppCompatActivity { Handler handler = new Handler(); Runnable runnable; int delay = 10000; + + float distance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -66,26 +69,33 @@ public class CheckInActivity extends AppCompatActivity { /*Intent intent = new Intent(CheckInActivity.this, EmployeeActivity.class); startActivity(intent);*/ - //location(); + location(); - progressDialog = new ProgressDialog(CheckInActivity.this); + /*progressDialog = new ProgressDialog(CheckInActivity.this); progressDialog.setTitle(""); progressDialog.setMessage("Loading this Content, please wait!"); progressDialog.show(); + location(); + startService(new Intent(CheckInActivity.this, MyLocationService.class)); - progressDialog.dismiss(); + progressDialog.dismiss();*/ + + btnCheckIn.setVisibility(View.INVISIBLE); } }); btncheckOut = findViewById(R.id.btncheckOut); + + btncheckOut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(CheckInActivity.this,"Stopping service",Toast.LENGTH_LONG).show(); stopService(new Intent(CheckInActivity.this, MyLocationService.class)); + btnCheckIn.setVisibility(View.VISIBLE); } }); @@ -93,8 +103,26 @@ public class CheckInActivity extends AppCompatActivity { logout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Intent intent = new Intent(CheckInActivity.this, LoginActivity.class); - startActivity(intent); + + + if (isMyServiceRunning(MyLocationService.class)){ + new SweetAlertDialog(CheckInActivity.this, SweetAlertDialog.WARNING_TYPE) + .setTitleText("Location service is running in the background") + .setContentText("Please stop the service to logout") + .setConfirmText("OK").setConfirmText("OK").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { + @Override + public void onClick(SweetAlertDialog sDialog) { + sDialog.dismissWithAnimation(); + } + }) + .show(); + } + else { + Toast.makeText(CheckInActivity.this,"Logging out",Toast.LENGTH_LONG).show(); + Intent intent = new Intent(CheckInActivity.this, LoginActivity.class); + startActivity(intent); + } + } }); @@ -141,6 +169,16 @@ public class CheckInActivity extends AppCompatActivity { } + private boolean isMyServiceRunning(Class serviceClass) { + ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (serviceClass.getName().equals(service.service.getClassName())) { + return true; + } + } + return false; + } + public void location(){ locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); //Check gps is enable or not @@ -198,6 +236,20 @@ public class CheckInActivity extends AppCompatActivity { double lat=LocationGps.getLatitude(); double longi=LocationGps.getLongitude(); + double newLat = 22.5150; + double newLong = 88.3930; + + float[] results = new float[1]; + Location.distanceBetween(lat,longi,newLat,newLong,results); + distance = results[0]; + Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_SHORT).show(); + if(distance > 400){ + moreThanFour(); + } + else{ + lessThanFour(); + } + latitude=String.valueOf(lat); longitude=String.valueOf(longi); System.out.println("Position 1"); @@ -222,6 +274,7 @@ public class CheckInActivity extends AppCompatActivity { moreThanFour(); } else{ + lessThanFour(); } latitude=String.valueOf(lat); @@ -252,6 +305,7 @@ public class CheckInActivity extends AppCompatActivity { } public void moreThanFour(){ + btncheckOut.setClickable(false); new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) .setTitleText("Are you sure you are near location?") .setContentText("Please be in your precise location") @@ -265,6 +319,16 @@ public class CheckInActivity extends AppCompatActivity { } public void lessThanFour(){ + progressDialog = new ProgressDialog(CheckInActivity.this); + progressDialog.setTitle(""); + progressDialog.setMessage("Loading this Content, please wait!"); + progressDialog.show(); + + + + startService(new Intent(CheckInActivity.this, MyLocationService.class)); + progressDialog.dismiss(); + new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE) .setTitleText("You are currently near location") .setContentText("Attendance can be captured now") diff --git a/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/login/LoginActivity.java b/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/login/LoginActivity.java index 3f01f82..7832f0c 100644 --- a/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/login/LoginActivity.java +++ b/examples/example_bestshot/example/app/src/main/java/ru/visionlab/femdemo/login/LoginActivity.java @@ -79,6 +79,8 @@ public class LoginActivity extends BaseActivity { LinearLayoutCompat lay_licence_verify; + private long pressedTime; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -465,6 +467,12 @@ public class LoginActivity extends BaseActivity { @Override public void onBackPressed () { - + if (pressedTime + 2000 > System.currentTimeMillis()) { + super.onBackPressed(); + finish(); + } else { + Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_SHORT).show(); + } + pressedTime = System.currentTimeMillis(); } } \ No newline at end of file