From 7894504791e20a81307a0b3e838f63dd83f6f1f5 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 28 Mar 2021 20:33:44 +0200 Subject: [PATCH] Notification listener started. --- app/src/apkFlavor/AndroidManifest.xml | 1 + .../java/com/jens/automation2/Rule.java | 104 +++++- app/src/fdroidFlavor/AndroidManifest.xml | 1 + .../java/com/jens/automation2/Rule.java | 88 +++++ app/src/googlePlayFlavor/AndroidManifest.xml | 1 + .../java/com/jens/automation2/Rule.java | 88 +++++ .../java/com/jens/automation2/Action.java | 12 + .../java/com/jens/automation2/Actions.java | 21 ++ .../ActivityManageActionPlaySound.java | 98 ++++++ .../jens/automation2/ActivityManageRule.java | 306 ++++++++++-------- .../ActivityManageTriggerNotification.java | 76 +++-- .../jens/automation2/ActivityPermissions.java | 82 ++++- .../jens/automation2/AutomationService.java | 2 - .../com/jens/automation2/Miscellaneous.java | 19 ++ .../receivers/NotificationListener.java | 97 ++++-- .../res/layout/activity_manage_play_sound.xml | 46 +++ app/src/main/res/values/strings.xml | 5 + 17 files changed, 839 insertions(+), 208 deletions(-) create mode 100644 app/src/main/java/com/jens/automation2/ActivityManageActionPlaySound.java create mode 100644 app/src/main/res/layout/activity_manage_play_sound.xml diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index 67011fa5..ff5c92e8 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -140,6 +140,7 @@ + diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 96531677..4252e079 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -1,13 +1,17 @@ package com.jens.automation2; +import android.annotation.SuppressLint; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.os.AsyncTask; +import android.os.Build; import android.os.Looper; +import android.service.notification.StatusBarNotification; import android.util.Log; import android.widget.Toast; import com.google.android.gms.location.DetectedActivity; +import com.jens.automation2.location.LocationProvider; import com.jens.automation2.location.WifiBroadcastReceiver; import com.jens.automation2.receivers.ActivityDetectionReceiver; import com.jens.automation2.receivers.BatteryReceiver; @@ -16,6 +20,7 @@ import com.jens.automation2.receivers.ConnectivityReceiver; import com.jens.automation2.receivers.HeadphoneJackListener; import com.jens.automation2.receivers.NfcReceiver; import com.jens.automation2.receivers.NoiseListener; +import com.jens.automation2.receivers.NotificationListener; import com.jens.automation2.receivers.PhoneStatusListener; import com.jens.automation2.receivers.ProcessListener; @@ -24,6 +29,10 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import static com.jens.automation2.Trigger.triggerParameter2Split; +import static com.jens.automation2.receivers.NotificationListener.EXTRA_TEXT; +import static com.jens.automation2.receivers.NotificationListener.EXTRA_TITLE; + public class Rule implements Comparable { @@ -114,6 +123,7 @@ public class Rule implements Comparable { return this.getName(); } + @SuppressLint("NewApi") public String toStringLong() { String returnString = ""; @@ -488,7 +498,7 @@ public class Rule implements Comparable { if(oneTrigger.getTriggerParameter()) { - if(com.jens.automation2.location.LocationProvider.getSpeed() < oneTrigger.getSpeed()) + if(LocationProvider.getSpeed() < oneTrigger.getSpeed()) { Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), context.getResources().getString(R.string.ruleDoesntApplyWeAreSlowerThan) + " " + String.valueOf(oneTrigger.getSpeed()), 3); return false; @@ -496,7 +506,7 @@ public class Rule implements Comparable } else { - if(com.jens.automation2.location.LocationProvider.getSpeed() > oneTrigger.getSpeed()) + if(LocationProvider.getSpeed() > oneTrigger.getSpeed()) { Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), context.getResources().getString(R.string.ruleDoesntApplyWeAreFasterThan) + " " + String.valueOf(oneTrigger.getSpeed()), 3); return false; @@ -725,14 +735,90 @@ public class Rule implements Comparable } else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.notification)) { - k - if(HeadphoneJackListener.isHeadsetConnected() != oneTrigger.getTriggerParameter()) - return false; - else - if(oneTrigger.getHeadphoneType() != 2 && oneTrigger.getHeadphoneType() != HeadphoneJackListener.getHeadphoneType()) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), context.getResources().getString(R.string.ruleDoesntApplyWrongHeadphoneType), 3); - return false; + String[] params = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); + + String myApp = params[0]; + String myTitleDir = params[1]; + String myTitle = params[2]; + String myTextDir = params[3]; + String myText; + if (params.length >= 5) + myText = params[4]; + else + myText = ""; + + if(oneTrigger.getTriggerParameter()) + { + // Check an active notification that is still there + + boolean foundMatch = false; + + for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) + { + String app = sbn.getPackageName(); + String title = sbn.getNotification().extras.getString(EXTRA_TITLE); + String text = sbn.getNotification().extras.getString(EXTRA_TEXT); + + if (!myApp.equals("-1")) + { + if (!app.equalsIgnoreCase(myApp)) + continue; + } + + if (myTitle.length() > 0) + { + if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + continue; + } + + if (myText.length() > 0) + { + if (!Miscellaneous.compare(myTextDir, text, myText)) + continue; + } + + foundMatch = true; + } + + if(!foundMatch) + return false; + } + else + { + // check a notification that is gone + + if(NotificationListener.getLastNotification() != null) + { + if(!NotificationListener.getLastNotification().isCreated()) + { + String app = NotificationListener.getLastNotification().getApp(); + String title = NotificationListener.getLastNotification().getTitle(); + String text = NotificationListener.getLastNotification().getText(); + + if (!myApp.equals("-1")) + { + if (!app.equalsIgnoreCase(myApp)) + return false; + } + + if (myTitle.length() > 0) + { + if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + return false; + } + + if (myText.length() > 0) + { + if (!Miscellaneous.compare(myTextDir, text, myText)) + return false; + } + } + else + return false; + } + } } } } diff --git a/app/src/fdroidFlavor/AndroidManifest.xml b/app/src/fdroidFlavor/AndroidManifest.xml index 4bb4fb74..3ce410e4 100644 --- a/app/src/fdroidFlavor/AndroidManifest.xml +++ b/app/src/fdroidFlavor/AndroidManifest.xml @@ -176,6 +176,7 @@ + diff --git a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java index e04d4a62..1aa8e1d3 100644 --- a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java @@ -732,6 +732,94 @@ public class Rule implements Comparable return false; } } + else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.notification)) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) + { + String[] params = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); + + String myApp = params[0]; + String myTitleDir = params[1]; + String myTitle = params[2]; + String myTextDir = params[3]; + String myText; + if (params.length >= 5) + myText = params[4]; + else + myText = ""; + + if(oneTrigger.getTriggerParameter()) + { + // Check an active notification that is still there + + boolean foundMatch = false; + + for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) + { + String app = sbn.getPackageName(); + String title = sbn.getNotification().extras.getString(EXTRA_TITLE); + String text = sbn.getNotification().extras.getString(EXTRA_TEXT); + + if (!myApp.equals("-1")) + { + if (!app.equalsIgnoreCase(myApp)) + continue; + } + + if (myTitle.length() > 0) + { + if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + continue; + } + + if (myText.length() > 0) + { + if (!Miscellaneous.compare(myTextDir, text, myText)) + continue; + } + + foundMatch = true; + } + + if(!foundMatch) + return false; + } + else + { + // check a notification that is gone + + if(NotificationListener.getLastNotification() != null) + { + if(!NotificationListener.getLastNotification().isCreated()) + { + String app = NotificationListener.getLastNotification().getApp(); + String title = NotificationListener.getLastNotification().getTitle(); + String text = NotificationListener.getLastNotification().getText(); + + if (!myApp.equals("-1")) + { + if (!app.equalsIgnoreCase(myApp)) + return false; + } + + if (myTitle.length() > 0) + { + if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + return false; + } + + if (myText.length() > 0) + { + if (!Miscellaneous.compare(myTextDir, text, myText)) + return false; + } + } + else + return false; + } + } + } + } } return true; diff --git a/app/src/googlePlayFlavor/AndroidManifest.xml b/app/src/googlePlayFlavor/AndroidManifest.xml index 575b8fe8..b434ce27 100644 --- a/app/src/googlePlayFlavor/AndroidManifest.xml +++ b/app/src/googlePlayFlavor/AndroidManifest.xml @@ -129,6 +129,7 @@ + diff --git a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java index 291f6743..641342d8 100644 --- a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java @@ -763,6 +763,94 @@ public class Rule implements Comparable return false; } } + else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.notification)) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) + { + String[] params = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); + + String myApp = params[0]; + String myTitleDir = params[1]; + String myTitle = params[2]; + String myTextDir = params[3]; + String myText; + if (params.length >= 5) + myText = params[4]; + else + myText = ""; + + if(oneTrigger.getTriggerParameter()) + { + // Check an active notification that is still there + + boolean foundMatch = false; + + for (StatusBarNotification sbn : NotificationListener.getInstance().getActiveNotifications()) + { + String app = sbn.getPackageName(); + String title = sbn.getNotification().extras.getString(EXTRA_TITLE); + String text = sbn.getNotification().extras.getString(EXTRA_TEXT); + + if (!myApp.equals("-1")) + { + if (!app.equalsIgnoreCase(myApp)) + continue; + } + + if (myTitle.length() > 0) + { + if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + continue; + } + + if (myText.length() > 0) + { + if (!Miscellaneous.compare(myTextDir, text, myText)) + continue; + } + + foundMatch = true; + } + + if(!foundMatch) + return false; + } + else + { + // check a notification that is gone + + if(NotificationListener.getLastNotification() != null) + { + if(!NotificationListener.getLastNotification().isCreated()) + { + String app = NotificationListener.getLastNotification().getApp(); + String title = NotificationListener.getLastNotification().getTitle(); + String text = NotificationListener.getLastNotification().getText(); + + if (!myApp.equals("-1")) + { + if (!app.equalsIgnoreCase(myApp)) + return false; + } + + if (myTitle.length() > 0) + { + if (!Miscellaneous.compare(myTitleDir, title, myTitle)) + return false; + } + + if (myText.length() > 0) + { + if (!Miscellaneous.compare(myTextDir, text, myText)) + return false; + } + } + else + return false; + } + } + } + } } return true; diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 576cfe3d..c4abd2bd 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -12,6 +12,8 @@ import java.util.Locale; public class Action { + public static final String actionParameter2Split = "ap2split"; + public enum Action_Enum { setWifi, setBluetooth, @@ -33,6 +35,7 @@ public class Action speakText, playMusic, setScreenBrightness, + playSound, sendTextMessage; public String getFullName(Context context) @@ -87,6 +90,8 @@ public class Action return context.getResources().getString(R.string.actionSpeakText); case playMusic: return context.getResources().getString(R.string.actionPlayMusic); + case playSound: + return context.getResources().getString(R.string.playSound); case sendTextMessage: return context.getResources().getString(R.string.sendTextMessage); case setScreenBrightness: @@ -210,6 +215,10 @@ public class Action { returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.wakeupDevice)); } + else if(this.getAction().equals(Action_Enum.playMusic)) + { + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.playSound) + " " + getParameter2()); + } else returnString.append(action.toString()); @@ -395,6 +404,9 @@ public class Action case setScreenBrightness: Actions.setScreenBrightness(getParameter1(), Integer.parseInt(getParameter2())); break; + case playSound: + Actions.playSound(getParameter1(), getParameter2()); + break; default: Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3); break; diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index fa6cf3ad..2cf9aa78 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -8,6 +8,7 @@ import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.media.AudioManager; +import android.media.MediaPlayer; import android.net.ConnectivityManager; import android.net.wifi.WifiManager; import android.os.Build; @@ -465,6 +466,26 @@ public class Actions return ""; } + public static void playSound(boolean alwaysPlay, String soundFileLocation) + { + if(alwaysPlay || ((AudioManager) Miscellaneous.getAnyContext().getSystemService(Context.AUDIO_SERVICE)).getRingerMode() == AudioManager.RINGER_MODE_NORMAL) + { + MediaPlayer mp = new MediaPlayer(); + try + { + mp.setDataSource(soundFileLocation); + mp.prepare(); + mp.start(); + } + catch (Exception e) + { + Miscellaneous.logEvent("e", "Play sound file", "Error playing sound: " + Log.getStackTraceString(e), 2); + } + } + else + Miscellaneous.logEvent("i", "Play sound file", "Not playing sound file because phone is on some kind of mute state.", 2); + } + public void useDownloadedWebpage(String result) { // Toast.makeText(context, "Result: " + result, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionPlaySound.java b/app/src/main/java/com/jens/automation2/ActivityManageActionPlaySound.java new file mode 100644 index 00000000..acc63974 --- /dev/null +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionPlaySound.java @@ -0,0 +1,98 @@ +package com.jens.automation2; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +public class ActivityManageActionPlaySound extends Activity +{ + final static int PICKFILE_RESULT_CODE = 4711; + + CheckBox chkPlaySoundAlwaysPlay; + TextView tvSelectedSoundFile; + Button bSelectSoundFile, bSavePlaySound; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_manage_play_sound); + + chkPlaySoundAlwaysPlay = (CheckBox)findViewById(R.id.chkPlaySoundAlwaysPlay); + tvSelectedSoundFile = (TextView)findViewById(R.id.tvSelectedSoundFile); + bSelectSoundFile = (Button)findViewById(R.id.bSelectSoundFile); + bSavePlaySound = (Button)findViewById(R.id.bSavePlaySound); + + boolean edit = getIntent().getBooleanExtra("edit", false); + + if(edit) + { + boolean param1 = getIntent().getBooleanExtra("actionParameter1", false); + String param2 = getIntent().getStringExtra("actionParameter2"); + chkPlaySoundAlwaysPlay.setChecked(param1); + tvSelectedSoundFile.setText(param2); + } + + bSelectSoundFile.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + //Need to check for storage permissions + Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT); + chooseFile.setType("*/*"); + chooseFile = Intent.createChooser(chooseFile, getResources().getString(R.string.selectSoundFile)); + startActivityForResult(chooseFile, PICKFILE_RESULT_CODE); + } + }); + + bSavePlaySound.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + savePlaySoundSettings(); + } + }); + } + + void savePlaySoundSettings() + { + if(tvSelectedSoundFile.getText().toString() == null || tvSelectedSoundFile.getText().toString().length() == 0) + { + Toast.makeText(ActivityManageActionPlaySound.this, getResources().getString(R.string.selectSoundFile), Toast.LENGTH_LONG).show(); + return; + } + + Intent returnData = new Intent(); + returnData.putExtra("actionParameter1", chkPlaySoundAlwaysPlay.isChecked()); + returnData.putExtra("actionParameter2", tvSelectedSoundFile.getText().toString()); + + setResult(RESULT_OK, returnData); + finish(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) + { + super.onActivityResult(requestCode, resultCode, data); + + if(resultCode == RESULT_OK) + { + if(requestCode == PICKFILE_RESULT_CODE) + { + Uri fileUri = data.getData(); + String filePath = fileUri.getPath(); + tvSelectedSoundFile.setText(filePath); + } + } + } +} diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index 53339cd7..377e0270 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -97,6 +97,8 @@ public class ActivityManageRule extends Activity final static int requestCodeActionSendTextMessage = 7001; final static int requestCodeTriggerNotificationAdd = 8000; final static int requestCodeTriggerNfcNotificationEdit = 8001; + final static int requestCodeActionPlaySoundAdd = 501; + final static int requestCodeActionPlaySoundEdit = 502; public static ActivityManageRule getInstance() { @@ -248,6 +250,12 @@ public class ActivityManageRule extends Activity Intent bluetoothEditor = new Intent(ActivityManageRule.this, ActivityManageBluetoothTrigger.class); startActivityForResult(bluetoothEditor, requestCodeTriggerBluetoothEdit); break; + case notification: + ActivityManageTriggerNotification.editedNotificationTrigger = selectedTrigger; + Intent notificationEditor = new Intent(ActivityManageRule.this, ActivityManageTriggerNotification.class); + notificationEditor.putExtra("edit", true); + startActivityForResult(notificationEditor, requestCodeTriggerNfcNotificationEdit); + break; default: break; } @@ -341,6 +349,13 @@ public class ActivityManageRule extends Activity activityEditScreenBrightnessIntent.putExtra("brightnessValue", Integer.parseInt(a.getParameter2())); startActivityForResult(activityEditScreenBrightnessIntent, requestCodeActionScreenBrightnessEdit); break; + case playSound: + Intent actionPlaySoundIntent = new Intent(context, ActivityManageActionPlaySound.class); + actionPlaySoundIntent.putExtra("edit", true); + actionPlaySoundIntent.putExtra("actionParameter1", a.getParameter1()); + actionPlaySoundIntent.putExtra("actionParameter2", a.getParameter2()); + startActivityForResult(actionPlaySoundIntent, requestCodeActionPlaySoundEdit); + break; default: Miscellaneous.logEvent("w", "Edit action", "Editing of action type " + a.getAction().toString() + " not implemented, yet.", 4); break; @@ -1170,6 +1185,14 @@ public class ActivityManageRule extends Activity else Miscellaneous.logEvent("w", "ActivityManageNfc", "No nfc id returned. Assuming abort.", 5); } + else if(requestCode == requestCodeTriggerNfcNotificationEdit) + { + if(resultCode == RESULT_OK) + { + newTrigger = ActivityManageTriggerNotification.resultingTrigger; + this.refreshTriggerList(); + } + } else if(requestCode == requestCodeActionSpeakTextAdd) { if(resultCode == RESULT_OK) @@ -1284,6 +1307,8 @@ public class ActivityManageRule extends Activity items.add(new Item(typesLong[i].toString(), R.drawable.tune)); else if(types[i].toString().equals(Action_Enum.setScreenBrightness.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.brightness)); + else if(types[i].toString().equals(Action_Enum.playSound.toString())) + items.add(new Item(typesLong[i].toString(), R.drawable.sound)); else if(types[i].toString().equals(Action_Enum.sendTextMessage.toString())) { // if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageSpecificRule.this, "android.permission.SEND_SMS") && !Miscellaneous.isGooglePlayInstalled(ActivityManageSpecificRule.this)) @@ -1293,159 +1318,158 @@ public class ActivityManageRule extends Activity else items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); } - -// = { -// new Item("Bluetooth", R.drawable.bluetooth), -// new Item("Wifi", R.drawable.wifi), -// new Item("...", 0), //no icon for this one -// }; -// ListAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_item, android.R.id.text1, items) - ListAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_item, android.R.id.text1, items) - { - public View getView(int position, View convertView, ViewGroup parent) - { - //User super class to create the View - View v = super.getView(position, convertView, parent); - - TextView tv = (TextView)v.findViewById(android.R.id.text1); + ListAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_item, android.R.id.text1, items) + { + public View getView(int position, View convertView, ViewGroup parent) + { + //User super class to create the View + View v = super.getView(position, convertView, parent); - //Put the image on the TextView - tv.setCompoundDrawablesWithIntrinsicBounds(items.get(position).icon, 0, 0, 0); + TextView tv = (TextView)v.findViewById(android.R.id.text1); - //Add margin between image and text (support various screen densities) - int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); - tv.setCompoundDrawablePadding(dp5); + //Put the image on the TextView + tv.setCompoundDrawablesWithIntrinsicBounds(items.get(position).icon, 0, 0, 0); - return v; - } - }; + //Add margin between image and text (support various screen densities) + int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); + tv.setCompoundDrawablePadding(dp5); - AlertDialog.Builder builder = new AlertDialog.Builder(this) - .setTitle(getResources().getString(R.string.selectTypeOfAction)) - .setAdapter(adapter, new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int which) - { - newAction = new Action(); - - if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.triggerUrl.toString())) + return v; + } + }; + + AlertDialog.Builder builder = new AlertDialog.Builder(this) + .setTitle(getResources().getString(R.string.selectTypeOfAction)) + .setAdapter(adapter, new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int which) + { + newAction = new Action(); + + if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.triggerUrl.toString())) + { + //launch other activity to enter a url and parameters; + newAction.setAction(Action_Enum.triggerUrl); + ActivityEditTriggerUrl.resultingAction = null; + Intent editTriggerIntent = new Intent(context, ActivityEditTriggerUrl.class); + startActivityForResult(editTriggerIntent, 1000); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setWifi.toString())) + { + newAction.setAction(Action_Enum.setWifi); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + Toast.makeText(context, context.getResources().getString(R.string.android10WifiToggleNotice), Toast.LENGTH_LONG).show(); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setBluetooth.toString())) + { + if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) + Miscellaneous.messageBox("Bluetooth", getResources().getString(R.string.deviceDoesNotHaveBluetooth), ActivityManageRule.this).show();; + newAction.setAction(Action_Enum.setBluetooth); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setUsbTethering.toString())) + { + newAction.setAction(Action_Enum.setUsbTethering); + if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) + Toast.makeText(context, context.getResources().getString(R.string.usbTetheringFailForAboveGingerbread), Toast.LENGTH_LONG).show(); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setWifiTethering.toString())) + { + newAction.setAction(Action_Enum.setWifiTethering); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setDisplayRotation.toString())) + { + newAction.setAction(Action_Enum.setDisplayRotation); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.changeSoundProfile.toString())) + { + if(Profile.getProfileCollection().size() > 0) { - //launch other activity to enter a url and parameters; - newAction.setAction(Action_Enum.triggerUrl); - ActivityEditTriggerUrl.resultingAction = null; - Intent editTriggerIntent = new Intent(context, ActivityEditTriggerUrl.class); - startActivityForResult(editTriggerIntent, 1000); + newAction.setAction(Action_Enum.changeSoundProfile); + getActionSoundProfileDialog(context).show(); } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setWifi.toString())) + else + Toast.makeText(context, getResources().getString(R.string.noProfilesCreateOneFirst), Toast.LENGTH_LONG).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.startOtherActivity.toString())) + { + newAction.setAction(Action_Enum.startOtherActivity); + Intent intent = new Intent(ActivityManageRule.this, ActivityManageStartActivity.class); + startActivityForResult(intent, 3000); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.waitBeforeNextAction.toString())) + { + newAction.setAction(Action_Enum.waitBeforeNextAction); + getActionWaitBeforeNextActionDialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.wakeupDevice.toString())) + { + newAction.setAction(Action_Enum.wakeupDevice); + getActionWakeupDeviceDialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setAirplaneMode.toString())) + { + if(Build.VERSION.SDK_INT >= 17) { - newAction.setAction(Action_Enum.setWifi); - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) - Toast.makeText(context, context.getResources().getString(R.string.android10WifiToggleNotice), Toast.LENGTH_LONG).show(); - getActionParameter1Dialog(ActivityManageRule.this).show(); + Toast.makeText(context, getResources().getString(R.string.airplaneModeSdk17Warning), Toast.LENGTH_LONG).show(); + Miscellaneous.messageBox(getResources().getString(R.string.airplaneMode), getResources().getString(R.string.rootExplanation), ActivityManageRule.this).show(); } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setBluetooth.toString())) - { - if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) - Miscellaneous.messageBox("Bluetooth", getResources().getString(R.string.deviceDoesNotHaveBluetooth), ActivityManageRule.this).show();; - newAction.setAction(Action_Enum.setBluetooth); - getActionParameter1Dialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setUsbTethering.toString())) - { - newAction.setAction(Action_Enum.setUsbTethering); - if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) - Toast.makeText(context, context.getResources().getString(R.string.usbTetheringFailForAboveGingerbread), Toast.LENGTH_LONG).show(); - getActionParameter1Dialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setWifiTethering.toString())) - { - newAction.setAction(Action_Enum.setWifiTethering); - getActionParameter1Dialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setDisplayRotation.toString())) - { - newAction.setAction(Action_Enum.setDisplayRotation); - getActionParameter1Dialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.changeSoundProfile.toString())) - { - if(Profile.getProfileCollection().size() > 0) - { - newAction.setAction(Action_Enum.changeSoundProfile); - getActionSoundProfileDialog(context).show(); - } - else - Toast.makeText(context, getResources().getString(R.string.noProfilesCreateOneFirst), Toast.LENGTH_LONG).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.startOtherActivity.toString())) - { - newAction.setAction(Action_Enum.startOtherActivity); - Intent intent = new Intent(ActivityManageRule.this, ActivityManageStartActivity.class); - startActivityForResult(intent, 3000); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.waitBeforeNextAction.toString())) - { - newAction.setAction(Action_Enum.waitBeforeNextAction); - getActionWaitBeforeNextActionDialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.wakeupDevice.toString())) - { - newAction.setAction(Action_Enum.wakeupDevice); - getActionWakeupDeviceDialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setAirplaneMode.toString())) - { - if(Build.VERSION.SDK_INT >= 17) - { - Toast.makeText(context, getResources().getString(R.string.airplaneModeSdk17Warning), Toast.LENGTH_LONG).show(); - Miscellaneous.messageBox(getResources().getString(R.string.airplaneMode), getResources().getString(R.string.rootExplanation), ActivityManageRule.this).show(); - } - newAction.setAction(Action_Enum.setAirplaneMode); - getActionParameter1Dialog(ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setDataConnection.toString())) - { - newAction.setAction(Action_Enum.setDataConnection); - getActionParameter1Dialog(ActivityManageRule.this).show(); - Miscellaneous.messageBox(getResources().getString(R.string.actionDataConnection), getResources().getString(R.string.rootExplanation), ActivityManageRule.this).show(); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.speakText.toString())) - { - //launch other activity to enter a url and parameters; - newAction.setAction(Action_Enum.speakText); - ActivityEditSpeakText.resultingAction = null; - Intent editTriggerIntent = new Intent(context, ActivityEditSpeakText.class); - startActivityForResult(editTriggerIntent, 5000); - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.sendTextMessage.toString())) - { + newAction.setAction(Action_Enum.setAirplaneMode); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setDataConnection.toString())) + { + newAction.setAction(Action_Enum.setDataConnection); + getActionParameter1Dialog(ActivityManageRule.this).show(); + Miscellaneous.messageBox(getResources().getString(R.string.actionDataConnection), getResources().getString(R.string.rootExplanation), ActivityManageRule.this).show(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.speakText.toString())) + { + //launch other activity to enter a url and parameters; + newAction.setAction(Action_Enum.speakText); + ActivityEditSpeakText.resultingAction = null; + Intent editTriggerIntent = new Intent(context, ActivityEditSpeakText.class); + startActivityForResult(editTriggerIntent, 5000); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.sendTextMessage.toString())) + { // if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageSpecificRule.this, "android.permission.SEND_SMS") && !Miscellaneous.isGooglePlayInstalled(ActivityManageSpecificRule.this)) - if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageRule.this, "android.permission.SEND_SMS")) - { - //launch other activity to enter parameters; - newAction.setAction(Action_Enum.sendTextMessage); - ActivityEditSendTextMessage.resultingAction = null; - Intent editTriggerIntent = new Intent(context, ActivityEditSendTextMessage.class); - startActivityForResult(editTriggerIntent, 5001); - } - } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.playMusic.toString())) + if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageRule.this, "android.permission.SEND_SMS")) { - newAction.setAction(Action_Enum.playMusic); - ruleToEdit.getActionSet().add(newAction); - refreshActionList(); + //launch other activity to enter parameters; + newAction.setAction(Action_Enum.sendTextMessage); + ActivityEditSendTextMessage.resultingAction = null; + Intent editTriggerIntent = new Intent(context, ActivityEditSendTextMessage.class); + startActivityForResult(editTriggerIntent, 5001); } - else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setScreenBrightness.toString())) - { - newAction.setAction(Action_Enum.setScreenBrightness); - Intent actionScreenBrightnessIntent = new Intent(context, ActivityManageBrightnessSetting.class); - startActivityForResult(actionScreenBrightnessIntent, requestCodeActionScreenBrightnessAdd); - } - } - }); - - return builder.create(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.playMusic.toString())) + { + newAction.setAction(Action_Enum.playMusic); + ruleToEdit.getActionSet().add(newAction); + refreshActionList(); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setScreenBrightness.toString())) + { + newAction.setAction(Action_Enum.setScreenBrightness); + Intent actionScreenBrightnessIntent = new Intent(context, ActivityManageBrightnessSetting.class); + startActivityForResult(actionScreenBrightnessIntent, requestCodeActionScreenBrightnessAdd); + } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.playSound.toString())) + { + newAction.setAction(Action_Enum.playSound); + Intent actionPlaySoundIntent = new Intent(context, ActivityManageActionPlaySound.class); + startActivityForResult(actionPlaySoundIntent, requestCodeActionPlaySoundAdd); + } + } + }); + + return builder.create(); } private AlertDialog getActionSoundProfileDialog(final Context myContext) { diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java index 7b90c849..d25ce28c 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerNotification.java @@ -29,8 +29,11 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import static com.jens.automation2.Trigger.triggerParameter2Split; + public class ActivityManageTriggerNotification extends Activity { + public static Trigger editedNotificationTrigger; EditText etNotificationTitle, etNotificationText; Button bSelectApp, bSaveTriggerNotification; Spinner spinnerTitleDirection, spinnerTextDirection; @@ -312,15 +315,24 @@ public class ActivityManageTriggerNotification extends Activity String textDir = Trigger.getMatchCode(spinnerTextDirection.getSelectedItem().toString()); String text = etNotificationText.getText().toString(); - Intent data = new Intent(); - data.putExtra("direction", chkNotificationDirection.isChecked()); - data.putExtra("app", app); - data.putExtra("titleDir", titleDir); - data.putExtra("title", title); - data.putExtra("textDir", textDir); - data.putExtra("text", text); + if(edit) + { + editedNotificationTrigger.setTriggerParameter(chkNotificationDirection.isChecked()); + editedNotificationTrigger.setTriggerParameter2(app + triggerParameter2Split + titleDir + triggerParameter2Split + title + triggerParameter2Split + textDir + triggerParameter2Split + text); + ActivityManageTriggerNotification.this.setResult(RESULT_OK); + } + else + { + Intent data = new Intent(); + data.putExtra("direction", chkNotificationDirection.isChecked()); + 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); + } - ActivityManageTriggerNotification.this.setResult(RESULT_OK, data); finish(); } }); @@ -335,23 +347,37 @@ public class ActivityManageTriggerNotification extends Activity private void loadValuesIntoGui() { -// String[] params = resultingAction.getParameter2().split(";"); -// if(params.length >= 2) -// { -// tvSelectedActivity.setText(params[0] + ";" + params[1]); -// -// if(params.length > 2) -// { -// intentPairList.clear(); -// -// for(int i=2; i= 5) + text = params[4]; + else + text = ""; + + if(!app.equals("-1")) + tvSelectedApplication.setText(app); + + for(int i = 0; i < directions.length; i++) + { + if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(titleDir)) + spinnerTitleDirection.setSelection(i); + + if(Trigger.getMatchCode(directions[i]).equalsIgnoreCase(textDir)) + spinnerTextDirection.setSelection(i); + } + + if(title.length() > 0) + etNotificationTitle.setText(title); + + if(text.length() > 0) + etNotificationText.setText(text); } private class GetActivityListTask extends AsyncTask diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index 512ae28c..eef0b285 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -2,6 +2,7 @@ package com.jens.automation2; import android.app.Activity; import android.app.NotificationManager; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; @@ -15,6 +16,8 @@ import android.view.View; import android.widget.Button; import android.widget.TextView; +import com.jens.automation2.receivers.NotificationListener; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -34,12 +37,13 @@ public class ActivityPermissions extends Activity private static final int requestCodeForPermissionsWriteSettings = 12043; private static final int requestCodeForPermissionsNotificationPolicy = 12044; private static final int requestCodeForPermissionsBackgroundLocation = 12045; + private static final int requestCodeForPermissionsNotifications = 12046; protected String[] specificPermissionsToRequest = null; public static String intentExtraName = "permissionsToBeRequested"; Button bCancelPermissions, bRequestPermissions; - TextView tvPermissionsExplanation, tvPermissionosExplanationSystemSettings, tvPermissionsExplanationLong; + TextView tvPermissionsExplanation, tvPermissionsExplanationSystemSettings, tvPermissionsExplanationLong; static ActivityPermissions instance = null; public static final String writeSystemSettingsPermissionName = "android.permission.WRITE_SETTINGS"; @@ -50,6 +54,7 @@ public class ActivityPermissions extends Activity public static final String permissionNameLocationBackground = "android.permission.ACCESS_BACKGROUND_LOCATION"; public static final String permissionNameCall = "android.permission.PROCESS_OUTGOING_CALLS"; public static final String permissionNameStartService = "android.permission.FOREGROUND_SERVICE"; + public static final String permissionNameReadNotifications = "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"; public static ActivityPermissions getInstance() { @@ -72,7 +77,7 @@ public class ActivityPermissions extends Activity bCancelPermissions = (Button)findViewById(R.id.bCancelPermissions); bRequestPermissions = (Button)findViewById(R.id.bRequestPermissions); tvPermissionsExplanation = (TextView)findViewById(R.id.tvPermissionsExplanation); - tvPermissionosExplanationSystemSettings = (TextView)findViewById(R.id.tvPermissionsExplanationSystemSettings); + tvPermissionsExplanationSystemSettings = (TextView)findViewById(R.id.tvPermissionsExplanationSystemSettings); tvPermissionsExplanationLong = (TextView)findViewById(R.id.tvPermissionsExplanationLong); bCancelPermissions.setOnClickListener(new View.OnClickListener() @@ -168,8 +173,6 @@ public class ActivityPermissions extends Activity explanation.append( "
" + - - "" + getResources().getString(R.string.readLocation) + "" @@ -188,8 +191,6 @@ public class ActivityPermissions extends Activity explanation.append( "
" + - - "" + getResources().getString(getResources().getIdentifier(s, "string", getPackageName())) + "" @@ -208,9 +209,9 @@ public class ActivityPermissions extends Activity if (s.equalsIgnoreCase(writeSystemSettingsPermissionName)) { if (requiredPerms.length == 1) - tvPermissionosExplanationSystemSettings.setText(getResources().getString(R.string.systemSettingsNote1)); + tvPermissionsExplanationSystemSettings.setText(getResources().getString(R.string.systemSettingsNote1)); else if (requiredPerms.length > 1) - tvPermissionosExplanationSystemSettings.setText(getResources().getString(R.string.systemSettingsNote1) + getResources().getString(R.string.systemSettingsNote2)); + tvPermissionsExplanationSystemSettings.setText(getResources().getString(R.string.systemSettingsNote1) + getResources().getString(R.string.systemSettingsNote2)); break; } @@ -231,7 +232,19 @@ public class ActivityPermissions extends Activity { for (String s : getRequiredPermissions(false)) { - if(!s.equalsIgnoreCase(permissionNameLocationBackground) && !s.equalsIgnoreCase(permissionNameLocationFine) && !s.equalsIgnoreCase(permissionNameLocationCoarse) && Miscellaneous.googleToBlameForLocation(true)) + if( + s.equalsIgnoreCase(permissionNameLocationBackground) + || + s.equalsIgnoreCase(permissionNameLocationFine) + || + s.equalsIgnoreCase(permissionNameLocationCoarse) + ) + { + if (!Miscellaneous.googleToBlameForLocation(true)) + if (!havePermission(s, context)) + return true; + } + else if (!havePermission(s, context)) return true; } @@ -256,6 +269,10 @@ public class ActivityPermissions extends Activity else return true; } + else if (s.equals(permissionNameReadNotifications)) + { + return verifyNotificationPermission(); + } else { int res = context.checkCallingOrSelfPermission(s); @@ -300,7 +317,19 @@ public class ActivityPermissions extends Activity for (String singlePermission : getPermissionsForRule(rule)) if (!havePermission(singlePermission, workingContext)) { - if(!singlePermission.equalsIgnoreCase(permissionNameLocationBackground) && !singlePermission.equalsIgnoreCase(permissionNameLocationFine) && !singlePermission.equalsIgnoreCase(permissionNameLocationCoarse) && Miscellaneous.googleToBlameForLocation(true)) + if( + + singlePermission.equalsIgnoreCase(permissionNameLocationBackground) + || + singlePermission.equalsIgnoreCase(permissionNameLocationFine) + || + singlePermission.equalsIgnoreCase(permissionNameLocationCoarse) + ) + { + if (!Miscellaneous.googleToBlameForLocation(true)) + addToArrayListUnique(singlePermission, requiredPermissions); + } + else addToArrayListUnique(singlePermission, requiredPermissions); } } @@ -425,6 +454,9 @@ public class ActivityPermissions extends Activity addToArrayListUnique("android.permission.ACCESS_NETWORK_STATE", requiredPermissions); addToArrayListUnique("android.permission.ACCESS_WIFI_STATE", requiredPermissions); break; + case notification: + addToArrayListUnique(permissionNameReadNotifications, requiredPermissions); + break; default: break; } @@ -600,6 +632,11 @@ public class ActivityPermissions extends Activity break; case "android.permission.WRITE_EXTERNAL_STORAGE": usingElements.add(getResources().getString(R.string.storeSettings)); + break; + case permissionNameReadNotifications: + for(String ruleName : getRulesUsing(Trigger.Trigger_Enum.notification)) + usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName)); + break; case "com.google.android.gms.permission.ACTIVITY_RECOGNITION": for(String ruleName : getRulesUsing(Trigger.Trigger_Enum.activityDetection)) @@ -783,6 +820,10 @@ public class ActivityPermissions extends Activity requestPermissions(cachedPermissionsToRequest, true); } } + + if (requestCode == requestCodeForPermissionsNotifications) + if(havePermission(permissionNameReadNotifications, ActivityPermissions.this)) + requestPermissions(cachedPermissionsToRequest, true); } } @@ -842,6 +883,14 @@ public class ActivityPermissions extends Activity startActivityForResult(intent, requestCodeForPermissionsNotificationPolicy); return; } + else if (s.equalsIgnoreCase(permissionNameReadNotifications)) + { + requiredPermissions.remove(s); + cachedPermissionsToRequest = requiredPermissions; + Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); + startActivityForResult(intent, requestCodeForPermissionsNotifications); + return; + } // else if (s.equalsIgnoreCase(permissionNameLocationBackground) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) // { // requiredPermissions.remove(s); @@ -1392,4 +1441,17 @@ public class ActivityPermissions extends Activity return false; } + + public static Boolean verifyNotificationPermission() + { + String theList = android.provider.Settings.Secure.getString(Miscellaneous.getAnyContext().getContentResolver(), "enabled_notification_listeners"); + String[] theListList = theList.split(":"); + String me = (new ComponentName(Miscellaneous.getAnyContext(), NotificationListener.class)).flattenToString(); + for ( String next : theListList ) + { + if ( me.equals(next) ) + return true; + } + return false; + } } diff --git a/app/src/main/java/com/jens/automation2/AutomationService.java b/app/src/main/java/com/jens/automation2/AutomationService.java index 072eec03..487598d3 100644 --- a/app/src/main/java/com/jens/automation2/AutomationService.java +++ b/app/src/main/java/com/jens/automation2/AutomationService.java @@ -287,8 +287,6 @@ public class AutomationService extends Service implements OnInitListener myLocationProvider.applySettingsAndRules(); ReceiverCoordinator.applySettingsAndRules(); - - Miscellaneous.createDismissableNotification("test", 4711, null); } @Override diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index f9581db4..d16d2f88 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -440,6 +440,25 @@ public class Miscellaneous extends Service // Miscellaneous.logEvent("i", TAG, "isEmulator=" + isEmulator); return isEmulator; } + + public static boolean compare(String direction, String needle, String haystack) + { + switch(direction) + { + case Trigger.directionEquals: + return haystack.equalsIgnoreCase(needle); + case Trigger.directionNotEquals: + return !haystack.equalsIgnoreCase(needle); + case Trigger.directionContains: + return haystack.toLowerCase().contains(needle.toLowerCase()); + case Trigger.directionStartsWith: + return haystack.toLowerCase().startsWith(needle.toLowerCase()); + case Trigger.directionEndsWith: + return haystack.toLowerCase().endsWith(needle.toLowerCase()); + default: + return false; + } + } public static int compareTimes(Time time1, Time time2) { diff --git a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java index f295ea5e..9eb3ac3d 100644 --- a/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/NotificationListener.java @@ -1,8 +1,6 @@ package com.jens.automation2.receivers; import android.annotation.SuppressLint; -import android.content.Context; -import android.content.Intent; import android.os.Build; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; @@ -21,6 +19,9 @@ import java.util.ArrayList; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public class NotificationListener extends NotificationListenerService { + static NotificationListener instance; + static SimpleNotification lastNotification = null; + // the title of the notification, public static final String EXTRA_TITLE = "android.title"; @@ -33,10 +34,21 @@ public class NotificationListener extends NotificationListenerService // a bitmap to be used instead of the small icon when showing the notification payload public static final String EXTRA_LARGE_ICON = "android.largeIcon"; + public static SimpleNotification getLastNotification() + { + return lastNotification; + } + @Override public void onCreate() { super.onCreate(); + instance = this; + } + + public static NotificationListener getInstance() + { + return instance; } @RequiresApi(api = Build.VERSION_CODES.KITKAT) @@ -46,13 +58,7 @@ public class NotificationListener extends NotificationListenerService super.onNotificationPosted(sbn); 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); - - checkNotification(true, app, title, text); - } + checkNotification(true, sbn); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) @@ -62,22 +68,77 @@ public class NotificationListener extends NotificationListenerService super.onNotificationRemoved(sbn); if(AutomationService.isMyServiceRunning(NotificationListener.this)) + checkNotification(false, sbn); + } + + boolean checkNotification(boolean created, StatusBarNotification sbn) + { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { String app = sbn.getPackageName(); String title = sbn.getNotification().extras.getString(EXTRA_TITLE); String text = sbn.getNotification().extras.getString(EXTRA_TEXT); - checkNotification(true, app, title, text); + lastNotification = new SimpleNotification(); + lastNotification.created = created; + lastNotification.app = app; + lastNotification.title = title; + lastNotification.text = text; + + ArrayList ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); + for(int i=0; i ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.notification); - for(int i=0; i + + + + + + + + + + +