|
4 | 4 | import android.content.Context; |
5 | 5 | import android.content.Intent; |
6 | 6 | import android.content.SharedPreferences; |
| 7 | +import android.os.Build; |
7 | 8 | import android.os.Bundle; |
8 | 9 |
|
9 | 10 | import androidx.annotation.NonNull; |
@@ -44,6 +45,7 @@ public class IterableApi { |
44 | 45 | private String inboxSessionId; |
45 | 46 | private IterableAuthManager authManager; |
46 | 47 | private HashMap<String, String> deviceAttributes = new HashMap<>(); |
| 48 | + private IterableKeychain keychain; |
47 | 49 |
|
48 | 50 | void fetchRemoteConfiguration() { |
49 | 51 | apiClient.getRemoteConfiguration(new IterableHelper.IterableActionHandler() { |
@@ -130,6 +132,15 @@ IterableAuthManager getAuthManager() { |
130 | 132 | return authManager; |
131 | 133 | } |
132 | 134 |
|
| 135 | + @NonNull |
| 136 | + IterableKeychain getKeychain() { |
| 137 | + if (keychain == null) { |
| 138 | + keychain = new IterableKeychain(getMainActivityContext()); |
| 139 | + } |
| 140 | + |
| 141 | + return keychain; |
| 142 | + } |
| 143 | + |
133 | 144 | static void loadLastSavedConfiguration(Context context) { |
134 | 145 | SharedPreferences sharedPref = context.getSharedPreferences(IterableConstants.SHARED_PREFS_SAVED_CONFIGURATION, Context.MODE_PRIVATE); |
135 | 146 | boolean offlineMode = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_OFFLINE_MODE_KEY, false); |
@@ -330,31 +341,85 @@ private String getDeviceId() { |
330 | 341 | } |
331 | 342 |
|
332 | 343 | private void storeAuthData() { |
333 | | - try { |
334 | | - SharedPreferences.Editor editor = getPreferences().edit(); |
335 | | - editor.putString(IterableConstants.SHARED_PREFS_EMAIL_KEY, _email); |
336 | | - editor.putString(IterableConstants.SHARED_PREFS_USERID_KEY, _userId); |
337 | | - editor.putString(IterableConstants.SHARED_PREFS_AUTH_TOKEN_KEY, _authToken); |
338 | | - editor.commit(); |
339 | | - } catch (Exception e) { |
340 | | - IterableLogger.e(TAG, "Error while persisting email/userId", e); |
| 344 | + if (hasEncryptionDependency()) { |
| 345 | + getKeychain().saveEmail(_email); |
| 346 | + getKeychain().saveUserId(_userId); |
| 347 | + getKeychain().saveAuthToken(_authToken); |
| 348 | + } else { |
| 349 | + try { |
| 350 | + SharedPreferences.Editor editor = getPreferences().edit(); |
| 351 | + editor.putString(IterableConstants.SHARED_PREFS_EMAIL_KEY, _email); |
| 352 | + editor.putString(IterableConstants.SHARED_PREFS_USERID_KEY, _userId); |
| 353 | + editor.putString(IterableConstants.SHARED_PREFS_AUTH_TOKEN_KEY, _authToken); |
| 354 | + editor.commit(); |
| 355 | + } catch (Exception e) { |
| 356 | + IterableLogger.e(TAG, "Error while persisting email/userId", e); |
| 357 | + } |
341 | 358 | } |
342 | 359 | } |
343 | 360 |
|
344 | 361 | private void retrieveEmailAndUserId() { |
345 | | - try { |
| 362 | + if (hasEncryptionDependency()) { |
| 363 | + _email = getKeychain().getEmail(); |
| 364 | + _userId = getKeychain().getUserId(); |
| 365 | + _authToken = getKeychain().getAuthToken(); |
| 366 | + } else { |
346 | 367 | SharedPreferences prefs = getPreferences(); |
347 | 368 | _email = prefs.getString(IterableConstants.SHARED_PREFS_EMAIL_KEY, null); |
348 | 369 | _userId = prefs.getString(IterableConstants.SHARED_PREFS_USERID_KEY, null); |
349 | 370 | _authToken = prefs.getString(IterableConstants.SHARED_PREFS_AUTH_TOKEN_KEY, null); |
350 | | - if (_authToken != null) { |
351 | | - getAuthManager().queueExpirationRefresh(_authToken); |
352 | | - } |
353 | | - } catch (Exception e) { |
354 | | - IterableLogger.e(TAG, "Error while retrieving email/userId/authToken", e); |
| 371 | + } |
| 372 | + |
| 373 | + if (_authToken != null) { |
| 374 | + getAuthManager().queueExpirationRefresh(_authToken); |
| 375 | + } |
| 376 | + } |
| 377 | + |
| 378 | + private void updateSDKVersion() { |
| 379 | + if (hasEncryptionDependency()) { |
| 380 | + migrateAuthDataFromSharedPrefsToKeychain(); |
355 | 381 | } |
356 | 382 | } |
357 | 383 |
|
| 384 | + private void migrateAuthDataFromSharedPrefsToKeychain() { |
| 385 | + SharedPreferences prefs = getPreferences(); |
| 386 | + String sharedPrefsEmail = prefs.getString(IterableConstants.SHARED_PREFS_EMAIL_KEY, null); |
| 387 | + String sharedPrefsUserId = prefs.getString(IterableConstants.SHARED_PREFS_USERID_KEY, null); |
| 388 | + String sharedPrefsAuthToken = prefs.getString(IterableConstants.SHARED_PREFS_AUTH_TOKEN_KEY, null); |
| 389 | + |
| 390 | + SharedPreferences.Editor editor = getPreferences().edit(); |
| 391 | + |
| 392 | + if (getKeychain().getEmail() == null && sharedPrefsEmail != null) { |
| 393 | + getKeychain().saveEmail(sharedPrefsEmail); |
| 394 | + editor.remove(IterableConstants.SHARED_PREFS_EMAIL_KEY); |
| 395 | + IterableLogger.v(TAG, "UPDATED: migrated email from SharedPreferences to IterableKeychain"); |
| 396 | + } else if (sharedPrefsEmail != null) { |
| 397 | + editor.remove(IterableConstants.SHARED_PREFS_EMAIL_KEY); |
| 398 | + } |
| 399 | + |
| 400 | + if (getKeychain().getUserId() == null && sharedPrefsUserId != null) { |
| 401 | + getKeychain().saveUserId(sharedPrefsUserId); |
| 402 | + editor.remove(IterableConstants.SHARED_PREFS_USERID_KEY); |
| 403 | + IterableLogger.v(TAG, "UPDATED: migrated userId from SharedPreferences to IterableKeychain"); |
| 404 | + } else if (sharedPrefsUserId != null) { |
| 405 | + editor.remove(IterableConstants.SHARED_PREFS_USERID_KEY); |
| 406 | + } |
| 407 | + |
| 408 | + if (getKeychain().getAuthToken() == null && sharedPrefsAuthToken != null) { |
| 409 | + getKeychain().saveAuthToken(sharedPrefsAuthToken); |
| 410 | + editor.remove(IterableConstants.SHARED_PREFS_AUTH_TOKEN_KEY); |
| 411 | + IterableLogger.v(TAG, "UPDATED: migrated authToken from SharedPreferences to IterableKeychain"); |
| 412 | + } else if (sharedPrefsAuthToken != null) { |
| 413 | + editor.remove(IterableConstants.SHARED_PREFS_AUTH_TOKEN_KEY); |
| 414 | + } |
| 415 | + |
| 416 | + editor.apply(); |
| 417 | + } |
| 418 | + |
| 419 | + private boolean hasEncryptionDependency() { |
| 420 | + return Build.VERSION.SDK_INT >= 23; |
| 421 | + } |
| 422 | + |
358 | 423 | private class IterableApiAuthProvider implements IterableApiClient.AuthProvider { |
359 | 424 | @Nullable |
360 | 425 | @Override |
@@ -488,6 +553,8 @@ public static void initialize(@NonNull Context context, @NonNull String apiKey, |
488 | 553 | sharedInstance.config = new IterableConfig.Builder().build(); |
489 | 554 | } |
490 | 555 |
|
| 556 | + sharedInstance.updateSDKVersion(); |
| 557 | + |
491 | 558 | sharedInstance.retrieveEmailAndUserId(); |
492 | 559 |
|
493 | 560 | IterableActivityMonitor.getInstance().registerLifecycleCallbacks(context); |
|
0 commit comments