|
| 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 | +} |
0 commit comments