From 7366fc3c64a6001f00433f75fa03c5dc8d2ce69f Mon Sep 17 00:00:00 2001 From: Apalak Dutta Date: Fri, 23 Jun 2023 20:39:32 +0530 Subject: [PATCH] Implemented notification , progress dialog after entering radius of a given location in the Luna application . --- .../example/app/src/main/AndroidManifest.xml | 3 + .../java/ru/Service/MyLocationService.java | 351 ++++++++++++++++++ .../ru/visionlab/femdemo/CheckInActivity.java | 31 +- .../src/main/res/layout/activity_checkin.xml | 1 + 4 files changed, 384 insertions(+), 2 deletions(-) create mode 100644 examples/example_bestshot/example/app/src/main/java/ru/Service/MyLocationService.java diff --git a/examples/example_bestshot/example/app/src/main/AndroidManifest.xml b/examples/example_bestshot/example/app/src/main/AndroidManifest.xml index a130d2a..34ad45b 100644 --- a/examples/example_bestshot/example/app/src/main/AndroidManifest.xml +++ b/examples/example_bestshot/example/app/src/main/AndroidManifest.xml @@ -17,6 +17,8 @@ + + + \ No newline at end of file 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 new file mode 100644 index 0000000..bb1148d --- /dev/null +++ b/examples/example_bestshot/example/app/src/main/java/ru/Service/MyLocationService.java @@ -0,0 +1,351 @@ +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(); + } + + } + }, 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; + + 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; + } +} 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 54a0e10..4a29017 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 @@ -4,6 +4,7 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.app.AlertDialog; +import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -13,6 +14,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 +28,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 +42,16 @@ 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -57,11 +66,29 @@ public class CheckInActivity extends AppCompatActivity { /*Intent intent = new Intent(CheckInActivity.this, EmployeeActivity.class); startActivity(intent);*/ - location(); + //location(); + + 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(); + } }); + 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)); + } + }); + logout.setOnClickListener(new View.OnClickListener() { @Override diff --git a/examples/example_bestshot/example/app/src/main/res/layout/activity_checkin.xml b/examples/example_bestshot/example/app/src/main/res/layout/activity_checkin.xml index 5cc489b..1be42d1 100644 --- a/examples/example_bestshot/example/app/src/main/res/layout/activity_checkin.xml +++ b/examples/example_bestshot/example/app/src/main/res/layout/activity_checkin.xml @@ -60,6 +60,7 @@ android:gravity="center"/>