Skip to content

Commit 3a46233

Browse files
committed
Update Android to user model interface
1 parent 4204dab commit 3a46233

37 files changed

+1129
-641
lines changed

com.onesignal.unity.android/Editor/OneSignalAndroidDependencies.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<repositories>
44
<repository>https://repo.maven.apache.org/maven2</repository>
55
</repositories>
6-
<androidPackage spec="com.onesignal:OneSignal:[4.8.3]" />
6+
<androidPackage spec="com.onesignal:OneSignal:5.0.0-alpha1" />
77
</androidPackages>
88
</dependencies>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Modified MIT License
3+
*
4+
* Copyright 2022 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+
using UnityEngine;
29+
using OneSignalSDKNew.Debug.Models;
30+
31+
namespace OneSignalSDKNew.Debug {
32+
internal sealed class AndroidDebugManager : IDebugManager {
33+
private readonly AndroidJavaObject _debug;
34+
35+
private LogLevel _logLevel = LogLevel.Warn;
36+
private LogLevel _alertLevel = LogLevel.None;
37+
38+
public AndroidDebugManager(AndroidJavaClass sdkClass) {
39+
_debug = sdkClass.CallStatic<AndroidJavaObject>("getDebug");
40+
}
41+
42+
public LogLevel LogLevel {
43+
get => _logLevel;
44+
set {
45+
_logLevel = value;
46+
var jLogLevel = ToLogLevel(value);
47+
_debug.Call("setLogLevel", jLogLevel);
48+
}
49+
}
50+
51+
public LogLevel AlertLevel {
52+
get => _alertLevel;
53+
set {
54+
_alertLevel = value;
55+
_debug.Call("setAlertLevel", ToLogLevel(value));
56+
}
57+
}
58+
59+
private AndroidJavaObject ToLogLevel(LogLevel value) {
60+
var logLevelClass = new AndroidJavaClass("com.onesignal.debug.LogLevel");
61+
var logLevelValue = logLevelClass.CallStatic<AndroidJavaObject>("valueOf", logLevelClass, value.ToString().ToUpper());
62+
return logLevelValue;
63+
}
64+
65+
}
66+
}

com.onesignal.unity.android/Runtime/OneSignalAndroid.Callbacks.cs.meta renamed to com.onesignal.unity.android/Runtime/AndroidDebugManager.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Modified MIT License
3+
*
4+
* Copyright 2022 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+
29+
using UnityEngine;
30+
using System.Collections.Generic;
31+
using OneSignalSDKNew.InAppMessages.Models;
32+
33+
namespace OneSignalSDKNew.InAppMessages {
34+
internal sealed class AndroidInAppMessagesManager : IInAppMessagesManager {
35+
private readonly AndroidJavaObject _inAppMessages;
36+
37+
private const string SDKPackage = "com.onesignal.inAppMessages";
38+
private const string IAMLifecycleClassName = "UnityIAMLifecycleHandler";
39+
private const string QualifiedIAMLifecycleClass = SDKPackage + "." + IAMLifecycleClassName;
40+
41+
public AndroidInAppMessagesManager(AndroidJavaClass sdkClass) {
42+
_inAppMessages = sdkClass.CallStatic<AndroidJavaObject>("getInAppMessages");
43+
}
44+
public event InAppMessageLifecycleDelegate WillDisplay;
45+
public event InAppMessageLifecycleDelegate DidDisplay;
46+
public event InAppMessageLifecycleDelegate WillDismiss;
47+
public event InAppMessageLifecycleDelegate DidDismiss;
48+
49+
public event InAppMessageClickedDelegate Clicked;
50+
51+
public bool Paused {
52+
get => _inAppMessages.Call<bool>("getPaused");
53+
set => _inAppMessages.Call("setPaused", value);
54+
}
55+
56+
public void AddTrigger(string key, object value)
57+
=> _inAppMessages.Call("addTrigger", key, value);
58+
59+
public void AddTriggers(Dictionary<string, object> triggers)
60+
=> _inAppMessages.Call("addTriggers", triggers.ToMap());
61+
62+
public void RemoveTrigger(string key)
63+
=> _inAppMessages.Call("removeTrigger", key);
64+
65+
public void RemoveTriggers(params string[] keys)
66+
=> _inAppMessages.Call("removeTriggers", keys.ToArrayList());
67+
68+
public void ClearTriggers()
69+
=> _inAppMessages.Call("clearTriggers");
70+
71+
public void Initialize() {
72+
_inAppMessages.Call("setInAppMessageClickHandler", new IInAppMessageClickHandler(this));
73+
74+
var wrapperHandler = new AndroidJavaObject(QualifiedIAMLifecycleClass, new IInAppMessageLifecycleHandler(this)); // com.onesignal.UnityIAMLifecycleHandler
75+
_inAppMessages.Call("setInAppMessageLifecycleHandler", wrapperHandler);
76+
}
77+
78+
private sealed class IInAppMessageLifecycleHandler : OneSignalAndroidJavaProxy {
79+
private AndroidInAppMessagesManager _parent;
80+
81+
public IInAppMessageLifecycleHandler(AndroidInAppMessagesManager inAppMessagesManager) : base("inAppMessages." + IAMLifecycleClassName + "$WrapperLifecycleHandler") {
82+
_parent = inAppMessagesManager;
83+
}
84+
85+
/// <param name="message">IInAppMessage</param>
86+
public void onWillDisplayInAppMessage(AndroidJavaObject message) {
87+
_parent.WillDisplay?.Invoke(message.ToSerializable<InAppMessage>());
88+
}
89+
/// <param name="message">IInAppMessage</param>
90+
public void onDidDisplayInAppMessage(AndroidJavaObject message)
91+
=> _parent.DidDisplay?.Invoke(message.ToSerializable<InAppMessage>());
92+
93+
/// <param name="message">IInAppMessage</param>
94+
public void onWillDismissInAppMessage(AndroidJavaObject message)
95+
=> _parent.WillDismiss?.Invoke(message.ToSerializable<InAppMessage>());
96+
97+
/// <param name="message">IInAppMessage</param>
98+
public void onDidDismissInAppMessage(AndroidJavaObject message)
99+
=> _parent.DidDismiss?.Invoke(message.ToSerializable<InAppMessage>());
100+
}
101+
102+
private sealed class IInAppMessageClickHandler : OneSignalAndroidJavaProxy {
103+
private AndroidInAppMessagesManager _parent;
104+
105+
public IInAppMessageClickHandler(AndroidInAppMessagesManager inAppMessagesManager) : base("inAppMessages.IInAppMessageClickHandler") {
106+
_parent = inAppMessagesManager;
107+
}
108+
109+
/// <param name="result">IInAppMessageClickResult</param>
110+
public void inAppMessageClicked(AndroidJavaObject result) {
111+
_parent.Clicked?.Invoke(result.ToSerializable<InAppMessageClickedResult>());
112+
}
113+
}
114+
}
115+
}

com.onesignal.unity.android/Runtime/AndroidInAppMessagesManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Laters;
2+
using UnityEngine;
3+
using System;
4+
using System.Runtime.CompilerServices;
5+
using System.Threading.Tasks;
6+
7+
namespace OneSignalSDKNew {
8+
public abstract class OneSignalAndroidJavaProxy : AndroidJavaProxy {
9+
private const string SDKPackage = "com.onesignal";
10+
private const string SDKClassName = "OneSignal";
11+
private const string QualifiedSDKClass = SDKPackage + "." + SDKClassName;
12+
13+
protected OneSignalAndroidJavaProxy(string listenerClassName, bool innerClass = false)
14+
: base(innerClass
15+
? QualifiedSDKClass + "$" + listenerClassName // com.onesignal.OneSignal$[name], true
16+
: SDKPackage + "." + listenerClassName // com.onesignal.[name], false
17+
) { }
18+
}
19+
20+
public abstract class OneSignalAwaitableAndroidJavaProxy<TResult> : AwaitableAndroidJavaProxy<TResult> {
21+
private const string SDKPackage = "com.onesignal";
22+
private const string SDKClassName = "OneSignal";
23+
private const string QualifiedSDKClass = SDKPackage + "." + SDKClassName;
24+
25+
protected OneSignalAwaitableAndroidJavaProxy(string listenerClassName, bool innerClass = false)
26+
: base(innerClass
27+
? QualifiedSDKClass + "$" + listenerClassName
28+
: SDKPackage + "." + listenerClassName
29+
) { }
30+
}
31+
32+
public sealed class Continuation {
33+
public AndroidJavaObject Proxy { get; }
34+
private AndroidConsumer _consumer;
35+
36+
public Continuation() {
37+
var continuation = new AndroidJavaClass("com.onesignal.Continue");
38+
_consumer = new AndroidConsumer();
39+
Proxy = continuation.CallStatic<AndroidJavaObject>("with", _consumer);
40+
}
41+
42+
public TaskAwaiter<object> GetAwaiter() => _consumer.GetAwaiter();
43+
}
44+
45+
public sealed class AndroidConsumer : AwaitableAndroidJavaProxy<object> {
46+
public AndroidConsumer() : base("java.util.function.Consumer") { }
47+
48+
public void accept(AndroidJavaObject obj) {
49+
var result = obj.Call<bool>("isSuccess");
50+
51+
if (result) {
52+
_complete(null);
53+
} else {
54+
var throwable = obj.Call<AndroidJavaObject>("getThrowable");
55+
if (throwable != null) {
56+
_fail(throwable.Call<string>("getMessage"));
57+
} else {
58+
_fail("error with async method");
59+
}
60+
}
61+
}
62+
}
63+
64+
public sealed class BoolContinuation {
65+
public AndroidJavaObject Proxy { get; }
66+
private AndroidBoolConsumer _consumer;
67+
68+
public BoolContinuation() {
69+
var continuation = new AndroidJavaClass("com.onesignal.Continue");
70+
_consumer = new AndroidBoolConsumer();
71+
Proxy = continuation.CallStatic<AndroidJavaObject>("with", _consumer);
72+
}
73+
74+
public TaskAwaiter<bool> GetAwaiter() => _consumer.GetAwaiter();
75+
}
76+
77+
public sealed class AndroidBoolConsumer : AwaitableAndroidJavaProxy<bool> {
78+
public AndroidBoolConsumer() : base("java.util.function.Consumer") { }
79+
80+
public void accept(AndroidJavaObject obj) {
81+
var result = obj.Call<bool>("isSuccess");
82+
83+
if (result) {
84+
var data = obj.Call<AndroidJavaObject>("getData");
85+
var value = data.Call<bool>("booleanValue");
86+
_complete(value);
87+
} else {
88+
var throwable = obj.Call<AndroidJavaObject>("getThrowable");
89+
if (throwable != null)
90+
_fail(throwable.Call<string>("getMessage"));
91+
else
92+
_fail("error with async method");
93+
}
94+
}
95+
}
96+
}

com.onesignal.unity.android/Runtime/AndroidJavaProxy.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Modified MIT License
3+
*
4+
* Copyright 2022 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+
using UnityEngine;
29+
using System.Threading.Tasks;
30+
31+
namespace OneSignalSDKNew.Location {
32+
internal sealed class AndroidLocationManager : ILocationManager {
33+
private readonly AndroidJavaObject _location;
34+
35+
public AndroidLocationManager(AndroidJavaClass sdkClass) {
36+
_location = sdkClass.CallStatic<AndroidJavaObject>("getLocation");
37+
}
38+
39+
public bool IsShared {
40+
get => _location.Call<bool>("isShared");
41+
set => _location.Call("setShared", value);
42+
}
43+
44+
public async Task<bool> RequestPermissionAsync(bool fallbackToSettings) {
45+
var continuation = new BoolContinuation();
46+
_location.Call<AndroidJavaObject>("requestPermission", fallbackToSettings, continuation.Proxy);
47+
return await continuation;
48+
}
49+
}
50+
}

com.onesignal.unity.android/Runtime/AndroidLocationManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)