|
38 | 38 | import com.espressif.db.EspDatabase;
|
39 | 39 | import com.espressif.rainmaker.BuildConfig;
|
40 | 40 | import com.espressif.rainmaker.R;
|
| 41 | +import com.espressif.ui.activities.FwUpdateActivity; |
41 | 42 | import com.espressif.ui.activities.GroupShareActivity;
|
42 | 43 | import com.espressif.ui.activities.NotificationsActivity;
|
43 | 44 | import com.espressif.ui.activities.SplashActivity;
|
@@ -120,6 +121,10 @@ public Result doWork() {
|
120 | 121 |
|
121 | 122 | processAutomationTriggerEvent(title, notificationEvent, jsonEventData);
|
122 | 123 |
|
| 124 | + } else if (AppConstants.EVENT_NODE_OTA.equals(eventType)) { |
| 125 | + |
| 126 | + processNodeOta(title, notificationEvent, jsonEventData); |
| 127 | + |
123 | 128 | } else if (AppConstants.EVENT_NODE_PARAM_MODIFIED.equals(eventType)) {
|
124 | 129 |
|
125 | 130 | String nodeId = jsonEventData.optString(AppConstants.KEY_NODE_ID);
|
@@ -712,12 +717,12 @@ private void sendSharingRequestNotification(String title, String contentText, St
|
712 | 717 | declineIntent.putExtra(AppConstants.KEY_ID, id);
|
713 | 718 | PendingIntent declinePendingIntent;
|
714 | 719 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
715 |
| - declinePendingIntent = PendingIntent.getActivity(espApp, |
| 720 | + declinePendingIntent = PendingIntent.getBroadcast(espApp, |
716 | 721 | notificationId++ /* Request code */,
|
717 | 722 | declineIntent,
|
718 | 723 | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);
|
719 | 724 | } else {
|
720 |
| - declinePendingIntent = PendingIntent.getActivity(espApp, |
| 725 | + declinePendingIntent = PendingIntent.getBroadcast(espApp, |
721 | 726 | notificationId++ /* Request code */,
|
722 | 727 | declineIntent,
|
723 | 728 | PendingIntent.FLAG_ONE_SHOT);
|
@@ -930,9 +935,50 @@ private void processAlertEvent(String title, NotificationEvent notificationEvent
|
930 | 935 | EventBus.getDefault().post(new UpdateEvent(UpdateEventType.EVENT_DEVICE_STATUS_UPDATE));
|
931 | 936 | }
|
932 | 937 |
|
933 |
| - private void sendNotification(String title, String messageBody, String channelId, Class activityClass) { |
| 938 | + // Event type - Node OTA |
| 939 | + |
| 940 | + private void processNodeOta(String title, NotificationEvent notificationEvent, JSONObject jsonEventData) { |
| 941 | + |
| 942 | + EspApplication espApp = (EspApplication) getApplicationContext(); |
| 943 | + String nodeId = jsonEventData.optString(AppConstants.KEY_NODE_ID); |
| 944 | + StringBuilder msgBuilder = new StringBuilder(); |
| 945 | + msgBuilder.append("New OTA is available"); |
| 946 | + Log.d(TAG, "Node Id : " + nodeId); |
| 947 | + |
| 948 | + if (!espApp.nodeMap.containsKey(nodeId)) { |
| 949 | + ApiManager.getInstance(espApp).getNodeDetails(nodeId); |
| 950 | + } |
| 951 | + |
| 952 | + if (!espApp.nodeMap.containsKey(nodeId)) { |
| 953 | + loadDataFromLocalStorage(); |
| 954 | + } |
934 | 955 |
|
935 |
| - Log.e(TAG, "Message : " + messageBody); |
| 956 | + if (espApp.nodeMap.containsKey(nodeId)) { |
| 957 | + |
| 958 | + EspNode node = espApp.nodeMap.get(nodeId); |
| 959 | + ArrayList<Device> devices = node.getDevices(); |
| 960 | + ArrayList<String> deviceNames = new ArrayList<>(); |
| 961 | + if (devices != null) { |
| 962 | + for (int deviceIndex = 0; deviceIndex < devices.size(); deviceIndex++) { |
| 963 | + deviceNames.add(devices.get(deviceIndex).getUserVisibleName()); |
| 964 | + } |
| 965 | + } |
| 966 | + msgBuilder.append(" for "); |
| 967 | + msgBuilder.append(getDeviceNameString(deviceNames)); |
| 968 | + |
| 969 | + } else { |
| 970 | + Log.e(TAG, "Node id is not available for this event."); |
| 971 | + } |
| 972 | + |
| 973 | + notificationEvent.setNotificationMsg(msgBuilder.toString()); |
| 974 | + EspDatabase.getInstance(espApp).getNotificationDao().insertOrUpdate(notificationEvent); |
| 975 | + Log.d(TAG, "OTA Notification inserted in database"); |
| 976 | + if (isNotificationAllowed) { |
| 977 | + sendOtaNotification(title, msgBuilder.toString(), AppConstants.CHANNEL_ADMIN); |
| 978 | + } |
| 979 | + } |
| 980 | + |
| 981 | + private void sendNotification(String title, String messageBody, String channelId, Class activityClass) { |
936 | 982 |
|
937 | 983 | Intent activityIntent = new Intent(espApp, activityClass);
|
938 | 984 |
|
@@ -974,6 +1020,44 @@ private void sendNotification(String title, String messageBody, String channelId
|
974 | 1020 | notificationManager.notify(notificationId++, notification);
|
975 | 1021 | }
|
976 | 1022 |
|
| 1023 | + private void sendOtaNotification(String title, String messageBody, String nodeId) { |
| 1024 | + |
| 1025 | + Intent activityIntent = new Intent(espApp, FwUpdateActivity.class); |
| 1026 | + activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1027 | + activityIntent.putExtra(AppConstants.KEY_NODE_ID, nodeId); |
| 1028 | + |
| 1029 | + PendingIntent contentIntent; |
| 1030 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 1031 | + contentIntent = PendingIntent.getActivity(espApp, |
| 1032 | + 0 /* Request code */, |
| 1033 | + activityIntent, |
| 1034 | + PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); |
| 1035 | + } else { |
| 1036 | + contentIntent = PendingIntent.getActivity(espApp, |
| 1037 | + 0 /* Request code */, |
| 1038 | + activityIntent, |
| 1039 | + PendingIntent.FLAG_UPDATE_CURRENT); |
| 1040 | + } |
| 1041 | + |
| 1042 | + Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 1043 | + |
| 1044 | + Notification notification = new NotificationCompat.Builder(espApp, AppConstants.CHANNEL_ADMIN) |
| 1045 | + .setSmallIcon(R.drawable.ic_notify_rainmaker) |
| 1046 | + .setColor(espApp.getColor(R.color.color_esp_logo)) |
| 1047 | + .setContentTitle(title) |
| 1048 | + .setContentText(messageBody) |
| 1049 | + .setStyle(new NotificationCompat.BigTextStyle() |
| 1050 | + .bigText(messageBody)) |
| 1051 | + .setContentIntent(contentIntent) |
| 1052 | + .setShowWhen(true) |
| 1053 | + .setAutoCancel(true) |
| 1054 | + .setSound(defaultSoundUri) |
| 1055 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 1056 | + .build(); |
| 1057 | + |
| 1058 | + notificationManager.notify(notificationId++, notification); |
| 1059 | + } |
| 1060 | + |
977 | 1061 | private void loadDataFromLocalStorage() {
|
978 | 1062 |
|
979 | 1063 | EspDatabase espDatabase = EspDatabase.getInstance(getApplicationContext());
|
|
0 commit comments