Skip to content

Commit

Permalink
Merge pull request #958 from akvo/issue/953-revert-923
Browse files Browse the repository at this point in the history
[#923] Reverted reload all surveys is back to its original version
  • Loading branch information
muloem authored Dec 5, 2017
2 parents a89e017 + cb06f59 commit 0b9032d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.akvo.flow.presentation.settings;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
Expand Down Expand Up @@ -239,10 +240,33 @@ public void onClick(DialogInterface dialog, int whichButton) {
});
}

@OnClick(R.id.preference_reload_forms_title)
@OnClick({R.id.preference_reload_forms_title,
R.id.preference_reload_forms_subtitle
})
void onReloadAllSurveysOptionTap() {
ReloadFormsConfirmationDialog dialog = ReloadFormsConfirmationDialog.newInstance();
dialog.show(getSupportFragmentManager(), ReloadFormsConfirmationDialog.TAG);
ViewUtil.showAdminAuthDialog(this, new ViewUtil.AdminAuthDialogListener() {
@Override
public void onAuthenticated() {
AlertDialog.Builder builder = new AlertDialog.Builder(PreferenceActivity.this);
builder.setTitle(R.string.conftitle);
builder.setMessage(R.string.reloadconftext);
builder.setPositiveButton(R.string.okbutton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Context c = PreferenceActivity.this;
Intent i = new Intent(c, SurveyDownloadService.class);
i.putExtra(SurveyDownloadService.EXTRA_DELETE_SURVEYS, true);
c.startService(i);
}
});
builder.setNegativeButton(R.string.cancelbutton,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
}
});
}

@OnClick(R.id.preference_gps_fixes)
Expand Down

This file was deleted.

90 changes: 47 additions & 43 deletions app/src/main/java/org/akvo/flow/service/SurveyDownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class SurveyDownloadService extends IntentService {
* Intent parameter to specify which survey needs to be downloaded
*/
public static final String EXTRA_SURVEY_ID = "survey";
public static final String EXTRA_DELETE_SURVEYS = "delete_surveys";

private static final String DEFAULT_TYPE = "Survey";
public static final String TEST_SURVEY_ID = "0";
Expand All @@ -91,9 +92,11 @@ public void onHandleIntent(@Nullable Intent intent) {
prefs = new Prefs(getApplicationContext());
connectivityStateManager = new ConnectivityStateManager(getApplicationContext());
if (intent != null && intent.hasExtra(EXTRA_SURVEY_ID)) {
installSurvey(intent);
downloadSurvey(intent);
} else if (intent != null && intent.getBooleanExtra(EXTRA_DELETE_SURVEYS, false)) {
reDownloadAllSurveys(intent);
} else {
downloadAndInstallSurveys();
checkAndDownload(null);
}
} catch (Exception e) {
Timber.e(e, e.getMessage());
Expand All @@ -103,53 +106,53 @@ public void onHandleIntent(@Nullable Intent intent) {
}
}

private void downloadAndInstallSurveys() {
if (!isConnectionAvailable()) {
return;
}
List<Survey> surveys = checkForSurveys();
updateSurveys(surveys);
private void reDownloadAllSurveys(@NonNull Intent intent) {
intent.removeExtra(EXTRA_DELETE_SURVEYS);
String[] surveyIds = databaseAdaptor.getSurveyIds();
databaseAdaptor.deleteAllSurveys();
checkAndDownload(surveyIds);
}

private void installSurvey(@NonNull Intent intent) {
private void downloadSurvey(@NonNull Intent intent) {
String surveyId = intent.getStringExtra(EXTRA_SURVEY_ID);
intent.removeExtra(EXTRA_SURVEY_ID);
if (TEST_SURVEY_ID.equals(surveyId)) {
installTestSurvey();
databaseAdaptor.reinstallTestSurvey();
} else {
downloadAndInstallSurvey(surveyId);
checkAndDownload(new String[] { surveyId });
}
}

private void downloadAndInstallSurvey(String surveyId) {
if (!isConnectionAvailable()) {
/**
* if no surveyIds are passed in, this will check for new surveys and, if
* there are some new ones, downloads them to the DATA_DIR. If surveyIds are
* passed in, then those specific surveys will be downloaded. If they're already
* on the device, the surveys will be replaced with the new ones.
*/
private void checkAndDownload(@Nullable String[] surveyIds) {
if (!connectivityStateManager.isConnectionAvailable(
prefs.getBoolean(Prefs.KEY_CELL_UPLOAD, Prefs.DEFAULT_VALUE_CELL_UPLOAD))) {
//No internet or not allowed to sync
return;
}
List<Survey>surveys = getSurveyHeaders(surveyId);
updateSurveys(surveys);
}

private void installTestSurvey() {
databaseAdaptor.reinstallTestSurvey();
}

private boolean isConnectionAvailable() {
return connectivityStateManager.isConnectionAvailable(
prefs.getBoolean(Prefs.KEY_CELL_UPLOAD, Prefs.DEFAULT_VALUE_CELL_UPLOAD));
}
List<Survey> surveys;
if (surveyIds != null && surveyIds.length > 0) {
surveys = getSurveyHeaders(surveyIds);
} else {
surveys = checkForSurveys();
}

private void updateSurveys(List<Survey> surveys) {
// Update all survey groups
syncSurveyGroups(surveys);

// Check synced versions, and omit up-to-date surveys
List<Survey> outDatedSurveys = databaseAdaptor.checkSurveyVersions(surveys);
surveys = databaseAdaptor.checkSurveyVersions(surveys);

if (!outDatedSurveys.isEmpty()) {
if (!surveys.isEmpty()) {
int synced = 0, failed = 0;
int numberOfSurveysToBeSynced = outDatedSurveys.size();
displayNotification(synced, failed, numberOfSurveysToBeSynced);
for (Survey survey : outDatedSurveys) {
displayNotification(synced, failed, surveys.size());
for (Survey survey : surveys) {
try {
downloadSurvey(survey);
databaseAdaptor.saveSurvey(survey);
Expand All @@ -161,18 +164,12 @@ private void updateSurveys(List<Survey> surveys) {
displayErrorNotification(ConstantUtil.NOTIFICATION_FORM_ERROR,
getString(R.string.error_form_download));
}
displayNotification(synced, failed, numberOfSurveysToBeSynced);
displayNotification(synced, failed, surveys.size());
}
}

downloadAllSurveysHelp();
}

/**
* Check if any previously downloaded surveys still miss help media or cascade resources
*/
private void downloadAllSurveysHelp() {
List<Survey> surveys;
// now check if any previously downloaded surveys still need
// don't have their help media pre-cached
surveys = databaseAdaptor.getSurveyList(SurveyGroup.ID_NONE);
for (Survey survey : surveys) {
if (!survey.isHelpDownloaded()) {
Expand Down Expand Up @@ -329,21 +326,28 @@ private void downloadGaeResource(@NonNull String sid, @NonNull String url) throw
* invokes a service call to get the header information for multiple surveys
*/
@NonNull
private List<Survey> getSurveyHeaders(@NonNull String surveyId) {
private List<Survey> getSurveyHeaders(@NonNull String[] surveyIds) {
List<Survey> surveys = new ArrayList<>();
FlowApi flowApi = new FlowApi(getApplicationContext());
for (String id : surveyIds) {
try {
surveys.addAll(flowApi.getSurveyHeader(surveyId));
surveys.addAll(flowApi.getSurveyHeader(id));
} catch (IllegalArgumentException | IOException e) {
if (e instanceof IllegalArgumentException) {
if (!surveyHasBeenUnAssigned(e)) {
Timber.e(e);
}
displayErrorNotification(ConstantUtil.NOTIFICATION_HEADER_ERROR,
getString(R.string.error_form_header, surveyId));
getString(R.string.error_form_header, id));
}
}
return surveys;
}

private boolean surveyHasBeenUnAssigned(Exception e) {
return e instanceof IllegalArgumentException && e.getMessage() != null && e.getMessage()
.contains("null");
}

/**
* invokes a service call to list all surveys that have been designated for
* this device (based on phone number).
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/res/layout/activity_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@
style="@style/PreferenceItemSeparator"/>
<TextView
android:id="@+id/preference_reload_forms_title"
android:text="@string/reload_forms"
android:text="@string/reloadsurveyslabel"
style="@style/PreferenceTitleStyle"
android:paddingBottom="8dp"/>
android:paddingBottom="4dp"/>

<TextView
android:id="@+id/preference_reload_forms_subtitle"
android:text="@string/reloadsurveysdesc"
style="@style/PreferenceSubtitleStyle"/>
<View
style="@style/PreferenceSectionSeparator"/>

Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<string name="toosmallerr">Value is too small. Minimum value: </string>
<string name="toolargeerr">Value is too large. Maximum value: </string>
<string name="baddatatypeerr">Answer must be numeric</string>
<string name="reloadsurveyslabel">Reload all surveys</string>
<string name="reloadsurveysdesc">Deletes and re-downloads assigned surveys (only run if instructed)</string>
<string name="conftitle">Please Confirm</string>
<string name="reloadconftext">Are you sure you want to purge and re-download surveys?</string>
<string name="submitcompletetitle">Survey submitted</string>
Expand Down Expand Up @@ -270,8 +272,5 @@

<string name="no_gps_status_message">GPS Status is not installed on this device. Would you like to install it?</string>
<string name="data_points_sync_no_data_points">No datapoints to sync</string>
<string name="reload_forms">Reload all forms</string>
<string name="reload_forms_title">Reload all forms?</string>
<string name="reload">Reload</string>

</resources>

0 comments on commit 0b9032d

Please sign in to comment.