-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move initialization steps to background (#681)
* feat: create background task manager and rewrite status requester #676 * feat: move fetching of user agent and consents to background #676 * feat: add consents reading to background tasks #676 * fix: existing tests #676 * fix: sdk initialization timing #676 * test: add new unit tests #676 * feat: replace async mechanism to ExecutorsService #676 * feat: fix existing unit tests #676 * feat: add one more test #676 * test: cover background tasks with unit tests #676 * refactor: move logic #676
- Loading branch information
1 parent
017fb31
commit 798f566
Showing
16 changed files
with
663 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ebidMobile-core/src/main/java/org/prebid/mobile/rendering/sdk/InitializationNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.prebid.mobile.rendering.sdk; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.prebid.mobile.LogUtil; | ||
import org.prebid.mobile.PrebidMobile; | ||
import org.prebid.mobile.api.data.InitializationStatus; | ||
import org.prebid.mobile.api.exceptions.InitError; | ||
import org.prebid.mobile.rendering.listeners.SdkInitializationListener; | ||
|
||
|
||
public class InitializationNotifier { | ||
|
||
private static final String TAG = "InitializationNotifier"; | ||
|
||
private static boolean tasksCompletedSuccessfully = false; | ||
private static boolean initializationInProgress = false; | ||
|
||
@Nullable | ||
private SdkInitializationListener listener; | ||
|
||
public InitializationNotifier(@Nullable SdkInitializationListener listener) { | ||
this.listener = listener; | ||
initializationInProgress = true; | ||
} | ||
|
||
/** | ||
* @param statusRequesterError must be null, if status requester completed successfully. | ||
*/ | ||
public void initializationCompleted(@Nullable String statusRequesterError) { | ||
postOnMainThread(() -> { | ||
boolean statusRequestSuccessful = statusRequesterError == null; | ||
if (statusRequestSuccessful) { | ||
LogUtil.debug(TAG, "Prebid SDK " + PrebidMobile.SDK_VERSION + " initialized"); | ||
|
||
if (listener != null) { | ||
listener.onInitializationComplete(InitializationStatus.SUCCEEDED); | ||
|
||
listener.onSdkInit(); | ||
} | ||
} else { | ||
LogUtil.error(TAG, statusRequesterError); | ||
|
||
if (listener != null) { | ||
InitializationStatus serverStatusWarning = InitializationStatus.SERVER_STATUS_WARNING; | ||
serverStatusWarning.setDescription(statusRequesterError); | ||
listener.onInitializationComplete(serverStatusWarning); | ||
|
||
listener.onSdkFailedToInit(new InitError(statusRequesterError)); | ||
} | ||
} | ||
|
||
tasksCompletedSuccessfully = true; | ||
initializationInProgress = false; | ||
listener = null; | ||
}); | ||
} | ||
|
||
public void initializationFailed(@NotNull String error) { | ||
postOnMainThread(() -> { | ||
LogUtil.error(error); | ||
|
||
if (listener != null) { | ||
InitializationStatus status = InitializationStatus.FAILED; | ||
status.setDescription(error); | ||
listener.onInitializationComplete(status); | ||
|
||
listener.onSdkFailedToInit(new InitError(error)); | ||
} | ||
|
||
PrebidContextHolder.clearContext(); | ||
listener = null; | ||
initializationInProgress = false; | ||
}); | ||
} | ||
|
||
|
||
private static void postOnMainThread(Runnable runnable) { | ||
new Handler(Looper.getMainLooper()).post(runnable); | ||
} | ||
|
||
public static boolean wereTasksCompletedSuccessfully() { | ||
return tasksCompletedSuccessfully; | ||
} | ||
|
||
public static boolean isInitializationInProgress() { | ||
return initializationInProgress; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.