From 73b61de0ca8c65a42be5224a6e521e03b552a8ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Grennerat?= Date: Tue, 29 Aug 2023 17:29:54 +0200 Subject: [PATCH] Fix ANR issues with locate command --- app/src/main/AndroidManifest.xml | 1 + .../commander/CommandExecutor.java | 8 +++--- .../commander/LocateCommandExecutor.java | 24 ++---------------- .../commander/LocationService.java | 25 +++++++++++-------- .../ui/commands/CommandsFragment.java | 2 +- 5 files changed, 22 insertions(+), 38 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ad47570..1350191 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,7 @@ + locationQueue = LocationService.getLocationQueue(); - - try{ - Location location = locationQueue.take(); - commandExecutor.replyAndTerminate( - "Maps : https://www.google.com/maps/place/" + location.getLatitude() + "%20" + location.getLongitude() + "\n" + - context.getString(R.string.info_latitude) + " : " + location.getLatitude() + "°\n" + - context.getString(R.string.info_longitude) + " : " + location.getLongitude() + "°\n" + - context.getString(R.string.info_accuracy) + " : " + location.getAccuracy() + " m" + "\n" + - context.getString(R.string.info_bearing) + " : " + location.getBearing() + "°\n" + - context.getString(R.string.info_speed) + " : " + location.getSpeed() + " m/s \n" + - "Date : " + new Date(location.getTime())); - }catch(InterruptedException e){ - e.printStackTrace(); - commandExecutor.replyAndTerminate(context.getString(R.string.info_location_unknown)); - } + commandExecutor.replyAndTerminate(context.getString(R.string.info_localizing)); } } diff --git a/app/src/main/java/fr/themsou/monitorinternetless/commander/LocationService.java b/app/src/main/java/fr/themsou/monitorinternetless/commander/LocationService.java index 57bc566..a057ae5 100644 --- a/app/src/main/java/fr/themsou/monitorinternetless/commander/LocationService.java +++ b/app/src/main/java/fr/themsou/monitorinternetless/commander/LocationService.java @@ -19,8 +19,7 @@ import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; +import java.util.Date; import fr.themsou.monitorinternetless.MainActivity; import fr.themsou.monitorinternetless.R; @@ -28,16 +27,16 @@ public class LocationService extends Service implements LocationListener { private LocationManager locationManager; - - private static final BlockingQueue locationQueue = new LinkedBlockingQueue<>(); + private String number; private static final int NOTIFICATION_ID = 2; private static final String CHANNEL_ID = "LocationService"; + + @Override public void onCreate() { super.onCreate(); - locationQueue.clear(); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } @@ -45,7 +44,7 @@ public void onCreate() { @RequiresApi(api = Build.VERSION_CODES.S) @Override public int onStartCommand(Intent intent, int flags, int startId){ - + number = intent.getStringExtra("number"); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){ startForeground(NOTIFICATION_ID, createNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION); @@ -60,7 +59,15 @@ public int onStartCommand(Intent intent, int flags, int startId){ @Override public void onLocationChanged(@NonNull Location location) { Log.d(CHANNEL_ID, "Location listener called. lat,long:" + location.getLatitude() + "," + location.getLongitude()); - locationQueue.add(location); + + CommandExecutor.reply(number, + "Maps : https://www.google.com/maps/place/" + location.getLatitude() + "%20" + location.getLongitude() + "\n" + + getString(R.string.info_latitude) + " : " + location.getLatitude() + "°\n" + + getString(R.string.info_longitude) + " : " + location.getLongitude() + "°\n" + + getString(R.string.info_accuracy) + " : " + location.getAccuracy() + " m" + "\n" + + getString(R.string.info_bearing) + " : " + location.getBearing() + "°\n" + + getString(R.string.info_speed) + " : " + location.getSpeed() + " m/s \n" + + "Date : " + new Date(location.getTime())); stopForeground(true); stopSelf(); @@ -87,8 +94,4 @@ private Notification createNotification() { public IBinder onBind(Intent intent) { return null; } - - public static BlockingQueue getLocationQueue() { - return locationQueue; - } } diff --git a/app/src/main/java/fr/themsou/monitorinternetless/ui/commands/CommandsFragment.java b/app/src/main/java/fr/themsou/monitorinternetless/ui/commands/CommandsFragment.java index 26b1141..34b327c 100644 --- a/app/src/main/java/fr/themsou/monitorinternetless/ui/commands/CommandsFragment.java +++ b/app/src/main/java/fr/themsou/monitorinternetless/ui/commands/CommandsFragment.java @@ -32,7 +32,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, listView = root.findViewById(R.id.command_listview); listView.addHeaderView(layoutInflater.inflate(R.layout.header_commands, null)); - adapter = new CommandsListAdapter(getContext(), ((MainActivity) getActivity()), getCommands((MainActivity) getActivity())); + adapter = new CommandsListAdapter(getContext(), ((MainActivity) getActivity()), getCommands(getActivity())); listView.setAdapter(adapter);