media playback listener

This commit is contained in:
jens 2022-01-29 02:12:55 +01:00
parent 5ed024774e
commit 9387e8bdb2
6 changed files with 141 additions and 8 deletions

View File

@ -525,6 +525,8 @@ public class ActivityManageRule extends Activity
items.add(new Item(typesLong[i].toString(), R.drawable.smartphone)); items.add(new Item(typesLong[i].toString(), R.drawable.smartphone));
else if(types[i].toString().equals(Trigger_Enum.profileActive.toString())) else if(types[i].toString().equals(Trigger_Enum.profileActive.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.sound)); items.add(new Item(typesLong[i].toString(), R.drawable.sound));
else if(types[i].toString().equals(Trigger_Enum.musicPlaying.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.sound));
else if(types[i].toString().equals(Trigger_Enum.screenState.toString())) else if(types[i].toString().equals(Trigger_Enum.screenState.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.smartphone)); items.add(new Item(typesLong[i].toString(), R.drawable.smartphone));
else else
@ -587,7 +589,7 @@ public class ActivityManageRule extends Activity
startActivityForResult(timeFrameEditor, requestCodeTriggerTimeframeAdd); startActivityForResult(timeFrameEditor, requestCodeTriggerTimeframeAdd);
return; return;
} }
else if(triggerType == Trigger_Enum.charging) else if(triggerType == Trigger_Enum.charging || triggerType == Trigger_Enum.musicPlaying)
booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)}; booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)};
else if(triggerType == Trigger_Enum.usb_host_connection) else if(triggerType == Trigger_Enum.usb_host_connection)
booleanChoices = new String[]{getResources().getString(R.string.connected), getResources().getString(R.string.disconnected)}; booleanChoices = new String[]{getResources().getString(R.string.connected), getResources().getString(R.string.disconnected)};
@ -718,7 +720,7 @@ public class ActivityManageRule extends Activity
private AlertDialog getTriggerParameterDialog(final Context myContext, final String[] choices) private AlertDialog getTriggerParameterDialog(final Context myContext, final String[] choices)
{ {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle(getResources().getString(R.string.selectTypeOfTrigger)); alertDialogBuilder.setTitle(getResources().getString(R.string.selectParameters));
alertDialogBuilder.setItems(choices, new DialogInterface.OnClickListener() alertDialogBuilder.setItems(choices, new DialogInterface.OnClickListener()
{ {
@Override @Override

View File

@ -12,6 +12,7 @@ import com.jens.automation2.receivers.BluetoothReceiver;
import com.jens.automation2.receivers.ConnectivityReceiver; import com.jens.automation2.receivers.ConnectivityReceiver;
import com.jens.automation2.receivers.DeviceOrientationListener; import com.jens.automation2.receivers.DeviceOrientationListener;
import com.jens.automation2.receivers.HeadphoneJackListener; import com.jens.automation2.receivers.HeadphoneJackListener;
import com.jens.automation2.receivers.MediaPlayerListener;
import com.jens.automation2.receivers.NoiseListener; import com.jens.automation2.receivers.NoiseListener;
import com.jens.automation2.receivers.PhoneStatusListener; import com.jens.automation2.receivers.PhoneStatusListener;
import com.jens.automation2.receivers.ProcessListener; import com.jens.automation2.receivers.ProcessListener;
@ -55,6 +56,7 @@ public class ReceiverCoordinator
//NotificationListener.class, //NotificationListener.class,
PhoneStatusListener.class, PhoneStatusListener.class,
ProcessListener.class, ProcessListener.class,
MediaPlayerListener.class,
ScreenStateReceiver.class, ScreenStateReceiver.class,
TimeZoneListener.class TimeZoneListener.class
}; };
@ -186,6 +188,9 @@ public class ReceiverCoordinator
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.headsetPlugged)) if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.headsetPlugged))
HeadphoneJackListener.getInstance().startListener(AutomationService.getInstance()); HeadphoneJackListener.getInstance().startListener(AutomationService.getInstance());
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.musicPlaying))
MediaPlayerListener.getInstance().startListener(AutomationService.getInstance());
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.screenState)) if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.screenState))
ScreenStateReceiver.startScreenStateReceiver(AutomationService.getInstance()); ScreenStateReceiver.startScreenStateReceiver(AutomationService.getInstance());
} }
@ -202,6 +207,7 @@ public class ReceiverCoordinator
DateTimeListener.stopAlarmListener(AutomationService.getInstance()); DateTimeListener.stopAlarmListener(AutomationService.getInstance());
NoiseListener.stopNoiseListener(); NoiseListener.stopNoiseListener();
ProcessListener.stopProcessListener(AutomationService.getInstance()); ProcessListener.stopProcessListener(AutomationService.getInstance());
MediaPlayerListener.getInstance().stopListener(AutomationService.getInstance());
DeviceOrientationListener.getInstance().stopListener(AutomationService.getInstance()); DeviceOrientationListener.getInstance().stopListener(AutomationService.getInstance());
try try
@ -283,6 +289,17 @@ public class ReceiverCoordinator
ScreenStateReceiver.stopScreenStateReceiver(); ScreenStateReceiver.stopScreenStateReceiver();
} }
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.musicPlaying))
{
Miscellaneous.logEvent("i", "LocationProvider", "Starting MediaPlayerListener because used in a new/changed rule.", 4);
MediaPlayerListener.getInstance().startListener(AutomationService.getInstance());
}
else
{
Miscellaneous.logEvent("i", "LocationProvider", "Shutting down MediaPlayerListener because not used in any rule.", 4);
MediaPlayerListener.getInstance().stopListener(AutomationService.getInstance());
}
if(!BuildConfig.FLAVOR.equalsIgnoreCase("fdroidFlavor")) if(!BuildConfig.FLAVOR.equalsIgnoreCase("fdroidFlavor"))
{ {
if (Rule.isAnyRuleUsing(Trigger.Trigger_Enum.activityDetection)) if (Rule.isAnyRuleUsing(Trigger.Trigger_Enum.activityDetection))

View File

@ -66,6 +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 boolean lockSoundChanges; public static boolean lockSoundChanges;
public static boolean noticeAndroid9MicrophoneShown; public static boolean noticeAndroid9MicrophoneShown;

View File

@ -17,6 +17,7 @@ import com.jens.automation2.receivers.BluetoothReceiver;
import com.jens.automation2.receivers.ConnectivityReceiver; import com.jens.automation2.receivers.ConnectivityReceiver;
import com.jens.automation2.receivers.DeviceOrientationListener; import com.jens.automation2.receivers.DeviceOrientationListener;
import com.jens.automation2.receivers.HeadphoneJackListener; import com.jens.automation2.receivers.HeadphoneJackListener;
import com.jens.automation2.receivers.MediaPlayerListener;
import com.jens.automation2.receivers.NfcReceiver; import com.jens.automation2.receivers.NfcReceiver;
import com.jens.automation2.receivers.NoiseListener; import com.jens.automation2.receivers.NoiseListener;
import com.jens.automation2.receivers.NotificationListener; import com.jens.automation2.receivers.NotificationListener;
@ -38,7 +39,7 @@ import java.util.Date;
public class Trigger public class Trigger
{ {
public enum Trigger_Enum { public enum Trigger_Enum {
pointOfInterest, timeFrame, charging, batteryLevel, usb_host_connection, speed, noiseLevel, wifiConnection, process_started_stopped, airplaneMode, roaming, nfcTag, activityDetection, bluetoothConnection, headsetPlugged, notification, deviceOrientation, profileActive, screenState, phoneCall; //phoneCall always needs to be at the very end because of Google's shitty so called privacy pointOfInterest, timeFrame, charging, batteryLevel, usb_host_connection, speed, noiseLevel, wifiConnection, process_started_stopped, airplaneMode, roaming, nfcTag, activityDetection, bluetoothConnection, headsetPlugged, notification, deviceOrientation, profileActive, screenState, musicPlaying, phoneCall; //phoneCall always needs to be at the very end because of Google's shitty so called privacy
public String getFullName(Context context) public String getFullName(Context context)
{ {
@ -82,13 +83,14 @@ public class Trigger
return context.getResources().getString(R.string.deviceOrientation); return context.getResources().getString(R.string.deviceOrientation);
case profileActive: case profileActive:
return context.getResources().getString(R.string.profile); return context.getResources().getString(R.string.profile);
case musicPlaying:
return context.getResources().getString(R.string.musicPlaying);
case screenState: case screenState:
return context.getResources().getString(R.string.screenState); return context.getResources().getString(R.string.screenState);
default: default:
return "Unknown"; return "Unknown";
} }
} }
}; };
Rule parentRule = null; Rule parentRule = null;
@ -180,6 +182,10 @@ public class Trigger
if(!checkProfileActive()) if(!checkProfileActive())
result = false; result = false;
break; break;
case musicPlaying:
if(!checkMusicPlaying())
result = false;
break;
case screenState: case screenState:
if(!checkScreenState()) if(!checkScreenState())
result = false; result = false;
@ -332,6 +338,11 @@ public class Trigger
return true; return true;
} }
boolean checkMusicPlaying()
{
return triggerParameter == MediaPlayerListener.isAudioPlaying(Miscellaneous.getAnyContext());
}
boolean checkProfileActive() boolean checkProfileActive()
{ {
String demandedProfileName = getTriggerParameter2().split(Trigger.triggerParameter2Split)[0]; String demandedProfileName = getTriggerParameter2().split(Trigger.triggerParameter2Split)[0];
@ -1513,6 +1524,12 @@ public class Trigger
else else
returnString.append(String.format(Miscellaneous.getAnyContext().getString(R.string.profileNotActive), getTriggerParameter2().split(Trigger.triggerParameter2Split)[0])); returnString.append(String.format(Miscellaneous.getAnyContext().getString(R.string.profileNotActive), getTriggerParameter2().split(Trigger.triggerParameter2Split)[0]));
break; break;
case musicPlaying:
if(triggerParameter)
returnString.append(Miscellaneous.getAnyContext().getString(R.string.musicIsPlaying));
else
returnString.append(Miscellaneous.getAnyContext().getString(R.string.musicIsNotPlaying));
break;
case screenState: case screenState:
String state; String state;
switch(triggerParameter2) switch(triggerParameter2)

View File

@ -1,11 +1,103 @@
package com.jens.automation2.receivers; package com.jens.automation2.receivers;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
public class MediaPlayerListener import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous;
import com.jens.automation2.Rule;
import com.jens.automation2.Settings;
import com.jens.automation2.Trigger;
import java.util.ArrayList;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
public class MediaPlayerListener implements AutomationListenerInterface
{ {
void test() static MediaPlayerListener instance = null;
static AudioManager mAudioManager = null;
static boolean listenerActive = false;
Timer timer = null;
TimerTask task = null;
public static boolean isAudioPlaying(Context context)
{ {
MediaPlayer.OnInfoListener if(mAudioManager == null)
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
return mAudioManager.isMusicActive();
} }
}
public static MediaPlayerListener getInstance()
{
if(instance == null)
instance = new MediaPlayerListener();
return instance;
}
@Override
public void startListener(AutomationService automationService)
{
Miscellaneous.logEvent("i", "MediaPlayerListener", "Starting listener.",5);
if(!listenerActive)
{
if(timer == null)
{
timer = new Timer();
}
else
{
timer.cancel();
timer.purge();
}
task = new TimerTask()
{
@Override
public void run()
{
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);
}
}
};
timer.scheduleAtFixedRate(task, 0, Settings.musicCheckFrequency * 1000);
}
}
@Override
public void stopListener(AutomationService automationService)
{
Miscellaneous.logEvent("i", "MediaPlayerListener", "Stopping listener.",5);
if(listenerActive)
{
if (timer != null)
{
timer.cancel();
timer.purge();
}
}
}
@Override
public boolean isListenerRunning()
{
return listenerActive;
}
@Override
public Trigger.Trigger_Enum[] getMonitoredTrigger()
{
return new Trigger.Trigger_Enum[] { Trigger.Trigger_Enum.musicPlaying };
}
}

View File

@ -786,4 +786,8 @@
<string name="stop">stop</string> <string name="stop">stop</string>
<string name="pleaseSelectActionValue">Please select an action!</string> <string name="pleaseSelectActionValue">Please select an action!</string>
<string name="actionMediaControlNotice">Keep in mind that this action may not work with ALL players out there. And even if it does, not every buttons does necessarily work.</string> <string name="actionMediaControlNotice">Keep in mind that this action may not work with ALL players out there. And even if it does, not every buttons does necessarily work.</string>
<string name="musicPlaying">Music playing</string>
<string name="selectParameters">Select parameters</string>
<string name="musicIsPlaying">music is playing</string>
<string name="musicIsNotPlaying">music is not playing</string>
</resources> </resources>