|
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; |
32 | 35 | import android.content.res.XmlResourceParser; |
33 | 36 | import android.os.Looper; |
| 37 | +import android.util.Log; |
34 | 38 |
|
35 | 39 | import androidx.annotation.NonNull; |
36 | 40 | import androidx.lifecycle.MutableLiveData; |
@@ -130,7 +134,7 @@ public LoginServer getSelectedLoginServer() { |
130 | 134 | if (name != null && url != null) { |
131 | 135 | LoginServer server = new LoginServer(name, url, isCustom); |
132 | 136 |
|
133 | | - // Only notify livedata consumers if the value has changed. |
| 137 | + // Only notify live data consumers if the value has changed. |
134 | 138 | if (!server.equals(selectedServer.getValue())) { |
135 | 139 | selectedServer.postValue(server); |
136 | 140 | } |
@@ -323,6 +327,88 @@ public List<LoginServer> getLoginServersFromPreferences() { |
323 | 327 | return getLoginServersFromPreferences(settings); |
324 | 328 | } |
325 | 329 |
|
| 330 | + /** |
| 331 | + * Reorders a custom login server in the list of login servers. |
| 332 | + * |
| 333 | + * @param originalIndex The original index of the custom login server. If this is not the index |
| 334 | + * of a custom login server, this method will do nothing |
| 335 | + * @param updatedIndex The new index of the custom login server. This must be after the non-custom |
| 336 | + * login server and within the updatable bounds of the list. If it is not it will |
| 337 | + * be automatically corrected |
| 338 | + */ |
| 339 | + @SuppressWarnings("unused") |
| 340 | + public void reorderCustomLoginServer( |
| 341 | + final int originalIndex, |
| 342 | + int updatedIndex |
| 343 | + ) { |
| 344 | + // Get the login server at the original index. |
| 345 | + final List<LoginServer> loginServers = getLoginServers(); |
| 346 | + final LoginServer originalLoginServer = loginServers.get(originalIndex); |
| 347 | + |
| 348 | + // Guard against reordering a non-custom login server. |
| 349 | + if (!originalLoginServer.isCustom) { |
| 350 | + return; |
| 351 | + } |
| 352 | + |
| 353 | + // Determine the last non-custom login server index. |
| 354 | + final List<LoginServer> servers = getLoginServers(); |
| 355 | + int lastNonCustomIndex = -1; |
| 356 | + for (int i = servers.size() - 1; i >= 0; i--) { |
| 357 | + if (!servers.get(i).isCustom) { |
| 358 | + lastNonCustomIndex = i; |
| 359 | + break; |
| 360 | + } |
| 361 | + } |
| 362 | + |
| 363 | + // Adjust the re-ordered custom login server index to be within bounds. |
| 364 | + if (updatedIndex <= lastNonCustomIndex) { |
| 365 | + updatedIndex = lastNonCustomIndex + 1; |
| 366 | + } else if (updatedIndex >= servers.size()) { |
| 367 | + updatedIndex = servers.size() - 1; |
| 368 | + } |
| 369 | + |
| 370 | + // Update the login server list. |
| 371 | + loginServers.remove(originalIndex); |
| 372 | + loginServers.add(updatedIndex, originalLoginServer); |
| 373 | + |
| 374 | + // Edit each login server indexed after the updated index. |
| 375 | + final Editor editor = settings.edit(); |
| 376 | + for (int i = updatedIndex; i < loginServers.size(); i++) { |
| 377 | + final LoginServer loginServer = loginServers.get(i); |
| 378 | + Log.i("LSM", "Re-order removing '" + format(US, SERVER_NAME, i) + "', '" + format(US, SERVER_URL, i) + "' '" + format(US, IS_CUSTOM, i) + "'."); |
| 379 | + Log.i("LSM", "Re-order adding '" + loginServer.name + "', '" + loginServer.url + "' '" + loginServer.isCustom + "'."); |
| 380 | + editor.remove(format(US, SERVER_NAME, i)) |
| 381 | + .remove(format(US, SERVER_URL, i)) |
| 382 | + .remove(format(US, IS_CUSTOM, i)) |
| 383 | + .putString(format(US, SERVER_NAME, i), loginServer.name) |
| 384 | + .putString(format(US, SERVER_URL, i), loginServer.url) |
| 385 | + .putBoolean(format(US, IS_CUSTOM, i), loginServer.isCustom); |
| 386 | + } |
| 387 | + editor.apply(); |
| 388 | + } |
| 389 | + |
| 390 | + /** |
| 391 | + * Replaces one custom login server with another. |
| 392 | + * |
| 393 | + * @param originalCustomLoginServer The original custom login server |
| 394 | + * @param updatedCustomLoginServer The updated custom login server |
| 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 | + removeServer(originalCustomLoginServer); |
| 408 | + addCustomLoginServer(updatedCustomLoginServer.name, updatedCustomLoginServer.url); |
| 409 | + reorderCustomLoginServer(getLoginServers().size() - 1, originalIndex); |
| 410 | + } |
| 411 | + |
326 | 412 | /** |
327 | 413 | * Returns production and sandbox as the login servers |
328 | 414 | * (only called when servers.xml is missing). |
|
0 commit comments