Skip to content

Commit fd21a6d

Browse files
authored
Completely remove global static deviceType int (#994)
* Completely remove global static deviceType int * Better to use the OSUtils getDeviceType method since we wont know what part fo the SDK init we are in * The chance of a 0 deviceType is a race condition we do not want to fight with * Remove space from param key name "device_type" * Unit tests created for testing Android and Amazon device types * Make sure that the correct device_type appears in the player create network requests * Using ShadowOSUtils getDeviceType now relies on a availableClasses HashSet * Created a method called mockAmazonDevice that will add the ADM class string and when this exists we can pretend this device is a Amazon FireOS device * Added a few new asserts to check against device type in player create payloads and generic on session requests * Cleaned up device type unit tests * Checking against the deviceType in the player create right after OneSignal init * Added resetStatics to ADM shadow in unit tests * Without resetting the statics for ADM the on_session will not be called, need to find WIP need to fix the current test related to making sure previous usage deviceType fails and new usage passes * Finished unit test related to device type on Android and Amazon * Remove package private helper for setAppContext * Removed all refs to appId and now we try to use a cached appId in the network requests * Testing deviceType to reproduce the 0 deviceType and this is through two new tests, one for Android and one for Amazon
1 parent 2d235ef commit fd21a6d

16 files changed

+398
-128
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
import java.util.List;
1313
import java.util.concurrent.atomic.AtomicBoolean;
1414

15-
1615
/**
1716
* Entry points that could cause on_focus to fire:
1817
* 1. OSSessionManager.session changed (onSessionEnded) - Send any attributed session time
1918
* 2. App is foregrounded (appForegrounded) - Set start focused time
2019
* 3. App is backgrounded (appBackgrounded) - Kick off job to sync when session ends
2120
*/
22-
23-
2421
class FocusTimeController {
2522
// Only present if app is currently in focus.
2623
@Nullable private Long timeFocusedAtMs;
@@ -239,7 +236,7 @@ protected void additionalFieldsToAddToOnFocusPayload(@NonNull JSONObject jsonBod
239236

240237
private @NonNull JSONObject generateOnFocusPayload(long totalTimeActive) throws JSONException {
241238
JSONObject jsonBody = new JSONObject()
242-
.put("app_id", OneSignal.appId)
239+
.put("app_id", OneSignal.getSavedAppId())
243240
.put("type", 1) // Always 1, where this type means do NOT increase session_count
244241
.put("state", "ping") // Always ping, other types are not used
245242
.put("active_time", totalTimeActive)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private static void setAlertnessOptions(JSONObject gcmBundle, NotificationCompat
297297
else
298298
notificationDefaults |= Notification.DEFAULT_LIGHTS;
299299

300-
if (OneSignal.getVibrate(currentContext) && gcmBundle.optInt("vib", 1) == 1) {
300+
if (OneSignal.getVibrate() && gcmBundle.optInt("vib", 1) == 1) {
301301
if (gcmBundle.has("vib_pt")) {
302302
long[] vibrationPattern = OSUtils.parseVibrationPattern(gcmBundle);
303303
if (vibrationPattern != null)
@@ -1007,7 +1007,7 @@ private static boolean isSoundEnabled(JSONObject gcmBundle) {
10071007
String sound = gcmBundle.optString("sound", null);
10081008
if ("null".equals(sound) || "nil".equals(sound))
10091009
return false;
1010-
return OneSignal.getSoundEnabled(currentContext);
1010+
return OneSignal.getSoundEnabled();
10111011
}
10121012

10131013
// Android 5.0 accent color to use, only works when AndroidManifest.xml is targetSdkVersion >= 21

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222

23+
import static com.onesignal.OneSignal.getSavedAppId;
24+
2325
class OSInAppMessageController implements OSDynamicTriggerControllerObserver, OSSystemConditionController.OSSystemConditionObserver {
2426

2527
private static ArrayList<String> PREFERRED_VARIANT_ORDER = new ArrayList<String>() {{
@@ -400,7 +402,7 @@ private void fireRESTCallForClick(@NonNull final OSInAppMessage message, @NonNul
400402

401403
try {
402404
JSONObject json = new JSONObject() {{
403-
put("app_id", OneSignal.appId);
405+
put("app_id", OneSignal.getSavedAppId());
404406
put("device_type", new OSUtils().getDeviceType());
405407
put("player_id", OneSignal.getUserId());
406408
put("click_id", clickId);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import android.content.ContentResolver;
3232
import android.content.Context;
3333
import android.content.Intent;
34-
import android.content.pm.ActivityInfo;
3534
import android.content.pm.ApplicationInfo;
3635
import android.content.pm.PackageManager;
3736
import android.content.res.Resources;
@@ -100,8 +99,9 @@ public static boolean shouldRetryNetworkRequest(int statusCode) {
10099
return true;
101100
}
102101

103-
int initializationChecker(Context context, int deviceType, String oneSignalAppId) {
102+
int initializationChecker(Context context, String oneSignalAppId) {
104103
int subscribableStatus = 1;
104+
int deviceType = getDeviceType();
105105

106106
try {
107107
//noinspection ResultOfMethodCallIgnored

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

Lines changed: 86 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ public void onSessionEnding(@NonNull OSSessionManager.SessionResult lastSessionR
440440

441441
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
442442

443-
private static int deviceType;
444443
@SuppressWarnings("WeakerAccess")
445444
public static String sdkType = "native";
446445

@@ -600,7 +599,6 @@ public static void setAppContext(@NonNull Context context) {
600599
// Register the lifecycle listener of the app for state changes in activities with proper context
601600
ActivityLifecycleListener.registerActivityLifecycleCallbacks((Application)appContext);
602601

603-
604602
if (wasAppContextNull) {
605603
sessionManager = new OSSessionManager(getNewSessionListener());
606604
outcomeEventsController = new OutcomeEventsController(sessionManager, getDBHelperInstance());
@@ -684,8 +682,7 @@ public static void init(Context context, String googleProjectNumber, String oneS
684682
if (!isGoogleProjectNumberRemote())
685683
mGoogleProjectNumber = googleProjectNumber;
686684

687-
deviceType = osUtils.getDeviceType();
688-
subscribableStatus = osUtils.initializationChecker(context, deviceType, oneSignalAppId);
685+
subscribableStatus = osUtils.initializationChecker(context, oneSignalAppId);
689686
if (isSubscriptionStatusUninitializable())
690687
return;
691688

@@ -766,14 +763,14 @@ private static void handleAppIdChange() {
766763
if (oldAppId != null) {
767764
if (!oldAppId.equals(appId)) {
768765
Log(LOG_LEVEL.DEBUG, "APP ID changed, clearing user id as it is no longer valid.");
769-
SaveAppId(appId);
766+
saveAppId(appId);
770767
OneSignalStateSynchronizer.resetCurrentState();
771768
remoteParams = null;
772769
}
773770
}
774771
else {
775772
BadgeCountUpdater.updateCount(0, appContext);
776-
SaveAppId(appId);
773+
saveAppId(appId);
777774
}
778775
}
779776

@@ -955,6 +952,7 @@ private static PushRegistrator getPushRegistrator() {
955952
if (mPushRegistrator != null)
956953
return mPushRegistrator;
957954

955+
int deviceType = osUtils.getDeviceType();
958956
if (deviceType == UserState.DEVICE_TYPE_FIREOS)
959957
mPushRegistrator = new PushRegistratorADM();
960958
else if (OSUtils.hasFCMLibrary())
@@ -1317,7 +1315,7 @@ private static void registerUserTask() throws JSONException {
13171315

13181316
JSONObject deviceInfo = new JSONObject();
13191317

1320-
deviceInfo.put("app_id", appId);
1318+
deviceInfo.put("app_id", getSavedAppId());
13211319

13221320
String adId = mainAdIdProvider.getIdentifier(appContext);
13231321
if (adId != null)
@@ -1344,7 +1342,7 @@ private static void registerUserTask() throws JSONException {
13441342
pushState.put("identifier", lastRegistrationId);
13451343
pushState.put("subscribableStatus", subscribableStatus);
13461344
pushState.put("androidPermission", areNotificationsEnabledForSubscribedState());
1347-
pushState.put("device_type", deviceType);
1345+
pushState.put("device_type", osUtils.getDeviceType());
13481346
OneSignalStateSynchronizer.updatePushState(pushState);
13491347

13501348
if (shareLocation && lastLocationPoint != null)
@@ -1981,7 +1979,7 @@ static void sendPurchases(JSONArray purchases, boolean newAsExisting, OneSignalR
19811979

19821980
try {
19831981
JSONObject jsonBody = new JSONObject();
1984-
jsonBody.put("app_id", appId);
1982+
jsonBody.put("app_id", getSavedAppId());
19851983
if (newAsExisting)
19861984
jsonBody.put("existing", true);
19871985
jsonBody.put("purchases", purchases);
@@ -2177,7 +2175,7 @@ private static void notificationOpenedRESTCall(Context inContext, JSONArray data
21772175
jsonBody.put("app_id", getSavedAppId(inContext));
21782176
jsonBody.put("player_id", getSavedUserId(inContext));
21792177
jsonBody.put("opened", true);
2180-
jsonBody.put("device_type", deviceType);
2178+
jsonBody.put("device_type", osUtils.getDeviceType());
21812179

21822180
OneSignalRestClient.put("notifications/" + notificationId, jsonBody, new OneSignalRestClient.ResponseHandler() {
21832181
@Override
@@ -2192,11 +2190,14 @@ void onFailure(int statusCode, String response, Throwable throwable) {
21922190
}
21932191
}
21942192

2195-
private static void SaveAppId(String appId) {
2193+
private static void saveAppId(String appId) {
21962194
if (appContext == null)
21972195
return;
2198-
OneSignalPrefs.saveString(OneSignalPrefs.PREFS_ONESIGNAL,
2199-
OneSignalPrefs.PREFS_GT_APP_ID, appId);
2196+
2197+
OneSignalPrefs.saveString(
2198+
OneSignalPrefs.PREFS_ONESIGNAL,
2199+
OneSignalPrefs.PREFS_GT_APP_ID,
2200+
appId);
22002201
}
22012202

22022203
static String getSavedAppId() {
@@ -2207,35 +2208,44 @@ private static String getSavedAppId(Context inContext) {
22072208
if (inContext == null)
22082209
return null;
22092210

2210-
return OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
2211-
OneSignalPrefs.PREFS_GT_APP_ID,null);
2211+
return OneSignalPrefs.getString(
2212+
OneSignalPrefs.PREFS_ONESIGNAL,
2213+
OneSignalPrefs.PREFS_GT_APP_ID,
2214+
null);
22122215
}
22132216

22142217
static boolean getSavedUserConsentStatus() {
2215-
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL, OneSignalPrefs.PREFS_ONESIGNAL_USER_PROVIDED_CONSENT, false);
2218+
return OneSignalPrefs.getBool(
2219+
OneSignalPrefs.PREFS_ONESIGNAL,
2220+
OneSignalPrefs.PREFS_ONESIGNAL_USER_PROVIDED_CONSENT,
2221+
false);
22162222
}
22172223

22182224
static void saveUserConsentStatus(boolean consent) {
2219-
OneSignalPrefs.saveBool(OneSignalPrefs.PREFS_ONESIGNAL, OneSignalPrefs.PREFS_ONESIGNAL_USER_PROVIDED_CONSENT, consent);
2225+
OneSignalPrefs.saveBool(
2226+
OneSignalPrefs.PREFS_ONESIGNAL,
2227+
OneSignalPrefs.PREFS_ONESIGNAL_USER_PROVIDED_CONSENT,
2228+
consent);
22202229
}
22212230

22222231
private static String getSavedUserId(Context inContext) {
22232232
if (inContext == null)
2224-
return "";
2233+
return null;
22252234

2226-
return OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
2227-
OneSignalPrefs.PREFS_GT_PLAYER_ID,null);
2235+
return OneSignalPrefs.getString(
2236+
OneSignalPrefs.PREFS_ONESIGNAL,
2237+
OneSignalPrefs.PREFS_GT_PLAYER_ID,
2238+
null);
22282239
}
22292240

22302241
static boolean hasUserId() {
22312242
return getUserId() != null;
22322243
}
22332244

22342245
static String getUserId() {
2235-
if (userId == null && appContext != null) {
2236-
userId = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
2237-
OneSignalPrefs.PREFS_GT_PLAYER_ID,null);
2238-
}
2246+
if (userId == null && appContext != null)
2247+
userId = getSavedUserId(appContext);
2248+
22392249
return userId;
22402250
}
22412251

@@ -2244,8 +2254,10 @@ static void saveUserId(String id) {
22442254
if (appContext == null)
22452255
return;
22462256

2247-
OneSignalPrefs.saveString(OneSignalPrefs.PREFS_ONESIGNAL,
2248-
OneSignalPrefs.PREFS_GT_PLAYER_ID, userId);
2257+
OneSignalPrefs.saveString(
2258+
OneSignalPrefs.PREFS_ONESIGNAL,
2259+
OneSignalPrefs.PREFS_GT_PLAYER_ID,
2260+
userId);
22492261
}
22502262

22512263
static boolean hasEmailId() {
@@ -2254,8 +2266,10 @@ static boolean hasEmailId() {
22542266

22552267
static String getEmailId() {
22562268
if (TextUtils.isEmpty(emailId) && appContext != null) {
2257-
emailId = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
2258-
OneSignalPrefs.PREFS_OS_EMAIL_ID,null);
2269+
emailId = OneSignalPrefs.getString(
2270+
OneSignalPrefs.PREFS_ONESIGNAL,
2271+
OneSignalPrefs.PREFS_OS_EMAIL_ID,
2272+
null);
22592273
}
22602274
return emailId;
22612275
}
@@ -2265,20 +2279,27 @@ static void saveEmailId(String id) {
22652279
if (appContext == null)
22662280
return;
22672281

2268-
OneSignalPrefs.saveString(OneSignalPrefs.PREFS_ONESIGNAL,
2269-
OneSignalPrefs.PREFS_OS_EMAIL_ID, "".equals(emailId) ? null : emailId);
2282+
OneSignalPrefs.saveString(
2283+
OneSignalPrefs.PREFS_ONESIGNAL,
2284+
OneSignalPrefs.PREFS_OS_EMAIL_ID,
2285+
"".equals(emailId) ? null : emailId);
22702286
}
22712287

22722288
static boolean getFilterOtherGCMReceivers(Context context) {
2273-
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL,
2274-
OneSignalPrefs.PREFS_OS_FILTER_OTHER_GCM_RECEIVERS,false);
2289+
return OneSignalPrefs.getBool(
2290+
OneSignalPrefs.PREFS_ONESIGNAL,
2291+
OneSignalPrefs.PREFS_OS_FILTER_OTHER_GCM_RECEIVERS,
2292+
false);
22752293
}
22762294

22772295
static void saveFilterOtherGCMReceivers(boolean set) {
22782296
if (appContext == null)
22792297
return;
22802298

2281-
OneSignalPrefs.saveBool(OneSignalPrefs.PREFS_ONESIGNAL,"OS_FILTER_OTHER_GCM_RECEIVERS",set);
2299+
OneSignalPrefs.saveBool(
2300+
OneSignalPrefs.PREFS_ONESIGNAL,
2301+
"OS_FILTER_OTHER_GCM_RECEIVERS",
2302+
set);
22822303
}
22832304

22842305
// Called when a player id is returned from OneSignal
@@ -2312,13 +2333,17 @@ static void updateEmailIdDependents(String emailId) {
23122333
}
23132334

23142335
static boolean getFirebaseAnalyticsEnabled() {
2315-
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL,
2316-
OneSignalPrefs.PREFS_GT_FIREBASE_TRACKING_ENABLED,false);
2336+
return OneSignalPrefs.getBool(
2337+
OneSignalPrefs.PREFS_ONESIGNAL,
2338+
OneSignalPrefs.PREFS_GT_FIREBASE_TRACKING_ENABLED,
2339+
false);
23172340
}
23182341

23192342
static boolean getClearGroupSummaryClick() {
2320-
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL,
2321-
OneSignalPrefs.PREFS_OS_CLEAR_GROUP_SUMMARY_CLICK,true);
2343+
return OneSignalPrefs.getBool(
2344+
OneSignalPrefs.PREFS_ONESIGNAL,
2345+
OneSignalPrefs.PREFS_OS_CLEAR_GROUP_SUMMARY_CLICK,
2346+
true);
23222347
}
23232348

23242349

@@ -2335,13 +2360,17 @@ public static void enableVibrate(boolean enable) {
23352360
if (appContext == null)
23362361
return;
23372362

2338-
OneSignalPrefs.saveBool(OneSignalPrefs.PREFS_ONESIGNAL,
2339-
OneSignalPrefs.PREFS_GT_VIBRATE_ENABLED,enable);
2363+
OneSignalPrefs.saveBool(
2364+
OneSignalPrefs.PREFS_ONESIGNAL,
2365+
OneSignalPrefs.PREFS_GT_VIBRATE_ENABLED,
2366+
enable);
23402367
}
23412368

2342-
static boolean getVibrate(Context context) {
2343-
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL,
2344-
OneSignalPrefs.PREFS_GT_VIBRATE_ENABLED,true);
2369+
static boolean getVibrate() {
2370+
return OneSignalPrefs.getBool(
2371+
OneSignalPrefs.PREFS_ONESIGNAL,
2372+
OneSignalPrefs.PREFS_GT_VIBRATE_ENABLED,
2373+
true);
23452374
}
23462375

23472376
// If true(default) - Sound plays when receiving notification. Vibrates when device is on vibrate only mode.
@@ -2361,19 +2390,25 @@ public static void enableSound(boolean enable) {
23612390
OneSignalPrefs.PREFS_GT_SOUND_ENABLED,enable);
23622391
}
23632392

2364-
static boolean getSoundEnabled(Context context) {
2365-
return OneSignalPrefs.getBool(OneSignalPrefs.PREFS_ONESIGNAL,
2366-
OneSignalPrefs.PREFS_GT_SOUND_ENABLED,true);
2393+
static boolean getSoundEnabled() {
2394+
return OneSignalPrefs.getBool(
2395+
OneSignalPrefs.PREFS_ONESIGNAL,
2396+
OneSignalPrefs.PREFS_GT_SOUND_ENABLED,
2397+
true);
23672398
}
23682399

23692400
static void setLastSessionTime(long time) {
2370-
OneSignalPrefs.saveLong(OneSignalPrefs.PREFS_ONESIGNAL,
2371-
OneSignalPrefs.PREFS_OS_LAST_SESSION_TIME,time);
2401+
OneSignalPrefs.saveLong(
2402+
OneSignalPrefs.PREFS_ONESIGNAL,
2403+
OneSignalPrefs.PREFS_OS_LAST_SESSION_TIME,
2404+
time);
23722405
}
23732406

2374-
private static long getLastSessionTime(Context context) {
2375-
return OneSignalPrefs.getLong(OneSignalPrefs.PREFS_ONESIGNAL,
2376-
OneSignalPrefs.PREFS_OS_LAST_SESSION_TIME,-31*1000);
2407+
private static long getLastSessionTime() {
2408+
return OneSignalPrefs.getLong(
2409+
OneSignalPrefs.PREFS_ONESIGNAL,
2410+
OneSignalPrefs.PREFS_OS_LAST_SESSION_TIME,
2411+
-31 * 1000L);
23772412
}
23782413

23792414
/**
@@ -3090,7 +3125,7 @@ static boolean isAppActive() {
30903125
}
30913126

30923127
private static boolean isPastOnSessionTime() {
3093-
return (System.currentTimeMillis() - getLastSessionTime(appContext)) >= MIN_ON_SESSION_TIME_MILLIS;
3128+
return (System.currentTimeMillis() - getLastSessionTime()) >= MIN_ON_SESSION_TIME_MILLIS;
30943129
}
30953130

30963131
// Extra check to make sure we don't unsubscribe devices that rely on silent background notifications.

OneSignalSDK/unittest/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.onesignal.example">
3+
package="com.onesignal.example">
44

55
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
66
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

0 commit comments

Comments
 (0)