From bc8fe27268c4b96c349d041ae611cbfa58d2c173 Mon Sep 17 00:00:00 2001 From: Rahul Raveendran V P Date: Wed, 12 Jun 2024 14:51:22 +0530 Subject: [PATCH] [MOBL-1834] Remove the cached email from SharedPreferences (#360) * Renamed encrypted preference file to have the blueshift package name * Removed the check for email address change to fire identify event * Fixed the confusion regarding naming * Fixed the confusion regarding naming of the flag * Fixed the tests wit additional test case and naming corrections --- .../java/com/blueshift/model/UserInfoTest.kt | 41 +++++++++------- .../com/blueshift/BlueShiftPreference.java | 32 +++---------- .../main/java/com/blueshift/Blueshift.java | 5 +- .../BlueshiftEncryptedPreferences.kt | 2 +- .../com/blueshift/model/Configuration.java | 10 ++-- .../java/com/blueshift/model/UserInfo.java | 48 +++++++++++-------- .../request_queue/RequestDispatcher.java | 16 +------ 7 files changed, 71 insertions(+), 83 deletions(-) diff --git a/android-sdk/src/androidTest/java/com/blueshift/model/UserInfoTest.kt b/android-sdk/src/androidTest/java/com/blueshift/model/UserInfoTest.kt index 31be6d49..88321951 100644 --- a/android-sdk/src/androidTest/java/com/blueshift/model/UserInfoTest.kt +++ b/android-sdk/src/androidTest/java/com/blueshift/model/UserInfoTest.kt @@ -9,13 +9,13 @@ import org.junit.Test class UserInfoTest { private lateinit var context: Context - private lateinit var legacyPreference: SharedPreferences + private lateinit var sharedPreferences: SharedPreferences - private fun oldPreferenceFile(context: Context): String { + private fun sharedPreferencesFilename(context: Context): String { return context.packageName + ".user_info_file" } - private fun oldPreferenceKey(context: Context): String { + private fun sharedPreferencesKey(context: Context): String { return context.packageName + ".user_info_key" } @@ -24,10 +24,10 @@ class UserInfoTest { context = InstrumentationRegistry.getInstrumentation().targetContext // Reset old preferences - legacyPreference = context.getSharedPreferences( - oldPreferenceFile(context), Context.MODE_PRIVATE + sharedPreferences = context.getSharedPreferences( + sharedPreferencesFilename(context), Context.MODE_PRIVATE ) - legacyPreference.edit().remove(oldPreferenceKey(context)).commit() + sharedPreferences.edit().remove(sharedPreferencesKey(context)).commit() // Reset new preferences BlueshiftEncryptedPreferences.init(context) @@ -44,18 +44,27 @@ class UserInfoTest { @Test fun load_updatedFromOldSDK_returnSameUserInfoObject() { + val emailPreferencesKey = "john.doe@examplepetstore.com" + val emailPreferencesFileName = "${context.packageName}.BsftEmailPrefFile" + val emailPreferences = context.getSharedPreferences(emailPreferencesFileName, Context.MODE_PRIVATE) + emailPreferences.edit().putBoolean(emailPreferencesKey, true).apply() + // Mock the presence of a user object in the old preference. - legacyPreference.edit().putString(oldPreferenceKey(context), USER_JSON).apply() + sharedPreferences.edit().putString(sharedPreferencesKey(context), USER_JSON).apply() // When encryption is not enabled, the user info class should provide the same value // for its members as we saved in the old preference. - val userinfo = UserInfo.load(context, false) - assert(userinfo.name == JOHN) + val name = UserInfo.load(context, false).name + assert(name == JOHN) - // When encryption is not enabled, the user info class should provide the same value + // When encryption is enabled, the user info class should provide the same value // for its members as we saved in the old preference. - val userinfo2 = UserInfo.load(context, true) - assert(userinfo2.name == JOHN) + val encryptedName = UserInfo.load(context, true).name + assert(encryptedName == JOHN) + + // When encryption is enabled, the data stored in email preferences (if any) should be deleted. + val status = emailPreferences.getBoolean(emailPreferencesKey, false) + assert(!status) // Kill the existing instance for the next test. UserInfo.killInstance() @@ -64,7 +73,7 @@ class UserInfoTest { @Test fun load_updatedFromOldSDK_copiesTheContentOfOldPrefToNewPref() { // Mock the presence of a user object in the old preference. - legacyPreference.edit().putString(oldPreferenceKey(context), USER_JSON).apply() + sharedPreferences.edit().putString(sharedPreferencesKey(context), USER_JSON).apply() // case1 : When encryption is not enabled. val userInfo = UserInfo.load(context, false) @@ -82,7 +91,7 @@ class UserInfoTest { @Test fun load_updatedFromOldSDK_deletesTheDataInOldPreference() { // Mock the presence of a user object in the old preference. - legacyPreference.edit().putString(oldPreferenceKey(context), USER_JSON).apply() + sharedPreferences.edit().putString(sharedPreferencesKey(context), USER_JSON).apply() // case1 : When encryption is not enabled. val userInfo = UserInfo.load(context, false) @@ -91,8 +100,8 @@ class UserInfoTest { // case2 : When encryption is enabled. UserInfo.load(context, true) // Make sure the value stored in the old preferences is removed after copying it to the new preferences. - val legacyJson = legacyPreference.getString(oldPreferenceKey(context), null) - assert(legacyJson == null) + val spUserJson = sharedPreferences.getString(sharedPreferencesKey(context), null) + assert(spUserJson == null) // Kill the existing instance for the next test. UserInfo.killInstance() diff --git a/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java b/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java index c75f76fe..004795fa 100644 --- a/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java +++ b/android-sdk/src/main/java/com/blueshift/BlueShiftPreference.java @@ -126,31 +126,6 @@ public static boolean didPushPermissionStatusChange(Context context) { return true; } - public static boolean isEmailAlreadyIdentified(Context context, String email) { - boolean result = false; - - if (context != null && !TextUtils.isEmpty(email)) { - SharedPreferences preferences = getEmailPreference(context); - if (preferences != null) { - result = preferences.getBoolean(email, false); - } - } - - return result; - } - - public static void markEmailAsIdentified(Context context, String email) { - if (context != null && !TextUtils.isEmpty(email)) { - SharedPreferences preferences = getEmailPreference(context); - if (preferences != null) { - preferences - .edit() - .putBoolean(email, true) - .apply(); - } - } - } - private static SharedPreferences getBlueshiftPreferences(Context context) { SharedPreferences preferences = null; @@ -161,6 +136,13 @@ private static SharedPreferences getBlueshiftPreferences(Context context) { return preferences; } + public static void removeCachedEmailAddress(Context context) { + SharedPreferences preferences = getEmailPreference(context); + if (preferences != null) { + preferences.edit().clear().apply(); + } + } + private static SharedPreferences getEmailPreference(Context context) { SharedPreferences preferences = null; diff --git a/android-sdk/src/main/java/com/blueshift/Blueshift.java b/android-sdk/src/main/java/com/blueshift/Blueshift.java index 15e1d86f..8318de38 100644 --- a/android-sdk/src/main/java/com/blueshift/Blueshift.java +++ b/android-sdk/src/main/java/com/blueshift/Blueshift.java @@ -354,7 +354,10 @@ public void getLiveContentByCustomerId(@NonNull String slot, HashMap toHashMap() { public void save(@NonNull Context context) { Configuration configuration = BlueshiftUtils.getConfiguration(context); - boolean isEncryptionEnabled = configuration != null && configuration.shouldEncryptUserInfo(); + boolean isEncryptionEnabled = configuration != null && configuration.shouldSaveUserInfoAsEncrypted(); save(context, isEncryptionEnabled); } void save(Context context, boolean encryptionEnabled) { if (encryptionEnabled) { - saveEncrypted(); + saveToEncryptedSharedPreferences(); } else { - saveLegacy(context); + saveToSharedPreferences(context); } } - private void saveLegacy(Context context) { + private void saveToSharedPreferences(Context context) { String json = new Gson().toJson(this); - context.getSharedPreferences(getLegacyPreferenceFile(context), Context.MODE_PRIVATE) - .edit().putString(getLegacyPreferenceKey(context), json).apply(); + context.getSharedPreferences(getSharedPreferencesFilename(context), Context.MODE_PRIVATE) + .edit().putString(getSharedPreferencesKey(context), json).apply(); } - private void saveEncrypted() { + private void saveToEncryptedSharedPreferences() { String json = new Gson().toJson(this); BlueshiftEncryptedPreferences.INSTANCE.putString(PREF_KEY_ENCRYPTED, json); } diff --git a/android-sdk/src/main/java/com/blueshift/request_queue/RequestDispatcher.java b/android-sdk/src/main/java/com/blueshift/request_queue/RequestDispatcher.java index 9899d9d9..c8527f8a 100644 --- a/android-sdk/src/main/java/com/blueshift/request_queue/RequestDispatcher.java +++ b/android-sdk/src/main/java/com/blueshift/request_queue/RequestDispatcher.java @@ -224,24 +224,10 @@ private boolean addDeviceIdAndTokenToParams(String deviceId, String token) { } private void doAutoIdentifyCheck(Context context) { - boolean identified = didEmailChange(context) || didPushPermissionChange(context); + boolean identified = didPushPermissionChange(context); if (identified) BlueshiftLogger.d(LOG_TAG, "Auto identify call fired."); } - private boolean didEmailChange(Context context) { - UserInfo userInfo = UserInfo.getInstance(context); - if (userInfo != null && !TextUtils.isEmpty(userInfo.getEmail())) { - if (!BlueShiftPreference.isEmailAlreadyIdentified(context, userInfo.getEmail())) { - identify(context); - BlueShiftPreference.markEmailAsIdentified(context, userInfo.getEmail()); - BlueshiftLogger.d(LOG_TAG, "Change in email detected. Sending \"identify\"."); - return true; - } - } - - return false; - } - private boolean didPushPermissionChange(Context context) { if (BlueShiftPreference.didPushPermissionStatusChange(context)) { identify(context);