Proximity sensor trigger
This commit is contained in:
@@ -245,6 +245,7 @@
|
|||||||
<activity android:name=".ActivityPermissions"></activity>
|
<activity android:name=".ActivityPermissions"></activity>
|
||||||
<activity android:name=".ActivityManageTriggerNotification" />
|
<activity android:name=".ActivityManageTriggerNotification" />
|
||||||
<activity android:name=".ActivityManageTriggerCalendar" />
|
<activity android:name=".ActivityManageTriggerCalendar" />
|
||||||
|
<activity android:name=".ActivityManageTriggerProximity" />
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".receivers.NotificationListener"
|
android:name=".receivers.NotificationListener"
|
||||||
|
|||||||
@@ -243,6 +243,7 @@
|
|||||||
<activity android:name=".ActivityPermissions"></activity>
|
<activity android:name=".ActivityPermissions"></activity>
|
||||||
<activity android:name=".ActivityManageTriggerNotification" />
|
<activity android:name=".ActivityManageTriggerNotification" />
|
||||||
<activity android:name=".ActivityManageTriggerCalendar" />
|
<activity android:name=".ActivityManageTriggerCalendar" />
|
||||||
|
<activity android:name=".ActivityManageTriggerProximity" />
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".receivers.NotificationListener"
|
android:name=".receivers.NotificationListener"
|
||||||
|
|||||||
@@ -231,6 +231,7 @@
|
|||||||
<activity android:name=".ActivityPermissions"></activity>
|
<activity android:name=".ActivityPermissions"></activity>
|
||||||
<activity android:name=".ActivityManageTriggerNotification" />
|
<activity android:name=".ActivityManageTriggerNotification" />
|
||||||
<activity android:name=".ActivityManageTriggerCalendar" />
|
<activity android:name=".ActivityManageTriggerCalendar" />
|
||||||
|
<activity android:name=".ActivityManageTriggerProximity" />
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".receivers.NotificationListener"
|
android:name=".receivers.NotificationListener"
|
||||||
|
|||||||
@@ -0,0 +1,219 @@
|
|||||||
|
package com.jens.automation2;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.wifi.ScanResult;
|
||||||
|
import android.net.wifi.WifiConfiguration;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.text.Editable;
|
||||||
|
import android.text.TextWatcher;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.CompoundButton;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.RadioButton;
|
||||||
|
import android.widget.SeekBar;
|
||||||
|
import android.widget.Spinner;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ActivityManageTriggerProximity extends Activity
|
||||||
|
{
|
||||||
|
TextView tvTriggerProximityHint;
|
||||||
|
Button bTriggerProximitySave;
|
||||||
|
SeekBar sbProximity, sbProximityTest;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(@Nullable Bundle savedInstanceState)
|
||||||
|
{
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
Miscellaneous.setDisplayLanguage(this);
|
||||||
|
Miscellaneous.setUiTheme(this);
|
||||||
|
setContentView(R.layout.activity_manage_trigger_proximity);
|
||||||
|
|
||||||
|
sbProximity = (SeekBar) findViewById(R.id.sbProximity);
|
||||||
|
sbProximityTest = (SeekBar) findViewById(R.id.sbProximityTest);
|
||||||
|
tvTriggerProximityHint = (TextView) findViewById(R.id.tvTriggerProximityHint);
|
||||||
|
bTriggerProximitySave = (Button) findViewById(R.id.bTriggerProximitySave);
|
||||||
|
|
||||||
|
tvTriggerProximityHint.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
|
||||||
|
bTriggerProximitySave.setOnClickListener(new View.OnClickListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onClick(View v)
|
||||||
|
{
|
||||||
|
Intent response = new Intent();
|
||||||
|
response.putExtra("wifiState", rbTriggerWifiConnected.isChecked());
|
||||||
|
response.putExtra("wifiName", etTriggerWifiName.getText().toString());
|
||||||
|
setResult(RESULT_OK, response);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
spinnerWifiList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
|
||||||
|
{
|
||||||
|
etTriggerWifiName.setText(wifiList.get(position));
|
||||||
|
|
||||||
|
if(etTriggerWifiName.getText().toString().length() > 0 && rbTriggerWifiDisconnected.isChecked())
|
||||||
|
tvWifiTriggerDisconnectionHint.setVisibility(View.VISIBLE);
|
||||||
|
else
|
||||||
|
tvWifiTriggerDisconnectionHint.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNothingSelected(AdapterView<?> parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
bLoadWifiList.setOnClickListener(new View.OnClickListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onClick(View v)
|
||||||
|
{
|
||||||
|
loadWifis();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
rbTriggerWifiDisconnected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
|
||||||
|
{
|
||||||
|
if(etTriggerWifiName.getText().toString().length() > 0 && b)
|
||||||
|
tvWifiTriggerDisconnectionHint.setVisibility(View.VISIBLE);
|
||||||
|
else
|
||||||
|
tvWifiTriggerDisconnectionHint.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
etTriggerWifiName.addTextChangedListener(new TextWatcher()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
||||||
|
{
|
||||||
|
if(etTriggerWifiName.getText().toString().length() > 0 && rbTriggerWifiDisconnected.isChecked())
|
||||||
|
tvWifiTriggerDisconnectionHint.setVisibility(View.VISIBLE);
|
||||||
|
else
|
||||||
|
tvWifiTriggerDisconnectionHint.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterTextChanged(Editable editable)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadWifis()
|
||||||
|
{
|
||||||
|
if(!ActivityPermissions.havePermission(Manifest.permission.ACCESS_FINE_LOCATION, ActivityManageTriggerProximity.this))
|
||||||
|
{
|
||||||
|
AlertDialog dialog = Miscellaneous.messageBox(getResources().getString(R.string.permissionsTitle), getResources().getString(R.string.needLocationPermForWifiList), ActivityManageTriggerProximity.this);
|
||||||
|
dialog.setOnDismissListener(new DialogInterface.OnDismissListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onDismiss(DialogInterface dialog)
|
||||||
|
{
|
||||||
|
ActivityCompat.requestPermissions(ActivityManageTriggerProximity.this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, requestCodeLocationPermission);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.show();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reallyLoadWifiList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void reallyLoadWifiList()
|
||||||
|
{
|
||||||
|
if(Build.VERSION.SDK_INT >= 30)
|
||||||
|
{
|
||||||
|
Miscellaneous.messageBox(getResources().getString(R.string.hint), getResources().getString(R.string.wifiApi30), ActivityManageTriggerProximity.this).show();
|
||||||
|
loadListOfVisibleWifis();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WifiManager myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
||||||
|
|
||||||
|
for (WifiConfiguration wifi : myWifiManager.getConfiguredNetworks())
|
||||||
|
wifiList.add(wifi.SSID.replaceAll("\"+$", "").replaceAll("^\"+", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wifiList.size() > 0)
|
||||||
|
{
|
||||||
|
spinnerWifiList.setEnabled(true);
|
||||||
|
Collections.sort(wifiList);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
spinnerWifiList.setEnabled(false);
|
||||||
|
Toast.makeText(ActivityManageTriggerProximity.this, getResources().getString(R.string.noKnownWifis), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
wifiSpinnerAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadListOfVisibleWifis()
|
||||||
|
{
|
||||||
|
List<ScanResult> results = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
||||||
|
results = wifiManager.getScanResults();
|
||||||
|
|
||||||
|
for (ScanResult wifi : results)
|
||||||
|
wifiList.add(wifi.SSID.replaceAll("\"+$", "").replaceAll("^\"+", ""));
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Miscellaneous.logEvent("e", "loadListOfVisibleWifis()", Log.getStackTraceString(e), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
|
||||||
|
{
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
|
||||||
|
switch (requestCode)
|
||||||
|
{
|
||||||
|
case requestCodeLocationPermission:
|
||||||
|
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
||||||
|
reallyLoadWifiList();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.appcompat.widget.LinearLayoutCompat 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">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/proximity"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
|
||||||
|
android:layout_marginBottom="@dimen/default_margin" />
|
||||||
|
|
||||||
|
<TableLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:shrinkColumns="1"
|
||||||
|
android:stretchColumns="1" >
|
||||||
|
|
||||||
|
<TableRow>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:paddingRight="@dimen/default_margin"
|
||||||
|
android:text="@string/state"/>
|
||||||
|
|
||||||
|
<RadioGroup
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_width="match_parent">
|
||||||
|
|
||||||
|
<SeekBar
|
||||||
|
android:id="@+id/sbProximity"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:max="100" />
|
||||||
|
|
||||||
|
</RadioGroup>
|
||||||
|
|
||||||
|
</TableRow>
|
||||||
|
|
||||||
|
<TableRow>
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvWifiTriggerNameLocationNotice"
|
||||||
|
android:layout_span="2"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:textColor="@color/red"
|
||||||
|
android:text="@string/locationRequiredToDetermineWifiName"
|
||||||
|
android:layout_marginVertical="@dimen/default_margin"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
</TableRow>
|
||||||
|
|
||||||
|
</TableLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvTriggerProximityHint"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginVertical="@dimen/default_margin"
|
||||||
|
android:textColor="@color/red"
|
||||||
|
android:text="@string/deviceDoesntHaveProximitySensor" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/testArea"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/testAreaProximityComment" />
|
||||||
|
|
||||||
|
<SeekBar
|
||||||
|
android:id="@+id/sbProximityTest"
|
||||||
|
android:layout_marginTop="@dimen/default_margin"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/bTriggerProximitySave"
|
||||||
|
android:layout_marginTop="@dimen/default_margin"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/save" />
|
||||||
|
|
||||||
|
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||||
@@ -962,4 +962,8 @@
|
|||||||
<string name="permissionForegroundServiceTypeSpecialUseRequired">Permission FOREGROUND_SERVICE_TYPE_SPECIAL_USE is required to start.</string>
|
<string name="permissionForegroundServiceTypeSpecialUseRequired">Permission FOREGROUND_SERVICE_TYPE_SPECIAL_USE is required to start.</string>
|
||||||
<string name="uiTheme">UI theme</string>
|
<string name="uiTheme">UI theme</string>
|
||||||
<string name="uiThemeSummary">The theme of the graphical user interface. Application required.</string>
|
<string name="uiThemeSummary">The theme of the graphical user interface. Application required.</string>
|
||||||
|
<string name="proximity">Proximity</string>
|
||||||
|
<string name="testArea">Test area</string>
|
||||||
|
<string name="testAreaProximityComment">Approach your device to test how it reacts.</string>
|
||||||
|
<string name="deviceDoesntHaveProximitySensor">Your device doesn\'t seem to have a proximity sensor.</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user