altosdroid: Catch log file open failures and show an error dialog
authorKeith Packard <keithp@keithp.com>
Sun, 10 Oct 2021 04:32:17 +0000 (21:32 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 10 Oct 2021 04:32:17 +0000 (21:32 -0700)
Android changed which directories we can write to once, let's hope it
doesn't happen again.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/TelemetryLogger.java
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/TelemetryService.java

index 5caee5f87aab5323ea0032cf54f865649ff22a8b..1fcb0a470a54b6800af756d12cd845835beb5422 100644 (file)
@@ -20,6 +20,7 @@ package org.altusmetrum.AltosDroid;
 
 import java.lang.ref.WeakReference;
 import java.util.*;
+import java.io.*;
 
 import android.Manifest;
 import android.app.Activity;
@@ -86,6 +87,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
        public static final int MSG_UPDATE_AGE      = 2;
        public static final int MSG_IDLE_MODE       = 3;
        public static final int MSG_IGNITER_STATUS  = 4;
+       public static final int MSG_FILE_FAILED     = 5;
 
        // Intent request codes
        public static final int REQUEST_CONNECT_DEVICE = 1;
@@ -190,6 +192,9 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                                ad.idle_mode = (Boolean) msg.obj;
                                ad.update_state(null);
                                break;
+                       case MSG_FILE_FAILED:
+                               ad.file_failed((File) msg.obj);
+                               break;
                        }
                }
        };
@@ -1010,6 +1015,24 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                }
        }
 
+       boolean fail_shown;
+
+       private void file_failed(File file) {
+               if (!fail_shown) {
+                       fail_shown = true;
+                       AlertDialog fail = new AlertDialog.Builder(this).create();
+                       fail.setTitle("Failed to Create Log File");
+                       fail.setMessage(file.getPath());
+                       fail.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
+                                      new DialogInterface.OnClickListener() {
+                                              public void onClick(DialogInterface dialog, int which) {
+                                                      dialog.dismiss();
+                                              }
+                                      });
+                       fail.show();
+               }
+       }
+
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
index 589e15d07a351d96e6f1d0011521f2726191b758..ae5702b48b87dfa9cca64bcaa443057724b6f5f3 100644 (file)
@@ -1,5 +1,24 @@
+/*
+ * Copyright © 2021 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
 package org.altusmetrum.AltosDroid;
 
+import java.io.*;
+
 import org.altusmetrum.altoslib_14.*;
 
 import android.content.BroadcastReceiver;
@@ -8,15 +27,24 @@ import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Environment;
 
-public class TelemetryLogger {
-       private Context   context = null;
+public class TelemetryLogger implements AltosLogTrace {
+       private TelemetryService service = null;
        private AltosLink link    = null;
        private AltosLog  logger  = null;
 
        private BroadcastReceiver mExternalStorageReceiver;
 
-       public TelemetryLogger(Context in_context, AltosLink in_link) {
-               context = in_context;
+       /* AltosLogTrace interface */
+       public void trace(String format, Object ... arguments) {
+               AltosDebug.debug(format, arguments);
+       }
+
+       public void open_failed(File file) {
+               service.send_file_failed_to_clients(file);
+       }
+
+       public TelemetryLogger(TelemetryService in_service, AltosLink in_link) {
+               service = in_service;
                link    = in_link;
 
                startWatchingExternalStorage();
@@ -40,7 +68,7 @@ public class TelemetryLogger {
                if (Environment.MEDIA_MOUNTED.equals(state)) {
                        if (logger == null) {
                                AltosDebug.debug("Starting up Telemetry Logging");
-                               logger = new AltosLog(link);
+                               logger = new AltosLog(link,this);
                        }
                } else {
                        AltosDebug.debug("External Storage not present - stopping");
@@ -58,12 +86,12 @@ public class TelemetryLogger {
                IntentFilter filter = new IntentFilter();
                filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
                filter.addAction(Intent.ACTION_MEDIA_REMOVED);
-               context.registerReceiver(mExternalStorageReceiver, filter);
+               service.registerReceiver(mExternalStorageReceiver, filter);
                handleExternalStorageState();
        }
 
        void stopWatchingExternalStorage() {
-               context.unregisterReceiver(mExternalStorageReceiver);
+               service.unregisterReceiver(mExternalStorageReceiver);
        }
 
 }
index 2c2095df68186847225e4480993754123be524a2..b0b82ab9c9673643f8fead134a8a9e9445a8a1d8 100644 (file)
@@ -20,6 +20,7 @@ package org.altusmetrum.AltosDroid;
 
 import java.lang.ref.WeakReference;
 import java.util.concurrent.TimeoutException;
+import java.io.*;
 import java.util.*;
 
 import android.app.*;
@@ -340,6 +341,21 @@ public class TelemetryService extends Service implements AltosIdleMonitorListene
                        send_idle_mode_to_client(client);
        }
 
+       private void send_file_failed_to_client(Messenger client, File f) {
+               Message m = Message.obtain(null, AltosDroid.MSG_FILE_FAILED, f);
+               try {
+                       client.send(m);
+               } catch (RemoteException e) {
+                       AltosDebug.error("Client %s disappeared", client.toString());
+                       remove_client(client);
+               }
+       }
+
+       public void send_file_failed_to_clients(File f) {
+               for (Messenger client : clients)
+                       send_file_failed_to_client(client, f);
+       }
+
        private void telemetry_start() {
                if (telemetry_reader == null && idle_monitor == null && !ignite_running) {
                        telemetry_reader = new TelemetryReader(altos_link, handler);