service is starting can be inverted
parent
d17e8b70fe
commit
7fdbf74906
@ -0,0 +1,72 @@
|
||||
package com.jens.automation2;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.RadioButton;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.jens.automation2.Trigger.subSystemStates;
|
||||
|
||||
public class ActivityManageTriggerSubSystemState extends Activity
|
||||
{
|
||||
RadioButton rbSubSystemStateWifi, rbSubSystemStateBluetooth;
|
||||
RadioButton rbSubSystemStateEnabled, rbSubSystemStateDisabled;
|
||||
Button bSubSystemStateSave;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_manage_trigger_subsystemstate);
|
||||
|
||||
rbSubSystemStateWifi = (RadioButton)findViewById(R.id.rbSubSystemStateWifi);
|
||||
rbSubSystemStateBluetooth = (RadioButton)findViewById(R.id.rbSubSystemStateBluetooth);
|
||||
rbSubSystemStateEnabled = (RadioButton)findViewById(R.id.rbSubSystemStateEnabled);
|
||||
rbSubSystemStateDisabled = (RadioButton)findViewById(R.id.rbSubSystemStateDisabled);
|
||||
bSubSystemStateSave = (Button)findViewById(R.id.bSubSystemStateSave);
|
||||
|
||||
if(getIntent().hasExtra(ActivityManageRule.intentNameTriggerParameter1) && getIntent().hasExtra(ActivityManageRule.intentNameTriggerParameter2))
|
||||
{
|
||||
subSystemStates desiredState = subSystemStates.valueOf(getIntent().getStringExtra(ActivityManageRule.intentNameTriggerParameter2));
|
||||
|
||||
switch(desiredState)
|
||||
{
|
||||
case wifi:
|
||||
rbSubSystemStateWifi.setChecked(true);
|
||||
break;
|
||||
case bluetooth:
|
||||
rbSubSystemStateBluetooth.setChecked(true);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
if(getIntent().getBooleanExtra(ActivityManageRule.intentNameTriggerParameter1, true))
|
||||
rbSubSystemStateEnabled.setChecked(true);
|
||||
else
|
||||
rbSubSystemStateDisabled.setChecked(true);
|
||||
}
|
||||
|
||||
bSubSystemStateSave.setOnClickListener(new View.OnClickListener()
|
||||
{
|
||||
@Override
|
||||
public void onClick(View view)
|
||||
{
|
||||
Intent data = new Intent();
|
||||
|
||||
data.putExtra(ActivityManageRule.intentNameTriggerParameter1, rbSubSystemStateEnabled.isChecked());
|
||||
|
||||
if(rbSubSystemStateWifi.isChecked())
|
||||
data.putExtra(ActivityManageRule.intentNameTriggerParameter2, subSystemStates.wifi.name());
|
||||
else if(rbSubSystemStateBluetooth.isChecked())
|
||||
data.putExtra(ActivityManageRule.intentNameTriggerParameter2, subSystemStates.bluetooth.name());
|
||||
|
||||
ActivityManageTriggerSubSystemState.this.setResult(RESULT_OK, data);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
package com.jens.automation2.receivers;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import com.jens.automation2.AutomationService;
|
||||
import com.jens.automation2.Miscellaneous;
|
||||
import com.jens.automation2.Rule;
|
||||
import com.jens.automation2.Trigger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SubSystemStateReceiver extends BroadcastReceiver implements AutomationListenerInterface
|
||||
{
|
||||
public static AutomationService automationServiceRef = null;
|
||||
private static IntentFilter subSystemStateIntentFilter = null;
|
||||
private static BroadcastReceiver subSystemStateReceiverInstance = null;
|
||||
private static Intent subSystemStatusIntent = null;
|
||||
private static boolean subSystemStateReceiverActive = false;
|
||||
static SubSystemStateReceiver instance;
|
||||
|
||||
final static String stateBluetooth = "android.bluetooth.adapter.action.STATE_CHANGED";
|
||||
final static String stateWifi = "android.net.wifi.STATE_CHANGE";
|
||||
final static String connectivityBroadcast = "android.net.conn.CONNECTIVITY_CHANGE";
|
||||
|
||||
static Map<String, Boolean> stateMap = null;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
if (intent == null)
|
||||
return;
|
||||
if (context == null)
|
||||
return;
|
||||
|
||||
Miscellaneous.logEvent("e", "ScreenStateReceiver", "Received: " + intent.getAction(), 3);
|
||||
|
||||
if(stateMap == null)
|
||||
stateMap = new HashMap<>();
|
||||
|
||||
try
|
||||
{
|
||||
/*if (intent.getAction().equals(stateWifi) || intent.getAction().equals(connectivityBroadcast))
|
||||
{
|
||||
if(intent.hasExtra(WifiManager.EXTRA_WIFI_STATE))
|
||||
{
|
||||
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
|
||||
|
||||
if (wifiState == WifiManager.WIFI_STATE_ENABLED)
|
||||
stateMap.put("wifi", true);
|
||||
else if (wifiState == WifiManager.WIFI_STATE_DISABLED)
|
||||
stateMap.put("wifi", false);
|
||||
}
|
||||
}
|
||||
else if (intent.getAction().equals(stateBluetooth))
|
||||
{
|
||||
if(intent.hasExtra(BluetoothAdapter.EXTRA_STATE))
|
||||
{
|
||||
int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
|
||||
|
||||
if (bluetoothState == BluetoothAdapter.STATE_ON)
|
||||
stateMap.put("bluetooth", true);
|
||||
else if (bluetoothState == BluetoothAdapter.STATE_OFF)
|
||||
stateMap.put("bluetooth", false);
|
||||
}
|
||||
}*/
|
||||
if (intent.getAction().equals(stateWifi) || intent.getAction().equals(connectivityBroadcast) || intent.getAction().equals(stateBluetooth))
|
||||
{
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.subSystemState);
|
||||
for (int i = 0; i < ruleCandidates.size(); i++)
|
||||
{
|
||||
if (ruleCandidates.get(i).getsGreenLight(context))
|
||||
ruleCandidates.get(i).activate(automationServiceRef, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Miscellaneous.logEvent("e", "SubSystemStateReceiver", "Unknown state received: " + intent.getAction(), 3);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("e", "SubSystemStateReceiver", "Error receiving screen state: " + e.getMessage(), 3);
|
||||
}
|
||||
}
|
||||
|
||||
public static SubSystemStateReceiver getInstance()
|
||||
{
|
||||
if(instance == null)
|
||||
instance = new SubSystemStateReceiver();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startListener(AutomationService automationService)
|
||||
{
|
||||
if (!subSystemStateReceiverActive)
|
||||
{
|
||||
automationServiceRef = automationService;
|
||||
|
||||
if (subSystemStateReceiverInstance == null)
|
||||
subSystemStateReceiverInstance = new SubSystemStateReceiver();
|
||||
|
||||
if (subSystemStateIntentFilter == null)
|
||||
{
|
||||
subSystemStateIntentFilter = new IntentFilter();
|
||||
subSystemStateIntentFilter.addAction(stateWifi);
|
||||
subSystemStateIntentFilter.addAction(connectivityBroadcast);
|
||||
subSystemStateIntentFilter.addAction(stateBluetooth);
|
||||
}
|
||||
|
||||
subSystemStatusIntent = automationServiceRef.registerReceiver(subSystemStateReceiverInstance, subSystemStateIntentFilter);
|
||||
|
||||
subSystemStateReceiverActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopListener(AutomationService automationService)
|
||||
{
|
||||
if (subSystemStateReceiverActive)
|
||||
{
|
||||
if (subSystemStateReceiverInstance != null)
|
||||
{
|
||||
automationServiceRef.unregisterReceiver(subSystemStateReceiverInstance);
|
||||
subSystemStateReceiverInstance = null;
|
||||
}
|
||||
|
||||
subSystemStateReceiverActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isListenerRunning()
|
||||
{
|
||||
return subSystemStateReceiverActive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Trigger.Trigger_Enum[] getMonitoredTrigger()
|
||||
{
|
||||
return new Trigger.Trigger_Enum[]{Trigger.Trigger_Enum.subSystemState};
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
@ -0,0 +1,75 @@
|
||||
<?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: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/subSystemState" />
|
||||
|
||||
<RadioGroup
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbSubSystemStateWifi"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
android:text="@string/wifi" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbSubSystemStateBluetooth"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/bluetooth" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_span="2"
|
||||
android:layout_height="1dp"
|
||||
android:layout_margin="10dp"
|
||||
android:background="#aa000000" />
|
||||
|
||||
<RadioGroup
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbSubSystemStateEnabled"
|
||||
android:checked="true"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/activated" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbSubSystemStateDisabled"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/deactivated" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
<Button
|
||||
android:id="@+id/bSubSystemStateSave"
|
||||
android:layout_marginTop="@dimen/default_margin"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/save" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
Loading…
Reference in New Issue