Skip to content

Commit 77f072a

Browse files
committed
Added missing HMS PushKit library detection
* Now correctly handles reporting missing HMS PushKit library if app is running on an HMS only device.
1 parent 2eb9fb3 commit 77f072a

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private static boolean hasGCMLibrary() {
152152
}
153153
}
154154

155-
private boolean hasHMSAvailability() {
155+
private static boolean hasHMSAvailabilityLibrary() {
156156
try {
157157
// noinspection ConstantConditions
158158
return com.huawei.hms.api.HuaweiApiAvailability.class != null;
@@ -161,7 +161,7 @@ private boolean hasHMSAvailability() {
161161
}
162162
}
163163

164-
private boolean hasHMSPushKitLibrary() {
164+
private static boolean hasHMSPushKitLibrary() {
165165
try {
166166
// noinspection ConstantConditions
167167
return com.huawei.hms.aaid.HmsInstanceId.class != null;
@@ -170,6 +170,21 @@ private boolean hasHMSPushKitLibrary() {
170170
}
171171
}
172172

173+
private static boolean hasHMSAGConnectLibrary() {
174+
try {
175+
// noinspection ConstantConditions
176+
return com.huawei.agconnect.config.AGConnectServicesConfig.class != null;
177+
} catch (Throwable e) {
178+
return false;
179+
}
180+
}
181+
182+
static boolean hasAllHMSLibrariesForPushKit() {
183+
// NOTE: hasHMSAvailabilityLibrary technically is not required,
184+
// just used as recommend way to detect if "HMS Core" app exists and is enabled
185+
return hasHMSAGConnectLibrary() && hasHMSPushKitLibrary();
186+
}
187+
173188
Integer checkForGooglePushLibrary() {
174189
boolean hasFCMLibrary = hasFCMLibrary();
175190
boolean hasGCMLibrary = hasGCMLibrary();
@@ -286,8 +301,8 @@ private boolean supportsADM() {
286301
}
287302

288303
private boolean supportsHMS() {
289-
// 1. App must have the HMSAvailability and PushKit libraries to support HMS push
290-
if (!hasHMSAvailability() || !hasHMSPushKitLibrary())
304+
// 1. App should have the HMSAvailability for best detection and must have PushKit libraries
305+
if (!hasHMSAvailabilityLibrary() || !hasAllHMSLibrariesForPushKit())
291306
return false;
292307

293308
// 2. Device must have HMS Core installed and enabled

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ public void run() {
5353
}
5454

5555
private synchronized void getHMSTokenTask(@NonNull Context context, @NonNull RegisteredHandler callback) throws ApiException {
56-
// TODO: See if we can handle an exact message like this
57-
// 2020-04-14 23:06:36.164 1565-1743/com.onesignal.example E/HMSSDK_Util: In getMetaDataAppId, Failed to read meta data for the AppID.
56+
// If here is required to prevent AGConnectServicesConfig or HmsInstanceId used below
57+
// from throwing a ClassNotFoundException
58+
if (!OSUtils.hasAllHMSLibrariesForPushKit()) {
59+
callback.complete(null, UserState.PUSH_STATUS_MISSING_HMS_PUSHKIT_LIBRARY);
60+
return;
61+
}
5862

5963
String appId = AGConnectServicesConfig.fromContext(context).getString(HMS_CLIENT_APP_ID);
6064
HmsInstanceId hmsInstanceId = HmsInstanceId.getInstance(context);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class UserState {
3434
// Check that there is "apply plugin: 'com.huawei.agconnect'" in your app/build.gradle
3535
public static final int PUSH_STATUS_HMS_ARGUMENTS_INVALID = -26;
3636
public static final int PUSH_STATUS_HMS_API_EXCEPTION_OTHER = -27;
37+
public static final int PUSH_STATUS_MISSING_HMS_PUSHKIT_LIBRARY = -28;
3738

3839
private static final String[] LOCATION_FIELDS = new String[] { "lat", "long", "loc_acc", "loc_type", "loc_bg", "loc_time_stamp", "ad_id"};
3940
private static final Set<String> LOCATION_FIELDS_SET = new HashSet<>(Arrays.asList(LOCATION_FIELDS));

OneSignalSDK/unittest/src/test/java/com/onesignal/ShadowOSUtils.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ public static boolean isGMSInstalledAndEnabled() {
3232

3333
// Huawei: HMS
3434
public static boolean hasHMSAvailability;
35-
public boolean hasHMSAvailability() {
35+
public static boolean hasHMSAvailability() {
3636
return hasHMSAvailability;
3737
}
3838
public static boolean hasHMSPushKitLibrary;
39-
public boolean hasHMSPushKitLibrary() {
39+
public static boolean hasHMSPushKitLibrary() {
4040
return hasHMSPushKitLibrary;
4141
}
42+
public static boolean hasHMSAGConnectLibrary;
43+
public static boolean hasHMSAGConnectLibrary() {
44+
return hasHMSAGConnectLibrary;
45+
}
46+
4247
public static boolean isHMSCoreInstalledAndEnabled;
4348
public static boolean isHMSCoreInstalledAndEnabled() {
4449
return isHMSCoreInstalledAndEnabled;
@@ -47,9 +52,16 @@ public static boolean isHMSCoreInstalledAndEnabledFallback() {
4752
return isHMSCoreInstalledAndEnabled;
4853
}
4954

55+
public static void hasAllRecommendHMSLibraries(boolean value) {
56+
// required
57+
hasHMSPushKitLibrary = value;
58+
hasHMSAGConnectLibrary = value;
59+
// recommend
60+
hasHMSAvailability = value;
61+
}
62+
5063
public static void supportsHMS(boolean value) {
51-
hasHMSPushKitLibrary = true;
52-
hasHMSAvailability = true;
64+
hasAllRecommendHMSLibraries(value);
5365
isHMSCoreInstalledAndEnabled = true;
5466
}
5567

@@ -66,6 +78,7 @@ public static void resetStatics() {
6678
isGMSInstalledAndEnabled = false;
6779
hasHMSAvailability = false;
6880
hasHMSPushKitLibrary = false;
81+
hasHMSAGConnectLibrary = false;
6982
isHMSCoreInstalledAndEnabled = false;
7083

7184
}

OneSignalSDK/unittest/src/test/java/com/test/onesignal/DeviceTypeTestsRunner.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public void GCMAndGMSEnabled_isAndroid() {
8383
@Test
8484
public void supportedHMS_isHuawei() {
8585
ShadowOSUtils.isHMSCoreInstalledAndEnabled = true;
86-
ShadowOSUtils.hasHMSAvailability = true;
87-
ShadowOSUtils.hasHMSPushKitLibrary = true;
86+
ShadowOSUtils.hasAllRecommendHMSLibraries(true);
8887

8988
assertEquals(DEVICE_TYPE_HUAWEI, getDeviceType());
9089
}
@@ -95,8 +94,7 @@ public void supportsFCMAndHMS_PreferAndroid() {
9594
ShadowOSUtils.hasFCMLibrary = true;
9695

9796
ShadowOSUtils.isHMSCoreInstalledAndEnabled = true;
98-
ShadowOSUtils.hasHMSAvailability = true;
99-
ShadowOSUtils.hasHMSPushKitLibrary = true;
97+
ShadowOSUtils.hasAllRecommendHMSLibraries(true);
10098

10199
// Prefer Google Services over Huawei if both available
102100
assertEquals(DEVICE_TYPE_ANDROID, getDeviceType());
@@ -108,8 +106,7 @@ public void hasFCMButNoGMSOnDeviceAndHasHMS_isHuawei() {
108106
ShadowOSUtils.hasFCMLibrary = true;
109107

110108
ShadowOSUtils.isHMSCoreInstalledAndEnabled = true;
111-
ShadowOSUtils.hasHMSAvailability = true;
112-
ShadowOSUtils.hasHMSPushKitLibrary = true;
109+
ShadowOSUtils.hasAllRecommendHMSLibraries(true);
113110

114111
// Use HMS since device does not have the "Google Play services" app or it is disabled
115112
assertEquals(DEVICE_TYPE_HUAWEI, getDeviceType());

0 commit comments

Comments
 (0)