Skip to content

Commit

Permalink
Merge pull request getodk#2377 from jd-alexander/sms_status_fix
Browse files Browse the repository at this point in the history
Resolved SMS/internet submission status issue
  • Loading branch information
lognaturel authored Jul 18, 2018
2 parents 93937e9 + f966cdc commit 68fd1d1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.odk.collect.android.tasks.InstanceSyncTask;
import org.odk.collect.android.tasks.sms.SmsNotificationReceiver;
import org.odk.collect.android.tasks.sms.SmsService;
import org.odk.collect.android.tasks.sms.contracts.SmsSubmissionManagerContract;
import org.odk.collect.android.tasks.sms.models.SmsSubmission;
import org.odk.collect.android.utilities.PlayServicesUtil;
import org.odk.collect.android.utilities.ToastUtils;

Expand All @@ -58,6 +60,7 @@

import static org.odk.collect.android.preferences.PreferenceKeys.KEY_PROTOCOL;
import static org.odk.collect.android.preferences.PreferenceKeys.KEY_SUBMISSION_TRANSPORT_TYPE;
import static org.odk.collect.android.tasks.sms.SmsSender.SMS_INSTANCE_ID;
import static org.odk.collect.android.utilities.PermissionUtils.finishAllActivities;
import static org.odk.collect.android.utilities.PermissionUtils.requestStoragePermissions;

Expand Down Expand Up @@ -89,11 +92,18 @@ public class InstanceUploaderList extends InstanceListActivity implements
public void onReceive(Context context, Intent intent) {
//Stops the notification from being sent to others that are listening for this broadcast.
abortBroadcast();

//deletes submission since the app is in the foreground and the NotificationReceiver won't be triggered.
if (intent.hasExtra(SMS_INSTANCE_ID)) {
deleteIfSubmissionCompleted(intent.getStringExtra(SMS_INSTANCE_ID));
}
}
};

@Inject
SmsService smsService;
@Inject
SmsSubmissionManagerContract smsSubmissionManager;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -444,4 +454,11 @@ protected void onDestroy() {
((InstanceUploaderAdapter) listAdapter).onDestroy();
}
}

private void deleteIfSubmissionCompleted(String instanceId) {
SmsSubmission model = smsSubmissionManager.getSubmissionModel(instanceId);
if (model.isSubmissionComplete()) {
smsSubmissionManager.forgetSubmission(model.getInstanceId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.events.RxEventBus;
import org.odk.collect.android.events.SmsRxEvent;
import org.odk.collect.android.preferences.GeneralSharedPreferences;
import org.odk.collect.android.provider.InstanceProviderAPI;
import org.odk.collect.android.tasks.sms.SmsService;
import org.odk.collect.android.tasks.sms.contracts.SmsSubmissionManagerContract;
Expand All @@ -32,11 +33,12 @@
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

import static org.odk.collect.android.preferences.PreferenceKeys.KEY_SUBMISSION_TRANSPORT_TYPE;
import static org.odk.collect.android.provider.InstanceProviderAPI.STATUS_SUBMISSION_FAILED;
import static org.odk.collect.android.provider.InstanceProviderAPI.STATUS_SUBMITTED;
import static org.odk.collect.android.tasks.sms.SmsService.RESULT_MESSAGE_READY;
import static org.odk.collect.android.tasks.sms.SmsService.RESULT_QUEUED;
import static org.odk.collect.android.tasks.sms.SmsService.RESULT_OK_OTHERS_PENDING;
import static org.odk.collect.android.tasks.sms.SmsService.RESULT_QUEUED;
import static org.odk.collect.android.tasks.sms.SmsService.RESULT_SENDING;
import static org.odk.collect.android.tasks.sms.SmsService.getDisplaySubtext;

Expand Down Expand Up @@ -88,7 +90,9 @@ public void bindView(View view, Context context, Cursor cursor) {

SmsSubmission model = submissionManager.getSubmissionModel(String.valueOf(instanceId));

boolean isSmsSubmission = model != null;
boolean smsTransportEnabled = ((String) GeneralSharedPreferences.getInstance().get(KEY_SUBMISSION_TRANSPORT_TYPE)).equalsIgnoreCase(context.getString(R.string.transport_type_value_sms));

boolean isSmsSubmission = model != null && smsTransportEnabled;

String status = cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.STATUS));

Expand All @@ -112,15 +116,7 @@ public void bindView(View view, Context context, Cursor cursor) {

if (isSmsSubmission) {
viewHolder.progressBar.setProgressPercent((int) model.getCompletion().getPercentage(), false);
} else {
if (status.equals(STATUS_SUBMITTED)) {
viewHolder.progressBar.setProgressPercent(100, false);
} else if (status.equals(STATUS_SUBMISSION_FAILED)) {
viewHolder.progressBar.setProgressPercent(50, false);
}
}

if (isSmsSubmission) {
int smsStatus = submissionManager.checkNextMessageResultCode(String.valueOf(instanceId));

setSmsSubmissionStateIcons(smsStatus, viewHolder);
Expand All @@ -134,6 +130,12 @@ public void bindView(View view, Context context, Cursor cursor) {

setupCloseButton(viewHolder, smsStatus);
viewHolder.closeButton.setOnClickListener(v -> smsService.cancelFormSubmission(String.valueOf(instanceId)));
} else {
if (status.equals(STATUS_SUBMITTED)) {
viewHolder.progressBar.setProgressPercent(100, false);
} else if (status.equals(STATUS_SUBMISSION_FAILED)) {
viewHolder.progressBar.setProgressPercent(50, false);
}
}

compositeDisposable.add(eventBus.register(SmsRxEvent.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void onReceive(Context context, Intent intent) {
notificationManager = NotificationManagerCompat.from(context);

sendBundledNotification();
deleteIfSubmissionCompleted();
}

void sendBundledNotification() {
Expand Down Expand Up @@ -102,6 +103,16 @@ private Notification buildSummary() {
.build();
}

/**
* Once the submission is completed, it's deleted here since this is the final point
* at which it's data is needed.
*/
private void deleteIfSubmissionCompleted() {
if (smsSubmission.isSubmissionComplete()) {
submissionManagerContract.forgetSubmission(smsSubmission.getInstanceId());
}
}

private String getContentText() {
Date date = smsSubmission.getLastUpdated();
SmsProgress progress = smsSubmission.getCompletion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SmsSender {
private final String instanceId;

public static final String SMS_SEND_ACTION = "org.odk.collect.android.COLLECT_SMS_SEND_ACTION";
static final String SMS_INSTANCE_ID = "COLLECT_SMS_INSTANCE_ID";
public static final String SMS_INSTANCE_ID = "COLLECT_SMS_INSTANCE_ID";
static final String SMS_MESSAGE_ID = "COLLECT_SMS_MESSAGE_ID";
static final String SMS_RESULT_CODE = "COLLECT_SMS_RESULT_CODE";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void submitForms(long[] instanceIds) {
* background job.
*/
public boolean submitForm(String instanceId, FormInfo info, String displayName) {

String text;

if (formsDao.isFormEncrypted(info.getFormID(), info.getFormVersion())) {
Expand Down Expand Up @@ -164,20 +163,10 @@ public boolean submitForm(String instanceId, FormInfo info, String displayName)

SmsSubmission model = smsSubmissionManager.getSubmissionModel(instanceId);

/*
* Checks to see if a previous instance was sent successfully. If so
* remove that instance so a new one can be sent.
* */
if (model != null && model.isSubmissionComplete()) {
model = null;
smsSubmissionManager.forgetSubmission(instanceId);
}

/*
* If the model exists that means this instance was probably sent in the past.
*/
if (model != null) {

/*
* If the background job for this instance is running then the messages won't be sent again.
*/
Expand All @@ -191,7 +180,6 @@ public boolean submitForm(String instanceId, FormInfo info, String displayName)
}

for (Message message : model.getMessages()) {

/*
* If there's a message that hasn't sent then a job should be added to continue the process.
*/
Expand All @@ -201,7 +189,6 @@ public boolean submitForm(String instanceId, FormInfo info, String displayName)
}
}
} else {

final List<String> parts = smsManager.divideMessage(text);

model = new SmsSubmission();
Expand Down Expand Up @@ -297,7 +284,7 @@ void processMessageSentResult(String instanceId, int messageId, int resultCode)
* so that error message is persisted along with SUBMISSION_STATUS_FAILED
* */
if (model.isSubmissionComplete()) {
markInstanceAsSubmittedOrDelete(instanceId);
markInstanceAsSubmittedOrDelete(instanceId, model.getCompletion(), model.getLastUpdated(), context);
} else if (resultCode != RESULT_OK && resultCode != RESULT_OK_OTHERS_PENDING) {
updateInstanceStatusFailedText(instanceId, event);
}
Expand All @@ -311,7 +298,6 @@ void processMessageSentResult(String instanceId, int messageId, int resultCode)
* @param instanceId from instanceDao
*/
protected void startSendMessagesJob(String instanceId) {

PersistableBundleCompat extras = new PersistableBundleCompat();
extras.putString(SmsSenderJob.INSTANCE_ID, instanceId);

Expand All @@ -329,11 +315,12 @@ protected void startSendMessagesJob(String instanceId) {
rxEventBus.post(new SmsRxEvent(instanceId, RESULT_QUEUED));
}

private void markInstanceAsSubmittedOrDelete(String instanceId) {
private void markInstanceAsSubmittedOrDelete(String instanceId, SmsProgress progress, Date date, Context context) {
String where = InstanceProviderAPI.InstanceColumns._ID + "=?";
String[] whereArgs = {instanceId};

ContentValues contentValues = new ContentValues();
contentValues.put(InstanceProviderAPI.InstanceColumns.DISPLAY_SUBTEXT, getDisplaySubtext(RESULT_OK, date, progress, context));
contentValues.put(InstanceProviderAPI.InstanceColumns.STATUS, InstanceProviderAPI.STATUS_SUBMITTED);

Collect.getInstance()
Expand Down

0 comments on commit 68fd1d1

Please sign in to comment.