2929
3030public class SnabbleSdk {
3131
32- public static class Config {
33- /**
34- * The endpoint url of the snabble backend. For example "snabble.io" for the Production environment.
35- */
36- public String endpointBaseUrl ;
37-
38- /**
39- * The project identifier, which is used in the communication with the backend.
40- */
41- public String projectId ;
42-
43- /**
44- * The JWT based client token required for all requests to the backend.
45- */
46- public String clientToken ;
47-
48- /**
49- * Relative url to the metadata.
50- * <p>
51- * In the format: /api/{projectId}/metadata/app/{platform}/{version}
52- */
53- public String metadataUrl ;
54-
55- /**
56- * Relative path from the assets folder which points to a bundled file which contains the metadata
57- * from the metadataUrl specified before.
58- * <p>
59- * This file gets initially used to initialize the sdk before network requests are made,
60- * or be able to use the sdk in the case of no network connection.
61- * <p>
62- * Optional. If no file is specified every time the sdk is initialized we wait for a network response
63- * from the backend.
64- * <p>
65- * It is HIGHLY recommended to provide bundled metadata to allow the sdk to function
66- * without having a network connection.
67- */
68- public String bundledMetadataAssetPath ;
69-
70- /**
71- * The name if the product database which will be used to store the database under
72- * the default android database directory.
73- * <p>
74- * The resulting path is the path from {@link android.content.Context#getDatabasePath(String)}
75- *
76- * If set to null, only online lookup of products is available.
77- *
78- */
79- public String productDbName ;
80-
81- /**
82- * Relative path from the assets folder which points to a bundled file which contains the products
83- * as an sqlite3 database.
84- * <p>
85- * This file gets initially used to initialize the product database before network requests are made,
86- * or be able to use the product database in the case of no network connection.
87- * <p>
88- * Optional. If no file is specified and no product database is already present, the sdk initialization
89- * is delayed until the network request for the product db is successful.
90- * <p>
91- * It is HIGHLY recommended to provide a bundled database to allow the sdk to function
92- * without having a network connection.
93- */
94- public String productDbBundledAssetPath ;
95-
96- /**
97- * This is the revision id of the bundled product database. This is used to prevent
98- * extracting the bundled database when the stored database is already newer.
99- * <p>
100- * When the bundled database revision is newer then the stored database, the stored
101- * database will be overwritten by the bundled database
102- */
103- public int productDbBundledRevisionId = -1 ;
104-
105- /**
106- * The bundled major schema version.
107- */
108- public int productDbBundledSchemaVersionMajor = -1 ;
109-
110- /**
111- * The bundled minor schema version.
112- */
113- public int productDbBundledSchemaVersionMinor = -1 ;
114-
115- /**
116- * If set to true, allows the database to be downloaded even if no seed is provided.
117- *
118- * When set to false, calls to {@link ProductDatabase#update()} will still download
119- * the database if its missing, allowing for the ability of database downloads after
120- * sdk initialization
121- */
122- public boolean productDbDownloadIfMissing = true ;
123-
124- public String encodedCodesPrefix = null ;
125- public String encodedCodesSeperator = null ;
126- public String encodedCodesSuffix = null ;
127- }
32+ private boolean useGermanPrintPrefix = false ;
12833
12934 private String endpointBaseUrl ;
13035 private String metadataUrl ;
@@ -159,70 +64,6 @@ public static class Config {
15964 private String encodedCodesSeperator = null ;
16065 private String encodedCodesSuffix = null ;
16166
162- private SnabbleSdk () {
163-
164- }
165-
166- /**
167- * Initializes the snabble SDK. Returns a {@link SnabbleSdk} context without requiring network access in
168- * {@link SetupCompletionListener#onReady(SnabbleSdk)} when
169- * {@link Config#bundledMetadataAssetPath} and {@link Config#productDbBundledAssetPath} is set.
170- * <p>
171- * Otherwise a network request will be made and after successfully downloading the metadata and product database,
172- * {@link SetupCompletionListener#onReady(SnabbleSdk)} will be called.
173- * <p>
174- * If no network request could be made and no {@link Config#bundledMetadataAssetPath} is provided
175- * {@link Error#CONNECTION_TIMEOUT} will be called.
176- */
177- public static void setup (Application app , Config config ,
178- SetupCompletionListener setupCompletionListener ) {
179- final SnabbleSdk sdk = new SnabbleSdk ();
180- sdk .init (app , config , setupCompletionListener );
181- }
182-
183- /**
184- * The blocking version of {@link SnabbleSdk#setup(Application, Config, SetupCompletionListener)}
185- * <p>
186- * Blocks until every initialization is completed, that includes waiting for necessary
187- * network calls if bundled data is not provided.
188- * <p>
189- * If all needed bundled data is provided (See {@link Config}), initialization requires
190- * no network calls and returns after initialization of the product database.
191- *
192- * @throws SnabbleException If an error occurs while initializing the sdk.
193- */
194- public static SnabbleSdk setupBlocking (Application app , Config config ) throws SnabbleException {
195- final CountDownLatch countDownLatch = new CountDownLatch (1 );
196- final SnabbleSdk [] snabbleSdk = new SnabbleSdk [1 ];
197- final Error [] snabbleError = new Error [1 ];
198-
199- setup (app , config , new SetupCompletionListener () {
200- @ Override
201- public void onReady (SnabbleSdk sdk ) {
202- snabbleSdk [0 ] = sdk ;
203- countDownLatch .countDown ();
204- }
205-
206- @ Override
207- public void onError (Error error ) {
208- snabbleError [0 ] = error ;
209- countDownLatch .countDown ();
210- }
211- });
212-
213- try {
214- countDownLatch .await ();
215- } catch (InterruptedException e ) {
216- throw new SnabbleException (Error .UNSPECIFIED_ERROR );
217- }
218-
219- if (snabbleError [0 ] != null ) {
220- throw new SnabbleException (snabbleError [0 ]);
221- }
222-
223- return snabbleSdk [0 ];
224- }
225-
22667 private void init (final Application app ,
22768 final Config config ,
22869 final SetupCompletionListener setupCompletionListener ) {
@@ -279,6 +120,8 @@ private void init(final Application app,
279120 encodedCodesSeperator = config .encodedCodesSeperator != null ? config .encodedCodesSeperator : "\n " ;
280121 encodedCodesSuffix = config .encodedCodesSuffix != null ? config .encodedCodesSuffix : "" ;
281122
123+ useGermanPrintPrefix = config .useGermanPrintPrefix ;
124+
282125 updateShops ();
283126
284127 if (config .bundledMetadataAssetPath != null ) {
@@ -302,6 +145,74 @@ protected void onError() {
302145 }
303146 }
304147
148+ private SnabbleSdk () {
149+
150+ }
151+
152+ /**
153+ * Initializes the snabble SDK. Returns a {@link SnabbleSdk} context without requiring network access in
154+ * {@link SetupCompletionListener#onReady(SnabbleSdk)} when
155+ * {@link Config#bundledMetadataAssetPath} and {@link Config#productDbBundledAssetPath} is set.
156+ * <p>
157+ * Otherwise a network request will be made and after successfully downloading the metadata and product database,
158+ * {@link SetupCompletionListener#onReady(SnabbleSdk)} will be called.
159+ * <p>
160+ * If no network request could be made and no {@link Config#bundledMetadataAssetPath} is provided
161+ * {@link Error#CONNECTION_TIMEOUT} will be called.
162+ */
163+ public static void setup (Application app , Config config ,
164+ SetupCompletionListener setupCompletionListener ) {
165+ final SnabbleSdk sdk = new SnabbleSdk ();
166+ sdk .init (app , config , setupCompletionListener );
167+ }
168+
169+ /**
170+ * The blocking version of {@link SnabbleSdk#setup(Application, Config, SetupCompletionListener)}
171+ * <p>
172+ * Blocks until every initialization is completed, that includes waiting for necessary
173+ * network calls if bundled data is not provided.
174+ * <p>
175+ * If all needed bundled data is provided (See {@link Config}), initialization requires
176+ * no network calls and returns after initialization of the product database.
177+ *
178+ * @throws SnabbleException If an error occurs while initializing the sdk.
179+ */
180+ public static SnabbleSdk setupBlocking (Application app , Config config ) throws SnabbleException {
181+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
182+ final SnabbleSdk [] snabbleSdk = new SnabbleSdk [1 ];
183+ final Error [] snabbleError = new Error [1 ];
184+
185+ setup (app , config , new SetupCompletionListener () {
186+ @ Override
187+ public void onReady (SnabbleSdk sdk ) {
188+ snabbleSdk [0 ] = sdk ;
189+ countDownLatch .countDown ();
190+ }
191+
192+ @ Override
193+ public void onError (Error error ) {
194+ snabbleError [0 ] = error ;
195+ countDownLatch .countDown ();
196+ }
197+ });
198+
199+ try {
200+ countDownLatch .await ();
201+ } catch (InterruptedException e ) {
202+ throw new SnabbleException (Error .UNSPECIFIED_ERROR );
203+ }
204+
205+ if (snabbleError [0 ] != null ) {
206+ throw new SnabbleException (snabbleError [0 ]);
207+ }
208+
209+ return snabbleSdk [0 ];
210+ }
211+
212+ public boolean isUsingGermanPrintPrefix () {
213+ return useGermanPrintPrefix ;
214+ }
215+
305216 private void updateShops () {
306217 String shopsJson = metadataDownloader .getExtras ().get ("shops" );
307218 if (shopsJson != null ) {
@@ -497,6 +408,105 @@ public String getEncodedCodesSuffix() {
497408 return encodedCodesSuffix ;
498409 }
499410
411+ public static class Config {
412+ /**
413+ * The endpoint url of the snabble backend. For example "snabble.io" for the Production environment.
414+ */
415+ public String endpointBaseUrl ;
416+
417+ /**
418+ * The project identifier, which is used in the communication with the backend.
419+ */
420+ public String projectId ;
421+
422+ /**
423+ * The JWT based client token required for all requests to the backend.
424+ */
425+ public String clientToken ;
426+
427+ /**
428+ * Relative url to the metadata.
429+ * <p>
430+ * In the format: /api/{projectId}/metadata/app/{platform}/{version}
431+ */
432+ public String metadataUrl ;
433+
434+ /**
435+ * Relative path from the assets folder which points to a bundled file which contains the metadata
436+ * from the metadataUrl specified before.
437+ * <p>
438+ * This file gets initially used to initialize the sdk before network requests are made,
439+ * or be able to use the sdk in the case of no network connection.
440+ * <p>
441+ * Optional. If no file is specified every time the sdk is initialized we wait for a network response
442+ * from the backend.
443+ * <p>
444+ * It is HIGHLY recommended to provide bundled metadata to allow the sdk to function
445+ * without having a network connection.
446+ */
447+ public String bundledMetadataAssetPath ;
448+
449+ /**
450+ * The name if the product database which will be used to store the database under
451+ * the default android database directory.
452+ * <p>
453+ * The resulting path is the path from {@link android.content.Context#getDatabasePath(String)}
454+ *
455+ * If set to null, only online lookup of products is available.
456+ *
457+ */
458+ public String productDbName ;
459+
460+ /**
461+ * Relative path from the assets folder which points to a bundled file which contains the products
462+ * as an sqlite3 database.
463+ * <p>
464+ * This file gets initially used to initialize the product database before network requests are made,
465+ * or be able to use the product database in the case of no network connection.
466+ * <p>
467+ * Optional. If no file is specified and no product database is already present, the sdk initialization
468+ * is delayed until the network request for the product db is successful.
469+ * <p>
470+ * It is HIGHLY recommended to provide a bundled database to allow the sdk to function
471+ * without having a network connection.
472+ */
473+ public String productDbBundledAssetPath ;
474+
475+ /**
476+ * This is the revision id of the bundled product database. This is used to prevent
477+ * extracting the bundled database when the stored database is already newer.
478+ * <p>
479+ * When the bundled database revision is newer then the stored database, the stored
480+ * database will be overwritten by the bundled database
481+ */
482+ public int productDbBundledRevisionId = -1 ;
483+
484+ /**
485+ * The bundled major schema version.
486+ */
487+ public int productDbBundledSchemaVersionMajor = -1 ;
488+
489+ /**
490+ * The bundled minor schema version.
491+ */
492+ public int productDbBundledSchemaVersionMinor = -1 ;
493+
494+ /**
495+ * If set to true, allows the database to be downloaded even if no seed is provided.
496+ *
497+ * When set to false, calls to {@link ProductDatabase#update()} will still download
498+ * the database if its missing, allowing for the ability of database downloads after
499+ * sdk initialization
500+ */
501+ public boolean productDbDownloadIfMissing = true ;
502+
503+ public boolean useGermanPrintPrefix = false ;
504+
505+ public String encodedCodesPrefix = null ;
506+ public String encodedCodesSeperator = null ;
507+ public String encodedCodesSuffix = null ;
508+ }
509+
500510 String absoluteUrl (String url ) {
501511 if (url .startsWith ("http" )) {
502512 return url ;
0 commit comments