Notification listener started.

This commit is contained in:
jens 2021-03-27 14:11:39 +01:00
parent 1d9ed8b3ff
commit 80f8f9cfe2
13 changed files with 158 additions and 41 deletions

View File

@ -202,10 +202,10 @@
<!-- https://developer.android.com/about/versions/pie/android-9.0-changes-28#apache-p-->
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<!--<service android:name=".location.GeofenceIntentService"/>-->
<service android:name=".location.GeofenceIntentService"/>
<activity android:name=".ActivityManageNotificationTrigger"></activity>
<activity android:name=".ActivityManageTriggerNotification"></activity>
<service
android:name=".receivers.NotificationListener"

View File

@ -181,7 +181,7 @@
<activity android:name=".ActivityManageProfile" />
<activity android:name=".ActivityVolumeTest" />
<activity android:name=".ActivityPermissions"></activity>
<activity android:name=".ActivityManageNotificationTrigger" />
<activity android:name=".ActivityManageTriggerNotification" />
<service
android:name=".receivers.NotificationListener"

View File

@ -176,7 +176,7 @@
<activity android:name=".ActivityManageProfile" />
<activity android:name=".ActivityVolumeTest" />
<activity android:name=".ActivityPermissions"></activity>
<activity android:name=".ActivityManageNotificationTrigger"></activity>
<activity android:name=".ActivityManageTriggerNotification"></activity>
<service
android:name=".receivers.NotificationListener"

View File

@ -543,7 +543,7 @@ public class ActivityManageRule extends Activity
else if(triggerType == Trigger_Enum.notification)
{
newTrigger.setTriggerType(Trigger_Enum.notification);
Intent nfcEditor = new Intent(myContext, ActivityManageNotificationTrigger.class);
Intent nfcEditor = new Intent(myContext, ActivityManageTriggerNotification.class);
startActivityForResult(nfcEditor, requestCodeTriggerNotificationAdd);
return;
}
@ -1157,6 +1157,14 @@ public class ActivityManageRule extends Activity
{
//newTrigger.setNfcTagId(ActivityManageNfc.generatedId);
ruleToEdit.getTriggerSet().add(newTrigger);
newTrigger.setTriggerParameter2(
data.getStringExtra("app") + Trigger.triggerParameter2Split +
data.getStringExtra("titleDir") + Trigger.triggerParameter2Split +
data.getStringExtra("title") + Trigger.triggerParameter2Split +
data.getStringExtra("textDir") + Trigger.triggerParameter2Split +
data.getStringExtra("text")
);
this.refreshTriggerList();
}
else

View File

@ -27,7 +27,7 @@ public class ActivityManageTimeFrame extends Activity
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.trigger_timeframe_editor);
setContentView(R.layout.manage_trigger_timeframe);
startPicker = (TimePicker)findViewById(R.id.tpTimeFrameStart);
stopPicker = (TimePicker)findViewById(R.id.tpTimeFrameStop);

View File

@ -12,14 +12,8 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
@ -34,7 +28,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ActivityManageNotificationTrigger extends Activity
public class ActivityManageTriggerNotification extends Activity
{
EditText etNotificationTitle, etNotificationText;
Button bSelectApp, bSaveTriggerNotification;
@ -197,7 +191,7 @@ public class ActivityManageNotificationTrigger extends Activity
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(getResources().getString(R.string.selectApplication));
final String[] applicationArray = ActivityManageNotificationTrigger.getApplicationNameListString(this);
final String[] applicationArray = ActivityManageTriggerNotification.getApplicationNameListString(this);
alertDialogBuilder.setItems(applicationArray, new DialogInterface.OnClickListener()
{
@Override
@ -215,7 +209,7 @@ public class ActivityManageNotificationTrigger extends Activity
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(getResources().getString(R.string.selectPackageOfApplication));
final String[] packageArray = ActivityManageNotificationTrigger.getPackageListString(this, applicationName);
final String[] packageArray = ActivityManageTriggerNotification.getPackageListString(this, applicationName);
alertDialogBuilder.setItems(packageArray, new DialogInterface.OnClickListener()
{
@Override
@ -234,13 +228,13 @@ public class ActivityManageNotificationTrigger extends Activity
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(getResources().getString(R.string.selectActivityToBeStarted));
final String activityArray[] = ActivityManageNotificationTrigger.getActivityListForPackageName(packageName);
final String activityArray[] = ActivityManageTriggerNotification.getActivityListForPackageName(packageName);
alertDialogBuilder.setItems(activityArray, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
ActivityInfo ai = ActivityManageNotificationTrigger.getActivityInfoForPackageNameAndActivityName(packageName, activityArray[which]);
ActivityInfo ai = ActivityManageTriggerNotification.getActivityInfoForPackageNameAndActivityName(packageName, activityArray[which]);
tvSelectedActivity.setText(ai.packageName + ";" + ai.name);
}
});
@ -253,7 +247,7 @@ public class ActivityManageNotificationTrigger extends Activity
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.trigger_notification);
setContentView(R.layout.manage_trigger_notification);
etNotificationTitle = (EditText)findViewById(R.id.etNotificationTitle);
etNotificationText = (EditText)findViewById(R.id.etNotificationText);
@ -266,12 +260,12 @@ public class ActivityManageNotificationTrigger extends Activity
directions = new String[] {
getResources().getString(R.string.directionStringEquals),
getResources().getString(R.string.directionStringContains),
getResources().getString(R.string.directionStringStartsWidth),
getResources().getString(R.string.directionStringStartsWith),
getResources().getString(R.string.directionStringEndsWith),
getResources().getString(R.string.directionStringNotEquals)
};
directionSpinnerAdapter = new ArrayAdapter<String>(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageNotificationTrigger.directions);
directionSpinnerAdapter = new ArrayAdapter<String>(this, R.layout.text_view_for_poi_listview_mediumtextsize, ActivityManageTriggerNotification.directions);
spinnerTitleDirection.setAdapter(directionSpinnerAdapter);
spinnerTextDirection.setAdapter(directionSpinnerAdapter);
directionSpinnerAdapter.notifyDataSetChanged();
@ -283,7 +277,7 @@ public class ActivityManageNotificationTrigger extends Activity
{
GetActivityListTask getActivityListTask = new GetActivityListTask();
getActivityListTask.execute();
progressDialog = ProgressDialog.show(ActivityManageNotificationTrigger.this, "", ActivityManageNotificationTrigger.this.getResources().getString(R.string.gettingListOfInstalledApplications));
progressDialog = ProgressDialog.show(ActivityManageTriggerNotification.this, "", ActivityManageTriggerNotification.this.getResources().getString(R.string.gettingListOfInstalledApplications));
}
});
@ -294,12 +288,25 @@ public class ActivityManageNotificationTrigger extends Activity
{
if(saveAction())
{
String app = tvSelectedActivity.getText().toString();
String titleDir = spinnerTitleDirection.getSelectedItem().toString();
String app;
if(tvSelectedActivity.getText().toString().equalsIgnoreCase(getResources().getString(R.string.anyApp)))
app = "-1";
else
app = tvSelectedActivity.getText().toString();
String titleDir = Trigger.getMatchCode(spinnerTitleDirection.getSelectedItem().toString());
String title = etNotificationTitle.getText().toString();
String textDir = spinnerTextDirection.getSelectedItem().toString();
String textDir = Trigger.getMatchCode(spinnerTextDirection.getSelectedItem().toString());
String text = etNotificationText.getText().toString();
ActivityManageNotificationTrigger.this.setResult(RESULT_OK);
Intent data = new Intent();
data.putExtra("app", app);
data.putExtra("titleDir", titleDir);
data.putExtra("title", title);
data.putExtra("textDir", textDir);
data.putExtra("text", text);
ActivityManageTriggerNotification.this.setResult(RESULT_OK, data);
finish();
}
}
@ -338,7 +345,7 @@ public class ActivityManageNotificationTrigger extends Activity
{
if(tvSelectedActivity.getText().toString().length() == 0)
{
Toast.makeText(ActivityManageNotificationTrigger.this, getResources().getString(R.string.selectApplication), Toast.LENGTH_LONG).show();
Toast.makeText(ActivityManageTriggerNotification.this, getResources().getString(R.string.selectApplication), Toast.LENGTH_LONG).show();
return false;
}
@ -365,7 +372,7 @@ public class ActivityManageNotificationTrigger extends Activity
@Override
protected Void doInBackground(Void... params)
{
getActivityList(ActivityManageNotificationTrigger.this);
getActivityList(ActivityManageTriggerNotification.this);
return null;
}

View File

@ -69,6 +69,9 @@ public class Trigger
};
private boolean triggerParameter; //if true->started event, if false->stopped
private String triggerParameter2;
public static final String triggerParameter2Split = "tp2split";
private Trigger_Enum triggerType = null;
private PointOfInterest pointOfInterest = null;
@ -208,6 +211,16 @@ public class Trigger
this.triggerParameter = triggerParameter;
}
public String getTriggerParameter2()
{
return triggerParameter2;
}
public void setTriggerParameter2(String triggerParameter2)
{
this.triggerParameter2 = triggerParameter2;
}
public TimeFrame getTimeFrame()
{
return timeFrame;
@ -442,6 +455,32 @@ public class Trigger
else
returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.headsetDisconnected), type));
break;
case notification:
String[] params = getTriggerParameter2().split(triggerParameter2Split);
String app = params[0];
String titleDir = params[1];
String title = params[2];
String textDir = params[3];
String text = params[4];
StringBuilder triggerBuilder = new StringBuilder();
String appString;
if(app.equalsIgnoreCase("-1"))
appString = Miscellaneous.getAnyContext().getResources().getString(R.string.anyApp);
else
appString = "app " + app;
triggerBuilder.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.postsNotification), appString));
if(title.length() > 0)
triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.title) + " " + Trigger.getMatchString(titleDir) + " " + title);
if(text.length() > 0)
triggerBuilder.append(", " + Miscellaneous.getAnyContext().getString(R.string.text) + " " + Trigger.getMatchString(textDir) + " " + text);
returnString.append(triggerBuilder.toString());
break;
default:
returnString.append("error");
break;
@ -449,7 +488,47 @@ public class Trigger
return returnString.toString();
}
public static final String directionEquals = "eq";
public static final String directionContains = "ct";
public static final String directionStartsWith = "sw";
public static final String directionEndsWith = "ew";
public static final String directionNotEquals = "ne";
public static String getMatchString(String direction)
{
switch(direction)
{
case directionEquals:
return Miscellaneous.getAnyContext().getString(R.string.directionStringEquals);
case directionContains:
return Miscellaneous.getAnyContext().getString(R.string.directionStringContains);
case directionStartsWith:
return Miscellaneous.getAnyContext().getString(R.string.directionStringStartsWith);
case directionEndsWith:
return Miscellaneous.getAnyContext().getString(R.string.directionStringEndsWith);
case directionNotEquals:
return Miscellaneous.getAnyContext().getString(R.string.directionStringNotEquals);
default:
return Miscellaneous.getAnyContext().getString(R.string.error);
}
}
public static String getMatchCode(String direction)
{
if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringEquals)))
return directionEquals;
else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringContains)))
return directionContains;
else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringStartsWith)))
return directionStartsWith;
else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringEndsWith)))
return directionEndsWith;
else if(direction.equalsIgnoreCase(Miscellaneous.getAnyContext().getString(R.string.directionStringNotEquals)))
return directionNotEquals;
else
return Miscellaneous.getAnyContext().getString(R.string.error);
}
public static String[] getTriggerTypesAsArray()
{

View File

@ -889,6 +889,8 @@ public class XmlFileInterface
newTrigger.setTriggerType(Trigger_Enum.phoneCall);
else if(triggerEventString.equals("nfcTag"))
newTrigger.setTriggerType(Trigger_Enum.nfcTag);
else if(triggerEventString.equals("notification"))
newTrigger.setTriggerType(Trigger_Enum.notification);
else if(triggerEventString.equals("activityDetection"))
newTrigger.setTriggerType(Trigger_Enum.activityDetection);
else if(triggerEventString.equals("bluetoothConnection"))
@ -985,6 +987,8 @@ public class XmlFileInterface
newTrigger.setHeadphoneType(-1);
}
}
newTrigger.setTriggerParameter2(triggerParameter2);
}
else
{

View File

@ -9,6 +9,8 @@ import android.service.notification.StatusBarNotification;
import androidx.annotation.RequiresApi;
import com.jens.automation2.AutomationService;
// See here for reference: http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html
@SuppressLint("OverrideAbstract")
@ -38,9 +40,15 @@ public class NotificationListener extends NotificationListenerService
public void onNotificationPosted(StatusBarNotification sbn)
{
super.onNotificationPosted(sbn);
String app = sbn.getPackageName();
String title = sbn.getNotification().extras.getString(EXTRA_TITLE);
String text = sbn.getNotification().extras.getString(EXTRA_TEXT);
if(AutomationService.isMyServiceRunning(NotificationListener.this))
{
String app = sbn.getPackageName();
String title = sbn.getNotification().extras.getString(EXTRA_TITLE);
String text = sbn.getNotification().extras.getString(EXTRA_TEXT);
}
}
// @Override

View File

@ -1,14 +1,21 @@
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<LinearLayout
android:layout_margin="@dimen/default_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/default_margin"
android:text="@string/notificationTriggerExplanation" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
@ -26,19 +33,20 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvSelectedActivity"
android:layout_marginHorizontal="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/anyApp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/bSelectApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selectApplication" />
<TextView
android:id="@+id/tvSelectedActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/anyApp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</TableRow>
@ -103,6 +111,7 @@
<Button
android:id="@+id/bSaveTriggerNotification"
android:layout_marginTop="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save" />

View File

@ -600,7 +600,7 @@
<string name="filesStoredAt">Konfigurations- und Logdateien werden hier gespeichert: %1$s</string>
<string name="directionStringEquals">ist gleich</string>
<string name="directionStringContains">enthält</string>
<string name="directionStringStartsWidth">beginnt mit</string>
<string name="directionStringStartsWith">beginnt mit</string>
<string name="directionStringEndsWith">endet mit</string>
<string name="directionStringNotEquals">ist nicht gleich</string>
</resources>

View File

@ -616,8 +616,10 @@
<string name="text">Text</string>
<string name="directionStringEquals">equals</string>
<string name="directionStringContains">contains</string>
<string name="directionStringStartsWidth">starts with</string>
<string name="directionStringStartsWith">starts with</string>
<string name="directionStringEndsWith">ends with</string>
<string name="directionStringNotEquals">not equals</string>
<string name="anyApp">Any app</string>
<string name="notificationTriggerExplanation">This trigger will respond to other applications opening notifications in the notification area. You can specify another application from which the notification has to come from. If you don\'t the notifications from any other application will count.\nYou can also specify strings that must be or must not be in the notification title or notification body.</string>
<string name="postsNotification">%1$s posts notification</string>
</resources>