Skip to content

Commit 19b35da

Browse files
committed
Merge pull request #71 from SandroMachado/feature/notify-the-user-on-new-tests
Generate a notification when an update includes new tests
2 parents 585014a + a3ea39f commit 19b35da

File tree

9 files changed

+141
-0
lines changed

9 files changed

+141
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,31 @@
1111
android:icon="@drawable/ic_launcher"
1212
android:label="@string/app_name"
1313
android:theme="@style/AppTheme" >
14+
1415
<activity
1516
android:name="fuzion24.device.vulnerability.test.ui.MainActivity"
1617
android:configChanges="orientation|screenSize|keyboardHidden"
18+
android:launchMode="singleTask"
1719
android:label="@string/app_name" >
1820
<intent-filter>
1921
<action android:name="android.intent.action.MAIN" />
2022

2123
<category android:name="android.intent.category.LAUNCHER" />
2224
</intent-filter>
2325
</activity>
26+
27+
<receiver android:name="fuzion24.device.vulnerability.broadcastreceiver.ApplicationUpdateBroadcastReceiver">
28+
<intent-filter>
29+
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
30+
<data android:scheme="package" />
31+
</intent-filter>
32+
33+
<intent-filter>
34+
<action android:name="android.intent.action.PACKAGE_ADDED"/>
35+
<data android:scheme="package" />
36+
</intent-filter>
37+
</receiver>
38+
2439
</application>
2540

2641
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package fuzion24.device.vulnerability.broadcastreceiver;
2+
3+
import android.app.NotificationManager;
4+
import android.app.PendingIntent;
5+
import android.content.BroadcastReceiver;
6+
import android.content.Context;
7+
import android.content.Intent;
8+
import android.support.v4.app.NotificationCompat;
9+
import android.util.Log;
10+
11+
import com.nowsecure.android.vts.R;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
import fuzion24.device.vulnerability.test.ui.MainActivity;
17+
import fuzion24.device.vulnerability.util.SharedPreferencesUtils;
18+
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityOrganizer;
19+
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityTest;
20+
21+
/**
22+
* Handles the application update/installation to show a notification to the user if there are
23+
* new vulnerabilities scans available.
24+
*/
25+
26+
public class ApplicationUpdateBroadcastReceiver extends BroadcastReceiver {
27+
28+
private static final String TAG = "VULN_TEST_BROAD_UPDATE";
29+
30+
private static final int NOTIFICATION_ID = 1;
31+
32+
@Override
33+
public void onReceive(final Context context, final Intent intent) {
34+
List<String> currentTestsAvailable = new ArrayList<>();
35+
36+
for (VulnerabilityTest test : VulnerabilityOrganizer.getTests(context)) {
37+
currentTestsAvailable.add(test.getCVEorID());
38+
}
39+
40+
List<String> testsAvailableOnPreviousBuild = SharedPreferencesUtils.getTheListOfScansAvailable(context);
41+
42+
// First run, save the current list of scans.
43+
if (testsAvailableOnPreviousBuild == null) {
44+
Log.d(TAG, "This is the first time this detection is running.");
45+
SharedPreferencesUtils.setTheListOfScansAvailable(context, currentTestsAvailable);
46+
47+
return;
48+
}
49+
50+
// Update application with the tests available.
51+
SharedPreferencesUtils.setTheListOfScansAvailable(context, currentTestsAvailable);
52+
53+
for (String testName : currentTestsAvailable) {
54+
if (!testsAvailableOnPreviousBuild.contains(testName)) {
55+
56+
// This test is new, show a notification to the user.
57+
buildNotification(context);
58+
Log.d(TAG, String.format("New test available:, %s", testName));
59+
60+
return;
61+
}
62+
}
63+
64+
Log.d(TAG, "Without new tests detected.");
65+
}
66+
67+
private void buildNotification(Context context) {
68+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification)
69+
.setAutoCancel(true)
70+
.setContentTitle(context.getString(R.string.app_name))
71+
.setContentText(context.getString(R.string.notification_new_tests));
72+
73+
Intent notificationIntent = new Intent(context, MainActivity.class);
74+
75+
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
76+
builder.setContentIntent(contentIntent);
77+
78+
// Add the notification.
79+
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
80+
manager.notify(NOTIFICATION_ID, builder.build());
81+
}
82+
83+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fuzion24.device.vulnerability.util;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
public class SharedPreferencesUtils {
12+
13+
private static final String PREFS_LAST_LIST_OF_SCANS = "PREFS_LIST_OF_SCANS";
14+
15+
16+
private static final String PREFS_NAME = "com.nowsecure.android.vts.PREFERENCE_FILE_KEY";
17+
18+
private static SharedPreferences getPreferences(Context context) {
19+
return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
20+
}
21+
22+
public static List<String> getTheListOfScansAvailable(Context context) {
23+
Set<String> listOfPreviousScans = getPreferences(context).getStringSet(PREFS_LAST_LIST_OF_SCANS, null);
24+
25+
if (listOfPreviousScans == null) {
26+
return null;
27+
}
28+
29+
List<String> listOfScans = Arrays.asList(listOfPreviousScans.toArray(new String[listOfPreviousScans.size()]));
30+
31+
return listOfScans;
32+
}
33+
34+
public static void setTheListOfScansAvailable(Context context, List<String> listOfScans) {
35+
SharedPreferences.Editor editor = getPreferences(context).edit();
36+
37+
editor.putStringSet(PREFS_LAST_LIST_OF_SCANS, new HashSet<>(listOfScans));
38+
39+
editor.apply();
40+
}
41+
42+
}
750 Bytes
Loading
518 Bytes
Loading
947 Bytes
Loading
1.15 KB
Loading
1.6 KB
Loading

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
<string name="patches">Patches</string>
3535
<string name="cvssv2">CVSSV2: </string>
3636
<string name="cve_date">CVE Date: </string>
37+
<string name="notification_new_tests">New tests are available</string>
3738

3839
</resources>

0 commit comments

Comments
 (0)