@Test public void testInsertAliasRequest() throws JSONException { final AndroidUtils.Mutable<JSONObject> insertedAliasRequest = new AndroidUtils.Mutable<JSONObject>(null); Mockito.when(mConfigManager.getDeviceApplicationStamp()).thenReturn("das"); MParticleDBManager database = new MParticleDBManager(new MockContext()) { @Override public void insertAliasRequest(String apiKey, JSONObject request) { insertedAliasRequest.value = request; } @Override public MPDatabase getDatabase() { return null; } }; handler.mMParticleDBManager = database; assertNull(insertedAliasRequest.value); AliasRequest aliasRequest = TestingUtils.getInstance().getRandomAliasRequest(); MPAliasMessage aliasMessage = new MPAliasMessage(aliasRequest, "das","apiKey");; Message mockMessage = Mockito.mock(Message.class); mockMessage.what = MessageHandler.STORE_ALIAS_MESSAGE; mockMessage.obj = aliasMessage; handler.handleMessageImpl(mockMessage); assertNotNull(insertedAliasRequest.value); aliasMessage.remove(REQUEST_ID); insertedAliasRequest.value.remove(REQUEST_ID); assertJsonEqual(aliasMessage, insertedAliasRequest.value); }
@Override public void handleMessageImpl(Message msg) { try { if (!databaseAvailable()) { return; } mMessageManagerCallbacks.delayedStart(); }catch (Exception e) { Logger.verbose(e.toString()); } switch (msg.what) { case STORE_MESSAGE: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(MessageKey.STATE_INFO_KEY, MessageManager.getStateInfo()); String messageType = message.getString(MessageKey.TYPE); if (MessageType.SESSION_START.equals(messageType)) { dbInsertSession(message); }else{ mMParticleDBManager.updateSessionEndTime(message.getSessionId(), message.getLong(MessageKey.TIMESTAMP), 0); message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); } if (MessageType.ERROR.equals(messageType)){ mMParticleDBManager.appendBreadcrumbs(message); } try { mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), message, mDataplanId, mDataplanVersion); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } mMessageManagerCallbacks.checkForTrigger(message); } catch (Exception e) { Logger.error(e, "Error saving message to mParticle DB."); } break; case INSTALL_REFERRER_UPDATED: try { mMParticleDBManager.updateSessionInstallReferrer((String) msg.obj, mMessageManagerCallbacks.getDeviceAttributes().getAppInfo(mContext, true)); }catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_ATTRIBUTES: try { JSONObject sessionAttributes = (JSONObject) msg.obj; String sessionId = sessionAttributes.getString(MessageKey.SESSION_ID); String attributes = sessionAttributes.getString(MessageKey.ATTRIBUTES); mMParticleDBManager.updateSessionAttributes(sessionId, attributes); } catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_END: try { InternalSession session = (InternalSession) msg.obj; mMParticleDBManager.updateSessionEndTime(session.mSessionID, session.mLastEventTime, session.getForegroundTime()); } catch (Exception e) { Logger.error(e, "Error updating session end time in mParticle DB."); } break; case CREATE_SESSION_END_MESSAGE: try { Map.Entry<String, Set<Long>> entry = (Map.Entry<String, Set<Long>>) msg.obj; BaseMPMessage endMessage = null; String sessionId = entry.getKey(); try { endMessage = mMParticleDBManager.getSessionForSessionEndMessage(sessionId, ((MessageManager)mMessageManagerCallbacks).getLocation(), entry.getValue()); }catch (JSONException jse){ Logger.warning("Failed to create mParticle session end message."); } if (endMessage != null) { try { Logger.verbose("Creating session end message for session ID: " + sessionId); mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), endMessage, mDataplanId, mDataplanVersion); mMParticleDBManager.updateSessionStatus(sessionId, SessionTable.SessionStatus.CLOSED); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } } else { Logger.error("Error creating session end, no entry for sessionId in mParticle DB."); } if (msg.arg1 == 1){ mMessageManagerCallbacks.endUploadLoop(); } } catch (Exception e) { Logger.error(e, "Error creating session end message in mParticle DB."); }finally { } break; case END_ORPHAN_SESSIONS: try { Logger.verbose("Ending orphaned sessions."); Long mpid = (Long)msg.obj; List<String> sessionIds = mMParticleDBManager.getOrphanSessionIds(mMessageManagerCallbacks.getApiKey()); for (String sessionId: sessionIds) { Map.Entry<String, Set<Long>> entry = new HashMap.SimpleEntry<String, Set<Long>>(sessionId, Collections.singleton(mpid)); sendMessage(obtainMessage(MessageHandler.CREATE_SESSION_END_MESSAGE, 0, 0, entry)); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process initialization, API key and or API Secret is missing."); } catch (Exception e) { Logger.error(e, "Error processing initialization in mParticle DB."); } break; case STORE_BREADCRUMB: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); try { mMParticleDBManager.insertBreadcrumb(message, mMessageManagerCallbacks.getApiKey()); } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); } } catch (Exception e) { Logger.error(e, "Error saving breadcrumb to mParticle DB."); } break; case STORE_REPORTING_MESSAGE_LIST: try{ MessageManager.ReportingMpidMessage reportingMessages = (MessageManager.ReportingMpidMessage)msg.obj; mMParticleDBManager.insertReportingMessages((List<JsonReportingMessage>)reportingMessages.reportingMessages, reportingMessages.mpid); }catch (Exception e) { Logger.verbose(e, "Error while inserting reporting messages: ", e.toString()); } break; case REMOVE_USER_ATTRIBUTE: try { mMParticleDBManager.removeUserAttribute((MParticleDBManager.UserAttributeRemoval)msg.obj, mMessageManagerCallbacks); }catch (Exception e) { Logger.error(e, "Error while removing user attribute: ", e.toString()); } break; case SET_USER_ATTRIBUTE: try { setUserAttributes((MParticleDBManager.UserAttributeResponse)msg.obj); } catch (Exception e) { Logger.error(e, "Error while setting user attribute: ", e.toString()); } break; case INCREMENT_USER_ATTRIBUTE: try { Map.Entry<String, Long> obj = (Map.Entry<String, Long>)msg.obj; incrementUserAttribute(obj.getKey(), msg.arg1, obj.getValue()); } catch (Exception e) { Logger.error(e, "Error while incrementing user attribute: ", e.toString()); } break; case CLEAR_MESSAGES_FOR_UPLOAD: mMessageManagerCallbacks.messagesClearedForUpload(); break; case STORE_ALIAS_MESSAGE: try { MPAliasMessage aliasMessage = (MPAliasMessage) msg.obj; mMParticleDBManager.insertAliasRequest(mMessageManagerCallbacks.getApiKey(), aliasMessage); if (MParticle.getInstance() != null) { MParticle.getInstance().upload(); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to Alias Request, API key and or API Secret is missing"); } catch (Exception ex) { Logger.error("Error sending Alias Request"); } break; } }
MessageHandler extends BaseHandler { @Override public void handleMessageImpl(Message msg) { try { if (!databaseAvailable()) { return; } mMessageManagerCallbacks.delayedStart(); }catch (Exception e) { Logger.verbose(e.toString()); } switch (msg.what) { case STORE_MESSAGE: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(MessageKey.STATE_INFO_KEY, MessageManager.getStateInfo()); String messageType = message.getString(MessageKey.TYPE); if (MessageType.SESSION_START.equals(messageType)) { dbInsertSession(message); }else{ mMParticleDBManager.updateSessionEndTime(message.getSessionId(), message.getLong(MessageKey.TIMESTAMP), 0); message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); } if (MessageType.ERROR.equals(messageType)){ mMParticleDBManager.appendBreadcrumbs(message); } try { mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), message, mDataplanId, mDataplanVersion); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } mMessageManagerCallbacks.checkForTrigger(message); } catch (Exception e) { Logger.error(e, "Error saving message to mParticle DB."); } break; case INSTALL_REFERRER_UPDATED: try { mMParticleDBManager.updateSessionInstallReferrer((String) msg.obj, mMessageManagerCallbacks.getDeviceAttributes().getAppInfo(mContext, true)); }catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_ATTRIBUTES: try { JSONObject sessionAttributes = (JSONObject) msg.obj; String sessionId = sessionAttributes.getString(MessageKey.SESSION_ID); String attributes = sessionAttributes.getString(MessageKey.ATTRIBUTES); mMParticleDBManager.updateSessionAttributes(sessionId, attributes); } catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_END: try { InternalSession session = (InternalSession) msg.obj; mMParticleDBManager.updateSessionEndTime(session.mSessionID, session.mLastEventTime, session.getForegroundTime()); } catch (Exception e) { Logger.error(e, "Error updating session end time in mParticle DB."); } break; case CREATE_SESSION_END_MESSAGE: try { Map.Entry<String, Set<Long>> entry = (Map.Entry<String, Set<Long>>) msg.obj; BaseMPMessage endMessage = null; String sessionId = entry.getKey(); try { endMessage = mMParticleDBManager.getSessionForSessionEndMessage(sessionId, ((MessageManager)mMessageManagerCallbacks).getLocation(), entry.getValue()); }catch (JSONException jse){ Logger.warning("Failed to create mParticle session end message."); } if (endMessage != null) { try { Logger.verbose("Creating session end message for session ID: " + sessionId); mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), endMessage, mDataplanId, mDataplanVersion); mMParticleDBManager.updateSessionStatus(sessionId, SessionTable.SessionStatus.CLOSED); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } } else { Logger.error("Error creating session end, no entry for sessionId in mParticle DB."); } if (msg.arg1 == 1){ mMessageManagerCallbacks.endUploadLoop(); } } catch (Exception e) { Logger.error(e, "Error creating session end message in mParticle DB."); }finally { } break; case END_ORPHAN_SESSIONS: try { Logger.verbose("Ending orphaned sessions."); Long mpid = (Long)msg.obj; List<String> sessionIds = mMParticleDBManager.getOrphanSessionIds(mMessageManagerCallbacks.getApiKey()); for (String sessionId: sessionIds) { Map.Entry<String, Set<Long>> entry = new HashMap.SimpleEntry<String, Set<Long>>(sessionId, Collections.singleton(mpid)); sendMessage(obtainMessage(MessageHandler.CREATE_SESSION_END_MESSAGE, 0, 0, entry)); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process initialization, API key and or API Secret is missing."); } catch (Exception e) { Logger.error(e, "Error processing initialization in mParticle DB."); } break; case STORE_BREADCRUMB: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); try { mMParticleDBManager.insertBreadcrumb(message, mMessageManagerCallbacks.getApiKey()); } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); } } catch (Exception e) { Logger.error(e, "Error saving breadcrumb to mParticle DB."); } break; case STORE_REPORTING_MESSAGE_LIST: try{ MessageManager.ReportingMpidMessage reportingMessages = (MessageManager.ReportingMpidMessage)msg.obj; mMParticleDBManager.insertReportingMessages((List<JsonReportingMessage>)reportingMessages.reportingMessages, reportingMessages.mpid); }catch (Exception e) { Logger.verbose(e, "Error while inserting reporting messages: ", e.toString()); } break; case REMOVE_USER_ATTRIBUTE: try { mMParticleDBManager.removeUserAttribute((MParticleDBManager.UserAttributeRemoval)msg.obj, mMessageManagerCallbacks); }catch (Exception e) { Logger.error(e, "Error while removing user attribute: ", e.toString()); } break; case SET_USER_ATTRIBUTE: try { setUserAttributes((MParticleDBManager.UserAttributeResponse)msg.obj); } catch (Exception e) { Logger.error(e, "Error while setting user attribute: ", e.toString()); } break; case INCREMENT_USER_ATTRIBUTE: try { Map.Entry<String, Long> obj = (Map.Entry<String, Long>)msg.obj; incrementUserAttribute(obj.getKey(), msg.arg1, obj.getValue()); } catch (Exception e) { Logger.error(e, "Error while incrementing user attribute: ", e.toString()); } break; case CLEAR_MESSAGES_FOR_UPLOAD: mMessageManagerCallbacks.messagesClearedForUpload(); break; case STORE_ALIAS_MESSAGE: try { MPAliasMessage aliasMessage = (MPAliasMessage) msg.obj; mMParticleDBManager.insertAliasRequest(mMessageManagerCallbacks.getApiKey(), aliasMessage); if (MParticle.getInstance() != null) { MParticle.getInstance().upload(); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to Alias Request, API key and or API Secret is missing"); } catch (Exception ex) { Logger.error("Error sending Alias Request"); } break; } } }
MessageHandler extends BaseHandler { @Override public void handleMessageImpl(Message msg) { try { if (!databaseAvailable()) { return; } mMessageManagerCallbacks.delayedStart(); }catch (Exception e) { Logger.verbose(e.toString()); } switch (msg.what) { case STORE_MESSAGE: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(MessageKey.STATE_INFO_KEY, MessageManager.getStateInfo()); String messageType = message.getString(MessageKey.TYPE); if (MessageType.SESSION_START.equals(messageType)) { dbInsertSession(message); }else{ mMParticleDBManager.updateSessionEndTime(message.getSessionId(), message.getLong(MessageKey.TIMESTAMP), 0); message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); } if (MessageType.ERROR.equals(messageType)){ mMParticleDBManager.appendBreadcrumbs(message); } try { mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), message, mDataplanId, mDataplanVersion); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } mMessageManagerCallbacks.checkForTrigger(message); } catch (Exception e) { Logger.error(e, "Error saving message to mParticle DB."); } break; case INSTALL_REFERRER_UPDATED: try { mMParticleDBManager.updateSessionInstallReferrer((String) msg.obj, mMessageManagerCallbacks.getDeviceAttributes().getAppInfo(mContext, true)); }catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_ATTRIBUTES: try { JSONObject sessionAttributes = (JSONObject) msg.obj; String sessionId = sessionAttributes.getString(MessageKey.SESSION_ID); String attributes = sessionAttributes.getString(MessageKey.ATTRIBUTES); mMParticleDBManager.updateSessionAttributes(sessionId, attributes); } catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_END: try { InternalSession session = (InternalSession) msg.obj; mMParticleDBManager.updateSessionEndTime(session.mSessionID, session.mLastEventTime, session.getForegroundTime()); } catch (Exception e) { Logger.error(e, "Error updating session end time in mParticle DB."); } break; case CREATE_SESSION_END_MESSAGE: try { Map.Entry<String, Set<Long>> entry = (Map.Entry<String, Set<Long>>) msg.obj; BaseMPMessage endMessage = null; String sessionId = entry.getKey(); try { endMessage = mMParticleDBManager.getSessionForSessionEndMessage(sessionId, ((MessageManager)mMessageManagerCallbacks).getLocation(), entry.getValue()); }catch (JSONException jse){ Logger.warning("Failed to create mParticle session end message."); } if (endMessage != null) { try { Logger.verbose("Creating session end message for session ID: " + sessionId); mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), endMessage, mDataplanId, mDataplanVersion); mMParticleDBManager.updateSessionStatus(sessionId, SessionTable.SessionStatus.CLOSED); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } } else { Logger.error("Error creating session end, no entry for sessionId in mParticle DB."); } if (msg.arg1 == 1){ mMessageManagerCallbacks.endUploadLoop(); } } catch (Exception e) { Logger.error(e, "Error creating session end message in mParticle DB."); }finally { } break; case END_ORPHAN_SESSIONS: try { Logger.verbose("Ending orphaned sessions."); Long mpid = (Long)msg.obj; List<String> sessionIds = mMParticleDBManager.getOrphanSessionIds(mMessageManagerCallbacks.getApiKey()); for (String sessionId: sessionIds) { Map.Entry<String, Set<Long>> entry = new HashMap.SimpleEntry<String, Set<Long>>(sessionId, Collections.singleton(mpid)); sendMessage(obtainMessage(MessageHandler.CREATE_SESSION_END_MESSAGE, 0, 0, entry)); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process initialization, API key and or API Secret is missing."); } catch (Exception e) { Logger.error(e, "Error processing initialization in mParticle DB."); } break; case STORE_BREADCRUMB: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); try { mMParticleDBManager.insertBreadcrumb(message, mMessageManagerCallbacks.getApiKey()); } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); } } catch (Exception e) { Logger.error(e, "Error saving breadcrumb to mParticle DB."); } break; case STORE_REPORTING_MESSAGE_LIST: try{ MessageManager.ReportingMpidMessage reportingMessages = (MessageManager.ReportingMpidMessage)msg.obj; mMParticleDBManager.insertReportingMessages((List<JsonReportingMessage>)reportingMessages.reportingMessages, reportingMessages.mpid); }catch (Exception e) { Logger.verbose(e, "Error while inserting reporting messages: ", e.toString()); } break; case REMOVE_USER_ATTRIBUTE: try { mMParticleDBManager.removeUserAttribute((MParticleDBManager.UserAttributeRemoval)msg.obj, mMessageManagerCallbacks); }catch (Exception e) { Logger.error(e, "Error while removing user attribute: ", e.toString()); } break; case SET_USER_ATTRIBUTE: try { setUserAttributes((MParticleDBManager.UserAttributeResponse)msg.obj); } catch (Exception e) { Logger.error(e, "Error while setting user attribute: ", e.toString()); } break; case INCREMENT_USER_ATTRIBUTE: try { Map.Entry<String, Long> obj = (Map.Entry<String, Long>)msg.obj; incrementUserAttribute(obj.getKey(), msg.arg1, obj.getValue()); } catch (Exception e) { Logger.error(e, "Error while incrementing user attribute: ", e.toString()); } break; case CLEAR_MESSAGES_FOR_UPLOAD: mMessageManagerCallbacks.messagesClearedForUpload(); break; case STORE_ALIAS_MESSAGE: try { MPAliasMessage aliasMessage = (MPAliasMessage) msg.obj; mMParticleDBManager.insertAliasRequest(mMessageManagerCallbacks.getApiKey(), aliasMessage); if (MParticle.getInstance() != null) { MParticle.getInstance().upload(); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to Alias Request, API key and or API Secret is missing"); } catch (Exception ex) { Logger.error("Error sending Alias Request"); } break; } } MessageHandler(MessageManagerCallbacks messageManager, Context context, MParticleDBManager dbManager, String dataplanId, Integer dataplanVersion);  MessageHandler(Looper looper, MessageManagerCallbacks messageManager, Context context, MParticleDBManager dbManager, String dataplanId, Integer dataplanVersion); }
MessageHandler extends BaseHandler { @Override public void handleMessageImpl(Message msg) { try { if (!databaseAvailable()) { return; } mMessageManagerCallbacks.delayedStart(); }catch (Exception e) { Logger.verbose(e.toString()); } switch (msg.what) { case STORE_MESSAGE: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(MessageKey.STATE_INFO_KEY, MessageManager.getStateInfo()); String messageType = message.getString(MessageKey.TYPE); if (MessageType.SESSION_START.equals(messageType)) { dbInsertSession(message); }else{ mMParticleDBManager.updateSessionEndTime(message.getSessionId(), message.getLong(MessageKey.TIMESTAMP), 0); message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); } if (MessageType.ERROR.equals(messageType)){ mMParticleDBManager.appendBreadcrumbs(message); } try { mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), message, mDataplanId, mDataplanVersion); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } mMessageManagerCallbacks.checkForTrigger(message); } catch (Exception e) { Logger.error(e, "Error saving message to mParticle DB."); } break; case INSTALL_REFERRER_UPDATED: try { mMParticleDBManager.updateSessionInstallReferrer((String) msg.obj, mMessageManagerCallbacks.getDeviceAttributes().getAppInfo(mContext, true)); }catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_ATTRIBUTES: try { JSONObject sessionAttributes = (JSONObject) msg.obj; String sessionId = sessionAttributes.getString(MessageKey.SESSION_ID); String attributes = sessionAttributes.getString(MessageKey.ATTRIBUTES); mMParticleDBManager.updateSessionAttributes(sessionId, attributes); } catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_END: try { InternalSession session = (InternalSession) msg.obj; mMParticleDBManager.updateSessionEndTime(session.mSessionID, session.mLastEventTime, session.getForegroundTime()); } catch (Exception e) { Logger.error(e, "Error updating session end time in mParticle DB."); } break; case CREATE_SESSION_END_MESSAGE: try { Map.Entry<String, Set<Long>> entry = (Map.Entry<String, Set<Long>>) msg.obj; BaseMPMessage endMessage = null; String sessionId = entry.getKey(); try { endMessage = mMParticleDBManager.getSessionForSessionEndMessage(sessionId, ((MessageManager)mMessageManagerCallbacks).getLocation(), entry.getValue()); }catch (JSONException jse){ Logger.warning("Failed to create mParticle session end message."); } if (endMessage != null) { try { Logger.verbose("Creating session end message for session ID: " + sessionId); mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), endMessage, mDataplanId, mDataplanVersion); mMParticleDBManager.updateSessionStatus(sessionId, SessionTable.SessionStatus.CLOSED); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } } else { Logger.error("Error creating session end, no entry for sessionId in mParticle DB."); } if (msg.arg1 == 1){ mMessageManagerCallbacks.endUploadLoop(); } } catch (Exception e) { Logger.error(e, "Error creating session end message in mParticle DB."); }finally { } break; case END_ORPHAN_SESSIONS: try { Logger.verbose("Ending orphaned sessions."); Long mpid = (Long)msg.obj; List<String> sessionIds = mMParticleDBManager.getOrphanSessionIds(mMessageManagerCallbacks.getApiKey()); for (String sessionId: sessionIds) { Map.Entry<String, Set<Long>> entry = new HashMap.SimpleEntry<String, Set<Long>>(sessionId, Collections.singleton(mpid)); sendMessage(obtainMessage(MessageHandler.CREATE_SESSION_END_MESSAGE, 0, 0, entry)); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process initialization, API key and or API Secret is missing."); } catch (Exception e) { Logger.error(e, "Error processing initialization in mParticle DB."); } break; case STORE_BREADCRUMB: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); try { mMParticleDBManager.insertBreadcrumb(message, mMessageManagerCallbacks.getApiKey()); } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); } } catch (Exception e) { Logger.error(e, "Error saving breadcrumb to mParticle DB."); } break; case STORE_REPORTING_MESSAGE_LIST: try{ MessageManager.ReportingMpidMessage reportingMessages = (MessageManager.ReportingMpidMessage)msg.obj; mMParticleDBManager.insertReportingMessages((List<JsonReportingMessage>)reportingMessages.reportingMessages, reportingMessages.mpid); }catch (Exception e) { Logger.verbose(e, "Error while inserting reporting messages: ", e.toString()); } break; case REMOVE_USER_ATTRIBUTE: try { mMParticleDBManager.removeUserAttribute((MParticleDBManager.UserAttributeRemoval)msg.obj, mMessageManagerCallbacks); }catch (Exception e) { Logger.error(e, "Error while removing user attribute: ", e.toString()); } break; case SET_USER_ATTRIBUTE: try { setUserAttributes((MParticleDBManager.UserAttributeResponse)msg.obj); } catch (Exception e) { Logger.error(e, "Error while setting user attribute: ", e.toString()); } break; case INCREMENT_USER_ATTRIBUTE: try { Map.Entry<String, Long> obj = (Map.Entry<String, Long>)msg.obj; incrementUserAttribute(obj.getKey(), msg.arg1, obj.getValue()); } catch (Exception e) { Logger.error(e, "Error while incrementing user attribute: ", e.toString()); } break; case CLEAR_MESSAGES_FOR_UPLOAD: mMessageManagerCallbacks.messagesClearedForUpload(); break; case STORE_ALIAS_MESSAGE: try { MPAliasMessage aliasMessage = (MPAliasMessage) msg.obj; mMParticleDBManager.insertAliasRequest(mMessageManagerCallbacks.getApiKey(), aliasMessage); if (MParticle.getInstance() != null) { MParticle.getInstance().upload(); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to Alias Request, API key and or API Secret is missing"); } catch (Exception ex) { Logger.error("Error sending Alias Request"); } break; } } MessageHandler(MessageManagerCallbacks messageManager, Context context, MParticleDBManager dbManager, String dataplanId, Integer dataplanVersion);  MessageHandler(Looper looper, MessageManagerCallbacks messageManager, Context context, MParticleDBManager dbManager, String dataplanId, Integer dataplanVersion); @Override void handleMessageImpl(Message msg); }
MessageHandler extends BaseHandler { @Override public void handleMessageImpl(Message msg) { try { if (!databaseAvailable()) { return; } mMessageManagerCallbacks.delayedStart(); }catch (Exception e) { Logger.verbose(e.toString()); } switch (msg.what) { case STORE_MESSAGE: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(MessageKey.STATE_INFO_KEY, MessageManager.getStateInfo()); String messageType = message.getString(MessageKey.TYPE); if (MessageType.SESSION_START.equals(messageType)) { dbInsertSession(message); }else{ mMParticleDBManager.updateSessionEndTime(message.getSessionId(), message.getLong(MessageKey.TIMESTAMP), 0); message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); } if (MessageType.ERROR.equals(messageType)){ mMParticleDBManager.appendBreadcrumbs(message); } try { mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), message, mDataplanId, mDataplanVersion); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } mMessageManagerCallbacks.checkForTrigger(message); } catch (Exception e) { Logger.error(e, "Error saving message to mParticle DB."); } break; case INSTALL_REFERRER_UPDATED: try { mMParticleDBManager.updateSessionInstallReferrer((String) msg.obj, mMessageManagerCallbacks.getDeviceAttributes().getAppInfo(mContext, true)); }catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_ATTRIBUTES: try { JSONObject sessionAttributes = (JSONObject) msg.obj; String sessionId = sessionAttributes.getString(MessageKey.SESSION_ID); String attributes = sessionAttributes.getString(MessageKey.ATTRIBUTES); mMParticleDBManager.updateSessionAttributes(sessionId, attributes); } catch (Exception e) { Logger.error(e, "Error updating session attributes in mParticle DB."); } break; case UPDATE_SESSION_END: try { InternalSession session = (InternalSession) msg.obj; mMParticleDBManager.updateSessionEndTime(session.mSessionID, session.mLastEventTime, session.getForegroundTime()); } catch (Exception e) { Logger.error(e, "Error updating session end time in mParticle DB."); } break; case CREATE_SESSION_END_MESSAGE: try { Map.Entry<String, Set<Long>> entry = (Map.Entry<String, Set<Long>>) msg.obj; BaseMPMessage endMessage = null; String sessionId = entry.getKey(); try { endMessage = mMParticleDBManager.getSessionForSessionEndMessage(sessionId, ((MessageManager)mMessageManagerCallbacks).getLocation(), entry.getValue()); }catch (JSONException jse){ Logger.warning("Failed to create mParticle session end message."); } if (endMessage != null) { try { Logger.verbose("Creating session end message for session ID: " + sessionId); mMParticleDBManager.insertMessage(mMessageManagerCallbacks.getApiKey(), endMessage, mDataplanId, mDataplanVersion); mMParticleDBManager.updateSessionStatus(sessionId, SessionTable.SessionStatus.CLOSED); } catch (MParticleApiClientImpl.MPNoConfigException e) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); return; } } else { Logger.error("Error creating session end, no entry for sessionId in mParticle DB."); } if (msg.arg1 == 1){ mMessageManagerCallbacks.endUploadLoop(); } } catch (Exception e) { Logger.error(e, "Error creating session end message in mParticle DB."); }finally { } break; case END_ORPHAN_SESSIONS: try { Logger.verbose("Ending orphaned sessions."); Long mpid = (Long)msg.obj; List<String> sessionIds = mMParticleDBManager.getOrphanSessionIds(mMessageManagerCallbacks.getApiKey()); for (String sessionId: sessionIds) { Map.Entry<String, Set<Long>> entry = new HashMap.SimpleEntry<String, Set<Long>>(sessionId, Collections.singleton(mpid)); sendMessage(obtainMessage(MessageHandler.CREATE_SESSION_END_MESSAGE, 0, 0, entry)); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process initialization, API key and or API Secret is missing."); } catch (Exception e) { Logger.error(e, "Error processing initialization in mParticle DB."); } break; case STORE_BREADCRUMB: try { BaseMPMessage message = (BaseMPMessage) msg.obj; message.put(Constants.MessageKey.ID, UUID.randomUUID().toString()); try { mMParticleDBManager.insertBreadcrumb(message, mMessageManagerCallbacks.getApiKey()); } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to process uploads, API key and/or API Secret are missing."); } } catch (Exception e) { Logger.error(e, "Error saving breadcrumb to mParticle DB."); } break; case STORE_REPORTING_MESSAGE_LIST: try{ MessageManager.ReportingMpidMessage reportingMessages = (MessageManager.ReportingMpidMessage)msg.obj; mMParticleDBManager.insertReportingMessages((List<JsonReportingMessage>)reportingMessages.reportingMessages, reportingMessages.mpid); }catch (Exception e) { Logger.verbose(e, "Error while inserting reporting messages: ", e.toString()); } break; case REMOVE_USER_ATTRIBUTE: try { mMParticleDBManager.removeUserAttribute((MParticleDBManager.UserAttributeRemoval)msg.obj, mMessageManagerCallbacks); }catch (Exception e) { Logger.error(e, "Error while removing user attribute: ", e.toString()); } break; case SET_USER_ATTRIBUTE: try { setUserAttributes((MParticleDBManager.UserAttributeResponse)msg.obj); } catch (Exception e) { Logger.error(e, "Error while setting user attribute: ", e.toString()); } break; case INCREMENT_USER_ATTRIBUTE: try { Map.Entry<String, Long> obj = (Map.Entry<String, Long>)msg.obj; incrementUserAttribute(obj.getKey(), msg.arg1, obj.getValue()); } catch (Exception e) { Logger.error(e, "Error while incrementing user attribute: ", e.toString()); } break; case CLEAR_MESSAGES_FOR_UPLOAD: mMessageManagerCallbacks.messagesClearedForUpload(); break; case STORE_ALIAS_MESSAGE: try { MPAliasMessage aliasMessage = (MPAliasMessage) msg.obj; mMParticleDBManager.insertAliasRequest(mMessageManagerCallbacks.getApiKey(), aliasMessage); if (MParticle.getInstance() != null) { MParticle.getInstance().upload(); } } catch (MParticleApiClientImpl.MPNoConfigException ex) { Logger.error("Unable to Alias Request, API key and or API Secret is missing"); } catch (Exception ex) { Logger.error("Error sending Alias Request"); } break; } } MessageHandler(MessageManagerCallbacks messageManager, Context context, MParticleDBManager dbManager, String dataplanId, Integer dataplanVersion);  MessageHandler(Looper looper, MessageManagerCallbacks messageManager, Context context, MParticleDBManager dbManager, String dataplanId, Integer dataplanVersion); @Override void handleMessageImpl(Message msg); static final int STORE_MESSAGE; static final int UPDATE_SESSION_ATTRIBUTES; static final int UPDATE_SESSION_END; static final int CREATE_SESSION_END_MESSAGE; static final int END_ORPHAN_SESSIONS; static final int STORE_BREADCRUMB; static final int STORE_REPORTING_MESSAGE_LIST; static final int REMOVE_USER_ATTRIBUTE; static final int SET_USER_ATTRIBUTE; static final int INCREMENT_USER_ATTRIBUTE; static final int INSTALL_REFERRER_UPDATED; static final int CLEAR_MESSAGES_FOR_UPLOAD; static final int STORE_ALIAS_MESSAGE; }