blob: af8bf6f864e05b599ed3683b84d832f1e37b6f17 [file] [log] [blame]
Dirk Vogt47ac2d42017-08-29 23:15:57 +02001package com.fairphone.cameraswapinfo;
2
3import android.app.Notification;
4import android.app.NotificationManager;
5import android.app.PendingIntent;
6import android.content.Context;
7import android.content.Intent;
8import android.support.v4.app.NotificationCompat;
9
10public 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 Tchakaloff96e58eb2017-08-31 12:54:16 +020016 static protected String getNotificationTitle(Context context, int amountOfCameras) {
17 return context.getResources().getQuantityString(R.plurals.camera_swap_notification_title, amountOfCameras);
Dirk Vogt47ac2d42017-08-29 23:15:57 +020018 }
19
Borjan Tchakaloff96e58eb2017-08-31 12:54:16 +020020 static protected String getNotificationShortSummary(Context context, int amountOfCameras) {
21 return context.getResources().getQuantityString(R.plurals.camera_swap_notification_summary, amountOfCameras);
Dirk Vogt47ac2d42017-08-29 23:15:57 +020022 }
23
Borjan Tchakaloff96e58eb2017-08-31 12:54:16 +020024 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 Vogt47ac2d42017-08-29 23:15:57 +020027 }
28
29 public static void showNotification(Context context) {
30 Notification notification = getNotification(context);
Dirk Vogt47ac2d42017-08-29 23:15:57 +020031 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 Tchakaloff96e58eb2017-08-31 12:54:16 +020043 int amountOfCameras = CameraSwapInfoPreferences.getAmountOfCamerasChanged(context);
44
Dirk Vogt47ac2d42017-08-29 23:15:57 +020045 PendingIntent openIntent = getPendingIntent(context);
46 NotificationCompat.Builder mBuilder =
47 new NotificationCompat.Builder(context)
48 .setSmallIcon(R.drawable.ic_stat_camera_swap)
Borjan Tchakaloff96e58eb2017-08-31 12:54:16 +020049 .setContentTitle(getNotificationTitle(context, amountOfCameras))
50 .setContentText(getNotificationShortSummary(context, amountOfCameras))
Dirk Vogt47ac2d42017-08-29 23:15:57 +020051 .setStyle(new NotificationCompat.BigTextStyle().bigText(
Borjan Tchakaloff96e58eb2017-08-31 12:54:16 +020052 getNotificationExtendedSummary(context, amountOfCameras)))
Dirk Vogt47ac2d42017-08-29 23:15:57 +020053 .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 Tchakaloff973196b2017-09-04 14:31:20 +020062 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 Vogt47ac2d42017-08-29 23:15:57 +020066 }
67}