Skip to content

Commit 5f9e5c1

Browse files
ksv510ksv105
andauthored
BKNDLSS-23954 Add possibility to init android sdk with only domain, without appId and apiKey (#483)
Co-authored-by: sergey.kukurudzyak <[email protected]>
1 parent 92712f6 commit 5f9e5c1

16 files changed

+99
-478
lines changed

src/com/backendless/AndroidHeadersManager.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ static AndroidHeadersManager getInstance() throws BackendlessException
3131

3232
AndroidHeadersManager()
3333
{
34-
if( Backendless.getApplicationId() == null || Backendless.getApiKey() == null )
35-
{
36-
throw new IllegalStateException( ExceptionMessage.NOT_INITIALIZED );
37-
}
3834
initialFill();
3935
}
4036

src/com/backendless/Backendless.java

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import weborb.writer.amf.AmfV3Formatter;
5858

5959
import java.io.IOException;
60+
import java.net.URI;
61+
import java.net.URISyntaxException;
6062
import java.util.HashMap;
6163
import java.util.Map;
6264

@@ -170,26 +172,69 @@ public static void initApp( String applicationId, String apiKey )
170172
initApp( null, applicationId, apiKey );
171173
}
172174

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+
173188
public static boolean isInitialized()
174189
{
175190
return initialized;
176191
}
177192

178-
public static void initApp( Object context, final String applicationId, final String apiKey )
193+
public static void initApp( Object context, final String customDomain )
179194
{
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+
}
182212

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+
{
183220
if( applicationId == null || applicationId.equals( "" ) )
184221
throw new IllegalArgumentException( ExceptionMessage.NULL_APPLICATION_ID );
185222

186223
if( apiKey == null || apiKey.equals( "" ) )
187224
throw new IllegalArgumentException( ExceptionMessage.NULL_API_KEY );
188225

189-
prefs.onCreate( context );
190226
prefs.initPreferences( applicationId, apiKey );
191227
prefs.setUrl( url );
192228

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 );
193238
MessageWriter.addTypeWriter( BackendlessUser.class, new BackendlessUserWriter() );
194239
MessageWriter.addTypeWriter( Double.class, new DoubleWriter() );
195240
MessageWriter.addTypeWriter( Geometry.class, new BackendlessGeometryWriter() );
@@ -227,7 +272,7 @@ public static void initApp( Object context, final String applicationId, final St
227272
ThreadPoolService.getThreadPoolExecutor();
228273
return;
229274
}
230-
275+
231276
String userToken = UserTokenStorageFactory.instance().getStorage().get();
232277

233278
if( userToken != null && !userToken.equals( "" ) )
@@ -269,12 +314,18 @@ public static void setUIState( String state )
269314
}
270315
}
271316

272-
public static String getApplicationId()
317+
public static String getApplicationIdOrDomain()
273318
{
274319
if( prefs == null )
275320
throw new IllegalStateException( ExceptionMessage.NOT_INITIALIZED );
276321

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 );
278329
}
279330

280331
public static String getApiKey()
@@ -302,13 +353,28 @@ public static void setUrl( String url )
302353
{
303354
Backendless.url = url;
304355

305-
if( prefs != null && prefs.isAuthKeysExist() )
356+
if( prefs != null )
306357
{
307358
prefs.setUrl( url );
308359
Invoker.reinitialize();
309360
}
310361
}
311362

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+
312378
public static boolean isCodeRunner()
313379
{
314380
return isCodeRunner;

src/com/backendless/BackendlessPrefs.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BackendlessPrefs
1212
protected AuthKeys authKeys;
1313
protected Map<String, String> headers;
1414
protected String url;
15+
protected String customDomain;
1516

1617
public BackendlessPrefs()
1718
{
@@ -44,7 +45,9 @@ public void onCreate( Object context )
4445

4546
public String getApplicationId()
4647
{
47-
return getAuthKeys().getApplicationId();
48+
49+
final AuthKeys authKeys = getAuthKeys();
50+
return authKeys == null ? null : authKeys.getApplicationId();
4851
}
4952

5053
public String getApiKey()
@@ -59,9 +62,6 @@ public synchronized Map getHeaders()
5962

6063
private synchronized AuthKeys getAuthKeys()
6164
{
62-
if( authKeys == null)
63-
throw new IllegalStateException( ExceptionMessage.NOT_INITIALIZED );
64-
6565
return authKeys;
6666
}
6767

@@ -82,4 +82,15 @@ public String getUrl()
8282

8383
return this.url;
8484
}
85+
86+
public String getCustomDomain()
87+
{
88+
return customDomain;
89+
}
90+
91+
public BackendlessPrefs setCustomDomain( String customDomain )
92+
{
93+
this.customDomain = customDomain;
94+
return this;
95+
}
8596
}

src/com/backendless/Files.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public BackendlessFile uploadFromStream( OutputStreamRouter outputStreamRouter,
125125

126126
try
127127
{
128-
String urlStr = Backendless.getUrl() + '/' + Backendless.getApplicationId() + '/' + Backendless.getApiKey() + "/files/" + encodeURL( path ) + "/" + encodeURL( name );
128+
String urlStr = Backendless.getApplicationUrl() + "/files/" + encodeURL( path ) + "/" + encodeURL( name );
129129

130130
if( overwrite )
131131
urlStr = urlStr + "?" + OVERWRITE_PARAMETER_NAME + "=" + overwrite;

src/com/backendless/Invoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Invoker
4040

4141
static void reinitialize()
4242
{
43-
String urlEnding = Backendless.getUrl() + '/' + Backendless.getApplicationId() + '/' + Backendless.getApiKey() + "/binary";
43+
String urlEnding = Backendless.getApplicationUrl() + "/binary";
4444
weborbClient = new WeborbClient( urlEnding, DEFAULT_TIMEOUT, DESTINATION );
4545
weborbClient.setCookiesDateFormat( "EEE, dd-MMM-yy HH:mm:ss z" );
4646

src/com/backendless/SocialAsyncCallback.java

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/com/backendless/exceptions/ExceptionMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class ExceptionMessage
2525
public final static String CLIENT_ERROR = "Internal client exception.";
2626

2727
public final static String WRONG_MANIFEST = "Wrong dependencies at the manifest";
28-
public final static String NOT_INITIALIZED = "Backendless application was not initialized";
28+
public final static String NOT_INITIALIZED = "Backendless application was not initialized. " +
29+
"Call Backendless.initApp method before use any backendless functionality";
2930

3031
public final static String NULL_USER = "User cannot be null or empty.";
3132
public final static String NULL_PASSWORD = "User password cannot be null or empty.";

src/com/backendless/push/BackendlessFCMService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void fallBackMode( Context context, String message, String contentTitle,
146146
// android.os.Build.VERSION_CODES.O == 26
147147
if( android.os.Build.VERSION.SDK_INT > 25 )
148148
{
149-
final String channelId = Backendless.getApplicationId() + ":" + channelName;
149+
final String channelId = Backendless.getApplicationIdOrDomain() + ":" + channelName;
150150
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
151151
NotificationChannel notificationChannel = notificationManager.getNotificationChannel( channelId );
152152

src/com/backendless/push/PushTemplateHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ static public void deleteNotificationChannel( Context context )
335335

336336
static public NotificationChannel getNotificationChannel( final Context context, final String templateName )
337337
{
338-
final String channelId = Backendless.getApplicationId() + ":" + templateName;
338+
final String channelId = Backendless.getApplicationIdOrDomain() + ":" + templateName;
339339
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
340340
return notificationManager.getNotificationChannel( channelId );
341341
}
342342

343343
static public NotificationChannel getOrCreateNotificationChannel( Context context, final AndroidPushTemplate template )
344344
{
345-
final String channelId = Backendless.getApplicationId() + ":" + template.getName();
345+
final String channelId = Backendless.getApplicationIdOrDomain() + ":" + template.getName();
346346
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
347347

348348
NotificationChannel notificationChannel = notificationManager.getNotificationChannel( channelId );

src/com/backendless/rt/SocketIOConnectionManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.socket.client.Socket;
1010
import io.socket.emitter.Emitter;
1111

12+
import java.net.URI;
1213
import java.net.URISyntaxException;
1314
import java.util.Arrays;
1415
import java.util.logging.Logger;
@@ -59,13 +60,13 @@ Socket get()
5960
final IO.Options opts = new IO.Options();
6061
opts.reconnection = false;
6162

62-
opts.path = "/" + Backendless.getApplicationId();
63+
opts.path = "/" + Backendless.getApplicationIdOrDomain();
6364

6465
opts.query = "apiKey=" + Backendless.getApiKey()
6566
+ "&clientId=" + Backendless.Messaging.getDeviceId()
6667
+ "&binary=true";
6768

68-
final String host = rtLookupService.lookup( ) + opts.path;
69+
final String host = rtLookupService.lookup() + opts.path;
6970
logger.info( "Looked up for server " + host );
7071

7172
String userToken = HeadersManager.getInstance().getHeader( HeadersManager.HeadersEnum.USER_TOKEN_KEY );

0 commit comments

Comments
 (0)