From cb06f5994ee62e0da11c3e48bef969aeb3775997 Mon Sep 17 00:00:00 2001 From: valeria Date: Mon, 4 Dec 2017 15:06:44 +0100 Subject: [PATCH] [#953] Reverted #923 reload all surveys is back to its original version --- .../settings/PreferenceActivity.java | 30 ++++++- .../ReloadFormsConfirmationDialog.java | 68 -------------- .../flow/service/SurveyDownloadService.java | 90 ++++++++++--------- .../main/res/layout/activity_preference.xml | 9 +- app/src/main/res/values/strings.xml | 5 +- 5 files changed, 83 insertions(+), 119 deletions(-) delete mode 100644 app/src/main/java/org/akvo/flow/presentation/settings/ReloadFormsConfirmationDialog.java diff --git a/app/src/main/java/org/akvo/flow/presentation/settings/PreferenceActivity.java b/app/src/main/java/org/akvo/flow/presentation/settings/PreferenceActivity.java index c060b1b52..083856f09 100644 --- a/app/src/main/java/org/akvo/flow/presentation/settings/PreferenceActivity.java +++ b/app/src/main/java/org/akvo/flow/presentation/settings/PreferenceActivity.java @@ -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; @@ -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) diff --git a/app/src/main/java/org/akvo/flow/presentation/settings/ReloadFormsConfirmationDialog.java b/app/src/main/java/org/akvo/flow/presentation/settings/ReloadFormsConfirmationDialog.java deleted file mode 100644 index a3c6f6bf2..000000000 --- a/app/src/main/java/org/akvo/flow/presentation/settings/ReloadFormsConfirmationDialog.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2017 Stichting Akvo (Akvo Foundation) - * - * This file is part of Akvo Flow. - * - * Akvo Flow is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Akvo Flow is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Akvo Flow. If not, see . - * - */ - -package org.akvo.flow.presentation.settings; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; - -import org.akvo.flow.R; -import org.akvo.flow.service.SurveyDownloadService; - -public class ReloadFormsConfirmationDialog extends DialogFragment { - - public static final String TAG = "ReloadFormsConfirmationDialog"; - - public ReloadFormsConfirmationDialog() { - } - - public static ReloadFormsConfirmationDialog newInstance() { - return new ReloadFormsConfirmationDialog(); - } - - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setMessage(R.string.reload_forms_title); - builder.setPositiveButton(R.string.reload, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - FragmentActivity activity = getActivity(); - if (activity != null) { - Intent i = new Intent(activity, SurveyDownloadService.class); - activity.startService(i); - } - } - }); - builder.setNegativeButton(R.string.cancelbutton, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }); - return builder.create(); - } -} diff --git a/app/src/main/java/org/akvo/flow/service/SurveyDownloadService.java b/app/src/main/java/org/akvo/flow/service/SurveyDownloadService.java index cc598fa48..51a61ed07 100644 --- a/app/src/main/java/org/akvo/flow/service/SurveyDownloadService.java +++ b/app/src/main/java/org/akvo/flow/service/SurveyDownloadService.java @@ -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"; @@ -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()); @@ -103,53 +106,53 @@ public void onHandleIntent(@Nullable Intent intent) { } } - private void downloadAndInstallSurveys() { - if (!isConnectionAvailable()) { - return; - } - List 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; } - Listsurveys = 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 surveys; + if (surveyIds != null && surveyIds.length > 0) { + surveys = getSurveyHeaders(surveyIds); + } else { + surveys = checkForSurveys(); + } - private void updateSurveys(List surveys) { // Update all survey groups syncSurveyGroups(surveys); // Check synced versions, and omit up-to-date surveys - List 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); @@ -161,18 +164,12 @@ private void updateSurveys(List 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 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()) { @@ -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 getSurveyHeaders(@NonNull String surveyId) { + private List getSurveyHeaders(@NonNull String[] surveyIds) { List 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). diff --git a/app/src/main/res/layout/activity_preference.xml b/app/src/main/res/layout/activity_preference.xml index 73ed874d1..997e1bd31 100644 --- a/app/src/main/res/layout/activity_preference.xml +++ b/app/src/main/res/layout/activity_preference.xml @@ -134,9 +134,14 @@ style="@style/PreferenceItemSeparator"/> + android:paddingBottom="4dp"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f73192ec0..c2aa2f005 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,6 +30,8 @@ Value is too small. Minimum value: Value is too large. Maximum value: Answer must be numeric + Reload all surveys + Deletes and re-downloads assigned surveys (only run if instructed) Please Confirm Are you sure you want to purge and re-download surveys? Survey submitted @@ -270,8 +272,5 @@ GPS Status is not installed on this device. Would you like to install it? No datapoints to sync - Reload all forms - Reload all forms? - Reload