media playback listener

This commit is contained in:
jens 2022-01-30 20:13:56 +01:00
parent 9387e8bdb2
commit 5899dd86f5
5 changed files with 91 additions and 58 deletions

View File

@ -218,6 +218,13 @@ public class AutomationService extends Service implements OnInitListener
// Actions.setData(true); // Actions.setData(true);
// ********** Test area ********** // ********** Test area **********
/*
On normal phones the app is supposed to automatically restart in case of any problems.
In the emulator we want it to stop to be able to better pinpoint the root cause.
*/
if(Miscellaneous.isAndroidEmulator())
return START_NOT_STICKY;
else
return START_STICKY; return START_STICKY;
} }
else else

View File

@ -66,7 +66,7 @@ public class Settings implements SharedPreferences
public static boolean executeRulesAndProfilesWithSingleClick; public static boolean executeRulesAndProfilesWithSingleClick;
public static boolean displayNewsOnMainScreen; public static boolean displayNewsOnMainScreen;
public static boolean automaticUpdateCheck; public static boolean automaticUpdateCheck;
public static long musicCheckFrequency = 5; public static long musicCheckFrequency;
public static boolean lockSoundChanges; public static boolean lockSoundChanges;
public static boolean noticeAndroid9MicrophoneShown; public static boolean noticeAndroid9MicrophoneShown;
@ -128,6 +128,7 @@ public class Settings implements SharedPreferences
protected static final boolean default_lockSoundChanges = false; protected static final boolean default_lockSoundChanges = false;
protected static final long default_lastNewsPolltime = -1; protected static final long default_lastNewsPolltime = -1;
protected static final long default_lastUpdateCheck = -1; protected static final long default_lastUpdateCheck = -1;
protected static final long default_musicCheckFrequency = 2500;
@Override @Override
public boolean contains(String arg0) public boolean contains(String arg0)
@ -273,6 +274,8 @@ public class Settings implements SharedPreferences
lastNewsPolltime = prefs.getLong("lastNewsPolltime", default_lastNewsPolltime); lastNewsPolltime = prefs.getLong("lastNewsPolltime", default_lastNewsPolltime);
lastUpdateCheck = prefs.getLong("lastUpdateCheck", default_lastUpdateCheck); lastUpdateCheck = prefs.getLong("lastUpdateCheck", default_lastUpdateCheck);
musicCheckFrequency = prefs.getLong("musicCheckFrequency", default_musicCheckFrequency);
String whbdString = prefs.getString("whatHasBeenDone", ""); String whbdString = prefs.getString("whatHasBeenDone", "");
if(whbdString != null && whbdString.length() > 0) if(whbdString != null && whbdString.length() > 0)
{ {
@ -328,154 +331,157 @@ public class Settings implements SharedPreferences
Editor editor = prefs.edit(); Editor editor = prefs.edit();
if(!prefs.contains("startServiceAtSystemBoot") | force) if(!prefs.contains("startServiceAtSystemBoot") || force)
editor.putBoolean("startServiceAtSystemBoot", default_startServiceAtSystemBoot); editor.putBoolean("startServiceAtSystemBoot", default_startServiceAtSystemBoot);
if(!prefs.contains("writeLogFile") | force) if(!prefs.contains("writeLogFile") || force)
editor.putBoolean("writeLogFile", default_writeLogFile); editor.putBoolean("writeLogFile", default_writeLogFile);
// if(!prefs.contains("useTextToSpeech") | force) // if(!prefs.contains("useTextToSpeech") || force)
// editor.putBoolean("useTextToSpeech", default_useTextToSpeech); // editor.putBoolean("useTextToSpeech", default_useTextToSpeech);
if(!prefs.contains("useTextToSpeechOnNormal") | force) if(!prefs.contains("useTextToSpeechOnNormal") || force)
editor.putBoolean("useTextToSpeechOnNormal", default_useTextToSpeechOnNormal); editor.putBoolean("useTextToSpeechOnNormal", default_useTextToSpeechOnNormal);
if(!prefs.contains("useTextToSpeechOnVibrate") | force) if(!prefs.contains("useTextToSpeechOnVibrate") || force)
editor.putBoolean("useTextToSpeechOnVibrate", default_useTextToSpeechOnVibrate); editor.putBoolean("useTextToSpeechOnVibrate", default_useTextToSpeechOnVibrate);
if(!prefs.contains("useTextToSpeechOnSilent") | force) if(!prefs.contains("useTextToSpeechOnSilent") || force)
editor.putBoolean("useTextToSpeechOnSilent", default_useTextToSpeechOnSilent); editor.putBoolean("useTextToSpeechOnSilent", default_useTextToSpeechOnSilent);
if(!prefs.contains("muteTextToSpeechDuringCalls") | force) if(!prefs.contains("muteTextToSpeechDuringCalls") || force)
editor.putBoolean("muteTextToSpeechDuringCalls", default_muteTextToSpeechDuringCalls); editor.putBoolean("muteTextToSpeechDuringCalls", default_muteTextToSpeechDuringCalls);
if(!prefs.contains("positioningEngine") | force) if(!prefs.contains("positioningEngine") || force)
editor.putString("positioningEngine", String.valueOf(default_positioningEngine)); editor.putString("positioningEngine", String.valueOf(default_positioningEngine));
if(!prefs.contains("useWifiForPositioning") | force) if(!prefs.contains("useWifiForPositioning") || force)
editor.putBoolean("useWifiForPositioning", default_useWifiForPositioning); editor.putBoolean("useWifiForPositioning", default_useWifiForPositioning);
if(!prefs.contains("hasServiceBeenRunning") | force) if(!prefs.contains("hasServiceBeenRunning") || force)
editor.putBoolean("hasServiceBeenRunning", default_hasServiceBeenRunning); editor.putBoolean("hasServiceBeenRunning", default_hasServiceBeenRunning);
if(!prefs.contains("startServiceAfterAppUpdate") | force) if(!prefs.contains("startServiceAfterAppUpdate") || force)
editor.putBoolean("startServiceAfterAppUpdate", default_startServiceAfterAppUpdate); editor.putBoolean("startServiceAfterAppUpdate", default_startServiceAfterAppUpdate);
if(!prefs.contains("startNewThreadForRuleActivation") | force) if(!prefs.contains("startNewThreadForRuleActivation") || force)
editor.putBoolean("startNewThreadForRuleActivation", default_startNewThreadForRuleActivation); editor.putBoolean("startNewThreadForRuleActivation", default_startNewThreadForRuleActivation);
if(!prefs.contains("showIconWhenServiceIsRunning") | force) if(!prefs.contains("showIconWhenServiceIsRunning") || force)
editor.putBoolean("showIconWhenServiceIsRunning", default_showIconWhenServiceIsRunning); editor.putBoolean("showIconWhenServiceIsRunning", default_showIconWhenServiceIsRunning);
if(!prefs.contains("useAccelerometerForPositioning") | force) if(!prefs.contains("useAccelerometerForPositioning") || force)
editor.putBoolean("useAccelerometerForPositioning", default_useAccelerometerForPositioning); editor.putBoolean("useAccelerometerForPositioning", default_useAccelerometerForPositioning);
if(!prefs.contains("useAccelerometerAfterIdleTime") | force) if(!prefs.contains("useAccelerometerAfterIdleTime") || force)
editor.putString("useAccelerometerAfterIdleTime", String.valueOf(default_useAccelerometerAfterIdleTime)); editor.putString("useAccelerometerAfterIdleTime", String.valueOf(default_useAccelerometerAfterIdleTime));
if(!prefs.contains("accelerometerMovementThreshold") | force) if(!prefs.contains("accelerometerMovementThreshold") || force)
editor.putString("accelerometerMovementThreshold", String.valueOf(default_accelerometerMovementThreshold)); editor.putString("accelerometerMovementThreshold", String.valueOf(default_accelerometerMovementThreshold));
if(!prefs.contains("speedMaximumTimeBetweenLocations") | force) if(!prefs.contains("speedMaximumTimeBetweenLocations") || force)
editor.putString("speedMaximumTimeBetweenLocations", String.valueOf(default_speedMaximumTimeBetweenLocations)); editor.putString("speedMaximumTimeBetweenLocations", String.valueOf(default_speedMaximumTimeBetweenLocations));
if(!prefs.contains("MINIMUM_DISTANCE_CHANGE_FOR_GPS_UPDATE") | force) if(!prefs.contains("MINIMUM_DISTANCE_CHANGE_FOR_GPS_UPDATE") || force)
editor.putString("MINIMUM_DISTANCE_CHANGE_FOR_GPS_UPDATE", String.valueOf(default_minimumDistanceChangeForGpsUpdate)); editor.putString("MINIMUM_DISTANCE_CHANGE_FOR_GPS_UPDATE", String.valueOf(default_minimumDistanceChangeForGpsUpdate));
if(!prefs.contains("MINIMUM_DISTANCE_CHANGE_FOR_NETWORK_UPDATE") | force) if(!prefs.contains("MINIMUM_DISTANCE_CHANGE_FOR_NETWORK_UPDATE") || force)
editor.putString("MINIMUM_DISTANCE_CHANGE_FOR_NETWORK_UPDATE", String.valueOf(default_minimumDistanceChangeForNetworkUpdate)); editor.putString("MINIMUM_DISTANCE_CHANGE_FOR_NETWORK_UPDATE", String.valueOf(default_minimumDistanceChangeForNetworkUpdate));
if(!prefs.contains("SATISFACTORY_ACCURACY_GPS") | force) if(!prefs.contains("SATISFACTORY_ACCURACY_GPS") || force)
editor.putString("SATISFACTORY_ACCURACY_GPS", String.valueOf(default_satisfactoryAccuracyGps)); editor.putString("SATISFACTORY_ACCURACY_GPS", String.valueOf(default_satisfactoryAccuracyGps));
if(!prefs.contains("SATISFACTORY_ACCURACY_NETWORK") | force) if(!prefs.contains("SATISFACTORY_ACCURACY_NETWORK") || force)
editor.putString("SATISFACTORY_ACCURACY_NETWORK", String.valueOf(default_satisfactoryAccuracyNetwork)); editor.putString("SATISFACTORY_ACCURACY_NETWORK", String.valueOf(default_satisfactoryAccuracyNetwork));
if(!prefs.contains("gpsTimeout") | force) if(!prefs.contains("gpsTimeout") || force)
editor.putString("gpsTimeout", String.valueOf(default_gpsTimeout)); editor.putString("gpsTimeout", String.valueOf(default_gpsTimeout));
if(!prefs.contains("MINIMUM_TIME_BETWEEN_UPDATE") | force) if(!prefs.contains("MINIMUM_TIME_BETWEEN_UPDATE") || force)
editor.putString("MINIMUM_TIME_BETWEEN_UPDATE", String.valueOf(default_minimumTimeBetweenUpdate)); editor.putString("MINIMUM_TIME_BETWEEN_UPDATE", String.valueOf(default_minimumTimeBetweenUpdate));
if(!prefs.contains("timeBetweenNoiseLevelMeasurements") | force) if(!prefs.contains("timeBetweenNoiseLevelMeasurements") || force)
editor.putString("timeBetweenNoiseLevelMeasurements", String.valueOf(default_timeBetweenNoiseLevelMeasurements)); editor.putString("timeBetweenNoiseLevelMeasurements", String.valueOf(default_timeBetweenNoiseLevelMeasurements));
if(!prefs.contains("lengthOfNoiseLevelMeasurements") | force) if(!prefs.contains("lengthOfNoiseLevelMeasurements") || force)
editor.putString("lengthOfNoiseLevelMeasurements", String.valueOf(default_lengthOfNoiseLevelMeasurements)); editor.putString("lengthOfNoiseLevelMeasurements", String.valueOf(default_lengthOfNoiseLevelMeasurements));
if(!prefs.contains("referenceValueForNoiseLevelMeasurements") | force) if(!prefs.contains("referenceValueForNoiseLevelMeasurements") || force)
editor.putString("referenceValueForNoiseLevelMeasurements", String.valueOf(default_referenceValueForNoiseLevelMeasurements)); editor.putString("referenceValueForNoiseLevelMeasurements", String.valueOf(default_referenceValueForNoiseLevelMeasurements));
if(!prefs.contains("logLevel") | force) if(!prefs.contains("logLevel") || force)
editor.putString("logLevel", String.valueOf(default_logLevel)); editor.putString("logLevel", String.valueOf(default_logLevel));
if(!prefs.contains("logFileMaxSize") | force) if(!prefs.contains("logFileMaxSize") || force)
editor.putString("logFileMaxSize", String.valueOf(default_logFileMaxSize)); editor.putString("logFileMaxSize", String.valueOf(default_logFileMaxSize));
if(!prefs.contains("httpAcceptAllCertificates") | force) if(!prefs.contains("httpAcceptAllCertificates") || force)
editor.putBoolean("httpAcceptAllCertificates", default_httpAcceptAllCertificates); editor.putBoolean("httpAcceptAllCertificates", default_httpAcceptAllCertificates);
if(!prefs.contains("httpAttempts") | force) if(!prefs.contains("httpAttempts") || force)
editor.putString("httpAttempts", String.valueOf(default_httpAttempts)); editor.putString("httpAttempts", String.valueOf(default_httpAttempts));
if(!prefs.contains("httpAttemptsTimeout") | force) if(!prefs.contains("httpAttemptsTimeout") || force)
editor.putString("httpAttemptsTimeout", String.valueOf(default_httpAttemptsTimeout)); editor.putString("httpAttemptsTimeout", String.valueOf(default_httpAttemptsTimeout));
if(!prefs.contains("httpAttemptGap") | force) if(!prefs.contains("httpAttemptGap") || force)
editor.putString("httpAttemptGap", String.valueOf(default_httpAttemptGap)); editor.putString("httpAttemptGap", String.valueOf(default_httpAttemptGap));
if(!prefs.contains("lastActivePoi") | force) if(!prefs.contains("lastActivePoi") || force)
editor.putString("lastActivePoi", "null"); editor.putString("lastActivePoi", "null");
if(!prefs.contains("rememberLastActivePoi") | force) if(!prefs.contains("rememberLastActivePoi") || force)
editor.putBoolean("rememberLastActivePoi", default_rememberLastActivePoi); editor.putBoolean("rememberLastActivePoi", default_rememberLastActivePoi);
if(!prefs.contains("locationRingBufferSize") | force) if(!prefs.contains("locationRingBufferSize") || force)
editor.putString("locationRingBufferSize", String.valueOf(default_locationRingBufferSize)); editor.putString("locationRingBufferSize", String.valueOf(default_locationRingBufferSize));
if(!prefs.contains("timeBetweenProcessMonitorings") | force) if(!prefs.contains("timeBetweenProcessMonitorings") || force)
editor.putString("timeBetweenProcessMonitorings", String.valueOf(default_timeBetweenProcessMonitorings)); editor.putString("timeBetweenProcessMonitorings", String.valueOf(default_timeBetweenProcessMonitorings));
if(!prefs.contains("acceptDevicePositionSignalEveryX_MilliSeconds") | force) if(!prefs.contains("acceptDevicePositionSignalEveryX_MilliSeconds") || force)
editor.putString("acceptDevicePositionSignalEveryX_MilliSeconds", String.valueOf(default_acceptDevicePositionSignalEveryX_MilliSeconds)); editor.putString("acceptDevicePositionSignalEveryX_MilliSeconds", String.valueOf(default_acceptDevicePositionSignalEveryX_MilliSeconds));
if(!prefs.contains("activityDetectionFrequency") | force) if(!prefs.contains("activityDetectionFrequency") || force)
editor.putString("activityDetectionFrequency", String.valueOf(default_activityDetectionFrequency)); editor.putString("activityDetectionFrequency", String.valueOf(default_activityDetectionFrequency));
if(!prefs.contains("activityDetectionRequiredProbability") | force) if(!prefs.contains("activityDetectionRequiredProbability") || force)
editor.putString("activityDetectionRequiredProbability", String.valueOf(default_activityDetectionRequiredProbability)); editor.putString("activityDetectionRequiredProbability", String.valueOf(default_activityDetectionRequiredProbability));
if(!prefs.contains("privacyLocationing") | force) if(!prefs.contains("privacyLocationing") || force)
editor.putBoolean("privacyLocationing", default_privacyLocationing); editor.putBoolean("privacyLocationing", default_privacyLocationing);
if(!prefs.contains("startScreen") | force) if(!prefs.contains("startScreen") || force)
editor.putString("startScreen", String.valueOf(default_startScreen)); editor.putString("startScreen", String.valueOf(default_startScreen));
if(!prefs.contains("tabsPlacement") | force) if(!prefs.contains("tabsPlacement") || force)
editor.putString("tabsPlacement", String.valueOf(default_tabsPlacement)); editor.putString("tabsPlacement", String.valueOf(default_tabsPlacement));
if(!prefs.contains("executeRulesAndProfilesWithSingleClick") | force) if(!prefs.contains("executeRulesAndProfilesWithSingleClick") || force)
editor.putBoolean("executeRulesAndProfilesWithSingleClick", default_executeRulesAndProfilesWithSingleClick); editor.putBoolean("executeRulesAndProfilesWithSingleClick", default_executeRulesAndProfilesWithSingleClick);
if(!prefs.contains("automaticUpdateCheck") | force) if(!prefs.contains("automaticUpdateCheck") || force)
editor.putBoolean("automaticUpdateCheck", default_automaticUpdateCheck); editor.putBoolean("automaticUpdateCheck", default_automaticUpdateCheck);
if(!prefs.contains("displayNewsOnMainScreen") | force) if(!prefs.contains("displayNewsOnMainScreen") || force)
editor.putBoolean("displayNewsOnMainScreen", default_displayNewsOnMainScreen); editor.putBoolean("displayNewsOnMainScreen", default_displayNewsOnMainScreen);
if(!prefs.contains("lockSoundChanges") | force) if(!prefs.contains("musicCheckFrequency") || force)
editor.putLong("musicCheckFrequency", default_musicCheckFrequency);
if(!prefs.contains("lockSoundChanges") || force)
editor.putBoolean("lockSoundChanges", default_lockSoundChanges); editor.putBoolean("lockSoundChanges", default_lockSoundChanges);
if(!prefs.contains("noticeAndroid9MicrophoneShown") | force) if(!prefs.contains("noticeAndroid9MicrophoneShown") || force)
editor.putBoolean("noticeAndroid9MicrophoneShown", false); editor.putBoolean("noticeAndroid9MicrophoneShown", false);
if(!prefs.contains("lastNewsPolltime") | force) if(!prefs.contains("lastNewsPolltime") || force)
editor.putLong("lastNewsPolltime", default_lastNewsPolltime); editor.putLong("lastNewsPolltime", default_lastNewsPolltime);
if(!prefs.contains("lastUpdateCheck") | force) if(!prefs.contains("lastUpdateCheck") || force)
editor.putLong("lastUpdateCheck", default_lastUpdateCheck); editor.putLong("lastUpdateCheck", default_lastUpdateCheck);
if(!prefs.contains("whatHasBeenDone") | force) if(!prefs.contains("whatHasBeenDone") || force)
editor.putString("whatHasBeenDone", ""); editor.putString("whatHasBeenDone", "");
editor.commit(); editor.commit();
@ -551,6 +557,8 @@ public class Settings implements SharedPreferences
editor.putLong("lastNewsPolltime", lastNewsPolltime); editor.putLong("lastNewsPolltime", lastNewsPolltime);
editor.putLong("lastUpdateCheck", lastUpdateCheck); editor.putLong("lastUpdateCheck", lastUpdateCheck);
editor.putLong("musicCheckFrequency", musicCheckFrequency);
editor.putString("whatHasBeenDone", Miscellaneous.explode(";", whatHasBeenDone)); editor.putString("whatHasBeenDone", Miscellaneous.explode(";", whatHasBeenDone));
if(lastActivePoi == null) if(lastActivePoi == null)

View File

@ -52,7 +52,7 @@ public class MediaPlayerListener implements AutomationListenerInterface
} }
else else
{ {
timer.cancel(); task.cancel();
timer.purge(); timer.purge();
} }
@ -61,16 +61,19 @@ public class MediaPlayerListener implements AutomationListenerInterface
@Override @Override
public void run() public void run()
{ {
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.musicPlaying); synchronized(this)
for(int i=0; i<ruleCandidates.size(); i++)
{ {
if(ruleCandidates.get(i).getsGreenLight(AutomationService.getInstance())) ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.musicPlaying);
for (int i = 0; i < ruleCandidates.size(); i++)
{
if (ruleCandidates.get(i).getsGreenLight(AutomationService.getInstance()))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false); ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
} }
} }
}
}; };
timer.scheduleAtFixedRate(task, 0, Settings.musicCheckFrequency * 1000); timer.scheduleAtFixedRate(task, 0, Settings.musicCheckFrequency);
} }
} }

View File

@ -293,5 +293,17 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory
android:summary="@string/musicPlayingDetection"
android:title="@string/musicPlayingDetection">
<EditTextPreference
android:key="musicCheckFrequency"
android:summary="@string/musicCheckFrequencySummary"
android:title="@string/musicCheckFrequencyTitle"
android:inputType="number"></EditTextPreference>
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -790,4 +790,7 @@
<string name="selectParameters">Select parameters</string> <string name="selectParameters">Select parameters</string>
<string name="musicIsPlaying">music is playing</string> <string name="musicIsPlaying">music is playing</string>
<string name="musicIsNotPlaying">music is not playing</string> <string name="musicIsNotPlaying">music is not playing</string>
<string name="musicCheckFrequencyTitle">Frequency to check if music is playing [milliseconds]</string>
<string name="musicCheckFrequencySummary">Milliseconds between checks</string>
<string name="musicPlayingDetection">Music playing detection</string>
</resources> </resources>