Create a new class named ClientPolicyManager
by right-clicking on the package com.sap.flows
and choosing New > Java Class.
Replace the code for the class ClientPolicyManager
with the below code.
package com.sap.flows;
import android.support.annotation.NonNull;
import com.sap.cloud.mobile.flow.onboarding.ClientPolicy;
import com.sap.cloud.mobile.flow.onboarding.SecureStore;
import com.sap.cloud.mobile.flow.onboarding.SecureStoreException;
import com.sap.cloud.mobile.foundation.common.SettingsParameters;
import com.sap.cloud.mobile.foundation.settings.Settings;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import okhttp3.OkHttpClient;
public class ClientPolicyManager {
private static ClientPolicy policyFromServer;
private static Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
private SettingsParameters settingsParameters;
private SecureStore secureStore;
private OkHttpClient okHttpClient;
private final static String EULA = "eula";
private final static String EULA_VERSION = "eulaVersion";
private final static String EULA_TITLE = "eulaTitle";
private final static String EULA_URL = "eulaURL";
public ClientPolicyManager(SettingsParameters sParameters, SecureStore sStore, OkHttpClient httpClient) {
this.settingsParameters = sParameters;
this.secureStore = sStore;
this.okHttpClient = httpClient;
}
public void getClientPolicyFromServer() {
policyFromServer = null;
if (settingsParameters != null) {
CountDownLatch downloadLatch = new CountDownLatch(1);
Settings settings = new Settings(okHttpClient, settingsParameters);
settings.load(Settings.SettingTarget.DEVICE, "mobileservices/settingsExchange", new PolicyCallbackListener(downloadLatch, secureStore));
try {
downloadLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private class PolicyCallbackListener implements Settings.CallbackListener {
private CountDownLatch downloadLatch;
private SecureStore secureStore;
public PolicyCallbackListener(CountDownLatch downloadLatch, SecureStore sStore) {
this.downloadLatch = downloadLatch;
this.secureStore = sStore;
}
public ClientPolicy getPolicyFromServer() {
return policyFromServer;
}
@Override
public void onSuccess(@NonNull JSONObject result) {
JSONObject eulaJson = result.optJSONObject(EULA);
if (eulaJson != null) {
double eulaVersion = eulaJson.optDouble(EULA_VERSION, 1.0);
String eulaMessage = eulaJson.optString(EULA_TITLE, "End User License Agreement");
String eulaURL = eulaJson.optString(EULA_URL, "file:///android_asset/eula1.html");
try {
secureStore.put(EULA_VERSION, eulaVersion);
secureStore.put(EULA_TITLE, eulaMessage);
secureStore.put(EULA_URL, eulaURL);
LOGGER.debug("EULAUpdate : successfully updated secureStore values");
} catch (SecureStoreException e) {
e.printStackTrace();
}
}
downloadLatch.countDown();
}
@Override
public void onError(@NonNull Throwable throwable) {
throwable.printStackTrace();
policyFromServer = null;
downloadLatch.countDown();
}
}
}
Create a new class named EULAUpdateStep
by right-clicking on the package com.sap.flows
and choosing New > Java Class.
Replace the code for the class EULAUpdateStep
with the below code.
package com.sap.flows;
import com.sap.cloud.mobile.flow.FlowContext;
import com.sap.cloud.mobile.flow.Step;
import com.sap.cloud.mobile.flow.StepActionHandler;
import com.sap.cloud.mobile.flow.StepName;
import com.sap.cloud.mobile.flow.StepNames;
import com.sap.cloud.mobile.flow.onboarding.OnboardingContext;
import com.sap.cloud.mobile.flow.onboarding.SecureStore;
import com.sap.cloud.mobile.flow.onboarding.SecureStoreException;
import com.sap.cloud.mobile.flow.onboarding.eulascreen.EulaScreenStep;
import com.sap.cloud.mobile.foundation.common.SettingsParameters;
import com.sap.cloud.mobile.foundation.logging.Logging;
import com.sap.cloud.mobile.onboarding.eula.EULAScreenSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
public class EULAUpdateStep implements Step {
private static Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
private EulaScreenStep eulaScreenStep;
private final String EULA_VERSION = "eulaVersion";
private final String EULA_TITLE = "eulaTitle";
private final static String EULA_URL = "eulaURL";
public EULAUpdateStep(EulaScreenStep eulaStep) {
this.eulaScreenStep = eulaStep;
}
@StepNames({@StepName("onboard"), @StepName("restore")})
public void update(FlowContext flowContext, StepActionHandler stepActionHandler) {
OnboardingContext onboardingContext = (OnboardingContext) flowContext;
LOGGER.debug("EULAUpdateStep: In onboard update Step");
SettingsParameters settingsParameters = onboardingContext.getOnboardingParameters().getSettingsParameters();
SecureStore policyStore = onboardingContext.getPasscodePolicyStore();
ClientPolicyManager clientPolicyManager = new ClientPolicyManager(settingsParameters, policyStore, onboardingContext.getOkHttpClient());
try {
LOGGER.debug("EULAUpdateStep : Executing try-catch statement");
clientPolicyManager.getClientPolicyFromServer();
double eulaVersion = onboardingContext.getPasscodePolicyStore().get(EULA_VERSION);
String eulaTitle = onboardingContext.getPasscodePolicyStore().get(EULA_TITLE);
String eulaURL = onboardingContext.getPasscodePolicyStore().get(EULA_URL);
LOGGER.debug("eulaVersion from passcode policy store = " + eulaVersion);
eulaScreenStep.setEulaVersion(eulaVersion + "");
EULAScreenSettings eulaScreenSettings = new EULAScreenSettings();
eulaScreenSettings.setEULATitle(eulaTitle);
eulaScreenSettings.setEULAUrl(eulaURL);
eulaScreenStep.setEulaScreenSettings(eulaScreenSettings);
LOGGER.debug("EULAUpdate : Updated eulaScreenSettings successfully");
stepActionHandler.onSuccess(flowContext);
} catch (SecureStoreException e) {
e.printStackTrace();
LOGGER.debug("EULAUpdate: Update failed");
stepActionHandler.onFailure(e);
}
}
}
Notice above that the class implements Step.
The method update will be called for an onboard and restore flow type which was specified with the @StepName(...)
annotation.
In the class MainActivity
, add the following step to the array of onboarding steps before the eulaScreenStep
.
new EULAUpdateStep(eulaScreenStep),
Repeat the above step for the restore flow.