From 64f22a136904367c5af6cb8c231df8d8260f5ac8 Mon Sep 17 00:00:00 2001 From: 14Sandee Date: Tue, 2 Jan 2024 15:40:52 +0530 Subject: [PATCH] . --- .idea/deploymentTargetDropDown.xml | 12 + .../com/app/simplitend/apputils/AppUtil.java | 15 + .../apputils/BottomNotificationActivity.java | 23 +- .../locationupdates/LocationService.java | 83 +++- app/src/main/res/drawable/ic_battery_low.xml | 417 ++++++++++++++++++ 5 files changed, 524 insertions(+), 26 deletions(-) create mode 100644 app/src/main/res/drawable/ic_battery_low.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 03d9c98..d0da8ff 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -1,6 +1,18 @@ + + + + + + + + + + + + diff --git a/app/src/main/java/com/app/simplitend/apputils/AppUtil.java b/app/src/main/java/com/app/simplitend/apputils/AppUtil.java index 025cb5e..1ba8b91 100644 --- a/app/src/main/java/com/app/simplitend/apputils/AppUtil.java +++ b/app/src/main/java/com/app/simplitend/apputils/AppUtil.java @@ -392,6 +392,7 @@ public abstract class AppUtil { public static final String IS_CALL_BLOCKING_ENABLED = "is_call_blocking_enabled"; public static final String IS_BATTERY_LOW_NOTIFICATION_SENT = "battery_low_notification_sent"; + public static final String IS_BATTERY_LOW_NOTIFICATION_SHOWN = "battery_low_notification_shown"; public static void savePatientData(String token, int patient_uid, Context context, boolean isLoggedIn) { SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE); @@ -436,6 +437,20 @@ public abstract class AppUtil { return sp.getBoolean(IS_BATTERY_LOW_NOTIFICATION_SENT, false); } + public static void setBatteryLowNotificationShown(Context context, boolean sent){ + SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sp.edit(); + + editor.putBoolean(IS_BATTERY_LOW_NOTIFICATION_SHOWN, sent); + + editor.apply(); + } + + public static boolean isBatteryLowNotificationShown(Context context){ + SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE); + return sp.getBoolean(IS_BATTERY_LOW_NOTIFICATION_SHOWN, false); + } + public static void patientSignOut(Context context) { clearAllNotifications(context); clearAllChatNotificationsCount(context); diff --git a/app/src/main/java/com/app/simplitend/apputils/BottomNotificationActivity.java b/app/src/main/java/com/app/simplitend/apputils/BottomNotificationActivity.java index 7342142..53e502f 100644 --- a/app/src/main/java/com/app/simplitend/apputils/BottomNotificationActivity.java +++ b/app/src/main/java/com/app/simplitend/apputils/BottomNotificationActivity.java @@ -13,6 +13,7 @@ import static com.app.simplitend.apputils.Constants.REMINDER_EXTRA_KEY; import static com.app.simplitend.apputils.NotificationService.CONTENT_TYPE_KEY; import static com.app.simplitend.apputils.NotificationService.NOTIFICATION_CONTENT_ID_KEY; import static com.app.simplitend.apputils.NotificationService.NOTIFICATION_SENIOR_ADDRESS_KEY; +import static com.app.simplitend.locationupdates.LocationService.EXTRA_BATTERY_PERCENTAGE; import android.content.Context; import android.content.Intent; @@ -169,10 +170,12 @@ public class BottomNotificationActivity extends AppCompatActivity { switch (content_type) { case Constants.BATTERY_LOW: + int percentage = intent.getIntExtra(NOTIFICATION_CONTENT_ID_KEY, -1); + setUpCgBottomSheet(binding, - R.drawable.img_out_of_geo, - title, "Battery remaining", - "25%", "Text senior", view -> { + R.drawable.ic_battery_low, + patient_name + "'s phone low battery reminder", "Phone battery remaining", + "" + (percentage>=0?percentage+"%":"Unknown"), "Text senior", view -> { bsd.dismiss(); finish(); Intent chatsIntent = new Intent(this, ChatsActivity.class); @@ -453,6 +456,20 @@ public class BottomNotificationActivity extends AppCompatActivity { String body = intent.getStringExtra(NotificationService.NOTIFICATION_BODY_KEY); switch (content_type) { + case Constants.BATTERY_LOW: + + int percentage = intent.getIntExtra(EXTRA_BATTERY_PERCENTAGE, -1); + + setUpSeniorBottomSheet(binding, + R.drawable.ic_battery_low, + "" + (percentage>=0?percentage+"% phone battery remaining":"Phone battery low"), "Connect to charger soon", + null, "Close", view -> { + bsd.dismiss(); + finish(); + }); + + bsd.show(); + break; case Constants.ACTIVITY_TIME: if (!getCgNotificationPref(context, ACTIVITY_NOTIFICATIONS)) { diff --git a/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java index 5213034..0b9805f 100644 --- a/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java +++ b/app/src/main/java/com/app/simplitend/locationupdates/LocationService.java @@ -1,6 +1,7 @@ package com.app.simplitend.locationupdates; import static android.content.Intent.ACTION_BATTERY_CHANGED; +import static com.app.simplitend.apputils.NotificationService.CONTENT_TYPE_KEY; import static com.app.simplitend.patientgeofencing.GeoFenceHelper.GEOFENCE_TAG; import static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_EXTRA_KEY; import static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_REQUEST_TAG; @@ -20,10 +21,13 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; import com.app.simplitend.BuildConfig; import com.app.simplitend.R; import com.app.simplitend.apputils.AppUtil; +import com.app.simplitend.apputils.BottomNotificationActivity; +import com.app.simplitend.apputils.Constants; import com.app.simplitend.apputils.RetrofitHelper; import com.app.simplitend.caregiverdashboard.mvvm.NotificationApiService; import com.app.simplitend.chats.SocketHelper; @@ -53,6 +57,8 @@ public class LocationService extends Service implements LocationClient.DefaultLo public static final int LOCATION_INTERVAL_BASE_TIME = 25 * 1000; private static final int LOCATION_UPDATES_NOTIFICATION_ID = 112; + private static final int SENIOR_BATTERY_LOW_NOTIFICATION_ID = 2102; + public static final String EXTRA_BATTERY_PERCENTAGE = "extra_battery_percentage"; private DefaultLocationClient locationClient; @@ -78,33 +84,13 @@ public class LocationService extends Service implements LocationClient.DefaultLo if (batteryPct <= 25){ - if (AppUtil.isBatteryLowNotificationSent(context)) return; + notifyPatientAboutBatteryLow(context, batteryPct); - int[] chatsCred = AppUtil.getPatientGeofenceChatCred(context); + notifyCaregiver(context, batteryPct); - Map body = new HashMap<>(); - body.put("battery_perentage", batteryPct + ""); - body.put("caregiver_xid", chatsCred[1] + ""); - - NotificationApiService apiService = RetrofitHelper.getRetrofit().create(NotificationApiService.class); - apiService.notifyBatteryLow(body, "Bearer " + AppUtil.getPatientToken(context)) - .enqueue(new Callback>() { - @Override - public void onResponse(Call> call, Response> response) { - if (response.code() == 200){ - AppUtil.setBatteryLowNotificationSent(context, true); - }else{ - AppUtil.setBatteryLowNotificationSent(context, false); - } - } - - @Override - public void onFailure(Call> call, Throwable t) { - AppUtil.setBatteryLowNotificationSent(context, false); - } - }); }else{ AppUtil.setBatteryLowNotificationSent(context, false); + AppUtil.setBatteryLowNotificationShown(context, false); } } } @@ -117,6 +103,57 @@ public class LocationService extends Service implements LocationClient.DefaultLo } } + private void notifyPatientAboutBatteryLow(Context context, float batteryPct) { + if (AppUtil.isBatteryLowNotificationShown(context)) return; + + Notification notification = new Notification.Builder(context, LOCATION_NOTIFICATION_CHANNEL_ID) + .setContentTitle(batteryPct + "% phone battery remaining") + .setContentText("Connect to charger soon") + .setSmallIcon(R.mipmap.ic_launcher_round) + .setOnlyAlertOnce(true) + .setPriority(Notification.PRIORITY_HIGH) + .build(); + + NotificationManagerCompat.from(context).notify(SENIOR_BATTERY_LOW_NOTIFICATION_ID, notification); + + AppUtil.setBatteryLowNotificationShown(context, true); + + // showing bottom sheet + Intent intent = new Intent(this, BottomNotificationActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(EXTRA_BATTERY_PERCENTAGE, (int) batteryPct); + intent.putExtra(CONTENT_TYPE_KEY, Constants.BATTERY_LOW); + startActivity(intent); + } + + private void notifyCaregiver(Context context, float batteryPct) { + if (AppUtil.isBatteryLowNotificationSent(context)) return; + + int[] chatsCred = AppUtil.getPatientGeofenceChatCred(context); + + Map body = new HashMap<>(); + body.put("battery_perentage", batteryPct + ""); + body.put("caregiver_xid", chatsCred[1] + ""); + + NotificationApiService apiService = RetrofitHelper.getRetrofit().create(NotificationApiService.class); + apiService.notifyBatteryLow(body, "Bearer " + AppUtil.getPatientToken(context)) + .enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.code() == 200){ + AppUtil.setBatteryLowNotificationSent(context, true); + }else{ + AppUtil.setBatteryLowNotificationSent(context, false); + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + AppUtil.setBatteryLowNotificationSent(context, false); + } + }); + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && intent.getAction() != null) { diff --git a/app/src/main/res/drawable/ic_battery_low.xml b/app/src/main/res/drawable/ic_battery_low.xml new file mode 100644 index 0000000..216d666 --- /dev/null +++ b/app/src/main/res/drawable/ic_battery_low.xml @@ -0,0 +1,417 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +