Skip to content

Commit 211cf7f

Browse files
committed
Add OSDevice
* Unify all user device data on a same class * Getters for notification permission, user subscription, is user susbscribed, user id, push token, email id, email addess
1 parent 2fb9de7 commit 211cf7f

File tree

4 files changed

+167
-2
lines changed

4 files changed

+167
-2
lines changed

OneSignalSDK/app/src/main/java/com/onesignal/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ protected void onCreate(Bundle savedInstanceState) {
154154

155155
this.addObservers();
156156

157-
OSPermissionSubscriptionState state = OneSignal.getPermissionSubscriptionState();
157+
OSDevice device = OneSignal.getUserDevice();
158158

159-
this.didGetEmailStatus(state.getEmailSubscriptionStatus().getSubscribed());
159+
this.didGetEmailStatus(device.isSubscribed());
160160
}
161161

162162
this.debugTextView = this.findViewById(com.onesignal.example.R.id.debugTextView);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2020 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+
public class OSDevice {
31+
32+
/**
33+
* Get the app's notification permission
34+
* @return false if the user disabled notifications for the app, otherwise true
35+
*/
36+
public boolean isNotificationEnabled() {
37+
return OneSignal.getPermissionSubscriptionState().permissionStatus.getEnabled();
38+
}
39+
40+
/**
41+
* Get whether the user is subscribed to OneSignal notifications or not
42+
* @return false if the user is not subscribed to OneSignal notifications, otherwise true
43+
*/
44+
public boolean isUserSubscribed() {
45+
return OneSignal.getPermissionSubscriptionState().subscriptionStatus.getUserSubscriptionSetting();
46+
}
47+
48+
/**
49+
* Get whether the user is subscribed
50+
* @return true if {@link #isNotificationEnabled}, {@link #isUserSubscribed}, {@link #getUserId} and {@link #getPushToken} are true, otherwise false
51+
*/
52+
public boolean isSubscribed() {
53+
return OneSignal.getPermissionSubscriptionState().subscriptionStatus.getSubscribed();
54+
}
55+
56+
/**
57+
* Get user id from registration
58+
* @return user id if user is registered, otherwise false
59+
*/
60+
public String getUserId() {
61+
return OneSignal.getPermissionSubscriptionState().subscriptionStatus.getUserId();
62+
}
63+
64+
/**
65+
* Get device push token
66+
* @return push token if available, otherwise null
67+
*/
68+
public String getPushToken() {
69+
return OneSignal.getPermissionSubscriptionState().subscriptionStatus.getPushToken();
70+
}
71+
72+
/**
73+
* Get the user email id
74+
* @return email id if user address was registered, otherwise null
75+
*/
76+
public String getEmailUserId() {
77+
return OneSignal.getPermissionSubscriptionState().emailSubscriptionStatus.getEmailUserId();
78+
}
79+
80+
/**
81+
* Get the user email
82+
* @return email address if set, otherwise null
83+
*/
84+
public String getEmailAddress() {
85+
return OneSignal.getPermissionSubscriptionState().emailSubscriptionStatus.getEmailAddress();
86+
}
87+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,13 @@ static OSObservable<OSEmailSubscriptionObserver, OSEmailSubscriptionStateChanges
589589
}
590590
// End EmailSubscriptionState
591591

592+
private static OSDevice userDevice;
593+
public static OSDevice getUserDevice() {
594+
if (userDevice == null)
595+
userDevice = new OSDevice();
596+
597+
return userDevice;
598+
}
592599

593600
private static class IAPUpdateJob {
594601
JSONArray toReport;

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4399,6 +4399,77 @@ public void sendExternalUserId_forPush_afterLoggingOutEmail_withCompletion() thr
43994399
assertEquals(expectedExternalUserIdResponse.toString(), lastExternalUserIdResponse.toString());
44004400
}
44014401

4402+
@Test
4403+
public void testOSDeviceHasEmailAddress() throws Exception {
4404+
String testEmail = "test@onesignal.com";
4405+
4406+
OneSignalInit();
4407+
threadAndTaskWait();
4408+
4409+
assertNull(OneSignal.getUserDevice().getEmailAddress());
4410+
4411+
OneSignal.setEmail(testEmail);
4412+
threadAndTaskWait();
4413+
4414+
assertEquals(testEmail, OneSignal.getUserDevice().getEmailAddress());
4415+
}
4416+
4417+
@Test
4418+
public void testOSDeviceHasEmailId() throws Exception {
4419+
String testEmail = "test@onesignal.com";
4420+
4421+
4422+
OneSignalInit();
4423+
threadAndTaskWait();
4424+
4425+
assertNull(OneSignal.getUserDevice().getEmailUserId());
4426+
4427+
OneSignal.setEmail(testEmail);
4428+
threadAndTaskWait();
4429+
4430+
assertNotNull(OneSignal.getUserDevice().getEmailUserId());
4431+
}
4432+
4433+
@Test
4434+
public void testOSDeviceHasUserId() throws Exception {
4435+
OneSignalInit();
4436+
threadAndTaskWait();
4437+
4438+
assertNotNull(OneSignal.getUserDevice().getUserId());
4439+
}
4440+
4441+
@Test
4442+
public void testOSDeviceHasPushToken() throws Exception {
4443+
OneSignalInit();
4444+
threadAndTaskWait();
4445+
4446+
assertNotNull(OneSignal.getUserDevice().getPushToken());
4447+
}
4448+
4449+
@Test
4450+
public void testOSDeviceNotificationPermission() throws Exception {
4451+
OneSignalInit();
4452+
threadAndTaskWait();
4453+
4454+
assertTrue(OneSignal.getUserDevice().isNotificationEnabled());
4455+
}
4456+
4457+
@Test
4458+
public void testOSDeviceUserSubscriptionSetting() throws Exception {
4459+
OneSignalInit();
4460+
threadAndTaskWait();
4461+
4462+
assertTrue(OneSignal.getUserDevice().isUserSubscribed());
4463+
}
4464+
4465+
@Test
4466+
public void testOSDeviceSubscribed() throws Exception {
4467+
OneSignalInit();
4468+
threadAndTaskWait();
4469+
4470+
assertTrue(OneSignal.getUserDevice().isSubscribed());
4471+
}
4472+
44024473
@Test
44034474
public void testGetTagsQueuesCallbacks() throws Exception {
44044475
final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<>(2);

0 commit comments

Comments
 (0)