Skip to content

Commit fe1e443

Browse files
committed
Compatibility fix for GMS library 8.1.0 and newer.
* Fixed compatibility issues when using GMS (Google Play services library) 8.1.0 or newer in your app. - Also insured backwards compatibility to 7.0.0.
1 parent 4028737 commit fe1e443

File tree

8 files changed

+109
-15
lines changed

8 files changed

+109
-15
lines changed

Examples/AndroidStudio/app/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ android {
2727

2828
dependencies {
2929
compile fileTree(dir: 'libs', include: ['*.jar'])
30-
testCompile 'junit:junit:4.12'
30+
3131
compile 'com.android.support:appcompat-v7:+'
3232

33-
// OneSignal and required libraries
33+
// OneSignal SDK
3434
compile 'com.onesignal:OneSignal:2.+@aar'
35+
36+
// OneSignal requires at least version 7.0.0 of GMS but the newest version is recommend.
3537
compile 'com.google.android.gms:play-services-gcm:+'
3638
compile 'com.google.android.gms:play-services-analytics:+'
37-
compile "com.google.android.gms:play-services-location:+"
39+
compile 'com.google.android.gms:play-services-location:+'
3840
}

Examples/AndroidStudio/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
<!-- All OneSignal manifest entries are added for you automatically from the OneSignal aar gradle entry. -->
66

7+
<!-- Optional permission, needed for geo tagging feature. -->
8+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
9+
710
<application
811
android:icon="@drawable/ic_launcher"
912
android:label="@string/app_name"

OneSignalSDK.jar

1.28 KB
Binary file not shown.

OneSignalSDK/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ dependencies {
4848
// Use for released SDK
4949
//compile 'com.onesignal:OneSignal:2.+@aar'
5050

51+
// Test with 7.0.0 to make sure there are no breaking changes in Google's libraries.
52+
// This insure that the SDK will work if an app developer is using an older version of GMS.
5153
compile "com.google.android.gms:play-services-gcm:7.0.0"
5254
compile "com.google.android.gms:play-services-analytics:7.0.0"
5355
compile "com.google.android.gms:play-services-location:7.0.0"

OneSignalSDK/onesignal/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ android {
2525
dependencies {
2626
provided fileTree(dir: 'libs', include: ['*.jar'])
2727

28-
// Compiling oldest version so we know the minimum required version at compile time.
29-
// 7.0.0 depends on Android Support Library v4 22.0.0
30-
compile "com.google.android.gms:play-services-gcm:7.0.0"
31-
compile "com.google.android.gms:play-services-analytics:7.0.0"
32-
compile "com.google.android.gms:play-services-location:7.0.0"
28+
compile 'com.google.android.gms:play-services-gcm:8.4.0'
29+
compile 'com.google.android.gms:play-services-analytics:8.4.0'
30+
compile 'com.google.android.gms:play-services-location:8.4.0'
3331
}
3432

3533
apply from: 'maven-push.gradle'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2015 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal;
29+
30+
import com.google.android.gms.common.api.GoogleApiClient;
31+
32+
// Allows compatibility with pre-8.1.0 of GMS via reflection.
33+
// This allows the methods below to be used even if the app developer is using an old version Google Play services.
34+
35+
public class GoogleApiClientCompatProxy {
36+
private final GoogleApiClient googleApiClient;
37+
private final Class googleApiClientListenerClass;
38+
39+
public GoogleApiClientCompatProxy(GoogleApiClient googleApiClient) {
40+
this.googleApiClient = googleApiClient;
41+
googleApiClientListenerClass = googleApiClient.getClass();
42+
}
43+
44+
public void connect() {
45+
try {
46+
googleApiClientListenerClass.getMethod("connect").invoke(googleApiClient);
47+
} catch (Throwable t) {
48+
t.printStackTrace();
49+
}
50+
}
51+
52+
public void disconnect() {
53+
try {
54+
googleApiClientListenerClass.getMethod("disconnect").invoke(googleApiClient);
55+
} catch (Throwable t) {
56+
t.printStackTrace();
57+
}
58+
}
59+
60+
public GoogleApiClient realInstance() {
61+
return googleApiClient;
62+
}
63+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/LocationGMS.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2015 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
128
package com.onesignal;
229

330
import com.onesignal.AndroidSupportV4Compat.ContextCompat;
431

5-
import android.app.Activity;
632
import android.content.Context;
7-
import android.content.Intent;
833
import android.content.pm.PackageInfo;
934
import android.content.pm.PackageManager;
1035
import android.location.Location;
1136
import android.os.Build;
1237
import android.os.Bundle;
13-
import android.util.Log;
1438

1539
import com.google.android.gms.common.ConnectionResult;
1640
import com.google.android.gms.common.api.GoogleApiClient;
@@ -22,7 +46,7 @@
2246
import java.util.List;
2347

2448
class LocationGMS {
25-
private static GoogleApiClient mGoogleApiClient;
49+
private static GoogleApiClientCompatProxy mGoogleApiClient;
2650
static String requestPermission;
2751

2852
private static ActivityLifecycleHandler.ActivityAvailableListener activityAvailableListener;
@@ -78,11 +102,13 @@ else if (locationCoarsePermission == PackageManager.PERMISSION_GRANTED)
78102

79103
static void startGetLocation() {
80104
GoogleApiClientListener googleApiClientListener = new GoogleApiClientListener();
81-
mGoogleApiClient = new GoogleApiClient.Builder(OneSignal.appContext)
105+
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(OneSignal.appContext)
82106
.addApi(LocationServices.API)
83107
.addConnectionCallbacks(googleApiClientListener)
84108
.addOnConnectionFailedListener(googleApiClientListener)
85109
.build();
110+
mGoogleApiClient = new GoogleApiClientCompatProxy(googleApiClient);
111+
86112
mGoogleApiClient.connect();
87113
}
88114

@@ -99,7 +125,7 @@ private static class GoogleApiClientListener implements GoogleApiClient.Connecti
99125
public void onConnected(Bundle bundle) {
100126
PermissionsActivity.answered = false;
101127

102-
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
128+
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient.realInstance());
103129
// Coarse always gives out 14 digits and has an accuracy 2000. Always rounding to 7 as this is what fine returns.
104130
if (location != null)
105131
locationHandler.complete(new BigDecimal(location.getLatitude()).setScale(7, RoundingMode.HALF_UP).doubleValue(), new BigDecimal(location.getLongitude()).setScale(7, RoundingMode.HALF_UP).doubleValue());

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void init() {
162162
private static TrackGooglePurchase trackGooglePurchase;
163163
private static TrackAmazonPurchase trackAmazonPurchase;
164164

165-
public static final String VERSION = "020001";
165+
public static final String VERSION = "020002";
166166

167167
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
168168

0 commit comments

Comments
 (0)