Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Last active May 19, 2016 00:43
Show Gist options
  • Save jaredsburrows/d14008b4631ac3196c3addade9f99699 to your computer and use it in GitHub Desktop.
Save jaredsburrows/d14008b4631ac3196c3addade9f99699 to your computer and use it in GitHub Desktop.
PlayServicesUtils for checking GPS
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
/**
* Google Play Services utility class.
* https://developers.google.com/android/reference/com/google/android/gms/analytics/GoogleAnalytics. I have stopped
* checking for Google Play Services because it pops up a dialog forcing a user to upgrade.
*
* @author <a href="mailto:jaredsburrows@gmail.com">Jared Burrows</a>
*/
public final class PlayServicesUtils {
private PlayServicesUtils() {
// Utility class
}
/**
* Check if device has the correct Google Play Services version.
*
* @param activity Current activity.
* @param availability New instance of GoogleApiAvailability.
* @return True if there was a successful connection ot Google Play Services.
*/
public static boolean hasGooglePlayServices(final Activity activity, final GoogleApiAvailability availability) {
final int result = availability.isGooglePlayServicesAvailable(activity);
final Dialog dialog = availability.getErrorDialog(activity, result, 0);
if (result == ConnectionResult.SUCCESS) {
return true;
} else {
// Let user use the application
dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(final DialogInterface dialog) {
dialog.cancel();
}
});
dialog.show();
}
return false;
}
}
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import com.google.android.gms.common.GoogleApiAvailability;
import org.junit.Before;
import org.junit.Test;
import burrows.apps.lib.test.RoboTestBase;
import static com.google.android.gms.common.ConnectionResult.SERVICE_MISSING;
import static com.google.android.gms.common.ConnectionResult.SUCCESS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author <a href="mailto:jaredsburrows@gmail.com">Jared Burrows</a>
* @since 0.0.1
*/
public class PlayServicesUtilsTest extends RoboTestBase {
private Activity activity;
private GoogleApiAvailability sut;
@Before
public void setUp() throws Exception {
activity = mock(Activity.class);
sut = mock(GoogleApiAvailability.class);
}
// --------------------------------------------
// getDescription(final String threadName, final Throwable throwable)
// --------------------------------------------
@Test
public void testHasGooglePlayServicesShouldReturnError() {
final Dialog dialog = mock(Dialog.class);
when(sut.isGooglePlayServicesAvailable(activity)).thenReturn(SERVICE_MISSING);
when(sut.getErrorDialog(activity, SERVICE_MISSING, 0)).thenReturn(dialog);
assertThat(PlayServicesUtils.hasGooglePlayServices(activity, sut)).isFalse();
verify(dialog).setOnCancelListener(any(DialogInterface.OnCancelListener.class));
verify(dialog).show();
dialog.dismiss();
dialog.cancel();
assertThat(dialog.isShowing()).isFalse();
verify(dialog).cancel();
}
@Test
public void testHasGooglePlayServicesShouldReturnErrorWithClose() {
final Dialog dialog = new Dialog(context);
when(sut.isGooglePlayServicesAvailable(activity)).thenReturn(SERVICE_MISSING);
when(sut.getErrorDialog(activity, SERVICE_MISSING, 0)).thenReturn(dialog);
assertThat(PlayServicesUtils.hasGooglePlayServices(activity, sut)).isFalse();
dialog.cancel();
assertThat(dialog.isShowing()).isFalse();
}
@Test
public void testHasGooglePlayServicesShouldReturnSuccess() {
when(sut.isGooglePlayServicesAvailable(activity)).thenReturn(SUCCESS);
assertThat(PlayServicesUtils.hasGooglePlayServices(activity, sut)).isTrue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment