Skip to content

Commit 2eb9fb3

Browse files
committed
Fallback detection for Huawei device if no HMS SDK
* We should still detect a real Huawei with HMS only as device_type 13 even if there is no HMS library in the app.
1 parent 4feaa49 commit 2eb9fb3

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,37 @@ && getTargetSdkVersion(context) >= Build.VERSION_CODES.O) {
243243
return null;
244244
}
245245

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) {
251247
try {
252248
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);
254250
return info.applicationInfo.enabled;
255251
} catch (PackageManager.NameNotFoundException e) {
256252
return false;
257253
}
258254
}
259255

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+
260264
private static final int HMS_AVAILABLE_SUCCESSFUL = 0;
261265
private static boolean isHMSCoreInstalledAndEnabled() {
262266
HuaweiApiAvailability availability = HuaweiApiAvailability.getInstance();
263267
return availability.isHuaweiMobileServicesAvailable(OneSignal.appContext) == HMS_AVAILABLE_SUCCESSFUL;
264268
}
265269

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+
266277
private boolean supportsADM() {
267278
try {
268279
// Class only available on the FireOS and only when the following is in the AndroidManifest.xml.
@@ -310,13 +321,22 @@ int getDeviceType() {
310321

311322
if (supportsGooglePush())
312323
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-
}
319324

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
320340
// Fallback to device_type 1 (Android) if there are no supported push channels on the device
321341
return UserState.DEVICE_TYPE_ANDROID;
322342
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public boolean hasHMSPushKitLibrary() {
4343
public static boolean isHMSCoreInstalledAndEnabled() {
4444
return isHMSCoreInstalledAndEnabled;
4545
}
46+
public static boolean isHMSCoreInstalledAndEnabledFallback() {
47+
return isHMSCoreInstalledAndEnabled;
48+
}
4649

4750
public static void supportsHMS(boolean value) {
4851
hasHMSPushKitLibrary = true;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ public void hasFCMButNoGMSOnDeviceAndHasHMS_isHuawei() {
115115
assertEquals(DEVICE_TYPE_HUAWEI, getDeviceType());
116116
}
117117

118+
@Test
119+
public void noPushSDKsAndOnlyHMSCoreInstalled_isHuawei() {
120+
ShadowOSUtils.isHMSCoreInstalledAndEnabled = true;
121+
assertEquals(DEVICE_TYPE_HUAWEI, getDeviceType());
122+
}
123+
124+
@Test
125+
public void noPushSDKsAndOnlyGoogleServicesInstalled_isAndroid() {
126+
ShadowOSUtils.isGMSInstalledAndEnabled = true;
127+
assertEquals(DEVICE_TYPE_ANDROID, getDeviceType());
128+
}
129+
118130
@Test
119131
public void supportsFCMAndADM_PreferADM() {
120132
ShadowOSUtils.isGMSInstalledAndEnabled = true;

0 commit comments

Comments
 (0)