This commit is contained in:
14Sandee
2023-12-28 20:23:12 +05:30
parent 0e75a2cbc7
commit 5611a29b71
17 changed files with 386 additions and 24 deletions

View File

@@ -232,9 +232,6 @@
android:name=".locationupdates.LocationService"
android:foregroundServiceType="location" />
<service android:name=".apputils.SocketService"
android:stopWithTask="true"/>
<receiver android:name=".patientgeofencing.PatientLocationUpdatesReceiver" />
<receiver android:name=".apputils.BootCompleteReceiver"
android:exported="true">

View File

@@ -36,10 +36,12 @@ import androidx.annotation.RawRes;
import com.app.simplitend.R;
import com.app.simplitend.appblocking.TopAppDetectionService;
import com.app.simplitend.articles.ArticleShowerActivity;
import com.app.simplitend.caregiverdashboard.mvvm.CaregiverMainViewModel;
import com.app.simplitend.databinding.AlertDialogBinding;
import com.app.simplitend.databinding.DecisionBottomsheetBinding;
import com.app.simplitend.databinding.DoneBottomsheetBinding;
import com.app.simplitend.locationupdates.LocationService;
import com.app.simplitend.patient_dashboard.PatientMainViewModel;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.models.ContactData;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.PatientData;
import com.bumptech.glide.Glide;
@@ -389,6 +391,8 @@ 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 void savePatientData(String token, int patient_uid, Context context, boolean isLoggedIn) {
SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
@@ -418,6 +422,20 @@ public abstract class AppUtil {
return sp.getBoolean(IS_PATIENT_LOGGED_IN, false);
}
public static void setBatteryLowNotificationSent(Context context, boolean sent){
SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(IS_BATTERY_LOW_NOTIFICATION_SENT, sent);
editor.apply();
}
public static boolean isBatteryLowNotificationSent(Context context){
SharedPreferences sp = context.getSharedPreferences(PATIENT_DETAILS, Context.MODE_PRIVATE);
return sp.getBoolean(IS_BATTERY_LOW_NOTIFICATION_SENT, false);
}
public static void patientSignOut(Context context) {
clearAllNotifications(context);
clearAllChatNotificationsCount(context);
@@ -428,6 +446,9 @@ public abstract class AppUtil {
PatientDataCache.setPatientData(null);
PatientDataCache.setContactList(null);
PatientMainViewModel.remindersList = null;
PatientMainViewModel.activityList = null;
// turning off app_blocking accessibility permission
Intent stop_accessibility_intent = new Intent(context, TopAppDetectionService.class);
stop_accessibility_intent.setAction(TopAppDetectionService.STOP_ACCESSIBILITY_SERVICE);
@@ -546,6 +567,9 @@ public abstract class AppUtil {
CaregiverDataCache.setCareGiverData(null);
CaregiverMainViewModel.activityList = null;
CaregiverMainViewModel.remindersList = null;
clearAllNotifications(context);
clearAllChatNotificationsCount(context);
OneSignal.getNotifications().clearAllNotifications();

View File

@@ -167,6 +167,20 @@ public class BottomNotificationActivity extends AppCompatActivity {
String body = intent.getStringExtra(NotificationService.NOTIFICATION_BODY_KEY);
switch (content_type) {
case Constants.BATTERY_LOW:
setUpCgBottomSheet(binding,
R.drawable.img_out_of_geo,
title, "Battery remaining",
"25%", "Text senior", view -> {
bsd.dismiss();
finish();
Intent chatsIntent = new Intent(this, ChatsActivity.class);
startActivity(chatsIntent);
});
bsd.show();
break;
case Constants.PATIENT_OUT_OF_GEOFENCE:
if (!getCgNotificationPref(context, GEOFENCE_NOTIFICATIONS)){

View File

@@ -2,6 +2,8 @@ package com.app.simplitend.apputils;
public abstract class Constants {
public static final String NEW_SUBSCRIPTION = "new_subscription";
public static final String BATTERY_LOW = "battery_perentage";
public static final String ACTIVITY_TIME = "activity_time";
public static final String MEDICINE_TIME = "medicine_time";
public static final String GEOFENCING_RADIUS_UPDATED = "geofencing_radius_updated";

View File

@@ -3,6 +3,7 @@ package com.app.simplitend.apputils;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.app.simplitend.apputils.AppUtil.NOTIFICATION_ACTION;
import static com.app.simplitend.apputils.Constants.ACTIVITY_TIME;
import static com.app.simplitend.apputils.Constants.BATTERY_LOW;
import static com.app.simplitend.apputils.Constants.MEDICINE_TIME;
import static com.app.simplitend.patientgeofencing.GeoFenceHelper.GEOFENCE_TAG;
@@ -104,11 +105,12 @@ public class NotificationService implements INotificationServiceExtension {
// showing bottom sheet
try {
if (AppUtil.getCgToken(iNotificationReceivedEvent.getContext()) != null){
// when caregiver is logged in
iNotificationReceivedEvent.getContext().startActivity(intent);
}else{
// when patient is logged in
if (MEDICINE_TIME.equals(content_type) || ACTIVITY_TIME.equals(content_type)){
if (MEDICINE_TIME.equals(content_type) || ACTIVITY_TIME.equals(content_type) || BATTERY_LOW.equals(content_type)){
// Only for meds and activity reminders
iNotificationReceivedEvent.getContext().startActivity(intent);
}

View File

@@ -16,7 +16,6 @@ import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.apputils.SocketService;
import com.app.simplitend.articles.ArticlesActivity;
import com.app.simplitend.caregiverdashboard.fragments.CgDashBoardFragment;
import com.app.simplitend.caregiverdashboard.fragments.MyPatientFragment;
@@ -41,6 +40,8 @@ public class CaregiverDashActivity extends AppCompatActivity implements
DragStateListener,
HomeBottomNav.OnBottomNavItemSelectListener, INotificationLifecycleListener {
private static final String TAG = "SOCKET_CaregiverDashActivity";
// view binding
protected CaregiverDashboardActivityBinding binding;
private CaregiverDashboardMenuBinding menuBinding;
@@ -53,9 +54,6 @@ public class CaregiverDashActivity extends AppCompatActivity implements
binding = CaregiverDashboardActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Intent socketServiceIntent = new Intent(this, SocketService.class);
startService(socketServiceIntent);
initViews();
clickEvents();
@@ -63,6 +61,13 @@ public class CaregiverDashActivity extends AppCompatActivity implements
CaregiverDataCache.getCaregiverData(this, (careGiverData) -> {
this.careGiverData = careGiverData;
if (careGiverData != null){
SocketHelper.getInstance().establishConnection(null);
String uniqueKey = careGiverData.patientId + "medActivity" + careGiverData.caregiver_xid;
SocketHelper.getInstance().initializeSocket(this, uniqueKey);
}
watchSubscription();
}, true);
@@ -109,6 +114,9 @@ public class CaregiverDashActivity extends AppCompatActivity implements
super.onDestroy();
SocketHelper.getInstance().closeConnection();
OneSignal.getNotifications().removeForegroundLifecycleListener(this);
CaregiverMainViewModel.remindersList = null;
CaregiverMainViewModel.activityList = null;
}
private void initViews() {

View File

@@ -26,5 +26,8 @@ public interface NotificationApiService {
@Query("address") String address,
@Header("Authorization") String token);
@POST("api/battery-percentage")
Call<CallResponse<Object>> notifyBatteryLow(@Body Map<String, String> body,
@Header("Authorization") String token);
}

View File

@@ -1,5 +1,10 @@
package com.app.simplitend.chats;
import static com.app.simplitend.apputils.AppUtil.NOTIFICATION_ACTION;
import static com.app.simplitend.apputils.NotificationService.CONTENT_TYPE_KEY;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
@@ -7,6 +12,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.app.simplitend.BuildConfig;
import com.app.simplitend.apputils.Constants;
import com.app.simplitend.caregiverdashboard.mvvm.CaregiverMainViewModel;
import com.app.simplitend.chats.mvvm.Author;
import com.app.simplitend.chats.mvvm.Message;
import com.app.simplitend.chats.mvvm.Receiver;
@@ -203,10 +210,42 @@ public class SocketHelper {
});
}
public void sendUpdates(String uniqueId, String contentType){
if (mSocket != null){
mSocket.emit("medActivity", contentType, uniqueId);
}
}
public void removeLocationUpdateListener(String patientId) {
mSocket.off(LOCATION_EVENT_PREFIX+patientId);
}
public void initializeSocket(Context context, String uniqueKey) {
mSocket.on(uniqueKey, args -> {
try {
if (args.length >= 1) {
String content_type = (String) args[0];
Log.d(TAG, "call: " + content_type);
Intent broadcastIntent = new Intent(NOTIFICATION_ACTION);
if ("0".equals(content_type)){
// medications
CaregiverMainViewModel.remindersList = null;
broadcastIntent.putExtra(CONTENT_TYPE_KEY, Constants.MEDICINE_TIME);
}else if ("1".equals(content_type)){
// activities
CaregiverMainViewModel.activityList = null;
broadcastIntent.putExtra(CONTENT_TYPE_KEY, Constants.ACTIVITY_TIME);
}
context.sendBroadcast(broadcastIntent);
}
} catch (Exception e) {
// do nothing
Log.d(TAG, "initializeSocket: ");
}
});
}
public interface Callback<T> {
void onMessageReceived(T result);
void onMessageSentSuccessfully();

View File

@@ -75,6 +75,37 @@ public class LocationService extends Service implements LocationClient.DefaultLo
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level * 100 / (float)scale;
if (batteryPct <= 25){
if (AppUtil.isBatteryLowNotificationSent(context)) return;
int[] chatsCred = AppUtil.getPatientGeofenceChatCred(context);
Map<String, String> 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<CallResponse<Object>>() {
@Override
public void onResponse(Call<CallResponse<Object>> call, Response<CallResponse<Object>> response) {
if (response.code() == 200){
AppUtil.setBatteryLowNotificationSent(context, true);
}else{
AppUtil.setBatteryLowNotificationSent(context, false);
}
}
@Override
public void onFailure(Call<CallResponse<Object>> call, Throwable t) {
AppUtil.setBatteryLowNotificationSent(context, false);
}
});
}else{
AppUtil.setBatteryLowNotificationSent(context, false);
}
}
}
};

View File

@@ -1,5 +1,7 @@
package com.app.simplitend.patient_dashboard;
import static com.app.simplitend.apputils.AppUtil.NOTIFICATION_ACTION;
import static com.app.simplitend.apputils.NotificationService.CONTENT_TYPE_KEY;
import static com.app.simplitend.patientgeofencing.GeoFenceHelper.GEOFENCE_TAG;
import android.Manifest;
@@ -20,18 +22,27 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.ViewModelProvider;
import com.app.simplitend.BuildConfig;
import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.Constants;
import com.app.simplitend.apputils.PatientDataCache;
import com.app.simplitend.caregiverdashboard.mvvm.CgHomeContracts;
import com.app.simplitend.caregiverdashboard.mvvm.models.GeoFenceDetails;
import io.socket.client.IO;
import io.socket.client.Socket;
public class DashBoardActivity extends AppCompatActivity implements CgHomeContracts.GetGeoFenceCallback {
private static final String TAG = "DashBoardActivity_SOCKET";
protected PatientMainViewModel viewModel;
protected ActivityResultLauncher<String> finePermissionLauncher;
private Socket mSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -83,7 +94,12 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra
}
}).launch(Manifest.permission.READ_CONTACTS);
PatientDataCache.getPatientData(this, patientData -> {
if (patientData == null) return;
String uniqueKey = patientData.caregiverId + "medActivity" + patientData.patientId;
initSocket(uniqueKey);
}, false);
}
@Override
@@ -94,6 +110,70 @@ public class DashBoardActivity extends AppCompatActivity implements CgHomeContra
super.attachBaseContext(newBase);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mSocket != null){
mSocket.disconnect();
}
PatientMainViewModel.activityList = null;
PatientMainViewModel.remindersList = null;
}
private void initSocket(String uniqueKey) {
if (mSocket != null) return;
try {
IO.Options options = new IO.Options();
options.forceNew = true;
options.reconnection = true;
mSocket = IO.socket(BuildConfig.SIMPLITEND_SOKCET_HOST, options);
mSocket.on(uniqueKey, args -> {
try {
if (args.length >= 1) {
String content_type = (String) args[0];
Log.d(TAG, "call: " + content_type);
Intent broadcastIntent = new Intent(NOTIFICATION_ACTION);
if ("0".equals(content_type)){
// medications
PatientMainViewModel.remindersList = null;
broadcastIntent.putExtra(CONTENT_TYPE_KEY, Constants.MEDICINE_TIME);
}else if ("1".equals(content_type)){
// activities
PatientMainViewModel.activityList = null;
broadcastIntent.putExtra(CONTENT_TYPE_KEY, Constants.ACTIVITY_TIME);
}
sendBroadcast(broadcastIntent);
}
} catch (Exception e) {
// do nothing
Log.d(TAG, "initializeSocket: ");
}
});
mSocket.on(Socket.EVENT_CONNECT, args -> {
Log.d(TAG, "Socket connected ");
});
mSocket.on(Socket.EVENT_DISCONNECT, args -> {
Log.d(TAG, "Socket disconnected ");
});
mSocket.on(Socket.EVENT_CONNECT_ERROR, args -> {
Exception e = (Exception) args[0];
Log.e(TAG, "call: ", e);
});
mSocket.connect();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "SocketHelper: ", e);
}
}
// saves the geofence details
private void updateGeofenceDetails() {
// retrieving geofence

View File

@@ -126,9 +126,17 @@ public class PatientDashboardFragment extends Fragment implements ProfileContrac
clickEvents();
loadReminders();
if (PatientMainViewModel.remindersList == null){
loadReminders();
}else {
onRemindersListFetched(PatientMainViewModel.remindersList);
}
loadActivities();
if (PatientMainViewModel.activityList == null){
loadActivities();
}else {
onRoutinesFetched(PatientMainViewModel.activityList);
}
notification_receiver = new BroadcastReceiver() {
@Override

View File

@@ -773,6 +773,12 @@ public class AddReminderFragment extends Fragment implements CompoundButton.OnCh
public void onReminderAdded(ReminderResult reminderDetails) {
progressDialog.dismiss();
try {
viewModel.sendReminderUpdatedMessage(requireContext());
} catch (Exception e) {
// do nothing
}
if (reminder == null) {
Toast.makeText(requireContext(), "Reminder added successfully.", Toast.LENGTH_SHORT).show();

View File

@@ -328,16 +328,15 @@ public class ReminderFragment extends Fragment implements RecyclerTouchListener.
public void onRemindersListFetched(List<ReminderResult> reminderResultList) {
progressDialog.dismiss();
// updating global list of reminders
if (reminderViewModel.selected_dow == 0){
// only updating global list of reminders with current day's reminder list
CaregiverMainViewModel.remindersList = reminderResultList;
PatientMainViewModel.remindersList = reminderResultList;
}
if (reminderResultList != null && reminderResultList.size() > 0) {
// reminders are present
// updating global list of reminders
if (reminderViewModel.selected_dow == 0){
// only updating global list of reminders with current day's reminder list
CaregiverMainViewModel.remindersList = reminderResultList;
PatientMainViewModel.remindersList = reminderResultList;
}
binding.remindersRv.setVisibility(View.VISIBLE);
binding.noData.setVisibility(View.GONE);
@@ -370,6 +369,11 @@ public class ReminderFragment extends Fragment implements RecyclerTouchListener.
Toast.makeText(requireContext(), "Reminder deleted.", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
loadReminderList(weekDayViewsList.get(reminderViewModel.selected_dow).day_of_week);
try {
reminderViewModel.sendReminderUpdatedMessage(requireContext());
} catch (Exception e) {
// do nothing
}
}
@Override
@@ -453,6 +457,11 @@ public class ReminderFragment extends Fragment implements RecyclerTouchListener.
public void onReminderAdded(ReminderResult reminderDetails) {
progressDialog.dismiss();
loadReminderList(weekDayViewsList.get(reminderViewModel.selected_dow).day_of_week);
try {
reminderViewModel.sendReminderUpdatedMessage(requireContext());
} catch (Exception e) {
// do nothing
}
}
@Override

View File

@@ -1,8 +1,14 @@
package com.app.simplitend.patientprofile.medreminder.mvvm;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.apputils.PatientDataCache;
import com.app.simplitend.chats.SocketHelper;
import com.app.simplitend.patientprofile.ProfileContracts;
import com.app.simplitend.patientprofile.medreminder.mvvm.models.ReminderResult;
@@ -96,6 +102,60 @@ public class ReminderViewModel extends ViewModel {
return "";
}
// updating caregiver or senior about changes in reminders
public void sendReminderUpdatedMessage(Context context){
if (AppUtil.isPatientLoggedIn(context)){
// senior logged in
PatientDataCache.getPatientData(context, patientData -> {
if (patientData == null){
return;
}
String uniqueId = patientData.patientId + "medActivity" + patientData.caregiverId;
SocketHelper.getInstance().establishConnection(new SocketHelper.SockCallBack() {
@Override
public void onSocketConnected() {
SocketHelper.getInstance().sendUpdates(uniqueId, "0");
}
@Override
public void onConnectionError(Exception e) {
}
@Override
public void onDisconnected() {
}
});
}, false);
}else{
// caregiver logged in
CaregiverDataCache.getCaregiverData(context, careGiverData -> {
if (careGiverData == null){
return;
}
String uniqueId = careGiverData.caregiver_xid + "medActivity" + careGiverData.patientId;
SocketHelper.getInstance().establishConnection(new SocketHelper.SockCallBack() {
@Override
public void onSocketConnected() {
SocketHelper.getInstance().sendUpdates(uniqueId, "0");
}
@Override
public void onConnectionError(Exception e) {
}
@Override
public void onDisconnected() {
}
});
}, false);
}
}
}

View File

@@ -475,6 +475,12 @@ public class AddRoutineFragment extends Fragment implements CompoundButton.OnChe
public void onRoutineAdded(RoutineDetails medicationInfo) {
progressDialog.dismiss();
try {
routineViewModel.sendReminderUpdatedMessage(requireContext());
} catch (Exception e) {
// do nothing
}
if (this.routine == null) {
Toast.makeText(requireContext(), "Activity saved successfully.", Toast.LENGTH_SHORT).show();

View File

@@ -321,6 +321,12 @@ public class RoutineFragment extends Fragment implements RoutineAdapter.ClickLis
Toast.makeText(requireContext(), "Reminder deleted.", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
loadRoutineList(weekDayViewsList.get(routineViewModel.selected_dow).day_of_week);
try {
routineViewModel.sendReminderUpdatedMessage(requireContext());
} catch (Exception e) {
// do nothing
}
}
@Override
@@ -341,15 +347,15 @@ public class RoutineFragment extends Fragment implements RoutineAdapter.ClickLis
public void onRoutinesFetched(List<RoutineDetails> routineList) {
progressDialog.dismiss();
// updating global list of activities for today's day
if (routineViewModel.selected_dow == 0) {
CaregiverMainViewModel.activityList = routineList;
PatientMainViewModel.activityList = routineList;
}
if (routineList != null && routineList.size() > 0) {
// reminders are present
// updating global list of activities for today's day
if (routineViewModel.selected_dow == 0) {
CaregiverMainViewModel.activityList = routineList;
PatientMainViewModel.activityList = routineList;
}
binding.routineRv.setVisibility(View.VISIBLE);
binding.noData.setVisibility(View.GONE);
@@ -507,6 +513,11 @@ public class RoutineFragment extends Fragment implements RoutineAdapter.ClickLis
public void onRoutineAdded(RoutineDetails medicationInfo) {
progressDialog.dismiss();
loadRoutineList(weekDayViewsList.get(routineViewModel.selected_dow).day_of_week);
try {
routineViewModel.sendReminderUpdatedMessage(requireContext());
} catch (Exception e) {
// do nothing
}
}
@Override

View File

@@ -1,8 +1,14 @@
package com.app.simplitend.patientprofile.setuproutine.mvvm;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.apputils.PatientDataCache;
import com.app.simplitend.chats.SocketHelper;
import com.app.simplitend.patientprofile.ProfileContracts;
public class RoutineViewModel extends ViewModel {
@@ -93,4 +99,60 @@ public class RoutineViewModel extends ViewModel {
return "";
}
// updating caregiver or senior about changes in reminders
public void sendReminderUpdatedMessage(Context context){
if (AppUtil.isPatientLoggedIn(context)){
// senior logged in
PatientDataCache.getPatientData(context, patientData -> {
if (patientData == null){
return;
}
String uniqueId = patientData.patientId + "medActivity" + patientData.caregiverId;
SocketHelper.getInstance().establishConnection(new SocketHelper.SockCallBack() {
@Override
public void onSocketConnected() {
SocketHelper.getInstance().sendUpdates(uniqueId, "1");
}
@Override
public void onConnectionError(Exception e) {
}
@Override
public void onDisconnected() {
}
});
}, false);
}else{
// caregiver logged in
CaregiverDataCache.getCaregiverData(context, careGiverData -> {
if (careGiverData == null){
return;
}
String uniqueId = careGiverData.caregiver_xid + "medActivity" + careGiverData.patientId;
SocketHelper.getInstance().establishConnection(new SocketHelper.SockCallBack() {
@Override
public void onSocketConnected() {
SocketHelper.getInstance().sendUpdates(uniqueId, "1");
}
@Override
public void onConnectionError(Exception e) {
}
@Override
public void onDisconnected() {
}
});
}, false);
}
}
}