3232import com .leanplum .callbacks .RegisterDeviceFinishedCallback ;
3333import com .leanplum .callbacks .StartCallback ;
3434import com .leanplum .callbacks .VariablesChangedCallback ;
35+ import com .leanplum .internal .APIConfig ;
3536import com .leanplum .internal .ActionManager ;
3637import com .leanplum .internal .Constants ;
3738import com .leanplum .internal .CountAggregator ;
3839import com .leanplum .internal .FeatureFlagManager ;
3940import com .leanplum .internal .FileManager ;
41+ import com .leanplum .internal .FileTransferManager ;
4042import com .leanplum .internal .JsonConverter ;
4143import com .leanplum .internal .LeanplumEventDataManager ;
4244import com .leanplum .internal .LeanplumInternal ;
4345import com .leanplum .internal .LeanplumMessageMatchFilter ;
4446import com .leanplum .internal .Log ;
4547import com .leanplum .internal .OperationQueue ;
4648import com .leanplum .internal .Registration ;
47- import com .leanplum .internal .RequestOld ;
49+ import com .leanplum .internal .RequestBuilder ;
50+ import com .leanplum .internal .Request ;
51+ import com .leanplum .internal .RequestUtil ;
52+ import com .leanplum .internal .RequestSender ;
4853import com .leanplum .internal .Util ;
4954import com .leanplum .internal .Util .DeviceIdInfo ;
5055import com .leanplum .internal .VarCache ;
@@ -232,7 +237,7 @@ public static void setAppIdForDevelopmentMode(String appId, String accessKey) {
232237 }
233238
234239 Constants .isDevelopmentModeEnabled = true ;
235- RequestOld .setAppId (appId , accessKey );
240+ APIConfig . getInstance () .setAppId (appId , accessKey );
236241 }
237242
238243 /**
@@ -253,7 +258,7 @@ public static void setAppIdForProductionMode(String appId, String accessKey) {
253258 }
254259
255260 Constants .isDevelopmentModeEnabled = false ;
256- RequestOld .setAppId (appId , accessKey );
261+ APIConfig . getInstance () .setAppId (appId , accessKey );
257262 }
258263
259264 /**
@@ -329,7 +334,7 @@ public static String getDeviceId() {
329334 Log .e ("Leanplum.start() must be called before calling getDeviceId." );
330335 return null ;
331336 }
332- return RequestOld .deviceId ();
337+ return APIConfig . getInstance () .deviceId ();
333338 }
334339
335340 /**
@@ -559,7 +564,7 @@ static synchronized void start(final Context context, final String userId,
559564 LeanplumInternal .getUserAttributeChanges ().add (validAttributes );
560565 }
561566
562- RequestOld .loadToken ();
567+ APIConfig . getInstance () .loadToken ();
563568 VarCache .setSilent (true );
564569 VarCache .loadDiffs ();
565570 VarCache .setSilent (false );
@@ -570,16 +575,17 @@ static synchronized void start(final Context context, final String userId,
570575 @ Override
571576 public void updateCache () {
572577 triggerVariablesChanged ();
573- if (RequestOld .numPendingDownloads () == 0 ) {
578+ if (FileTransferManager . getInstance () .numPendingDownloads () == 0 ) {
574579 triggerVariablesChangedAndNoDownloadsPending ();
575580 }
576581 }
577582 });
578- RequestOld .onNoPendingDownloads (new RequestOld .NoPendingDownloadsCallback () {
579- @ Override
580- public void noPendingDownloads () {
581- triggerVariablesChangedAndNoDownloadsPending ();
582- }
583+ FileTransferManager .getInstance ().onNoPendingDownloads (
584+ new FileTransferManager .NoPendingDownloadsCallback () {
585+ @ Override
586+ public void noPendingDownloads () {
587+ triggerVariablesChangedAndNoDownloadsPending ();
588+ }
583589 });
584590
585591 // Reduce latency by running the rest of the start call in a background thread.
@@ -621,7 +627,7 @@ private static void startHelper(
621627 LeanplumEventDataManager .sharedInstance ();
622628 checkAndStartNotificationsModules ();
623629 Boolean limitAdTracking = null ;
624- String deviceId = RequestOld .deviceId ();
630+ String deviceId = APIConfig . getInstance () .deviceId ();
625631 if (deviceId == null ) {
626632 if (!userSpecifiedDeviceId && Constants .defaultDeviceId != null ) {
627633 deviceId = Constants .defaultDeviceId ;
@@ -632,16 +638,16 @@ private static void startHelper(
632638 deviceId = deviceIdInfo .id ;
633639 limitAdTracking = deviceIdInfo .limitAdTracking ;
634640 }
635- RequestOld .setDeviceId (deviceId );
641+ APIConfig . getInstance () .setDeviceId (deviceId );
636642 }
637643
638644 if (userId == null ) {
639- userId = RequestOld .userId ();
645+ userId = APIConfig . getInstance () .userId ();
640646 if (userId == null ) {
641- userId = RequestOld .deviceId ();
647+ userId = APIConfig . getInstance () .deviceId ();
642648 }
643649 }
644- RequestOld .setUserId (userId );
650+ APIConfig . getInstance () .setUserId (userId );
645651
646652 // Setup parameters.
647653 String versionName = Util .getVersionName ();
@@ -697,31 +703,31 @@ private static void startHelper(
697703 Util .initializePreLeanplumInstall (params );
698704
699705 // Issue start API call.
700- final RequestOld request = RequestOld . post ( Constants . Methods . START , params );
701- request .onResponse (new RequestOld .ResponseCallback () {
706+ final Request request = RequestBuilder . withStartAction (). andParams ( params ). create ( );
707+ request .onResponse (new Request .ResponseCallback () {
702708 @ Override
703709 public void response (JSONObject response ) {
704710 handleStartResponse (response );
705711 }
706712 });
707- request .onError (new RequestOld .ErrorCallback () {
713+ request .onError (new Request .ErrorCallback () {
708714 @ Override
709715 public void error (Exception e ) {
710716 handleStartResponse (null );
711717 }
712718 });
713719
714720 if (isBackground ) {
715- request . sendEventually ();
721+ RequestSender . getInstance (). sendEventually (request );
716722 } else {
717- request . sendIfConnected ();
723+ RequestSender . getInstance (). sendIfConnected (request );
718724 }
719725
720726 LeanplumInternal .triggerStartIssued ();
721727 }
722728
723729 private static void handleStartResponse (final JSONObject response ) {
724- boolean success = RequestOld .isResponseSuccess (response );
730+ boolean success = RequestUtil .isResponseSuccess (response );
725731 Leanplum .countAggregator ().incrementCount ("on_start_response" );
726732 if (!success ) {
727733 try {
@@ -782,8 +788,8 @@ private static void handleStartResponse(final JSONObject response) {
782788 }
783789
784790 String token = response .optString (Constants .Keys .TOKEN , null );
785- RequestOld .setToken (token );
786- RequestOld .saveToken ();
791+ APIConfig . getInstance () .setToken (token );
792+ APIConfig . getInstance () .saveToken ();
787793
788794 applyContentInResponse (response , true );
789795
@@ -969,7 +975,8 @@ public void run() {
969975 }
970976
971977 private static void pauseInternal () {
972- RequestOld .post (Constants .Methods .PAUSE_SESSION , null ).sendIfConnected ();
978+ Request request = RequestBuilder .withPauseSessionAction ().create ();
979+ RequestSender .getInstance ().sendIfConnected (request );
973980 pauseHeartbeat ();
974981 LeanplumInternal .setIsPaused (true );
975982 }
@@ -1003,12 +1010,12 @@ public void run() {
10031010 }
10041011
10051012 private static void resumeInternal () {
1006- RequestOld request = RequestOld . post ( Constants . Methods . RESUME_SESSION , null );
1013+ Request request = RequestBuilder . withResumeSessionAction (). create ( );
10071014 if (LeanplumInternal .hasStartedInBackground ()) {
10081015 LeanplumInternal .setStartedInBackground (false );
1009- request . sendIfConnected ();
1016+ RequestSender . getInstance (). sendIfConnected (request );
10101017 } else {
1011- request . sendIfDelayed ();
1018+ RequestSender . getInstance (). sendIfDelayed (request );
10121019 LeanplumInternal .maybePerformActions ("resume" , null ,
10131020 LeanplumMessageMatchFilter .LEANPLUM_ACTION_FILTER_ALL , null , null );
10141021 }
@@ -1045,7 +1052,8 @@ private static void createHeartbeatExecutor() {
10451052 heartbeatExecutor .scheduleAtFixedRate (new Runnable () {
10461053 public void run () {
10471054 try {
1048- RequestOld .post (Constants .Methods .HEARTBEAT , null ).sendIfDelayed ();
1055+ Request request = RequestBuilder .withHeartbeatAction ().create ();
1056+ RequestSender .getInstance ().sendIfDelayed (request );
10491057 } catch (Throwable t ) {
10501058 Util .handleException (t );
10511059 }
@@ -1083,7 +1091,8 @@ public void run() {
10831091 }
10841092
10851093 private static void stopInternal () {
1086- RequestOld .post (Constants .Methods .STOP , null ).sendIfConnected ();
1094+ Request request = RequestBuilder .withStopAction ().create ();
1095+ RequestSender .getInstance ().sendIfConnected (request );
10871096 }
10881097
10891098 /**
@@ -1099,7 +1108,7 @@ public static boolean hasStarted() {
10991108 */
11001109 public static String getUserId () {
11011110 if (hasStarted ()) {
1102- return RequestOld .userId ();
1111+ return APIConfig . getInstance () .userId ();
11031112 } else {
11041113 Log .e ("Leanplum.start() must be called before calling getUserId()" );
11051114 }
@@ -1224,7 +1233,7 @@ public static void addVariablesChangedAndNoDownloadsPendingHandler(
12241233 noDownloadsHandlers .add (handler );
12251234 }
12261235 if (VarCache .hasReceivedDiffs ()
1227- && RequestOld .numPendingDownloads () == 0 ) {
1236+ && FileTransferManager . getInstance () .numPendingDownloads () == 0 ) {
12281237 handler .variablesChanged ();
12291238 }
12301239 }
@@ -1334,7 +1343,7 @@ public static void addOnceVariablesChangedAndNoDownloadsPendingHandler(
13341343 }
13351344
13361345 if (VarCache .hasReceivedDiffs ()
1337- && RequestOld .numPendingDownloads () == 0 ) {
1346+ && FileTransferManager . getInstance () .numPendingDownloads () == 0 ) {
13381347 handler .variablesChanged ();
13391348 } else {
13401349 synchronized (onceNoDownloadsHandlers ) {
@@ -1501,9 +1510,10 @@ public void run() {
15011510
15021511 private static void setUserAttributesInternal (String userId ,
15031512 HashMap <String , Object > requestArgs ) {
1504- RequestOld .post (Constants .Methods .SET_USER_ATTRIBUTES , requestArgs ).send ();
1513+ Request request = RequestBuilder .withSetUserAttributesAction ().andParams (requestArgs ).create ();
1514+ RequestSender .getInstance ().send (request );
15051515 if (userId != null && userId .length () > 0 ) {
1506- RequestOld .setUserId (userId );
1516+ APIConfig . getInstance () .setUserId (userId );
15071517 if (LeanplumInternal .hasStarted ()) {
15081518 VarCache .saveDiffs ();
15091519 }
@@ -1549,9 +1559,11 @@ public void run() {
15491559 return ;
15501560 }
15511561 try {
1552- HashMap <String , Object > params = new HashMap <>();
1553- params .put (Constants .Params .DEVICE_PUSH_TOKEN , registrationId );
1554- RequestOld .post (Constants .Methods .SET_DEVICE_ATTRIBUTES , params ).sendIfConnected ();
1562+ Request request = RequestBuilder
1563+ .withSetDeviceAttributesAction ()
1564+ .andParam (Constants .Params .DEVICE_PUSH_TOKEN , registrationId )
1565+ .create ();
1566+ RequestSender .getInstance ().sendIfConnected (request );
15551567 } catch (Throwable t ) {
15561568 Util .handleException (t );
15571569 }
@@ -1602,7 +1614,8 @@ public void run() {
16021614 }
16031615
16041616 private static void setTrafficSourceInfoInternal (HashMap <String , Object > params ) {
1605- RequestOld .post (Constants .Methods .SET_TRAFFIC_SOURCE_INFO , params ).send ();
1617+ Request requets = RequestBuilder .withSetTrafficSourceInfoAction ().andParams (params ).create ();
1618+ RequestSender .getInstance ().send (requets );
16061619 }
16071620
16081621 /**
@@ -1881,7 +1894,8 @@ public void run() {
18811894 */
18821895 private static void advanceToInternal (String state , Map <String , ?> params ,
18831896 Map <String , Object > requestParams ) {
1884- RequestOld .post (Constants .Methods .ADVANCE , requestParams ).send ();
1897+ Request request = RequestBuilder .withAdvanceAction ().andParams (requestParams ).create ();
1898+ RequestSender .getInstance ().send (request );
18851899
18861900 ContextualValues contextualValues = new ContextualValues ();
18871901 contextualValues .parameters = params ;
@@ -1961,7 +1975,8 @@ public void run() {
19611975 }
19621976
19631977 private static void pauseStateInternal () {
1964- RequestOld .post (Constants .Methods .PAUSE_STATE , new HashMap <String , Object >()).send ();
1978+ Request request = RequestBuilder .withPauseStateAction ().create ();
1979+ RequestSender .getInstance ().send (request );
19651980 }
19661981
19671982 /**
@@ -1997,7 +2012,8 @@ public void run() {
19972012 }
19982013
19992014 private static void resumeStateInternal () {
2000- RequestOld .post (Constants .Methods .RESUME_STATE , new HashMap <String , Object >()).send ();
2015+ Request request = RequestBuilder .withResumeStateAction ().create ();
2016+ RequestSender .getInstance ().send (request );
20012017 }
20022018
20032019 /**
@@ -2024,13 +2040,13 @@ public static void forceContentUpdate(final VariablesChangedCallback callback) {
20242040 return ;
20252041 }
20262042 try {
2027- Map < String , Object > params = new HashMap <>();
2028- params . put ( Constants . Params . INCLUDE_DEFAULTS , Boolean . toString ( false ));
2029- params . put (Constants .Params .INBOX_MESSAGES , LeanplumInbox . getInstance (). messagesIds ());
2030- params . put (Constants .Params .INCLUDE_VARIANT_DEBUG_INFO , LeanplumInternal . getIsVariantDebugInfoEnabled ());
2031-
2032- RequestOld req = RequestOld . post ( Constants . Methods . GET_VARS , params );
2033- req .onResponse (new RequestOld .ResponseCallback () {
2043+ Request req = RequestBuilder
2044+ . withGetVarsAction ()
2045+ . andParam (Constants .Params .INCLUDE_DEFAULTS , Boolean . toString ( false ))
2046+ . andParam (Constants .Params .INBOX_MESSAGES , LeanplumInbox . getInstance (). messagesIds ())
2047+ . andParam ( Constants . Params . INCLUDE_VARIANT_DEBUG_INFO , LeanplumInternal . getIsVariantDebugInfoEnabled ())
2048+ . create ( );
2049+ req .onResponse (new Request .ResponseCallback () {
20342050 @ Override
20352051 public void response (JSONObject response ) {
20362052 try {
@@ -2057,14 +2073,14 @@ public void response(JSONObject response) {
20572073 }
20582074 }
20592075 });
2060- req .onError (new RequestOld .ErrorCallback () {
2076+ req .onError (new Request .ErrorCallback () {
20612077 @ Override
20622078 public void error (Exception e ) {
20632079 OperationQueue .sharedInstance ().addUiOperation (callback );
20642080 LeanplumInbox .getInstance ().triggerInboxSyncedWithStatus (false );
20652081 }
20662082 });
2067- req . sendIfConnected ();
2083+ RequestSender . getInstance (). sendIfConnected (req );
20682084 } catch (Throwable t ) {
20692085 Util .handleException (t );
20702086 }
0 commit comments