Search in sources :

Example 6 with EventBus

use of de.greenrobot.event.EventBus in project edx-app-android by edx.

the class NewVersionAvailableEventTest method postEventFromInterceptor.

/**
 * Post the provided data by passing the appropriate headers and status code in a request chain
 * through an {@link NewVersionBroadcastInterceptor}.
 *
 * @param interceptor The interceptor through which the data is to be posted.
 * @param newVersion The new version.
 * @param lastSupportedDate The last supported date.
 * @param isUnsupported Whether the current version is unsupported.
 * @return The posted event. This can be null if the data does not constitute a valid event.
 */
@Nullable
private static NewVersionAvailableEvent postEventFromInterceptor(@NonNull final NewVersionBroadcastInterceptor interceptor, @Nullable final Version newVersion, @Nullable final Date lastSupportedDate, final boolean isUnsupported) throws IOException {
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    final Interceptor.Chain chain = mock(Interceptor.Chain.class);
    final Request request = new Request.Builder().url("https://localhost:1/").build();
    final Response response;
    {
        final Response.Builder responseBuilder = new Response.Builder();
        responseBuilder.request(request);
        responseBuilder.protocol(Protocol.HTTP_1_1);
        responseBuilder.code(isUnsupported ? UPGRADE_REQUIRED : ACCEPTED);
        if (newVersion != null) {
            responseBuilder.header(HEADER_APP_LATEST_VERSION, newVersion.toString());
        }
        if (lastSupportedDate != null) {
            responseBuilder.header(HEADER_APP_VERSION_LAST_SUPPORTED_DATE, ISO8601Utils.format(lastSupportedDate, true));
        }
        response = responseBuilder.build();
    }
    when(chain.request()).thenReturn(request);
    when(chain.proceed(request)).thenReturn(response);
    interceptor.intercept(chain);
    final InOrder inOrder = inOrder(chain);
    inOrder.verify(chain).request();
    inOrder.verify(chain).proceed(request);
    verifyNoMoreInteractions(chain);
    return eventBus.getStickyEvent(NewVersionAvailableEvent.class);
}
Also used : Response(okhttp3.Response) InOrder(org.mockito.InOrder) Request(okhttp3.Request) EventBus(de.greenrobot.event.EventBus) Interceptor(okhttp3.Interceptor) NewVersionBroadcastInterceptor(org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor) Nullable(android.support.annotation.Nullable)

Example 7 with EventBus

use of de.greenrobot.event.EventBus in project edx-app-android by edx.

the class NewVersionAvailableEvent method post.

/**
 * Post an instance of NewVersionAvailableEvent on the event bus, based on the provided
 * properties, if this hasn't been posted before. The sticky events will be queried for an
 * existing report, and a new one will only be posted if it has more urgent information than the
 * previous one. The events are posted and retained as sticky events in order to have a
 * conveniently and semantically accessible session-based singleton of it to compare against,
 * but this has the implication that they can't be removed from the event bus after consumption
 * by the subscribers. To address this restriction, this class defined methods to mark instances
 * as having being consumed, which can be used by subscribers for this purpose.
 *
 * If all the parameters are null or false (or in the case of the new version number parameter,
 * lesser than the current build's version number), then it wouldn't be a valid event, and
 * nothing would be posted on the event bus.
 *
 * @param newVersion        The version number of the latest release of the app.
 * @param lastSupportedDate The last date on which the current version of the app will be
 *                          supported.
 * @param isUnsupported     Whether the current version is unsupported. This is based on whether
 *                          we're getting HTTP 426 errors, and thus can't be inferred from the
 *                          last supported date (the two properties may not be consistent with
 *                          each other due to wrong local clock time or an inconsistency in the
 *                          server configurations).
 */
public static void post(@Nullable final Version newVersion, @Nullable final Date lastSupportedDate, final boolean isUnsupported) {
    final NewVersionAvailableEvent event;
    try {
        event = new NewVersionAvailableEvent(newVersion, lastSupportedDate, isUnsupported);
    } catch (IllegalArgumentException | IllegalStateException e) {
        // If the event is not valid, then do nothing.
        return;
    }
    final EventBus eventBus = EventBus.getDefault();
    final NewVersionAvailableEvent postedEvent = eventBus.getStickyEvent(NewVersionAvailableEvent.class);
    if (postedEvent == null || event.compareTo(postedEvent) > 0) {
        eventBus.postSticky(event);
    }
}
Also used : EventBus(de.greenrobot.event.EventBus)

Aggregations

EventBus (de.greenrobot.event.EventBus)7 Nullable (android.support.annotation.Nullable)4 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 RequestLinkEvent (com.auth0.sharelock.event.RequestLinkEvent)2 NewVersionBroadcastInterceptor (org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor)2 ClipData (android.content.ClipData)1 ClipboardManager (android.content.ClipboardManager)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NewLinkEvent (com.auth0.sharelock.event.NewLinkEvent)1 RequestNewSecretEvent (com.auth0.sharelock.event.RequestNewSecretEvent)1 SharelockAPIErrorEvent (com.auth0.sharelock.event.SharelockAPIErrorEvent)1 ShareEditText (com.auth0.sharelock.widget.ShareEditText)1 Snackbar (com.nispok.snackbar.Snackbar)1 Interceptor (okhttp3.Interceptor)1