forked from jens/Automation
tethering detection
parent
b35208b7aa
commit
bb1b3b0149
@ -0,0 +1,46 @@
|
||||
package com.jens.automation2;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class ActivityManageTriggerTethering extends Activity
|
||||
{
|
||||
RadioButton rbTetheringOn, rTetheringOff;
|
||||
Button bTriggerTetheringSave;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_manage_trigger_tethering);
|
||||
|
||||
rbTetheringOn = (RadioButton) findViewById(R.id.rbTetheringOn);
|
||||
rTetheringOff = (RadioButton)findViewById(R.id.rTetheringOff);
|
||||
bTriggerTetheringSave = (Button) findViewById(R.id.bTriggerTetheringSave);
|
||||
|
||||
Intent input = getIntent();
|
||||
if(input.hasExtra(ActivityManageRule.intentNameTriggerParameter1))
|
||||
rbTetheringOn.setChecked(input.getBooleanExtra(ActivityManageRule.intentNameTriggerParameter1, true));
|
||||
|
||||
bTriggerTetheringSave.setOnClickListener(new View.OnClickListener()
|
||||
{
|
||||
@Override
|
||||
public void onClick(View view)
|
||||
{
|
||||
Intent response = new Intent();
|
||||
response.putExtra(ActivityManageRule.intentNameTriggerParameter1, rbTetheringOn.isChecked());
|
||||
setResult(RESULT_OK, response);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
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;
|
||||
|
||||
import com.jens.automation2.AutomationService;
|
||||
import com.jens.automation2.Miscellaneous;
|
||||
import com.jens.automation2.Rule;
|
||||
import com.jens.automation2.Trigger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
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;
|
||||
|
||||
private static boolean tetheringActive = false;
|
||||
|
||||
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)
|
||||
tetheringActive = true;
|
||||
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};
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:layout_margin="@dimen/default_margin" >
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_span="2"
|
||||
android:textSize="25dp"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginBottom="@dimen/default_margin"
|
||||
android:text="@string/tetheringState"/>
|
||||
|
||||
<TableLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/default_margin"
|
||||
android:stretchColumns="1"
|
||||
android:shrinkColumns="1" >
|
||||
|
||||
<TableRow
|
||||
android:layout_marginTop="@dimen/default_margin">
|
||||
|
||||
<TextView
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="@dimen/default_margin"
|
||||
android:text="@string/state" />
|
||||
|
||||
<RadioGroup
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbTetheringOn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
android:text="@string/on" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rTetheringOff"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/off" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
</TableRow>
|
||||
|
||||
</TableLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/bTriggerTetheringSave"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/default_margin"
|
||||
android:text="@string/save" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
Loading…
Reference in New Issue