Repository URL to install this package:
|
Version:
0.0.6 ▾
|
react-native-motif-sdk
/
android
/
src
/
main
/
java
/
com
/
vipera
/
de
/
rn
/
assetmanager
/
RNAssetManagerConfig.java
|
|---|
package com.vipera.de.rn.assetmanager;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.XmlRes;
import android.util.Log;
import android.util.Patterns;
import android.webkit.URLUtil;
import com.vipera.de.motifconnector.config.DEServerConfig;
import com.vipera.de.rn.commons.RNConnectorConfigHelper;
import com.vipera.de.rn.commons.ResourceHelper;
import com.vipera.de.rn.commons.config.ConfigParams;
import com.vipera.de.rn.commons.config.InvalidConfigurationException;
import com.vipera.de.utility.Version;
import com.vipera.de.utility.preferences.ConfigPreferences;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RNAssetManagerConfig {
private static final String DEFAULT_BUNDLE_NAME = "index.android.bundle";
private Version embeddedAssetVersion;
private DEServerConfig serverConfig;
private String assetManagerEngineName;
private String assetManagerAssetsName;
private String embeddedBundleName;
private String deployedBundleName;
private boolean assetManagerEnabled;
private boolean assetManagerAvailable;
public RNAssetManagerConfig(DEServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
public String getAssetManagerAssetsName() {
return assetManagerAssetsName;
}
public String getAssetManagerEngineName() {
return assetManagerEngineName;
}
public Version getEmbeddedAssetVersion() {
return embeddedAssetVersion;
}
public DEServerConfig getServerConfig() {
return serverConfig;
}
public String getEmbeddedBundleName() {
return embeddedBundleName;
}
public String getDeployedBundleName() {
return deployedBundleName;
}
public boolean isAssetManagerEnabled() {
return assetManagerEnabled;
}
public boolean isAssetManagerAvailable() {
return assetManagerAvailable;
}
@Override
public String toString() {
return "RNAssetManagerConfig{" +
"embeddedAssetVersion=" + embeddedAssetVersion +
", serverConfig=" + serverConfig +
", assetManagerEngineName='" + assetManagerEngineName + '\'' +
", assetManagerAssetsName='" + assetManagerAssetsName + '\'' +
", embeddedBundleName='" + embeddedBundleName + '\'' +
", deployedBundleName='" + deployedBundleName + '\'' +
", assetManagerEnabled=" + assetManagerEnabled +
", assetManagerAvailable=" + assetManagerAvailable +
'}';
}
public static class Builder {
private static final String LOG_TAG = "RNAssetManagerConfig";
private final DEServerConfig serverConfig;
private Version embeddedAssetVersion;
private String assetManagerEngineName;
private String assetManagerAssetsName;
private String embeddedBundleName;
private String deployedBundleName;
private boolean assetManagerEnabled;
public Builder(DEServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
public static Builder initFromResources(Context context, @XmlRes int xmlFileId){
ConfigParams configParams = null;
try {
configParams = ResourceHelper.readConfigParamsFromResource(context,xmlFileId);
} catch (InvalidConfigurationException ex) {
Log.e(LOG_TAG,ex.getMessage(),ex);
throw new IllegalStateException(ex);
}
DEServerConfig.DEServerConfigBuilder serverConfigBuilder = RNConnectorConfigHelper.builderFromConfig(configParams);
if(!configParams.contains("retryCount")){
// override default retry to 1 if not set
serverConfigBuilder.setRetryCount(1);
}
Builder builder = new Builder(serverConfigBuilder.build());
String engineName = configParams.getString("engineName",null);
builder.setAssetManagerEngineName(engineName);
String assetsName = configParams.getString("assetsName",null);
builder.setAssetManagerAssetsName(assetsName);
Version version = Version.tryParse(configParams.getString("embeddedAssetsVersion",""));
if(version.equalsVersion(Version.zero())){
throw new IllegalArgumentException("invalid embeddedAssetsVersion configuration");
}
builder.setEmbeddedAssetVersion(version);
String embeddedBundleName = configParams.getString("embeddedBundleName",DEFAULT_BUNDLE_NAME);
builder.setEmbeddedBundleName(embeddedBundleName);
String deployedBundleName = configParams.getString("deployedBundleName",DEFAULT_BUNDLE_NAME);
builder.setDeployedBundleName(deployedBundleName);
boolean enabled = configParams.getBoolean("enabled", false);
builder.setAssetManagerEnabled(enabled);
return builder;
}
private static void validateUrl(String url) {
if(!isValidUrl(url)){
throw new IllegalArgumentException("Url invalid format");
}
}
private static boolean isValidUrl(String url) {
if(url == null || url.isEmpty()){
throw new IllegalArgumentException("Url is null or empty");
}
return URLUtil.isValidUrl(url);
}
public Builder setEmbeddedAssetVersion(Version embeddedAssetVersion) {
this.embeddedAssetVersion = embeddedAssetVersion;
return this;
}
public Builder setEmbeddedAssetVersion(String embeddedAssetVersion) {
this.embeddedAssetVersion = Version.tryParse(embeddedAssetVersion);
return this;
}
public Builder setAssetManagerEngineName(String assetManagerEngineName) {
this.assetManagerEngineName = assetManagerEngineName;
return this;
}
public Builder setAssetManagerAssetsName(String assetManagerAssetsName) {
this.assetManagerAssetsName = assetManagerAssetsName;
return this;
}
public Builder setEmbeddedBundleName(String embeddedBundleName) {
this.embeddedBundleName = embeddedBundleName;
return this;
}
public Builder setDeployedBundleName(String deployedBundleName) {
this.deployedBundleName = deployedBundleName;
return this;
}
public Builder setAssetManagerEnabled(boolean assetManagerEnabled) {
this.assetManagerEnabled = assetManagerEnabled;
return this;
}
public RNAssetManagerConfig build(){
RNAssetManagerConfig configuration = new RNAssetManagerConfig(this.serverConfig);
configuration.assetManagerEngineName = assetManagerEngineName;
configuration.assetManagerAssetsName = assetManagerAssetsName;
configuration.embeddedAssetVersion = this.embeddedAssetVersion;
configuration.embeddedBundleName = this.embeddedBundleName;
configuration.deployedBundleName = this.deployedBundleName;
configuration.assetManagerEnabled = this.assetManagerEnabled;
configuration.assetManagerAvailable = this.assetManagerEnabled && isValidUrl(this.serverConfig.getServerUrl());
return configuration;
}
}
}