@@ -243,26 +243,37 @@ && getTargetSdkVersion(context) >= Build.VERSION_CODES.O) {
243
243
return null ;
244
244
}
245
245
246
- // TODO: Maybe able to switch to GoogleApiAvailability.isGooglePlayServicesAvailable to simplify
247
- // However before doing so we need to test with an old version of the "Google Play services"
248
- // on the device to make sure it would still be counted as "SUCCESS".
249
- // Or if we get back "SERVICE_VERSION_UPDATE_REQUIRED" then we may want to count that as successful too.
250
- static boolean isGMSInstalledAndEnabled () {
246
+ private static boolean packageInstalledAndEnabled (@ NonNull String packageName ) {
251
247
try {
252
248
PackageManager pm = OneSignal .appContext .getPackageManager ();
253
- PackageInfo info = pm .getPackageInfo (GoogleApiAvailability . GOOGLE_PLAY_SERVICES_PACKAGE , PackageManager .GET_META_DATA );
249
+ PackageInfo info = pm .getPackageInfo (packageName , PackageManager .GET_META_DATA );
254
250
return info .applicationInfo .enabled ;
255
251
} catch (PackageManager .NameNotFoundException e ) {
256
252
return false ;
257
253
}
258
254
}
259
255
256
+ // TODO: Maybe able to switch to GoogleApiAvailability.isGooglePlayServicesAvailable to simplify
257
+ // However before doing so we need to test with an old version of the "Google Play services"
258
+ // on the device to make sure it would still be counted as "SUCCESS".
259
+ // Or if we get back "SERVICE_VERSION_UPDATE_REQUIRED" then we may want to count that as successful too.
260
+ static boolean isGMSInstalledAndEnabled () {
261
+ return packageInstalledAndEnabled (GoogleApiAvailability .GOOGLE_PLAY_SERVICES_PACKAGE );
262
+ }
263
+
260
264
private static final int HMS_AVAILABLE_SUCCESSFUL = 0 ;
261
265
private static boolean isHMSCoreInstalledAndEnabled () {
262
266
HuaweiApiAvailability availability = HuaweiApiAvailability .getInstance ();
263
267
return availability .isHuaweiMobileServicesAvailable (OneSignal .appContext ) == HMS_AVAILABLE_SUCCESSFUL ;
264
268
}
265
269
270
+ private static final String HMS_CORE_SERVICES_PACKAGE = "com.huawei.hwid" ; // = HuaweiApiAvailability.SERVICES_PACKAGE
271
+ // HuaweiApiAvailability is the recommend way to detect if "HMS Core" is available but this fallback
272
+ // works even if the app developer doesn't include any HMS libraries in their app.
273
+ private static boolean isHMSCoreInstalledAndEnabledFallback () {
274
+ return packageInstalledAndEnabled (HMS_CORE_SERVICES_PACKAGE );
275
+ }
276
+
266
277
private boolean supportsADM () {
267
278
try {
268
279
// Class only available on the FireOS and only when the following is in the AndroidManifest.xml.
@@ -310,13 +321,22 @@ int getDeviceType() {
310
321
311
322
if (supportsGooglePush ())
312
323
return UserState .DEVICE_TYPE_ANDROID ;
313
- else {
314
- // If the Huawei device has both FCM & HMS support we prefer FCM (Google push)
315
- // HMS will only be used if it is the only option.
316
- if (supportsHMS ())
317
- return UserState .DEVICE_TYPE_HUAWEI ;
318
- }
319
324
325
+ // Some Huawei devices have both FCM & HMS support, but prefer FCM (Google push) over HMS
326
+ if (supportsHMS ())
327
+ return UserState .DEVICE_TYPE_HUAWEI ;
328
+
329
+ // Start - Fallback logic
330
+ // Libraries in the app (Google:FCM, HMS:PushKit) + Device may not have a valid combo
331
+ // Example: App with only the FCM library in it and a Huawei device with only HMS Core
332
+
333
+ if (isGMSInstalledAndEnabled ())
334
+ return UserState .DEVICE_TYPE_ANDROID ;
335
+
336
+ if (isHMSCoreInstalledAndEnabledFallback ())
337
+ return UserState .DEVICE_TYPE_HUAWEI ;
338
+
339
+ // Last fallback
320
340
// Fallback to device_type 1 (Android) if there are no supported push channels on the device
321
341
return UserState .DEVICE_TYPE_ANDROID ;
322
342
}
0 commit comments