package com.helpshift.user; import com.applovin.impl.sdk.utils.JsonUtils; import com.helpshift.concurrency.HSThreadingService; import com.helpshift.log.HSLogger; import com.helpshift.notification.CoreNotificationManager; import com.helpshift.notification.HSPushTokenManager; import com.helpshift.poller.ConversationPoller; import com.helpshift.poller.FetchNotificationUpdate; import com.helpshift.storage.HSGenericDataManager; import com.helpshift.storage.HSPersistentStorage; import com.helpshift.user_lifecyle.UserLifecycleListener; import com.helpshift.util.Utils; import com.helpshift.util.ValueListener; import com.helpshift.util.ValuePair; import com.mbridge.msdk.newreward.function.common.MBridgeCommon; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes3.dex */ public class UserManager { public ConversationPoller conversationPoller; public FetchNotificationUpdate fetchNotificationUpdate; public final HSGenericDataManager genericDataManager; public final HSThreadingService hsThreadingService; public final CoreNotificationManager notificationManager; public final HSPersistentStorage persistentStorage; public final HSPushTokenManager pushTokenManager; public WeakReference userLifecycleListenerRef = new WeakReference(null); public void setConversationPoller(ConversationPoller conversationPoller) { this.conversationPoller = conversationPoller; } public void setFetchNotificationUpdateFunction(FetchNotificationUpdate fetchNotificationUpdate) { this.fetchNotificationUpdate = fetchNotificationUpdate; } public UserManager(HSPersistentStorage hSPersistentStorage, HSPushTokenManager hSPushTokenManager, HSGenericDataManager hSGenericDataManager, HSThreadingService hSThreadingService, CoreNotificationManager coreNotificationManager) { this.persistentStorage = hSPersistentStorage; this.genericDataManager = hSGenericDataManager; this.pushTokenManager = hSPushTokenManager; this.hsThreadingService = hSThreadingService; this.notificationManager = coreNotificationManager; } public boolean retryPushTokenSync() { if (isPushTokenSynced() || !shouldPoll() || Utils.isEmpty(this.persistentStorage.getCurrentPushToken())) { return false; } this.pushTokenManager.registerPushTokenWithBackend(this.persistentStorage.getCurrentPushToken(), getActiveUserDataForNetworkCall(), new UpdatePushSyncStatus()); return true; } public void registerPushToken(final String str) { if (shouldSyncPushToken(str)) { boolean z = Utils.isNotEmpty(str) && !str.equals(this.persistentStorage.getCurrentPushToken()); this.pushTokenManager.savePushToken(str); setPushTokenSynced(false); Map loggedInUserDetails = getLoggedInUserDetails(); if (Utils.isEmpty(loggedInUserDetails)) { loggedInUserDetails = getAnonymousUserDetails(); } if (!Utils.isEmpty(loggedInUserDetails) && z && shouldPoll()) { this.hsThreadingService.getNetworkService().submit(new Runnable() { // from class: com.helpshift.user.UserManager.1 @Override // java.lang.Runnable public void run() { UserManager.this.fetchNotificationUpdate.execute(); if (UserManager.this.shouldPoll()) { UserManager.this.pushTokenManager.registerPushTokenWithBackend(str, UserManager.this.getActiveUserDataForNetworkCall(), UserManager.this.new UpdatePushSyncStatus()); } } }); } } } public void login(Map map) { if (Utils.isEmpty(map)) { HSLogger.e("UsrMngr", "Empty data for user login"); return; } Map loggedInUserDetails = getLoggedInUserDetails(); if (Utils.isNotEmpty(loggedInUserDetails) && loggedInUserDetails.equals(map)) { return; } if (Utils.isEmpty(loggedInUserDetails)) { loggedInUserDetails = getAnonymousUserDetails(); } this.conversationPoller.stopPoller(); if (isPushTokenSynced()) { this.pushTokenManager.deregisterPushTokenForUser(getUserDataForNetworkCall(loggedInUserDetails), new UpdatePushSyncStatus()); } cleanUpActiveUser(); this.persistentStorage.setActiveUser(new JSONObject(map).toString()); if (this.userLifecycleListenerRef.get() != null) { ((UserLifecycleListener) this.userLifecycleListenerRef.get()).onUserDidLogin(); } startNotificationUpdatesSync(); } public void logout() { Map loggedInUserDetails = getLoggedInUserDetails(); if (Utils.isEmpty(loggedInUserDetails)) { return; } this.conversationPoller.stopPoller(); cleanUpActiveUser(); this.pushTokenManager.deregisterPushTokenForUser(getUserDataForNetworkCall(loggedInUserDetails), new UpdatePushSyncStatus()); if (getClearAnonymousUserOnLoginFlag()) { removeAnonymousUser(); generateAndSaveAnonymousUserIdIfNeeded(); } if (this.userLifecycleListenerRef.get() != null) { ((UserLifecycleListener) this.userLifecycleListenerRef.get()).onUserDidLogout(); } startNotificationUpdatesSync(); } public final void cleanUpActiveUser() { this.persistentStorage.removeActiveUser(); this.persistentStorage.putString("active_user_data", JsonUtils.EMPTY_JSON); this.persistentStorage.setFailedAnalyticsEvents(new JSONArray()); this.notificationManager.cancelNotifications(); } public void removeAnonymousUser() { this.persistentStorage.removeAnonymousUserIdMap(); this.persistentStorage.putString("anon_user_data", JsonUtils.EMPTY_JSON); } public boolean getClearAnonymousUserOnLoginFlag() { return this.persistentStorage.isClearAnonymousUser(); } public String getActiveUserId() { String userInfoForKey = getUserInfoForKey("userId"); if (!Utils.isEmpty(userInfoForKey)) { return userInfoForKey; } Map anonymousUserDetails = getAnonymousUserDetails(); return !Utils.isEmpty(anonymousUserDetails) ? (String) anonymousUserDetails.get("userId") : userInfoForKey; } public String getActiveUserEmail() { return getUserInfoForKey("userEmail"); } public final String getUserInfoForKey(String str) { String activeUser = this.persistentStorage.getActiveUser(); if (activeUser.isEmpty()) { return ""; } try { return new JSONObject(activeUser).getString(str); } catch (JSONException e) { HSLogger.e("UsrMngr", "error in getting user info for key: " + str, e); return ""; } } public Map getActiveUserDataForNetworkCall() { Map loggedInUserDetails = getLoggedInUserDetails(); if (Utils.isEmpty(loggedInUserDetails)) { loggedInUserDetails = getAnonymousUserDetails(); } if (Utils.isEmpty(loggedInUserDetails)) { return new HashMap(); } return getUserDataForNetworkCall(loggedInUserDetails); } public Map getUserDataForNetworkCall(Map map) { Map userDataKeyMapping = this.genericDataManager.getUserDataKeyMapping(); if (Utils.isEmpty(userDataKeyMapping)) { return null; } HashMap hashMap = new HashMap(); for (String str : map.keySet()) { String str2 = (String) userDataKeyMapping.get(str); if (Utils.isNotEmpty(str2)) { hashMap.put(str2, map.get(str)); } } return hashMap; } public final Map getLoggedInUserDetails() { return Utils.jsonStringToStringMap(this.persistentStorage.getActiveUser()); } public final Map getAnonymousUserDetails() { return Utils.jsonStringToStringMap(this.persistentStorage.getAnonymousUserIdMap()); } public String generateAnonymousUserId() { return "hsft_anon_" + System.currentTimeMillis() + "-" + UUID.randomUUID().toString().replaceAll("-", "").substring(0, 15); } public void generateAndSaveAnonymousUserIdIfNeeded() { if (getAnonymousUserDetails().isEmpty()) { HSLogger.d("UsrMngr", "Existing anon user details not found. Generating new anon user ID"); JSONObject jSONObject = new JSONObject(); try { jSONObject.put("userId", generateAnonymousUserId()); this.persistentStorage.storeAnonymousUserIdMap(jSONObject.toString()); return; } catch (Exception unused) { HSLogger.d("UsrMngr", "Error in saving the anonymous local user id"); return; } } HSLogger.d("UsrMngr", "Existing anon user details found. Not generating new anon user ID"); } public void setUserLifecycleListener(UserLifecycleListener userLifecycleListener) { this.userLifecycleListenerRef = new WeakReference(userLifecycleListener); } public void removeUserLifeCycleListener() { this.userLifecycleListenerRef.clear(); } public final void startNotificationUpdatesSync() { this.hsThreadingService.getNetworkService().submit(new Runnable() { // from class: com.helpshift.user.UserManager.2 @Override // java.lang.Runnable public void run() { UserManager.this.fetchNotificationUpdate.execute(); if (UserManager.this.shouldPoll()) { String currentPushToken = UserManager.this.persistentStorage.getCurrentPushToken(); if (Utils.isEmpty(currentPushToken)) { UserManager.this.conversationPoller.startPoller(); } else { UserManager.this.pushTokenManager.registerPushTokenWithBackend(currentPushToken, UserManager.this.getActiveUserDataForNetworkCall(), UserManager.this.new UpdatePushSyncStatus()); } } } }); } public final boolean shouldSyncPushToken(String str) { String currentPushToken = this.persistentStorage.getCurrentPushToken(); return (!Utils.isEmpty(currentPushToken) && currentPushToken.equals(str) && isPushTokenSynced()) ? false : true; } public final ValuePair getActiveUserData() { String str = "anon_user_data"; JSONObject jSONObject = new JSONObject(); String str2 = ""; try { String str3 = JsonUtils.EMPTY_JSON; if (!Utils.isEmpty(getLoggedInUserDetails())) { str3 = this.persistentStorage.getString("active_user_data"); str = "active_user_data"; } else if (Utils.isEmpty(getAnonymousUserDetails())) { str = ""; } else { str3 = this.persistentStorage.getString("anon_user_data"); } try { if (!Utils.isEmpty(str3)) { jSONObject = new JSONObject(str3); } } catch (Exception e) { str2 = str; e = e; HSLogger.e("UsrMngr", "Error getting active user in user data", e); str = str2; return new ValuePair(str, jSONObject); } } catch (Exception e2) { e = e2; } return new ValuePair(str, jSONObject); } public void setShouldPollFlag(boolean z) { setUserDataValues("should_poll", Boolean.valueOf(z)); } public void setPollerCursor(long j) { setUserDataValues("cursor", Long.valueOf(j)); } public void setPollingBaseInterval(int i) { setUserDataValues("base_polling_interval", Integer.valueOf(i)); } public void setPollingMaxInterval(int i) { setUserDataValues("max_polling_interval", Integer.valueOf(i)); } public void updateUnreadCountBy(int i) { setUserDataValues("unread_count", Integer.valueOf(getUnreadNotificationCount() + i)); } public void markAllMessagesAsRead() { setUserDataValues("unread_count", 0); } public void setPushTokenSynced(boolean z) { setUserDataValues("push_token_synced", Boolean.valueOf(z)); } public boolean shouldPoll() { return ((Boolean) getUserDataValue("should_poll", Boolean.FALSE)).booleanValue(); } public long getPollerCursor() { return Long.valueOf(getUserDataValue("cursor", 0) + "").longValue(); } public int getPollingMaxInterval() { return ((Integer) getUserDataValue("max_polling_interval", Integer.valueOf(MBridgeCommon.DEFAULT_LOAD_TIMEOUT))).intValue(); } public int getPollingBaseInterval() { return ((Integer) getUserDataValue("base_polling_interval", 5000)).intValue(); } public int getUnreadNotificationCount() { return ((Integer) getUserDataValue("unread_count", 0)).intValue(); } public int getPushUnreadNotificationCount() { return ((Integer) getUserDataValue("push_unread_count", 0)).intValue(); } public void updatePushUnreadCountBy(int i) { setUserDataValues("push_unread_count", Integer.valueOf(getPushUnreadNotificationCount() + i)); } public void markAllPushMessagesAsRead() { setUserDataValues("push_unread_count", 0); } public boolean isPushTokenSynced() { return ((Boolean) getUserDataValue("push_token_synced", Boolean.FALSE)).booleanValue(); } public void setShowChatIconInHelpcenter(boolean z) { setUserDataValues("show_chat_icon_in_helpcenter", Boolean.valueOf(z)); } public boolean shouldShowChatIconInHelpcenter() { return ((Boolean) getUserDataValue("show_chat_icon_in_helpcenter", Boolean.FALSE)).booleanValue(); } public final Object getUserDataValue(String str, Object obj) { Object opt; ValuePair activeUserData = getActiveUserData(); return (Utils.isEmpty((String) activeUserData.first) || (opt = ((JSONObject) activeUserData.second).opt(str)) == null) ? obj : opt; } public final void setUserDataValues(String str, Object obj) { ValuePair activeUserData = getActiveUserData(); if (Utils.isEmpty((String) activeUserData.first)) { return; } ((JSONObject) activeUserData.second).put(str, obj); this.persistentStorage.putString((String) activeUserData.first, ((JSONObject) activeUserData.second).toString()); } public class UpdatePushSyncStatus implements ValueListener { public UpdatePushSyncStatus() { } @Override // com.helpshift.util.ValueListener public void update(Boolean bool) { UserManager.this.setPushTokenSynced(bool.booleanValue()); } } }