Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 1 | package com.fairphone.cameraswapinfo; |
| 2 | |
| 3 | import android.app.Notification; |
| 4 | import android.app.NotificationManager; |
| 5 | import android.app.PendingIntent; |
| 6 | import android.content.Context; |
| 7 | import android.content.Intent; |
| 8 | import android.support.v4.app.NotificationCompat; |
| 9 | |
| 10 | public class CameraSwapNotificationUtil { |
| 11 | static final String PACKAGE_CAMERA_SWAP_INFO = "com.fairphone.cameraswapinfo"; |
| 12 | static final String ACTIVITY_CAMERA_SWAP_INFO_DETAILS = |
| 13 | "com.fairphone.cameraswapinfo.CameraSwapDetailsActivity"; |
| 14 | static final int NOTIFICATION_ID = 0; |
| 15 | |
Borjan Tchakaloff | 96e58eb | 2017-08-31 12:54:16 +0200 | [diff] [blame] | 16 | static protected String getNotificationTitle(Context context, int amountOfCameras) { |
| 17 | return context.getResources().getQuantityString(R.plurals.camera_swap_notification_title, amountOfCameras); |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 18 | } |
| 19 | |
Borjan Tchakaloff | 96e58eb | 2017-08-31 12:54:16 +0200 | [diff] [blame] | 20 | static protected String getNotificationShortSummary(Context context, int amountOfCameras) { |
| 21 | return context.getResources().getQuantityString(R.plurals.camera_swap_notification_summary, amountOfCameras); |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 22 | } |
| 23 | |
Borjan Tchakaloff | 96e58eb | 2017-08-31 12:54:16 +0200 | [diff] [blame] | 24 | static protected String getNotificationExtendedSummary(Context context, int amountOfCameras) { |
| 25 | return context.getResources().getQuantityString(R.plurals.camera_swap_notification_summary, amountOfCameras) |
| 26 | + " " + context.getResources().getQuantityString(R.plurals.camera_swap_notification_text, amountOfCameras); |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | public static void showNotification(Context context) { |
| 30 | Notification notification = getNotification(context); |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 31 | NotificationManager notificationManager = (NotificationManager) context |
| 32 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 33 | notificationManager.notify(NOTIFICATION_ID, notification); |
| 34 | } |
| 35 | |
| 36 | public static void destroyNotification(Context context) { |
| 37 | NotificationManager notificationManager = (NotificationManager) context |
| 38 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 39 | notificationManager.cancel(NOTIFICATION_ID); |
| 40 | } |
| 41 | |
| 42 | static private Notification getNotification(Context context) { |
Borjan Tchakaloff | 96e58eb | 2017-08-31 12:54:16 +0200 | [diff] [blame] | 43 | int amountOfCameras = CameraSwapInfoPreferences.getAmountOfCamerasChanged(context); |
| 44 | |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 45 | PendingIntent openIntent = getPendingIntent(context); |
| 46 | NotificationCompat.Builder mBuilder = |
| 47 | new NotificationCompat.Builder(context) |
| 48 | .setSmallIcon(R.drawable.ic_stat_camera_swap) |
Borjan Tchakaloff | 96e58eb | 2017-08-31 12:54:16 +0200 | [diff] [blame] | 49 | .setContentTitle(getNotificationTitle(context, amountOfCameras)) |
| 50 | .setContentText(getNotificationShortSummary(context, amountOfCameras)) |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 51 | .setStyle(new NotificationCompat.BigTextStyle().bigText( |
Borjan Tchakaloff | 96e58eb | 2017-08-31 12:54:16 +0200 | [diff] [blame] | 52 | getNotificationExtendedSummary(context, amountOfCameras))) |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 53 | .setColor(context.getResources().getColor(R.color.colorPrimary)) |
| 54 | .setPriority(Notification.PRIORITY_MAX) |
| 55 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 56 | .setContentIntent(openIntent) |
| 57 | .setOngoing(true); |
| 58 | return mBuilder.build(); |
| 59 | } |
| 60 | |
| 61 | private static PendingIntent getPendingIntent(Context context) { |
Borjan Tchakaloff | 973196b | 2017-09-04 14:31:20 +0200 | [diff] [blame^] | 62 | return PendingIntent.getActivity(context, 0, |
| 63 | new Intent(context, CameraSwapDetailsActivity.class) |
| 64 | .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK), |
| 65 | PendingIntent.FLAG_UPDATE_CURRENT); |
Dirk Vogt | 47ac2d4 | 2017-08-29 23:15:57 +0200 | [diff] [blame] | 66 | } |
| 67 | } |