Compare commits

..

2 Commits

Author SHA1 Message Date
24dc3f6a9b 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 .
2023-06-26 21:45:35 +05:30
7366fc3c64 Implemented notification , progress dialog after entering radius of a given location in the Luna application . 2023-06-23 20:39:32 +05:30
5 changed files with 462 additions and 4 deletions

View File

@ -17,6 +17,8 @@
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:name=".App"
android:allowBackup="false"
@ -73,6 +75,7 @@
<activity
android:name=".authentication.AuthSuccessActivity"
android:screenOrientation="portrait" />
<service android:name="ru.Service.MyLocationService"/>
</application>
</manifest>

View File

@ -0,0 +1,355 @@
package ru.Service;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import ru.visionlab.femdemo.CheckInActivity;
import ru.visionlab.femdemo.R;
public class MyLocationService extends Service {
LocationManager locationManager;
private static final int REQUEST_LOCATION = 1;
String latitude, longitude;
ProgressDialog progressDialog;
Handler handler = new Handler();
Runnable runnable;
int delay = 10000;
@Override
// execution of service will start
// on calling this method
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Inside my location service");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
addNotification();
trackGPS();
// returns the status
// of the program
return START_STICKY;
}
private void trackGPS() {
handler.postDelayed(runnable = new Runnable() {
public void run() {
handler.postDelayed(runnable, delay);
System.out.println("Inside handler");
/*Intent intent = new Intent(MainActivity.this,MyLocationService.class);
startService(intent);*/
/*if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
OnGPS();
} else {
getLocation();
}*/
Toast.makeText(getApplicationContext(),"You are currently near location",Toast.LENGTH_LONG).show();
}
}, delay);
}
@Override
// execution of the service will
// stop on calling this method
public void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
private void addNotification() {
System.out.println("Inside addNotification");
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "notify_001");
Intent ii = new Intent(getApplicationContext(), CheckInActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.setBigContentTitle("Service started ");
bigText.setSummaryText("Location getting tracked ");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Service started");
mBuilder.setContentText("Location getting tracked");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);
mNotificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// === Removed some obsoletes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
mNotificationManager.notify(0, mBuilder.build());
System.out.println("Inside add notific");
/*NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());*/
}
private void locationNotification() {
System.out.println("Inside addNotification");
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "notify_001");
Intent ii = new Intent(getApplicationContext(), CheckInActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.setBigContentTitle("Location notification");
bigText.setSummaryText("Entered location perimeter");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Service started");
mBuilder.setContentText("Location getting tracked");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);
mNotificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// === Removed some obsoletes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
mNotificationManager.notify(0, mBuilder.build());
System.out.println("Inside add notific");
/*NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());*/
}
private void getLocation() {
//Check Permissions again
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location LocationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location LocationNetwork=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location LocationPassive=locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (LocationGps !=null)
{
double lat=LocationGps.getLatitude();
double longi=LocationGps.getLongitude();
double newLat = 22.5747;
double newLong = 88.4338;
/*double newLat = 22.5796;
double newLong = 88.4383;*/
float[] results = new float[1];
Location.distanceBetween(lat,longi,newLat,newLong,results);
float distance = results[0];
Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_LONG).show();
if(distance > 400){
Toast.makeText(this,"You are outside location range",Toast.LENGTH_LONG).show();
System.out.println("You are outside location range");
System.out.println("Position 1 ");
//moreThanFour();
}
else{
Toast.makeText(this,"You are inside location range",Toast.LENGTH_LONG).show();
locationNotification();
System.out.println("You are inside location range");
System.out.println("Position 2 ");
//lessThanFour();
}
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
System.out.println("Inside getLocation");
}
else if (LocationNetwork !=null)
{
System.out.println("Position 3 ");
double lat=LocationNetwork.getLatitude();
double longi=LocationNetwork.getLongitude();
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);
float distance = results[0];
Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_LONG).show();
if(distance > 400){
Toast.makeText(this,"You are outside location range",Toast.LENGTH_LONG).show();
System.out.println("You are outside location range");
//moreThanFour();
}
else{
Toast.makeText(this,"You are inside location range",Toast.LENGTH_LONG).show();
System.out.println("You are inside location range");
//lessThanFour();
}
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
}
else if (LocationPassive !=null)
{
double lat=LocationPassive.getLatitude();
double longi=LocationPassive.getLongitude();
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
}
else
{
// Toast.makeText(this, "Can't Get Your Location", Toast.LENGTH_SHORT).show();
double lat=LocationNetwork.getLatitude();
double longi=LocationNetwork.getLongitude();
double newLat = 22.5747;
double newLong = 88.4338;
float[] results = new float[1];
Location.distanceBetween(lat,longi,newLat,newLong,results);
float distance = results[0];
Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_LONG).show();
if(distance > 400){
Toast.makeText(this,"You are outside location range",Toast.LENGTH_LONG).show();
System.out.println("You are outside location range");
//moreThanFour();
}
else{
Toast.makeText(this,"You are inside location range",Toast.LENGTH_LONG).show();
System.out.println("You are inside location range");
//lessThanFour();
}
}
//Thats All Run Your App
}
private void OnGPS() {
final AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alertDialog=builder.create();
alertDialog.show();
System.out.println("Inside OnGPS");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View File

@ -3,7 +3,9 @@ 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;
import android.content.DialogInterface;
import android.content.Intent;
@ -13,6 +15,7 @@ import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
@ -26,6 +29,7 @@ import android.widget.TextView;
import android.widget.Toast;
import cn.pedant.SweetAlert.SweetAlertDialog;
import ru.Service.MyLocationService;
import ru.visionlab.femdemo.login.LoginActivity;
import ru.visionlab.femdemo.register.RegisterActivityNew;
import ru.visionlab.femdemo.views.EmployeeActivity;
@ -39,10 +43,18 @@ public class CheckInActivity extends AppCompatActivity {
AlertDialog dialogBuilder;
Button btnCheckIn;
Button btnCheckIn,btncheckOut;
LocationManager locationManager;
String latitude,longitude;
ProgressDialog progressDialog;
Handler handler = new Handler();
Runnable runnable;
int delay = 10000;
float distance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -59,6 +71,31 @@ public class CheckInActivity extends AppCompatActivity {
location();
/*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();*/
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);
}
});
@ -66,9 +103,27 @@ public class CheckInActivity extends AppCompatActivity {
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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);
}
}
});
menu = findViewById(R.id.menu);
@ -114,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
@ -171,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");
@ -195,6 +274,7 @@ public class CheckInActivity extends AppCompatActivity {
moreThanFour();
}
else{
lessThanFour();
}
latitude=String.valueOf(lat);
@ -225,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")
@ -238,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")

View File

@ -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();
}
}

View File

@ -60,6 +60,7 @@
android:gravity="center"/>
<Button
android:id="@+id/btncheckOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"