Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to log SignalStrength and GPS coordinates to a separate logfile #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

<!-- Internet is used to connect to local and remote rtl_tcp instances -->
<uses-permission android:name="android.permission.INTERNET" />


<!-- GPS is used for mapping signal strength -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Declare permissions to read and write the bookmarks database -->
<permission android:name="com.mantz_it.rfanalyzer.permission.READ_BOOKMARKS"
android:label="Read access to the bookmarks of RF Analyzer" />
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/mantz_it/rfanalyzer/AnalyzerSurface.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public class AnalyzerSurface extends SurfaceView implements SurfaceHolder.Callba
private int fontSize = FONT_SIZE_MEDIUM; // Indicates the font size of the grid labels
private boolean showDebugInformation = false;

public static float averageSignalStrength = -9999; // avg magnitude of the signal in the center of the selected channel. pulled out of draw() function to let GPS-logging access it

/**
* Constructor. Will initialize the Paint instances and register the callback
Expand Down Expand Up @@ -1019,20 +1020,19 @@ public void draw(float[] mag, long frequency, int sampleRate, int frameRate, dou
}

// Update squelchSatisfied:
float averageSignalStrengh = -9999; // avg magnitude of the signal in the center of the selected channel
if(demodulationEnabled) {
float sum = 0;
int chanStart = (int) ((channelFrequency - (frequency-sampleRate/2) - channelWidth/2) * samplesPerHz);
int chanEnd = (int)(chanStart + channelWidth * samplesPerHz);
if(chanStart > 0 && chanEnd <= mag.length) {
for (int i = chanStart; i < chanEnd; i++)
sum += mag[i];
averageSignalStrengh = sum / (chanEnd - chanStart);
if(averageSignalStrengh >= squelch && squelchSatisfied==false) {
averageSignalStrength = sum / (chanEnd - chanStart);
if(averageSignalStrength >= squelch && squelchSatisfied==false) {
squelchSatisfied = true;
this.squelchPaint.setColor(Color.GREEN);
rfControlInterface.updateSquelchSatisfied(squelchSatisfied);
} else if (averageSignalStrengh < squelch && squelchSatisfied==true) {
} else if (averageSignalStrength < squelch && squelchSatisfied==true) {
squelchSatisfied = false;
this.squelchPaint.setColor(Color.RED);
rfControlInterface.updateSquelchSatisfied(squelchSatisfied);
Expand All @@ -1053,7 +1053,7 @@ public void draw(float[] mag, long frequency, int sampleRate, int frameRate, dou
drawWaterfall(c);
drawFrequencyGrid(c);
drawPowerGrid(c);
drawPerformanceInfo(c, frameRate, load, averageSignalStrengh);
drawPerformanceInfo(c, frameRate, load, averageSignalStrength);
} else
Log.d(LOGTAG, "draw: Canvas is null.");
}
Expand Down
98 changes: 96 additions & 2 deletions app/src/main/java/com/mantz_it/rfanalyzer/MainActivity.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;

import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -45,6 +49,9 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;
import java.io.PrintWriter;
import java.io.FileWriter;

/**
* <h1>RF Analyzer - Main Activity</h1>
Expand All @@ -71,7 +78,7 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
public class MainActivity extends AppCompatActivity implements IQSourceInterface.Callback, RFControlInterface {
public class MainActivity extends AppCompatActivity implements IQSourceInterface.Callback, RFControlInterface, LocationListener {

private MenuItem mi_startStop = null;
private MenuItem mi_demodulationMode = null;
Expand All @@ -97,11 +104,15 @@ public class MainActivity extends AppCompatActivity implements IQSourceInterface
// permission to open file for the file source
public static final int PERMISSION_REQUEST_RECORDING_WRITE_FILES = 1112; // arbitrary value, used when requesting
// permission to write file for the recording feature
public static final int PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 1113; // arbitrary value, used when requesting
// permission to access GPS
private static final int FILE_SOURCE = 0;
private static final int HACKRF_SOURCE = 1;
private static final int RTLSDR_SOURCE = 2;
private static final String[] SOURCE_NAMES = new String[] {"filesource", "hackrf", "rtlsdr"};

private LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -143,6 +154,33 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

// GPS file:
defaultFile = getString(R.string.pref_gpsfile_default);
if(preferences.getString(getString(R.string.pref_gpsfile), "").equals(defaultFile))
preferences.edit().putString(getString(R.string.pref_gpsfile), extStorage + "/" + defaultFile).apply();

// Start GPS if enabled:
if(preferences.getBoolean(getString(R.string.pref_gps), false)) {
// Request permission for GPS
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
}
else {
// Permissions OK - Activate GPS
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, this);
}

if (ContextCompat.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE")
== PackageManager.PERMISSION_GRANTED) {
} else {
preferences.edit().putBoolean(getString(R.string.pref_gps), false).apply();
Log.i(LOGTAG, "onCreate: deactivate GPS logging because of missing storage permission.");
}
}

// Get version name:
try {
versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
Expand Down Expand Up @@ -215,7 +253,46 @@ public void run() {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

@Override

@Override
public void onLocationChanged(Location location) {
if (running && demodulationMode != Demodulator.DEMODULATION_OFF) {
// Message on screen
String msg = "Latitude: " + location.getLatitude() + "\n"
+ "Longitude: " + location.getLongitude() + "\n"
+ "SignalStrength: " + String.format(Locale.US, "%.1f dB", AnalyzerSurface.averageSignalStrength);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();

// Log to disk
try {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = df.format(c.getTime());

PrintWriter GPSLOG = new PrintWriter(new FileWriter(preferences.getString(getString(R.string.pref_gpsfile), ""), true));
GPSLOG.printf("%s\t%s\t%s\t%s\n", datetime, location.getLatitude(), location.getLongitude(), String.format(Locale.US, "%.1f", AnalyzerSurface.averageSignalStrength));
GPSLOG.flush();
GPSLOG.close();
} catch (Exception e) {
Log.e(LOGTAG, "onLocationChanged: couldn't log: " + e.getMessage());
}
}
}


@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
protected void onDestroy() {
super.onDestroy();
// close source
Expand Down Expand Up @@ -245,6 +322,15 @@ protected void onDestroy() {
Log.e(LOGTAG, "onDestroy: RTL2832U is not installed");
}
}
// stop GPS if active:
boolean gps_enabled = false;
try {
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch (Exception ex) {}
if(gps_enabled) {
locationManager.removeUpdates(this);
}
}

@Override
Expand Down Expand Up @@ -502,6 +588,14 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
}
break;
}
case PERMISSION_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, this);
}
break;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/mantz_it/rfanalyzer/SettingsActivity.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
Log.i(LOGTAG, "onRequestPermissionResult: User denied to write files for logging. deactivate setting..");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putBoolean(getString(R.string.pref_logging), false).apply();
preferences.edit().putBoolean(getString(R.string.pref_gps), false).apply();
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/mantz_it/rfanalyzer/SettingsFragment.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.File;

import static com.mantz_it.rfanalyzer.SettingsActivity.PERMISSION_REQUEST_LOGGING_WRITE_FILES;
import static com.mantz_it.rfanalyzer.MainActivity.PERMISSION_REQUEST_ACCESS_FINE_LOCATION;

/**
* <h1>RF Analyzer - Settings Fragment</h1>
Expand Down Expand Up @@ -167,6 +168,22 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
PERMISSION_REQUEST_LOGGING_WRITE_FILES);
}
}

// check WRITE_EXTERNAL_STORAGE and ACCESS_FINE_LOCATION permission if GPS is active:
if(sharedPreferences.getBoolean(getString(R.string.pref_gps), false)) {
if (ContextCompat.checkSelfPermission(this.getActivity(), "android.permission.WRITE_EXTERNAL_STORAGE")
!= PackageManager.PERMISSION_GRANTED) {
// request permission:
ActivityCompat.requestPermissions(this.getActivity(), new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
PERMISSION_REQUEST_LOGGING_WRITE_FILES);
}
if (ContextCompat.checkSelfPermission(this.getActivity(), "android.permission.ACCESS_FINE_LOCATION")
!= PackageManager.PERMISSION_GRANTED) {
// request permission:
ActivityCompat.requestPermissions(this.getActivity(), new String[]{"android.permission.ACCESS_FINE_LOCATION"},
PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
}
}
}

/**
Expand Down Expand Up @@ -260,13 +277,19 @@ public void updateSummaries() {
else
listPref.setSummary(getString(R.string.pref_frameRate_summ, listPref.getEntry()));

// GPSfile
editTextPref = (EditTextPreference) findPreference(getString(R.string.pref_gpsfile));
editTextPref.setSummary(getString(R.string.pref_gpsfile_summ, editTextPref.getText()));

// Logfile
editTextPref = (EditTextPreference) findPreference(getString(R.string.pref_logfile));
editTextPref.setSummary(getString(R.string.pref_logfile_summ, editTextPref.getText()));

// Shared preferences updated in e.g. the onRequestPermissionResult() method are
// not automatically updated in the preference fragment gui. do it manually:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
switchPref = (SwitchPreference) findPreference(getString(R.string.pref_gps));
switchPref.setChecked(preferences.getBoolean(getString(R.string.pref_gps), false));
switchPref = (SwitchPreference) findPreference(getString(R.string.pref_logging));
switchPref.setChecked(preferences.getBoolean(getString(R.string.pref_logging), false));
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,20 @@
<string name="pref_frameRate_title">Frame Rate</string>
<string name="pref_frameRate_default">10</string>
<string name="pref_frameRate_summ">Frame rate is set to: %s</string>
<string name="pref_gps">pref_gps</string>
<string name="pref_gps_title">GPS</string>
<string name="pref_gps_summ_on">GPS is enabled</string>
<string name="pref_gps_summ_off">GPS is disabled</string>
<string name="pref_logging">pref_logging</string>
<string name="pref_logging_title">Logging</string>
<string name="pref_logging_summ_on">Logging is enabled</string>
<string name="pref_logging_summ_off">Logging is disabled</string>
<string name="pref_showLog">pref_showLog</string>
<string name="pref_showLog_title">Show Log</string>
<string name="pref_gpsfile">pref_gpsfile</string>
<string name="pref_gpsfile_title">GPS File</string>
<string name="pref_gpsfile_summ">File path: %s</string>
<string name="pref_gpsfile_default">RFAnalyzer/gps.txt</string>
<string name="pref_logfile">pref_logfile</string>
<string name="pref_logfile_title">Log File</string>
<string name="pref_logfile_summ">File path: %s</string>
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/xml/preferences.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@
android:entries="@array/pref_frameRate_entries"
android:entryValues="@array/pref_frameRate_values"
android:defaultValue="@string/pref_frameRate_default" />
<SwitchPreference
android:key="@string/pref_gps"
android:title="@string/pref_gps_title"
android:summaryOn="@string/pref_gps_summ_on"
android:summaryOff="@string/pref_gps_summ_off"
android:defaultValue="false" />
<EditTextPreference
android:key="@string/pref_gpsfile"
android:title="@string/pref_gpsfile_title"
android:dependency="@string/pref_gps"
android:defaultValue="@string/pref_gpsfile_default" />
<SwitchPreference
android:key="@string/pref_logging"
android:title="@string/pref_logging_title"
Expand Down