|
26 | 26 | */ |
27 | 27 | package com.salesforce.androidsdk.config; |
28 | 28 |
|
| 29 | +import static java.lang.String.format; |
| 30 | +import static java.util.Locale.US; |
| 31 | + |
29 | 32 | import android.content.Context; |
30 | 33 | import android.content.SharedPreferences; |
31 | 34 | import android.content.SharedPreferences.Editor; |
@@ -130,7 +133,7 @@ public LoginServer getSelectedLoginServer() { |
130 | 133 | if (name != null && url != null) { |
131 | 134 | LoginServer server = new LoginServer(name, url, isCustom); |
132 | 135 |
|
133 | | - // Only notify livedata consumers if the value has changed. |
| 136 | + // Only notify live data consumers if the value has changed. |
134 | 137 | if (!server.equals(selectedServer.getValue())) { |
135 | 138 | selectedServer.postValue(server); |
136 | 139 | } |
@@ -323,6 +326,95 @@ public List<LoginServer> getLoginServersFromPreferences() { |
323 | 326 | return getLoginServersFromPreferences(settings); |
324 | 327 | } |
325 | 328 |
|
| 329 | + /** |
| 330 | + * Reorders a custom login server in the list of login servers. |
| 331 | + * |
| 332 | + * @param originalIndex The original index of the custom login server. If this is not the index |
| 333 | + * of a custom login server, this method will do nothing |
| 334 | + * @param updatedIndex The new index of the custom login server. This must be after the last |
| 335 | + * non-custom login server and within the updatable bounds of the list. If |
| 336 | + * it is not it will be automatically corrected |
| 337 | + */ |
| 338 | + @SuppressWarnings("unused") |
| 339 | + public void reorderCustomLoginServer( |
| 340 | + final int originalIndex, |
| 341 | + int updatedIndex |
| 342 | + ) { |
| 343 | + // Get the login server at the original index. |
| 344 | + final List<LoginServer> loginServers = getLoginServers(); |
| 345 | + final LoginServer originalLoginServer = loginServers.get(originalIndex); |
| 346 | + |
| 347 | + // Guard against reordering a non-custom login server. |
| 348 | + if (!originalLoginServer.isCustom) { |
| 349 | + return; |
| 350 | + } |
| 351 | + |
| 352 | + // Determine the last non-custom login server index. |
| 353 | + final List<LoginServer> servers = getLoginServers(); |
| 354 | + int lastNonCustomIndex = -1; |
| 355 | + for (int i = servers.size() - 1; i >= 0; i--) { |
| 356 | + if (!servers.get(i).isCustom) { |
| 357 | + lastNonCustomIndex = i; |
| 358 | + break; |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + // Adjust the re-ordered custom login server index to be within bounds. |
| 363 | + if (updatedIndex <= lastNonCustomIndex) { |
| 364 | + updatedIndex = lastNonCustomIndex + 1; |
| 365 | + } else if (updatedIndex >= servers.size()) { |
| 366 | + updatedIndex = servers.size() - 1; |
| 367 | + } |
| 368 | + |
| 369 | + // Update the login server list. |
| 370 | + loginServers.remove(originalIndex); |
| 371 | + loginServers.add(updatedIndex, originalLoginServer); |
| 372 | + |
| 373 | + // Edit each login server indexed after the updated index. |
| 374 | + final Editor editor = settings.edit(); |
| 375 | + for (int i = updatedIndex; i < loginServers.size(); i++) { |
| 376 | + final LoginServer loginServer = loginServers.get(i); |
| 377 | + editor.remove(format(US, SERVER_NAME, i)) |
| 378 | + .remove(format(US, SERVER_URL, i)) |
| 379 | + .remove(format(US, IS_CUSTOM, i)) |
| 380 | + .putString(format(US, SERVER_NAME, i), loginServer.name) |
| 381 | + .putString(format(US, SERVER_URL, i), loginServer.url) |
| 382 | + .putBoolean(format(US, IS_CUSTOM, i), loginServer.isCustom); |
| 383 | + } |
| 384 | + editor.apply(); |
| 385 | + } |
| 386 | + |
| 387 | + /** |
| 388 | + * Replaces one custom login server with another. |
| 389 | + * |
| 390 | + * @param originalCustomLoginServer The original custom login server. If this is not a custom |
| 391 | + * login server or doesn't match an existing login server this |
| 392 | + * method will do nothing. |
| 393 | + * @param updatedCustomLoginServer The updated custom login server. If this is not a custom |
| 394 | + * login server this method will do nothing. |
| 395 | + */ |
| 396 | + @SuppressWarnings("unused") |
| 397 | + public void replaceCustomLoginServer( |
| 398 | + final LoginServer originalCustomLoginServer, |
| 399 | + final LoginServer updatedCustomLoginServer |
| 400 | + ) { |
| 401 | + // Guard against replacing a non-custom login server. |
| 402 | + if (!originalCustomLoginServer.isCustom || !updatedCustomLoginServer.isCustom) { |
| 403 | + return; |
| 404 | + } |
| 405 | + |
| 406 | + final int originalIndex = getLoginServers().indexOf(originalCustomLoginServer); |
| 407 | + |
| 408 | + // Guard against an original login server that doesn't exist. |
| 409 | + if (originalIndex == -1) { |
| 410 | + return; |
| 411 | + } |
| 412 | + |
| 413 | + removeServer(originalCustomLoginServer); |
| 414 | + addCustomLoginServer(updatedCustomLoginServer.name, updatedCustomLoginServer.url); |
| 415 | + reorderCustomLoginServer(getLoginServers().size() - 1, originalIndex); |
| 416 | + } |
| 417 | + |
326 | 418 | /** |
327 | 419 | * Returns production and sandbox as the login servers |
328 | 420 | * (only called when servers.xml is missing). |
|
0 commit comments