Skip to content

Commit e99b101

Browse files
committed
Simplified sdk setup (endpointBaseUrl and metadataUrl are now optional)
1 parent ea0a647 commit e99b101

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

core/src/main/java/io/snabble/sdk/SnabbleSdk.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import android.app.Activity;
44
import android.app.Application;
5+
import android.content.pm.ApplicationInfo;
6+
import android.content.pm.PackageInfo;
7+
import android.content.pm.PackageManager;
58

69
import com.google.gson.JsonObject;
710

@@ -236,22 +239,43 @@ private void init(final Application app,
236239

237240
userPreferences = new UserPreferences(application);
238241

239-
if (config.endpointBaseUrl.startsWith("http://")
242+
projectId = config.projectId;
243+
244+
if (projectId == null || application == null) {
245+
setupCompletionListener.onError(Error.CONFIG_PARAMETER_MISSING);
246+
return;
247+
}
248+
249+
if(config.endpointBaseUrl == null){
250+
endpointBaseUrl = "https://api.snabble.io";
251+
} else if (config.endpointBaseUrl.startsWith("http://")
240252
|| config.endpointBaseUrl.startsWith("https://")) {
241253
endpointBaseUrl = config.endpointBaseUrl;
242254
} else {
243255
endpointBaseUrl = "https://" + config.endpointBaseUrl;
244256
}
245257

246-
projectId = config.projectId;
258+
if(config.metadataUrl == null){
259+
ApplicationInfo applicationInfo = app.getApplicationInfo();
260+
int stringId = applicationInfo.labelRes;
261+
String label = stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : app.getString(stringId);
262+
String version;
263+
264+
try {
265+
PackageInfo pInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), 0);
266+
version = pInfo.versionName;
267+
} catch (PackageManager.NameNotFoundException e) {
268+
version = "";
269+
}
247270

248-
if (endpointBaseUrl == null || projectId == null
249-
|| application == null || config.metadataUrl == null) {
250-
setupCompletionListener.onError(Error.CONFIG_PARAMETER_MISSING);
251-
return;
271+
label = label.toLowerCase().replace(" ", "");
272+
version = version.toLowerCase().replace(" ", "");
273+
274+
metadataUrl = "/" + config.projectId + "/metadata/app/android/" + label + "-" + version;
275+
} else {
276+
metadataUrl = absoluteUrl(config.metadataUrl);
252277
}
253278

254-
metadataUrl = absoluteUrl(config.metadataUrl);
255279
metadataDownloader = new MetadataDownloader(this, config.bundledMetadataAssetPath);
256280

257281
roundingMode = config.roundingMode != null ? config.roundingMode : RoundingMode.HALF_UP;

0 commit comments

Comments
 (0)