-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathOneSignalAndroid.cs
More file actions
210 lines (179 loc) · 7.09 KB
/
Copy pathOneSignalAndroid.cs
File metadata and controls
210 lines (179 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Modified MIT License
*
* Copyright 2023 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System.Collections.Generic;
using System.Linq;
using OneSignalSDK.Android.Debug;
using OneSignalSDK.Android.InAppMessages;
using OneSignalSDK.Android.LiveActivities;
using OneSignalSDK.Android.Location;
using OneSignalSDK.Android.Notifications;
using OneSignalSDK.Android.Session;
using OneSignalSDK.Android.User;
using OneSignalSDK.Debug;
using OneSignalSDK.Debug.Utilities;
using OneSignalSDK.InAppMessages;
using OneSignalSDK.LiveActivities;
using OneSignalSDK.Location;
using OneSignalSDK.Notifications;
using OneSignalSDK.Session;
using OneSignalSDK.User;
using UnityEngine;
namespace OneSignalSDK.Android
{
public sealed partial class OneSignalAndroid : OneSignalPlatform
{
private const string SDKPackage = "com.onesignal";
private const string SDKClassName = "OneSignal";
private const string QualifiedSDKClass = SDKPackage + "." + SDKClassName;
private readonly AndroidJavaClass _sdkClass = new AndroidJavaClass(QualifiedSDKClass);
private readonly AndroidJavaClass _sdkWrapperClass = new AndroidJavaClass(
SDKPackage + ".common.OneSignalWrapper"
);
private static OneSignalAndroid _instance;
private AndroidUserManager _user;
private AndroidSessionManager _session;
private AndroidNotificationsManager _notifications;
private AndroidLocationManager _location;
private AndroidInAppMessagesManager _inAppMessages;
private AndroidDebugManager _debug;
private AndroidLiveActivitiesManager _liveActivities;
/// <summary>
/// Used to provide a reference for the global callbacks
/// </summary>
public OneSignalAndroid()
{
if (_instance != null)
{
SDKDebug.Error("Additional instance of OneSignalAndroid created.");
return;
}
_instance = this;
_debug = new AndroidDebugManager(_sdkClass);
}
public override IUserManager User
{
get => _user;
}
public override ISessionManager Session
{
get => _session;
}
public override INotificationsManager Notifications
{
get => _notifications;
}
public override ILocationManager Location
{
get => _location;
}
public override IInAppMessagesManager InAppMessages
{
get => _inAppMessages;
}
public override IDebugManager Debug
{
get => _debug;
}
public override ILiveActivitiesManager LiveActivities
{
get => _liveActivities;
}
public override bool ConsentGiven
{
set => _sdkClass.CallStatic("setConsentGiven", value);
}
public override bool ConsentRequired
{
set => _sdkClass.CallStatic("setConsentRequired", value);
}
public override void Initialize(string appId)
{
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
_enableHardwareAcceleration(activity);
_sdkWrapperClass.CallStatic("setSdkType", "unity");
_sdkWrapperClass.CallStatic("setSdkVersion", VersionHeader);
_sdkClass.CallStatic("initWithContext", activity, appId);
if (_inAppMessages == null)
{
_inAppMessages = new AndroidInAppMessagesManager(_sdkClass);
_inAppMessages.Initialize();
}
if (_notifications == null)
{
_notifications = new AndroidNotificationsManager(_sdkClass);
_notifications.Initialize();
}
if (_user == null)
{
_user = new AndroidUserManager(_sdkClass);
_user.Initialize();
}
if (_location == null)
{
_location = new AndroidLocationManager(_sdkClass);
}
if (_session == null)
{
_session = new AndroidSessionManager(_sdkClass);
}
if (_liveActivities == null)
{
_liveActivities = new AndroidLiveActivitiesManager();
}
_completedInit(appId);
}
/// <summary>
/// Unity sets android:hardwareAccelerated="false" on its Activity which
/// prevents WebView transparent backgrounds from rendering. The native SDK
/// displays in-app messages via a PopupWindow whose window inherits this
/// setting, causing the IAM to render with an opaque white background.
/// Enabling the flag at the window level restores transparency without
/// affecting Unity's own GL/Vulkan rendering surface.
/// </summary>
private static void _enableHardwareAcceleration(AndroidJavaObject activity)
{
activity.Call(
"runOnUiThread",
new AndroidJavaRunnable(() =>
{
const int FLAG_HARDWARE_ACCELERATED = 0x01000000;
using var window = activity.Call<AndroidJavaObject>("getWindow");
window.Call("setFlags", FLAG_HARDWARE_ACCELERATED, FLAG_HARDWARE_ACCELERATED);
})
);
}
public override void Login(string externalId, string jwtBearerToken = null)
{
_sdkClass.CallStatic("login", externalId, jwtBearerToken);
}
public override void Logout()
{
_sdkClass.CallStatic("logout");
}
}
}