• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-client-core / #173

pending completion
#173

Pull #919

github-actions

web-flow
Merge a10bd445f into 5668d546b
Pull Request #919: v5 - Set Locales When Formatting Dates

18784 of 26961 relevant lines covered (69.67%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.21
opensrp-core/src/main/java/org/smartregister/view/activity/SecuredActivity.java
1
package org.smartregister.view.activity;
2

3
import static org.smartregister.AllConstants.ALERT_NAME_PARAM;
4
import static org.smartregister.AllConstants.ENTITY_ID;
5
import static org.smartregister.AllConstants.ENTITY_ID_PARAM;
6
import static org.smartregister.AllConstants.FIELD_OVERRIDES_PARAM;
7
import static org.smartregister.AllConstants.FORM_NAME_PARAM;
8
import static org.smartregister.AllConstants.FORM_SUCCESSFULLY_SUBMITTED_RESULT_CODE;
9
import static org.smartregister.event.Event.ON_LOGOUT;
10

11
import android.content.Intent;
12
import android.content.IntentFilter;
13
import android.os.Bundle;
14
import android.view.Menu;
15
import android.view.MenuInflater;
16
import android.view.MenuItem;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.view.ViewTreeObserver;
20

21
import androidx.annotation.NonNull;
22
import androidx.appcompat.app.AppCompatActivity;
23
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
24

25
import com.google.android.material.snackbar.BaseTransientBottomBar;
26
import com.google.gson.Gson;
27
import com.google.gson.reflect.TypeToken;
28

29
import org.apache.commons.collections.MapUtils;
30
import org.smartregister.AllConstants;
31
import org.smartregister.Context;
32
import org.smartregister.CoreLibrary;
33
import org.smartregister.R;
34
import org.smartregister.broadcastreceivers.OpenSRPClientBroadCastReceiver;
35
import org.smartregister.event.Listener;
36
import org.smartregister.receiver.P2pProcessingStatusBroadcastReceiver;
37
import org.smartregister.util.Utils;
38
import org.smartregister.view.controller.ANMController;
39
import org.smartregister.view.controller.FormController;
40
import org.smartregister.view.controller.NavigationController;
41
import org.smartregister.view.customcontrols.ProcessingInProgressSnackbar;
42

43
import java.util.Map;
44

45
import timber.log.Timber;
46

47
public abstract class SecuredActivity extends MultiLanguageActivity implements P2pProcessingStatusBroadcastReceiver.StatusUpdate {
1✔
48
    public static final String LOG_TAG = "SecuredActivity";
49
    protected final int MENU_ITEM_LOGOUT = 2312;
1✔
50
    protected Listener<Boolean> logoutListener;
51
    protected FormController formController;
52
    protected ANMController anmController;
53
    protected NavigationController navigationController;
54
    private String metaData;
55
    private OpenSRPClientBroadCastReceiver openSRPClientBroadCastReceiver;
56

57
    private ProcessingInProgressSnackbar processingInProgressSnackbar;
58
    private P2pProcessingStatusBroadcastReceiver p2pProcessingStatusBroadcastReceiver;
59

60
    @Override
61
    protected void onCreate(Bundle savedInstanceState) {
62
        super.onCreate(savedInstanceState);
1✔
63

64
        logoutListener = new Listener<Boolean>() {
1✔
65
            public void onEvent(Boolean data) {
66
                finish();
×
67
            }
×
68
        };
69
        ON_LOGOUT.addListener(logoutListener);
1✔
70

71
        if (context().IsUserLoggedOut()) {
1✔
72
            DrishtiApplication application = (DrishtiApplication) getApplication();
1✔
73
            application.logoutCurrentUser();
1✔
74
            return;
1✔
75
        }
76

77
        formController = new FormController(this);
1✔
78
        anmController = context().anmController();
1✔
79
        navigationController = new NavigationController(this, anmController);
1✔
80
        onCreation();
1✔
81

82
        // Intent replicationServiceIntent = new Intent(this, ReplicationIntentService.class);
83
        //startService(replicationServiceIntent);
84
    }
1✔
85

86
    @Override
87
    protected void onResume() {
88
        super.onResume();
1✔
89
        if (context().IsUserLoggedOut()) {
1✔
90
            DrishtiApplication application = (DrishtiApplication) getApplication();
1✔
91
            application.logoutCurrentUser();
1✔
92
            return;
1✔
93
        }
94

95
        onResumption();
1✔
96
        setupReplicationBroadcastReceiver();
1✔
97

98
        if (CoreLibrary.getInstance().getP2POptions() != null
1✔
99
                && CoreLibrary.getInstance().getP2POptions().isEnableP2PLibrary()) {
1✔
100
            if (p2pProcessingStatusBroadcastReceiver == null) {
1✔
101
                p2pProcessingStatusBroadcastReceiver = new P2pProcessingStatusBroadcastReceiver(this);
1✔
102
            }
103

104
            // Register listener to remove the SnackBar
105
            LocalBroadcastManager.getInstance(this)
1✔
106
                    .registerReceiver(p2pProcessingStatusBroadcastReceiver
1✔
107
                            , new IntentFilter(AllConstants.PeerToPeer.PROCESSING_ACTION));
108

109
            if (CoreLibrary.getInstance().isPeerToPeerProcessing()) {
1✔
110
                showProcessingInProgressBottomSnackbar(this);
×
111
            }
112
        }
113
    }
1✔
114

115
    @Override
116
    public boolean onOptionsItemSelected(MenuItem item) {
117
        int i = item.getItemId();
1✔
118
        if (i == R.id.switchLanguageMenuItem) {
1✔
119
            String newLanguagePreference = context().userService().switchLanguagePreference();
1✔
120
            Utils.showShortToast(this, getString(R.string.language_change_prepend_message) + " " + newLanguagePreference + ".");
1✔
121

122
            return super.onOptionsItemSelected(item);
1✔
123
        } else if (i == MENU_ITEM_LOGOUT) {
1✔
124
            DrishtiApplication application = (DrishtiApplication) getApplication();
1✔
125
            application.logoutCurrentUser();
1✔
126

127
            return super.onOptionsItemSelected(item);
1✔
128
        } else {
129
            return super.onOptionsItemSelected(item);
×
130
        }
131
    }
132

133
    /**
134
     * Attaches a logout menu item to the provided menu
135
     *
136
     * @param menu The menu to attach the logout menu item
137
     */
138
    protected void attachLogoutMenuItem(Menu menu) {
139
        menu.add(0, MENU_ITEM_LOGOUT, menu.size(), R.string.logout_text);
1✔
140
    }
1✔
141

142
    @Override
143
    public boolean onCreateOptionsMenu(Menu menu) {
144
        MenuInflater inflater = getMenuInflater();
1✔
145
        inflater.inflate(R.menu.main_menu, menu);
1✔
146
        attachLogoutMenuItem(menu);
1✔
147
        return true;
1✔
148
    }
149

150
    @Override
151
    protected void onDestroy() {
152
        super.onDestroy();
1✔
153
    }
1✔
154

155
    protected abstract void onCreation();
156

157
    protected abstract void onResumption();
158

159
    public void startFormActivity(String formName, String entityId, String metaData) {
160
        launchForm(formName, entityId, metaData, FormActivity.class);
1✔
161
    }
1✔
162

163
    public void startFormActivity(String formName, String entityId, Map<String, String> metaData) {
164
        String metaDataString = MapUtils.getString(metaData, FIELD_OVERRIDES_PARAM, "");
×
165

166
        launchForm(formName, entityId, metaDataString, FormActivity.class);
×
167
    }
×
168

169
    public void startMicroFormActivity(String formName, String entityId, String metaData) {
170
        launchForm(formName, entityId, metaData, MicroFormActivity.class);
×
171
    }
×
172

173
    private void launchForm(String formName, String entityId, String metaData, Class formType) {
174
        this.metaData = metaData;
1✔
175

176
        Intent intent = new Intent(this, formType);
1✔
177
        intent.putExtra(FORM_NAME_PARAM, formName);
1✔
178
        intent.putExtra(ENTITY_ID_PARAM, entityId);
1✔
179
        addFieldOverridesIfExist(intent);
1✔
180
        startActivityForResult(intent, FORM_SUCCESSFULLY_SUBMITTED_RESULT_CODE);
1✔
181
    }
1✔
182

183
    private void addFieldOverridesIfExist(Intent intent) {
184
        if (hasMetadata()) {
1✔
185
            Map<String, String> metaDataMap = new Gson()
1✔
186
                    .fromJson(this.metaData, new TypeToken<Map<String, String>>() {
1✔
187
                    }.getType());
1✔
188
            if (metaDataMap.containsKey(FIELD_OVERRIDES_PARAM)) {
1✔
189
                intent.putExtra(FIELD_OVERRIDES_PARAM, metaDataMap.get(FIELD_OVERRIDES_PARAM));
1✔
190
            }
191
        }
192
    }
1✔
193

194
    @Override
195
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
196
        super.onActivityResult(requestCode, resultCode, data);
1✔
197
        if (isSuccessfulFormSubmission(resultCode)) {
1✔
198
            Timber.i("Form successfully saved. MetaData: %s", metaData);
1✔
199
            if (hasMetadata()) {
1✔
200
                Map<String, String> metaDataMap = new Gson()
1✔
201
                        .fromJson(metaData, new TypeToken<Map<String, String>>() {
1✔
202
                        }.getType());
1✔
203
                if (metaDataMap.containsKey(ENTITY_ID) && metaDataMap
1✔
204
                        .containsKey(ALERT_NAME_PARAM)) {
1✔
205
                    CoreLibrary.getInstance().context().alertService()
1✔
206
                            .changeAlertStatusToInProcess(metaDataMap.get(ENTITY_ID),
1✔
207
                                    metaDataMap.get(ALERT_NAME_PARAM));
1✔
208
                }
209
            }
210
        }
211
    }
1✔
212

213
    private boolean isSuccessfulFormSubmission(int resultCode) {
214
        return resultCode == FORM_SUCCESSFULLY_SUBMITTED_RESULT_CODE;
1✔
215
    }
216

217
    private boolean hasMetadata() {
218
        return this.metaData != null && !this.metaData.equalsIgnoreCase("undefined");
1✔
219
    }
220

221
    /**
222
     * Called by TasksModel when it receives a replication error callback.
223
     * TasksModel takes care of calling this on the main thread.
224
     */
225
    public void replicationError() {
226
        Timber.e("%s error()", LOG_TAG);
×
227
        //Toast.makeText(getApplicationContext(), "Replication Error", Toast.LENGTH_LONG).show();
228
    }
×
229

230
    private void setupReplicationBroadcastReceiver() {
231
        // The filter's action is BROADCAST_ACTION
232
        IntentFilter opensrpClientIntentFilter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
1✔
233
        opensrpClientIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
1✔
234
        opensrpClientIntentFilter.addAction(Intent.ACTION_DATE_CHANGED);
1✔
235

236
        openSRPClientBroadCastReceiver = new OpenSRPClientBroadCastReceiver(this);
1✔
237
        // Registers the OpenSRPClientBroadCastReceiver and its intent filters
238
        registerReceiver(openSRPClientBroadCastReceiver, opensrpClientIntentFilter);
1✔
239
    }
1✔
240

241
    public void showToast(String message) {
242
        Utils.showToast(this, message);
×
243
    }
×
244

245
    protected Context context() {
246
        return CoreLibrary.getInstance().context().updateApplicationContext(this.getApplicationContext());
1✔
247
    }
248

249
    public void showProcessingInProgressSnackbar(@NonNull AppCompatActivity appCompatActivity, int margin) {
250
        if (processingInProgressSnackbar == null) {
1✔
251
            // Create the snackbar
252
            View parentView = ((ViewGroup) appCompatActivity.findViewById(android.R.id.content))
×
253
                    .getChildAt(0);
×
254

255
            processingInProgressSnackbar = ProcessingInProgressSnackbar.make(parentView);
×
256

257
            if (margin != 0) {
×
258
                processingInProgressSnackbar.addBottomBarMargin(margin);
×
259
            }
260
            processingInProgressSnackbar.setDuration(BaseTransientBottomBar.LENGTH_INDEFINITE);
×
261
            processingInProgressSnackbar.show();
×
262
        } else if (!processingInProgressSnackbar.isShown()) {
1✔
263
            processingInProgressSnackbar.show();
1✔
264
        }
265
    }
1✔
266

267
    public void showProcessingInProgressBottomSnackbar(final @NonNull AppCompatActivity appCompatActivity) {
268
        final View bottomNavigationBar = appCompatActivity.findViewById(R.id.bottom_navigation);
×
269
        if (bottomNavigationBar != null) {
×
270
            bottomNavigationBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
×
271
                @Override
272
                public void onGlobalLayout() {
273
                    int height = bottomNavigationBar.getHeight();
×
274
                    showProcessingInProgressSnackbar(appCompatActivity, height);
×
275

276
                    bottomNavigationBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
×
277
                }
×
278
            });
279
        } else {
280
            showProcessingInProgressSnackbar(appCompatActivity, 0);
×
281
        }
282
    }
×
283

284
    public void removeProcessingInProgressSnackbar() {
285
        if (processingInProgressSnackbar != null && processingInProgressSnackbar.isShown()) {
1✔
286
            processingInProgressSnackbar.dismiss();
1✔
287
        }
288
    }
1✔
289

290
    @Override
291
    public void onStatusUpdate(boolean isProcessing) {
292
        if (isProcessing) {
1✔
293
            showProcessingInProgressBottomSnackbar(this);
1✔
294
        } else {
295
            removeProcessingInProgressSnackbar();
1✔
296
        }
297
    }
1✔
298

299
    @Override
300
    protected void onPause() {
301

302
        super.onPause();
1✔
303

304
        if (openSRPClientBroadCastReceiver != null) {
1✔
305
            try {
306
                unregisterReceiver(openSRPClientBroadCastReceiver);
1✔
307
                openSRPClientBroadCastReceiver = null;
1✔
308
            } catch (IllegalArgumentException e) {
×
309
                Timber.e(e, "EXCEPTION %s", e.toString());
×
310
            }
1✔
311
        }
312

313
        if (p2pProcessingStatusBroadcastReceiver != null) {
1✔
314
            LocalBroadcastManager.getInstance(this)
1✔
315
                    .unregisterReceiver(p2pProcessingStatusBroadcastReceiver);
1✔
316
        }
317

318
        removeProcessingInProgressSnackbar();
1✔
319
    }
1✔
320
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc