|
57 | 57 | import weborb.writer.amf.AmfV3Formatter; |
58 | 58 |
|
59 | 59 | import java.io.IOException; |
| 60 | +import java.net.URI; |
| 61 | +import java.net.URISyntaxException; |
60 | 62 | import java.util.HashMap; |
61 | 63 | import java.util.Map; |
62 | 64 |
|
@@ -170,26 +172,69 @@ public static void initApp( String applicationId, String apiKey ) |
170 | 172 | initApp( null, applicationId, apiKey ); |
171 | 173 | } |
172 | 174 |
|
| 175 | + /** |
| 176 | + * Initializes the Backendless API and all Backendless dependencies. This is the first step in using the client API. |
| 177 | + * <p> |
| 178 | + * There is a low probability for internal API data to be cleared by the garbage collector. |
| 179 | + * In this case, an exception or fault, thrown by any of Backendless API methods, will contain 904 error code. |
| 180 | + * |
| 181 | + * @param customDomain custom domain which you setup in Backendless console https://backendless.com/docs/android/mgmt_custom_domain.html |
| 182 | + */ |
| 183 | + public static void initApp( String customDomain ) |
| 184 | + { |
| 185 | + initApp( (Object) null, customDomain ); |
| 186 | + } |
| 187 | + |
173 | 188 | public static boolean isInitialized() |
174 | 189 | { |
175 | 190 | return initialized; |
176 | 191 | } |
177 | 192 |
|
178 | | - public static void initApp( Object context, final String applicationId, final String apiKey ) |
| 193 | + public static void initApp( Object context, final String customDomain ) |
179 | 194 | { |
180 | | - if( isAndroid && context == null ) |
181 | | - throw new IllegalArgumentException( ExceptionMessage.NULL_CONTEXT ); |
| 195 | + if( customDomain == null || customDomain.equals( "" ) ) |
| 196 | + throw new IllegalArgumentException( "Custom domain cant be null or empty" ); |
| 197 | + |
| 198 | + if( customDomain.startsWith( "http" ) ) |
| 199 | + setUrl( customDomain ); |
| 200 | + else |
| 201 | + setUrl( "http://" + customDomain ); |
| 202 | + |
| 203 | + URI uri; |
| 204 | + try |
| 205 | + { |
| 206 | + uri = new URI( Backendless.getUrl() ); |
| 207 | + } |
| 208 | + catch( URISyntaxException e ) |
| 209 | + { |
| 210 | + throw new RuntimeException( e ); |
| 211 | + } |
182 | 212 |
|
| 213 | + prefs.setCustomDomain( uri.getHost() ); |
| 214 | + |
| 215 | + initApp( context ); |
| 216 | + } |
| 217 | + |
| 218 | + public static void initApp( Object context, final String applicationId, final String apiKey ) |
| 219 | + { |
183 | 220 | if( applicationId == null || applicationId.equals( "" ) ) |
184 | 221 | throw new IllegalArgumentException( ExceptionMessage.NULL_APPLICATION_ID ); |
185 | 222 |
|
186 | 223 | if( apiKey == null || apiKey.equals( "" ) ) |
187 | 224 | throw new IllegalArgumentException( ExceptionMessage.NULL_API_KEY ); |
188 | 225 |
|
189 | | - prefs.onCreate( context ); |
190 | 226 | prefs.initPreferences( applicationId, apiKey ); |
191 | 227 | prefs.setUrl( url ); |
192 | 228 |
|
| 229 | + initApp( context ); |
| 230 | + } |
| 231 | + |
| 232 | + private static void initApp( Object context ) |
| 233 | + { |
| 234 | + if( isAndroid && context == null ) |
| 235 | + throw new IllegalArgumentException( ExceptionMessage.NULL_CONTEXT ); |
| 236 | + |
| 237 | + prefs.onCreate( context ); |
193 | 238 | MessageWriter.addTypeWriter( BackendlessUser.class, new BackendlessUserWriter() ); |
194 | 239 | MessageWriter.addTypeWriter( Double.class, new DoubleWriter() ); |
195 | 240 | MessageWriter.addTypeWriter( Geometry.class, new BackendlessGeometryWriter() ); |
@@ -227,7 +272,7 @@ public static void initApp( Object context, final String applicationId, final St |
227 | 272 | ThreadPoolService.getThreadPoolExecutor(); |
228 | 273 | return; |
229 | 274 | } |
230 | | - |
| 275 | + |
231 | 276 | String userToken = UserTokenStorageFactory.instance().getStorage().get(); |
232 | 277 |
|
233 | 278 | if( userToken != null && !userToken.equals( "" ) ) |
@@ -269,12 +314,18 @@ public static void setUIState( String state ) |
269 | 314 | } |
270 | 315 | } |
271 | 316 |
|
272 | | - public static String getApplicationId() |
| 317 | + public static String getApplicationIdOrDomain() |
273 | 318 | { |
274 | 319 | if( prefs == null ) |
275 | 320 | throw new IllegalStateException( ExceptionMessage.NOT_INITIALIZED ); |
276 | 321 |
|
277 | | - return prefs.getApplicationId(); |
| 322 | + if( prefs.getApplicationId() != null ) |
| 323 | + return prefs.getApplicationId(); |
| 324 | + |
| 325 | + if( prefs.getCustomDomain() != null ) |
| 326 | + return prefs.getCustomDomain(); |
| 327 | + |
| 328 | + throw new IllegalStateException( ExceptionMessage.NOT_INITIALIZED ); |
278 | 329 | } |
279 | 330 |
|
280 | 331 | public static String getApiKey() |
@@ -302,13 +353,28 @@ public static void setUrl( String url ) |
302 | 353 | { |
303 | 354 | Backendless.url = url; |
304 | 355 |
|
305 | | - if( prefs != null && prefs.isAuthKeysExist() ) |
| 356 | + if( prefs != null ) |
306 | 357 | { |
307 | 358 | prefs.setUrl( url ); |
308 | 359 | Invoker.reinitialize(); |
309 | 360 | } |
310 | 361 | } |
311 | 362 |
|
| 363 | + /** |
| 364 | + * @return url to call to backendless |
| 365 | + * If application initialized with appId and api key returns url with the following pattern |
| 366 | + * "http(s)://<backendless-host>/<appId>/<apiKey>/..." |
| 367 | + * Else if application initialized with domain returns url with the following pattern |
| 368 | + * "http(s)://<backendless-host>/..." |
| 369 | + */ |
| 370 | + public static String getApplicationUrl() |
| 371 | + { |
| 372 | + final String applicationId = prefs.getApplicationId(); |
| 373 | + return applicationId == null |
| 374 | + ? getUrl() + "/api" |
| 375 | + : getUrl() + '/' + applicationId + '/' + getApiKey(); |
| 376 | + } |
| 377 | + |
312 | 378 | public static boolean isCodeRunner() |
313 | 379 | { |
314 | 380 | return isCodeRunner; |
|
0 commit comments