first cut at turnon scripts for EasyTimer v2
[fw/altos] / altosdroid / app / src / main / java / org / altusmetrum / AltosDroid / TelemetryLogger.java
1 /*
2  * Copyright © 2021 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 package org.altusmetrum.AltosDroid;
19
20 import java.io.*;
21
22 import org.altusmetrum.altoslib_14.*;
23
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.os.Environment;
29
30 public class TelemetryLogger implements AltosLogTrace {
31         private TelemetryService service = null;
32         private AltosLink link    = null;
33         private AltosLog  logger  = null;
34
35         private BroadcastReceiver mExternalStorageReceiver;
36
37         /* AltosLogTrace interface */
38         public void trace(String format, Object ... arguments) {
39                 AltosDebug.debug(format, arguments);
40         }
41
42         public void open_failed(File file) {
43                 service.send_file_failed_to_clients(file);
44         }
45
46         public TelemetryLogger(TelemetryService in_service, AltosLink in_link) {
47                 service = in_service;
48                 link    = in_link;
49
50                 startWatchingExternalStorage();
51         }
52
53         public void stop() {
54                 stopWatchingExternalStorage();
55                 close();
56         }
57
58         private void close() {
59                 if (logger != null) {
60                         AltosDebug.debug("Shutting down Telemetry Logging");
61                         logger.close();
62                         logger = null;
63                 }
64         }
65
66         void handleExternalStorageState() {
67                 String state = Environment.getExternalStorageState();
68                 if (Environment.MEDIA_MOUNTED.equals(state)) {
69                         if (logger == null) {
70                                 AltosDebug.debug("Starting up Telemetry Logging");
71                                 logger = new AltosLog(link,this);
72                         }
73                 } else {
74                         AltosDebug.debug("External Storage not present - stopping");
75                         close();
76                 }
77         }
78
79         void startWatchingExternalStorage() {
80                 mExternalStorageReceiver = new BroadcastReceiver() {
81                         @Override
82                         public void onReceive(Context context, Intent intent) {
83                                 handleExternalStorageState();
84                         }
85                 };
86                 IntentFilter filter = new IntentFilter();
87                 filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
88                 filter.addAction(Intent.ACTION_MEDIA_REMOVED);
89                 service.registerReceiver(mExternalStorageReceiver, filter);
90                 handleExternalStorageState();
91         }
92
93         void stopWatchingExternalStorage() {
94                 service.unregisterReceiver(mExternalStorageReceiver);
95         }
96
97 }