Notification Resender. Part 4

Notification Resender - allows Zeroner to resend notifications from all apps

Hi there!
I’m continuing my series of posts about creating Android application for resending notifications. Previous parts you can find here: http://dbondarchuk.com/tag/notification-resender/

So, we’ve already created our main activity, helper for working with database and now, we need to create an activity for adding/editing setting.

Let’s start! 🙂

We need to create a new Empty Activity. Let’s call it EditActivity.

First of all, we will create a layout for our activity.

content_edit.xml file:

This will create a form that will contain inputs for needed field. It will look like this:

Notification Resender for Android Zeroner

As you can see, we wrapped our content in ScrollView – it’s needed for enabling scrolling through the screen. Another strange thing is that this ScrollView contains LinearLayout instead of direct elements. This is caused by that fact, that ScrollView can have only one child element.

Let’s look on Java code for this activity:

Here we will initialize our activity. The simplest thing is to show help, when we click on help button. For this purpose let’s use simple alert dialog, using string resources:

Okay, now we have help, that shows how fields should be filled. Interesting moment, if you want to make line break in string resource, you can achieve that like this:

<string name="edit_info_message" formatted="false">You can use such macroses in title and body:\n%appName% - will set name of application that sent notification\n%title% - title of the notification\n%text% - text of the notification</string>

  1. Use disabled formatting
  2. Use ‘\n‘ for line break

Then, we should initialize activity, if we know, that we are editing existing setting instead of creating a new one:

Here we are waiting that Intent (https://developer.android.com/reference/android/content/Intent.html) has extra with name “setting” that is instance of ResendSetting, if so, set isEdit flag to true and initialize fields:

For selected applications we just show number of them. But we need to create some method to select them. We will create another Activity with list of all installed applications (for correct identifying application in notification receiver we will identify applications by their package name).

So, at first we need a class that will represent application in ListView, as we done this for list of settings.

AppSelect.java:

An object that will represent application should have an icon, display name, package name and boolean value that shows if application is selected.

No we will add layout  for ListView item (selectapp_listview_item.xml):

Just a checkbox, icon and display name. Also we need an adapter (AppSelectAdapter.java):

Here we setting selected property on checkbox click

Now we need an empty activity for that (AppSelectActivity):

And Java code for that:

Here, on activity create, we getting already selected package names from the intent extra, splitting them by ‘|‘ character (Note: as you can see, split method takes “\\|” as parameter, instead of simple “|“. It’s needed due to that fact, that split accept a regex pattern, so we need to escape “|” sign by back-slash, and then escape back-slash using back-slash for Java string)

The next step is to get all installed applications. PackageManager allows us to do that:

We are getting all installed applications and then converting them into our AppSelect objects. And finally we can push them into the ListView:

Also we need to apply our selection somehow. For simplify solution, let’s apply it on pressing back button:

The logic is simple: get selected applications, join their package names using “|” character, put this string into the Intent, set result (OK) with this intent.

Note: finish() is needed for closing current activity

Okay, so we created an activity for selecting apps, that will look like this:

Notification Resender. Android Zeroner

Now, we need to accept our returned package names in EditActivity:

Here we compare request and result codes for running appropriate code. When we will get request code 1 and result code OK – we will try to get extra “apps” from the intent and set count of packages to the label.

Okay, when this is done, we need to save our setting:

This is also pretty  simple: try to validate all fields (if the meet requirements, for example, for name we should check if there is no another setting with same name), show Snackbar on errors, and if everything is okay – save new or edit existing setting in database, show message that setting was saved, and when Snackbar will be dismissed – close the activity:

Okay, the only one thing that left is to open this activity for adding or editing.

For adding a new setting, add following code to the MainActivity onCreate method:

Here, on the floating button click callback we are creating a new Intent object, that shows wich activity to open and in which context, and calling startActivity method with intent as argument.

For editing it’s the same method, but we need to put our setting, which we want to edit, as an extra to the intent (ResendSettingArrayAdapter getView method):

That’s it!

The only one thing is left in our application – to receive and process notifications. But it’s a content for the next, the last article in this series 🙂

Thanks!

P.S. #1 All source code you can find in my GitHub – https://github.com/dbondarchuk/Notification-Resender

P.S. #2 You can also download app for your bracelet here – https://github.com/dbondarchuk/Notification-Resender/releases

 

 

Leave a Reply