2022-06-04 02:44:26 +02:00
|
|
|
package com.jens.automation2.receivers;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2022-08-18 23:32:57 +02:00
|
|
|
import com.jens.automation2.ActivityManageTriggerTethering;
|
2022-06-04 02:44:26 +02:00
|
|
|
import com.jens.automation2.AutomationService;
|
|
|
|
import com.jens.automation2.Miscellaneous;
|
|
|
|
import com.jens.automation2.Rule;
|
|
|
|
import com.jens.automation2.Trigger;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2022-08-17 23:36:30 +02:00
|
|
|
import java.util.List;
|
2022-06-04 02:44:26 +02:00
|
|
|
|
|
|
|
public class TetheringReceiver extends android.content.BroadcastReceiver implements AutomationListenerInterface
|
|
|
|
{
|
|
|
|
public static AutomationService automationServiceRef = null;
|
|
|
|
private static boolean receiverActive = false;
|
|
|
|
private static TetheringReceiver receiverInstance = null;
|
|
|
|
private static IntentFilter intentFilter = null;
|
|
|
|
|
2022-08-17 23:36:30 +02:00
|
|
|
private static List<String> lastTetheringTypes = null;
|
2022-06-04 02:44:26 +02:00
|
|
|
private static boolean tetheringActive = false;
|
|
|
|
|
2022-08-17 23:36:30 +02:00
|
|
|
public static List<String> getLastTetheringTypes()
|
|
|
|
{
|
|
|
|
return lastTetheringTypes;
|
|
|
|
}
|
|
|
|
|
2022-06-04 02:44:26 +02:00
|
|
|
public static TetheringReceiver getInstance()
|
|
|
|
{
|
|
|
|
if(receiverInstance == null)
|
|
|
|
receiverInstance = new TetheringReceiver();
|
|
|
|
|
|
|
|
return receiverInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isTetheringActive()
|
|
|
|
{
|
|
|
|
return tetheringActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
{
|
|
|
|
for(String key : intent.getExtras().keySet())
|
|
|
|
{
|
|
|
|
// Miscellaneous.logEvent("i", "Broadcast extra", "Broadcast " + intent.getAction() + " has extra " + key + " and type " + intent.getExtras().get(key).getClass().getName(), 4);
|
|
|
|
Object ob = intent.getExtras().get(key);
|
|
|
|
|
|
|
|
String target = null;
|
|
|
|
|
|
|
|
if(Build.VERSION.SDK_INT >= 26)
|
|
|
|
target = "tetherArray";
|
|
|
|
else
|
|
|
|
target = "activeArray";
|
|
|
|
|
|
|
|
if(key.equals(target) && ob instanceof ArrayList)
|
|
|
|
{
|
|
|
|
if(((ArrayList<String>)ob).size() > 0)
|
2022-08-17 23:36:30 +02:00
|
|
|
{
|
2022-06-04 02:44:26 +02:00
|
|
|
tetheringActive = true;
|
2022-08-18 23:32:57 +02:00
|
|
|
if(lastTetheringTypes == null)
|
|
|
|
lastTetheringTypes = new ArrayList<>();
|
|
|
|
else
|
|
|
|
lastTetheringTypes.clear();
|
2022-08-17 23:36:30 +02:00
|
|
|
|
|
|
|
for(String adapterName : (ArrayList<String>)ob)
|
|
|
|
{
|
2022-08-18 23:32:57 +02:00
|
|
|
if(adapterName.contains("wlan"))
|
|
|
|
lastTetheringTypes.add(ActivityManageTriggerTethering.tetheringTypeWifi);
|
|
|
|
else if(adapterName.contains("bluetooth"))
|
|
|
|
lastTetheringTypes.add(ActivityManageTriggerTethering.tetheringTypeBluetooth);
|
|
|
|
else if(adapterName.contains("usb"))
|
|
|
|
lastTetheringTypes.add(ActivityManageTriggerTethering.tetheringTypeUsb);
|
|
|
|
else if(adapterName.contains("ndis"))
|
|
|
|
lastTetheringTypes.add(ActivityManageTriggerTethering.tetheringTypeCable);
|
2022-08-17 23:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 02:44:26 +02:00
|
|
|
else
|
|
|
|
tetheringActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Miscellaneous.logEvent("i", "Broadcast extra", "Broadcast " + intent.getAction() + " has extra " + key + " and type " + intent.getExtras().get(key).getClass().getName(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.tethering);
|
|
|
|
for(int i=0; i<ruleCandidates.size(); i++)
|
|
|
|
{
|
|
|
|
if(ruleCandidates.get(i).getsGreenLight(context))
|
|
|
|
ruleCandidates.get(i).activate(automationServiceRef, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startListener(AutomationService automationService)
|
|
|
|
{
|
|
|
|
if(!receiverActive)
|
|
|
|
{
|
|
|
|
TetheringReceiver.automationServiceRef = automationService;
|
|
|
|
|
|
|
|
if(receiverInstance == null)
|
|
|
|
receiverInstance = new TetheringReceiver();
|
|
|
|
|
|
|
|
if(intentFilter == null)
|
|
|
|
{
|
|
|
|
intentFilter = new IntentFilter();
|
|
|
|
intentFilter.addAction("android.net.conn.TETHER_STATE_CHANGED");
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
automationServiceRef.registerReceiver(receiverInstance, intentFilter);
|
|
|
|
receiverActive = true;
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We might be confronted with permission issues here.
|
|
|
|
*/
|
|
|
|
Miscellaneous.logEvent("e", "TetheringReceiver", Log.getStackTraceString(e), 1);
|
|
|
|
receiverActive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void stopListener(AutomationService automationService)
|
|
|
|
{
|
|
|
|
if(receiverActive)
|
|
|
|
{
|
|
|
|
if(receiverInstance != null)
|
|
|
|
{
|
|
|
|
automationServiceRef.unregisterReceiver(receiverInstance);
|
|
|
|
receiverInstance = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiverActive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isListenerRunning()
|
|
|
|
{
|
|
|
|
return receiverActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Trigger.Trigger_Enum[] getMonitoredTrigger()
|
|
|
|
{
|
|
|
|
return new Trigger.Trigger_Enum[] { Trigger.Trigger_Enum.tethering};
|
|
|
|
}
|
|
|
|
}
|