Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App install Refactor #2704

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 4 additions & 91 deletions app/src/org/commcare/activities/CommCareSetupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.Intent;
import android.content.RestrictionsManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
Expand All @@ -25,10 +24,10 @@
import org.commcare.AppUtils;
import org.commcare.CommCareApp;
import org.commcare.CommCareApplication;
import org.commcare.android.database.global.models.ApplicationRecord;
import org.commcare.dalvik.BuildConfig;
import org.commcare.dalvik.R;
import org.commcare.engine.resource.AppInstallStatus;
import org.commcare.engine.resource.ResourceInstallUtils;
import org.commcare.engine.resource.installers.SingleAppInstallation;
import org.commcare.fragments.ContainerFragment;
import org.commcare.fragments.InstallConfirmFragment;
Expand All @@ -45,11 +44,10 @@
import org.commcare.resources.model.InvalidResourceException;
import org.commcare.resources.model.UnresolvedResourceException;
import org.commcare.tasks.ResourceEngineListener;
import org.commcare.tasks.ResourceEngineTask;
import org.commcare.tasks.RetrieveParseVerifyMessageListener;
import org.commcare.tasks.RetrieveParseVerifyMessageTask;
import org.commcare.utils.ConsumerAppsUtil;
import org.commcare.utils.ApkDependenciesUtils;
import org.commcare.utils.ConsumerAppsUtil;
import org.commcare.utils.MultipleAppsUtil;
import org.commcare.utils.Permissions;
import org.commcare.views.ManagedUi;
Expand All @@ -61,7 +59,6 @@
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.util.PropertyUtils;

import java.io.IOException;
import java.security.SignatureException;
Expand Down Expand Up @@ -460,103 +457,19 @@ public void stopBlockingForTask(int id) {

private void startResourceInstall() {
if (startAllowed) {
ccApp = getCommCareApp();
containerFragment.setData(ccApp);

CustomProgressDialog lastDialog = getCurrentProgressDialog();
// used to tell the ResourceEngineTask whether or not it should
// sleep before it starts, set based on whether we are currently
// in keep trying mode.
boolean shouldSleep = (lastDialog != null) && lastDialog.isChecked();

ResourceEngineTask<CommCareSetupActivity> task =
new ResourceEngineTask<CommCareSetupActivity>(ccApp,
DIALOG_INSTALL_PROGRESS, shouldSleep, false) {

@Override
protected void deliverResult(CommCareSetupActivity receiver,
AppInstallStatus result) {
handleAppInstallResult(this, receiver, result);
}

@Override
protected void deliverUpdate(CommCareSetupActivity receiver,
int[]... update) {
receiver.updateResourceProgress(update[0][0], update[0][1], update[0][2]);
}

@Override
protected void deliverError(CommCareSetupActivity receiver,
Exception e) {
receiver.failUnknown(AppInstallStatus.UnknownFailure);
}
};

task.connect(this);
task.executeParallel(incomingRef);
ccApp = ResourceInstallUtils.startAppInstallAsync(shouldSleep, DIALOG_INSTALL_PROGRESS, this, incomingRef);
containerFragment.setData(ccApp);
} else {
Log.i(TAG, "During install: blocked a resource install press since a task was already running");
}
}

public static void handleAppInstallResult(ResourceEngineTask resourceEngineTask, ResourceEngineListener receiver, AppInstallStatus result) {
switch (result) {
case Installed:
receiver.reportSuccess(true);
break;
case UpToDate:
receiver.reportSuccess(false);
break;
case MissingResourcesWithMessage:
// fall through to more general case:
case MissingResources:
receiver.failMissingResource(resourceEngineTask.getMissingResourceException(), result);
break;
case InvalidResource:
receiver.failInvalidResource(resourceEngineTask.getInvalidResourceException(), result);
break;
case InvalidReference:
receiver.failInvalidReference(resourceEngineTask.getInvalidReferenceException(), result);
break;
case IncompatibleReqs:
receiver.failBadReqs(resourceEngineTask.getVersionRequired(),
resourceEngineTask.getVersionAvailable(), resourceEngineTask.isMajorIsProblem());
break;
case NoLocalStorage:
receiver.failWithNotification(AppInstallStatus.NoLocalStorage);
break;
case NoConnection:
receiver.failWithNotification(AppInstallStatus.NoConnection);
break;
case BadSslCertificate:
receiver.failWithNotification(AppInstallStatus.BadSslCertificate, NotificationActionButtonInfo.ButtonAction.LAUNCH_DATE_SETTINGS);
break;
case DuplicateApp:
receiver.failWithNotification(AppInstallStatus.DuplicateApp);
break;
case IncorrectTargetPackage:
receiver.failTargetMismatch();
break;
case ReinstallFromInvalidCcz:
receiver.failUnknown(AppInstallStatus.ReinstallFromInvalidCcz);
break;
case CaptivePortal:
receiver.failWithNotification(AppInstallStatus.CaptivePortal);
break;
default:
receiver.failUnknown(AppInstallStatus.UnknownFailure);
break;
}
}

public static CommCareApp getCommCareApp() {
ApplicationRecord newRecord =
new ApplicationRecord(PropertyUtils.genUUID().replace("-", ""),
ApplicationRecord.STATUS_UNINITIALIZED);

return new CommCareApp(newRecord);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
Expand Down
100 changes: 98 additions & 2 deletions app/src/org/commcare/engine/resource/ResourceInstallUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@

import org.commcare.CommCareApp;
import org.commcare.CommCareApplication;
import org.commcare.android.database.global.models.ApplicationRecord;
import org.commcare.core.network.CaptivePortalRedirectException;
import org.commcare.engine.resource.installers.SingleAppInstallation;
import org.commcare.network.RateLimitedException;
import org.commcare.preferences.ServerUrls;
import org.commcare.preferences.HiddenPreferences;
import org.commcare.preferences.MainConfigurablePreferences;
import org.commcare.preferences.ServerUrls;
import org.commcare.resources.ResourceManager;
import org.commcare.resources.model.Resource;
import org.commcare.resources.model.ResourceTable;
import org.commcare.resources.model.UnreliableSourceException;
import org.commcare.resources.model.UnresolvedResourceException;
import org.commcare.suite.model.Profile;
import org.commcare.tasks.ResourceEngineListener;
import org.commcare.tasks.ResourceEngineTask;
import org.commcare.tasks.templates.CommCareTaskConnector;
import org.commcare.util.CommCarePlatform;
import org.commcare.util.LogTypes;
import org.commcare.utils.AndroidCommCarePlatform;
import org.commcare.utils.SessionUnavailableException;
import org.commcare.views.notifications.NotificationActionButtonInfo;
import org.javarosa.core.services.Logger;
import org.javarosa.core.util.PropertyUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.util.Date;

import javax.net.ssl.SSLException;
Expand All @@ -48,6 +53,97 @@ public static boolean isUpdateReadyToInstall() {
return ResourceManager.isTableStagedForUpgrade(upgradeTable);
}

/**
* Creates a new application record in db
* @return newly created CommCare App
*/
private static CommCareApp createNewCommCareApp() {
ApplicationRecord newRecord =
new ApplicationRecord(PropertyUtils.genUUID().replace("-", ""),
ApplicationRecord.STATUS_UNINITIALIZED);

return new CommCareApp(newRecord);
}

public static CommCareApp startAppInstallAsync(boolean shouldSleep, int taskId, CommCareTaskConnector connector,
String installRef) {
CommCareApp ccApp = createNewCommCareApp();
ResourceEngineTask<ResourceEngineListener> task =
new ResourceEngineTask<>(ccApp,
taskId, shouldSleep, false) {

@Override
protected void deliverResult(ResourceEngineListener receiver,
AppInstallStatus result) {
handleAppInstallResult(this, receiver, result);
}

@Override
protected void deliverUpdate(ResourceEngineListener receiver, int[]... update) {
receiver.updateResourceProgress(update[0][0], update[0][1], update[0][2]);
}

@Override
protected void deliverError(ResourceEngineListener receiver, Exception e) {
receiver.failUnknown(AppInstallStatus.UnknownFailure);
}
};

task.connect(connector);
task.executeParallel(installRef);
return ccApp;
}

public static void handleAppInstallResult(ResourceEngineTask resourceEngineTask, ResourceEngineListener receiver, AppInstallStatus result) {
switch (result) {
case Installed:
receiver.reportSuccess(true);
break;
case UpToDate:
receiver.reportSuccess(false);
break;
case MissingResourcesWithMessage:
// fall through to more general case:
case MissingResources:
receiver.failMissingResource(resourceEngineTask.getMissingResourceException(), result);
break;
case InvalidResource:
receiver.failInvalidResource(resourceEngineTask.getInvalidResourceException(), result);
break;
case InvalidReference:
receiver.failInvalidReference(resourceEngineTask.getInvalidReferenceException(), result);
break;
case IncompatibleReqs:
receiver.failBadReqs(resourceEngineTask.getVersionRequired(),
resourceEngineTask.getVersionAvailable(), resourceEngineTask.isMajorIsProblem());
break;
case NoLocalStorage:
receiver.failWithNotification(AppInstallStatus.NoLocalStorage);
break;
case NoConnection:
receiver.failWithNotification(AppInstallStatus.NoConnection);
break;
case BadSslCertificate:
receiver.failWithNotification(AppInstallStatus.BadSslCertificate, NotificationActionButtonInfo.ButtonAction.LAUNCH_DATE_SETTINGS);
break;
case DuplicateApp:
receiver.failWithNotification(AppInstallStatus.DuplicateApp);
break;
case IncorrectTargetPackage:
receiver.failTargetMismatch();
break;
case ReinstallFromInvalidCcz:
receiver.failUnknown(AppInstallStatus.ReinstallFromInvalidCcz);
break;
case CaptivePortal:
receiver.failWithNotification(AppInstallStatus.CaptivePortal);
break;
default:
receiver.failUnknown(AppInstallStatus.UnknownFailure);
break;
}
}

/**
* @return Version from profile in the app's upgrade table; -1 if upgrade
* profile not found.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.commcare.engine.resource.installers;

import org.commcare.CommCareApp;
import org.commcare.activities.CommCareSetupActivity;
import org.commcare.engine.resource.AppInstallStatus;
import org.commcare.tasks.ResourceEngineTask;

import static org.commcare.activities.CommCareSetupActivity.handleAppInstallResult;
import org.commcare.engine.resource.ResourceInstallUtils;

/**
* Install CC app from the APK's asset directory
Expand All @@ -22,29 +18,6 @@ public class SingleAppInstallation {
* without prompting the user.
*/
public static void installSingleApp(CommCareSetupActivity activity, int dialogId) {
CommCareApp app = CommCareSetupActivity.getCommCareApp();

ResourceEngineTask<CommCareSetupActivity> task =
new ResourceEngineTask<CommCareSetupActivity>(app, dialogId, false, false) {
@Override
protected void deliverResult(CommCareSetupActivity receiver,
AppInstallStatus result) {
handleAppInstallResult(this,receiver,result);
}

@Override
protected void deliverUpdate(CommCareSetupActivity receiver,
int[]... update) {
receiver.updateResourceProgress(update[0][0], update[0][1], update[0][2]);
}

@Override
protected void deliverError(CommCareSetupActivity receiver,
Exception e) {
receiver.failUnknown(AppInstallStatus.UnknownFailure);
}
};
task.connect(activity);
task.executeParallel(SINGLE_APP_REFERENCE);
ResourceInstallUtils.startAppInstallAsync(false, dialogId, activity, SINGLE_APP_REFERENCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import androidx.appcompat.app.AlertDialog;

import static org.commcare.engine.resource.ResourceInstallUtils.getProfileReference;
import static org.commcare.engine.resource.ResourceInstallUtils.handleAppInstallResult;
import static org.commcare.recovery.measures.RecoveryMeasure.MEASURE_TYPE_APP_OFFLINE_REINSTALL_AND_UPDATE;
import static org.commcare.recovery.measures.RecoveryMeasure.MEASURE_TYPE_APP_REINSTALL_AND_UPDATE;
import static org.commcare.recovery.measures.RecoveryMeasure.MEASURE_TYPE_APP_UPDATE;
Expand Down Expand Up @@ -539,7 +540,7 @@ static class sResourceEngineTask extends ResourceEngineTask<ExecuteRecoveryMeasu

@Override
protected void deliverResult(ExecuteRecoveryMeasuresActivity receiver, AppInstallStatus result) {
CommCareSetupActivity.handleAppInstallResult(this, receiver, result);
handleAppInstallResult(this, receiver, result);
}

@Override
Expand Down