Skip to content

Commit

Permalink
Merge pull request #820 from Microsoft/fix/pause-resume
Browse files Browse the repository at this point in the history
Align pause/resume APIs with specification
  • Loading branch information
guperrot committed Sep 28, 2018
2 parents 592d9ed + 1f508cd commit 6a879ee
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class SettingsActivity extends AppCompatActivity {
private static boolean sAnalyticsPaused;

@Override
@SuppressWarnings("deprecation")
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sNeedRestartOnStartTypeUpdate = !MainActivity.sSharedPreferences.getString(APPCENTER_START_TYPE, StartType.APP_SECRET.toString()).equals(StartType.SKIP_START.toString());
Expand All @@ -76,6 +77,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.commit();
}

@SuppressWarnings("deprecation")
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

private static final String UUID_FORMAT_REGEX = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
Expand Down Expand Up @@ -178,10 +180,7 @@ public void setEnabled(boolean enabled) {
method = resumeMethod;
}
try {

@SuppressWarnings("unchecked")
AppCenterFuture<Void> future = (AppCenterFuture<Void>) method.invoke(null);
future.get();
method.invoke(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,24 @@ public static AppCenterFuture<Void> setEnabled(boolean enabled) {
}

/**
* Pauses log transmission.
*
* @return future with null result to monitor when the operation completes.
* Pauses log transmission. This API cannot be used if the service is disabled.
* Transmission is resumed:
* <ul>
* <li>when calling {@link #resume()}.</li>
* <li>when restarting the application process and calling AppCenter.start again.</li>
* <li>when disabling and re-enabling the SDK or the Analytics module.</li>
* </ul>
*/
public static AppCenterFuture<Void> pause() {
return getInstance().pauseInstanceAsync();
public static void pause() {
getInstance().pauseInstanceAsync();
}

/**
* Resumes log transmission if paused.
*
* @return future with null result to monitor when the operation completes.
* This API cannot be used if the service is disabled.
*/
public static AppCenterFuture<Void> resume() {
return getInstance().resumeInstanceAsync();
public static void resume() {
getInstance().resumeInstanceAsync();
}

/**
Expand Down Expand Up @@ -629,33 +632,27 @@ private synchronized void setInstanceListener(AnalyticsListener listener) {
/**
* Implements {@link #pause()}}.
*/
private synchronized AppCenterFuture<Void> pauseInstanceAsync() {
final DefaultAppCenterFuture<Void> future = new DefaultAppCenterFuture<>();
postAsyncGetter(new Runnable() {
private synchronized void pauseInstanceAsync() {
post(new Runnable() {

@Override
public void run() {
mChannel.pauseGroup(ANALYTICS_GROUP);
future.complete(null);
}
}, future, null);
return future;
});
}

/**
* Implements {@link #resume()}}.
*/
private synchronized AppCenterFuture<Void> resumeInstanceAsync() {
final DefaultAppCenterFuture<Void> future = new DefaultAppCenterFuture<>();
postAsyncGetter(new Runnable() {
private synchronized void resumeInstanceAsync() {
post(new Runnable() {

@Override
public void run() {
mChannel.resumeGroup(ANALYTICS_GROUP);
future.complete(null);
}
}, future, null);
return future;
});
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand Down Expand Up @@ -61,9 +60,6 @@

public class AnalyticsTest extends AbstractAnalyticsTest {

@Mock
private Channel mChannel;

@Test
public void singleton() {
Assert.assertSame(Analytics.getInstance(), Analytics.getInstance());
Expand Down Expand Up @@ -347,7 +343,7 @@ public void notSendingLogsOnPause() {
verify(channel).addListener(isA(AnalyticsValidator.class));

/* Pause Analytics. */
Analytics.pause().get();
Analytics.pause();

/* Check if Analytics group is paused. */
verify(channel).pauseGroup(eq(analytics.getGroupName()));
Expand All @@ -358,7 +354,7 @@ public void notSendingLogsOnPause() {
verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));

/* Resume Analytics. */
Analytics.resume().get();
Analytics.resume();

/* Check if Analytics group is resumed. */
verify(channel).resumeGroup(eq(analytics.getGroupName()));
Expand All @@ -385,7 +381,7 @@ public void pauseResumeWhileDisabled() {

/* Disable and pause Analytics. */
Analytics.setEnabled(false);
Analytics.pause().get();
Analytics.pause();

/* Check if Analytics group is paused even while disabled. */
verify(channel, never()).pauseGroup(eq(analytics.getGroupName()));
Expand All @@ -396,7 +392,7 @@ public void pauseResumeWhileDisabled() {
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));

/* Resume Analytics. */
Analytics.resume().get();
Analytics.resume();

/* Check if Analytics group is resumed even while paused. */
verify(channel, never()).resumeGroup(eq(analytics.getGroupName()));
Expand Down
2 changes: 1 addition & 1 deletion versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ ext {
minSdkVersion = 16
targetSdkVersion = 28
compileSdkVersion = 28
supportLibVersion = '27.1.1'
supportLibVersion = '28.0.0'
}

0 comments on commit 6a879ee

Please sign in to comment.