Skip to content

Commit

Permalink
Handle present-but-unhappy play services in lollipop
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bowers committed Dec 18, 2014
1 parent d4bd07b commit 4e105a0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 123 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group = com.mixpanel.android
version = 4.5.0-SNAPSHOT
version = 4.5.0
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class EditProtocolTest extends AndroidTestCase {
public void setUp() throws JSONException {
mProtocol = new EditProtocol(getContext());
mSnapshotConfig = new JSONObject(
"{\"config\": {\"classes\":[{\"name\":\"android.view.View\",\"properties\":[{\"name\":\"importantForAccessibility\",\"get\":{\"selector\":\"isImportantForAccessibility\",\"parameters\":[],\"result\":{\"type\":\"java.lang.Boolean\"}}},{\"name\":\"backgroundColor\",\"get\":{\"selector\":\"getBackgroundColor\",\"parameters\":[],\"result\":{\"type\":\"java.lang.Integer\"}},\"set\":{\"selector\":\"setBackgroundColor\",\"parameters\":[{\"type\":\"java.lang.Integer\"}]},\"editor\":\"hexstring\"}]},{\"name\":\"android.widget.TextView\",\"properties\":[{\"name\":\"text\",\"get\":{\"selector\":\"getText\",\"parameters\":[],\"result\":{\"type\":\"java.lang.CharSequence\"}},\"set\":{\"selector\":\"setText\",\"parameters\":[{\"type\":\"java.lang.CharSequence\"}]}}]},{\"name\":\"android.widget.ImageView\",\"properties\":[{\"name\":\"image\",\"set\":{\"selector\":\"setImageBitmap\",\"parameters\":[{\"type\":\"android.graphics.Bitmap\"}]}}]}]}}"
"{\"config\": {\"classes\":[{\"name\":\"android.view.View\",\"properties\":[{\"name\":\"importantForAccessibility\",\"get\":{\"selector\":\"isImportantForAccessibility\",\"parameters\":[],\"result\":{\"type\":\"java.lang.Boolean\"}}}]},{\"name\":\"android.widget.TextView\",\"properties\":[{\"name\":\"text\",\"get\":{\"selector\":\"getText\",\"parameters\":[],\"result\":{\"type\":\"java.lang.CharSequence\"}},\"set\":{\"selector\":\"setText\",\"parameters\":[{\"type\":\"java.lang.CharSequence\"}]}}]},{\"name\":\"android.widget.ImageView\",\"properties\":[{\"name\":\"image\",\"set\":{\"selector\":\"setImageBitmap\",\"parameters\":[{\"type\":\"android.graphics.Bitmap\"}]}}]}]}}"
);
mPropertyEdit = new JSONObject(
"{\"path\":[{\"view_class\":\"com.mixpanel.android.viewcrawler.TestView\",\"index\":0},{\"view_class\":\"android.widget.LinearLayout\",\"index\":0},{\"view_class\":\"android.widget.LinearLayout\",\"index\":0},{\"view_class\":\"android.widget.Button\",\"index\":1}],\"property\":{\"name\":\"text\",\"get\":{\"selector\":\"getText\",\"parameters\":[],\"result\":{\"type\":\"java.lang.CharSequence\"}},\"set\":{\"selector\":\"setText\",\"parameters\":[{\"type\":\"java.lang.CharSequence\"}]}},\"args\":[[\"Ground Control to Major Tom\",\"java.lang.CharSequence\"]]}"
Expand Down Expand Up @@ -56,21 +56,18 @@ public void testSnapshotConfig() throws EditProtocol.BadInstructionsException {
final ViewSnapshot snapshot = mProtocol.readSnapshotConfig(mSnapshotConfig);
final List<PropertyDescription> properties = snapshot.getProperties();

assertEquals(properties.size(), 4);
assertEquals(properties.size(), 3);
final PropertyDescription prop1 = properties.get(0);
final PropertyDescription prop2 = properties.get(1);
final PropertyDescription prop3 = properties.get(2);
final PropertyDescription prop4 = properties.get(3);

assertEquals(prop1.name, "importantForAccessibility");
assertEquals(prop2.name, "backgroundColor");
assertEquals(prop3.name, "text");
assertEquals(prop4.name, "image");
assertEquals(prop2.name, "text");
assertEquals(prop3.name, "image");

assertEquals(prop1.targetClass, View.class);
assertEquals(prop2.targetClass, View.class);
assertEquals(prop3.targetClass, TextView.class);
assertEquals(prop4.targetClass, ImageView.class);
assertEquals(prop2.targetClass, TextView.class);
assertEquals(prop3.targetClass, ImageView.class);
}

public void testReadPaths() throws JSONException {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import java.util.Set;

public class ViewSnapshotTest extends AndroidTestCase {
public void setUp() {
public void setUp() throws NoSuchMethodException {
mRootView = new TestView(this.getContext());

final List<PropertyDescription> props = new ArrayList<PropertyDescription>();

final Caller textGetter = new Caller("getText", new Object[0], CharSequence.class);
final Caller textGetter = new Caller(TextView.class, "getText", new Object[0], CharSequence.class);
final PropertyDescription text = new PropertyDescription("text", TextView.class, textGetter, "setText");
props.add(text);

final Caller customPropGetter = new Caller("getCustomProperty", new Object[0], CharSequence.class);
final Caller customPropGetter = new Caller(TestView.CustomPropButton.class, "getCustomProperty", new Object[0], CharSequence.class);
final PropertyDescription custom = new PropertyDescription(
"custom",
TestView.CustomPropButton.class,
Expand All @@ -42,24 +42,6 @@ public void setUp() {
);
props.add(custom);

final Caller crazyGetter = new Caller("CRAZY GETTER", new Object[0], Void.TYPE);
final PropertyDescription crazy = new PropertyDescription(
"crazy",
View.class,
crazyGetter,
"CRAZY SETTER"
);
props.add(crazy);

final Caller badTypesGetter = new Caller("getText", new Object[0], Integer.class);
final PropertyDescription badTypes = new PropertyDescription(
"badTypes",
TextView.class,
badTypesGetter,
"setText"
);
props.add(badTypes);

final SparseArray<String> idNamesById = new SparseArray<String>();
idNamesById.put(TestView.ROOT_ID, "ROOT_ID");
idNamesById.put(TestView.TEXT_VIEW_ID, "TEXT_VIEW_ID");
Expand All @@ -69,6 +51,22 @@ public void setUp() {
mSnapshot = new ViewSnapshot(props, idNamesById);
}

public void testBadMethods() {
try {
final Caller crazyGetter = new Caller(View.class, "CRAZY GETTER", new Object[0], Void.TYPE);
fail("Exception was not thrown when constructing a bad caller");
} catch (NoSuchMethodException e) {
// OK!
}

try {
final Caller badTypesGetter = new Caller(TextView.class, "getText", new Object[0], Integer.class);
fail("Exception was not thrown when constructing a caller with bad types");
} catch (NoSuchMethodException e) {
// OK!
}
}

public void testViewSnapshot() throws IOException, JSONException {
int width = View.MeasureSpec.makeMeasureSpec(768, View.MeasureSpec.EXACTLY);
int height = View.MeasureSpec.makeMeasureSpec(1280, View.MeasureSpec.EXACTLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public void testResetSameEventOnClick() {
mTrackListener.events.clear();
}

public void testDuplicateBitmapSet() {
public void testDuplicateBitmapSet() throws NoSuchMethodException {
final Paint paint = new Paint();
paint.setColor(Color.BLUE);

Expand All @@ -319,10 +319,10 @@ public void testDuplicateBitmapSet() {
final Canvas canvas2 = new Canvas(bitmap2);
canvas2.drawCircle(6, 6, 4, paint);

final Caller mutateBitmap1a = new Caller("setCountingProperty", new Object[] { bitmap1a }, Void.TYPE);
final Caller mutateBitmap1b = new Caller("setCountingProperty", new Object[] { bitmap1b }, Void.TYPE);
final Caller mutateBitmap2 = new Caller("setCountingProperty", new Object[] { bitmap2 }, Void.TYPE);
final Caller accessBitmap = new Caller("getCountingProperty", new Object[]{}, Object.class);
final Caller mutateBitmap1a = new Caller(TestView.AdHocButton2.class, "setCountingProperty", new Object[] { bitmap1a }, Void.TYPE);
final Caller mutateBitmap1b = new Caller(TestView.AdHocButton2.class, "setCountingProperty", new Object[] { bitmap1b }, Void.TYPE);
final Caller mutateBitmap2 = new Caller(TestView.AdHocButton2.class, "setCountingProperty", new Object[] { bitmap2 }, Void.TYPE);
final Caller accessBitmap = new Caller(TestView.AdHocButton2.class, "getCountingProperty", new Object[]{}, Object.class);

{
final ViewVisitor propertySetVisitor1_1 =
Expand Down Expand Up @@ -354,11 +354,11 @@ public void testDuplicateBitmapSet() {
}
}

public void testDuplicateTextSet() {
final Caller mutateCountingProperty1a = new Caller("setCountingProperty", new Object[]{"Set String1"}, Void.TYPE);
final Caller mutateCountingProperty1b = new Caller("setCountingProperty", new Object[]{"Set String1"}, Void.TYPE);
final Caller mutateCountingProperty2 = new Caller("setCountingProperty", new Object[]{"Set String2"}, Void.TYPE);
final Caller accessCountingProperty = new Caller("getCountingProperty", new Object[]{}, Object.class);
public void testDuplicateTextSet() throws NoSuchMethodException {
final Caller mutateCountingProperty1a = new Caller(TestView.AdHocButton2.class, "setCountingProperty", new Object[]{"Set String1"}, Void.TYPE);
final Caller mutateCountingProperty1b = new Caller(TestView.AdHocButton2.class, "setCountingProperty", new Object[]{"Set String1"}, Void.TYPE);
final Caller mutateCountingProperty2 = new Caller(TestView.AdHocButton2.class, "setCountingProperty", new Object[]{"Set String2"}, Void.TYPE);
final Caller accessCountingProperty = new Caller(TestView.AdHocButton2.class, "getCountingProperty", new Object[]{}, Object.class);

{
final ViewVisitor propertySetVisitor1_1 =
Expand Down
54 changes: 33 additions & 21 deletions src/main/java/com/mixpanel/android/mpmetrics/AnalyticsMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,18 @@ private void runGCMRegistration(String senderID) {
// Consider adding a transitive dependency on the latest
// Google Play Services version and requiring Java 1.7
// in the next major library release.

final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (resultCode != ConnectionResult.SUCCESS) {
Log.i(LOGTAG, "Can't register for push notifications, Google Play Services are not installed.");
try {
final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (resultCode != ConnectionResult.SUCCESS) {
Log.i(LOGTAG, "Can't register for push notifications, Google Play Services are not installed.");
return;
}
} catch (RuntimeException e) {
Log.i(LOGTAG, "Can't register for push notifications, Google Play services are not configured.");
return;
}


final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(mContext);
registrationId = gcm.register(senderID);
} catch (IOException e) {
Expand Down Expand Up @@ -439,24 +444,31 @@ private JSONObject getDefaultEventProperties()
ret.put("$model", Build.MODEL == null ? "UNKNOWN" : Build.MODEL);

try {
final int servicesAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
switch (servicesAvailable) {
case ConnectionResult.SUCCESS:
ret.put("$google_play_services", "available");
break;
case ConnectionResult.SERVICE_MISSING:
ret.put("$google_play_services", "missing");
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
ret.put("$google_play_services", "out of date");
break;
case ConnectionResult.SERVICE_DISABLED:
ret.put("$google_play_services", "disabled");
break;
case ConnectionResult.SERVICE_INVALID:
ret.put("$google_play_services", "invalid");
break;
try {
final int servicesAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
switch (servicesAvailable) {
case ConnectionResult.SUCCESS:
ret.put("$google_play_services", "available");
break;
case ConnectionResult.SERVICE_MISSING:
ret.put("$google_play_services", "missing");
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
ret.put("$google_play_services", "out of date");
break;
case ConnectionResult.SERVICE_DISABLED:
ret.put("$google_play_services", "disabled");
break;
case ConnectionResult.SERVICE_INVALID:
ret.put("$google_play_services", "invalid");
break;
}
} catch (RuntimeException e) {
// Turns out even checking for the service will cause explosions
// unless we've set up meta-data
ret.put("$google_play_services", "not configured");
}

} catch (NoClassDefFoundError e) {
ret.put("$google_play_services", "not included");
}
Expand Down

0 comments on commit 4e105a0

Please sign in to comment.