Skip to content

Commit 1ff9bb4

Browse files
authored
Merge pull request #121 from GetStream/PBE-6345-Add-permissions-handling-for-IOS-to-the-demo-project
Pbe 6345 add permissions handling for ios to the demo project
2 parents c972905 + 8c46243 commit 1ff9bb4

File tree

9 files changed

+1331
-368
lines changed

9 files changed

+1331
-368
lines changed

Assets/Samples/Stream Video & Audio Chat SDK/0.7.0/Video & Audio Chat Example Project/MainScene.unity

Lines changed: 521 additions & 105 deletions
Large diffs are not rendered by default.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using UnityEngine;
3+
#if UNITY_ANDROID
4+
using UnityEngine.Android;
5+
#endif
6+
#if UNITY_IOS
7+
using System.Collections;
8+
#endif
9+
10+
namespace StreamVideo.ExampleProject.UI
11+
{
12+
public class PermissionsManager
13+
{
14+
public enum PermissionType
15+
{
16+
Camera,
17+
Microphone
18+
}
19+
20+
public PermissionsManager(MonoBehaviour coroutineRunner)
21+
{
22+
_coroutineRunner = coroutineRunner;
23+
}
24+
25+
public bool HasPermission(PermissionType permissionType)
26+
{
27+
#if UNITY_STANDALONE
28+
return true;
29+
#elif UNITY_ANDROID
30+
var androidPermission = PermissionTypeToAndroidPermission(permissionType);
31+
return Permission.HasUserAuthorizedPermission(androidPermission);
32+
#elif UNITY_IOS
33+
var iosPermission = PermissionTypeToIOSPermission(permissionType);
34+
return Application.HasUserAuthorization(iosPermission);
35+
#else
36+
Debug.LogWarning($"Handling permissions not implemented for platform: {Application.platform}. Requested {permissionType}. Assuming permission is granted.");
37+
return true;
38+
#endif
39+
}
40+
41+
42+
public void RequestPermission(PermissionType permissionType, Action onGranted = null, Action onDenied = null)
43+
{
44+
#if UNITY_ANDROID
45+
RequestAndroidPermission(permissionType, onGranted, onDenied);
46+
#elif UNITY_IOS
47+
_coroutineRunner.StartCoroutine(RequestIOSPermissionCoroutine(permissionType, onGranted, onDenied));
48+
#else
49+
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
50+
#endif
51+
}
52+
53+
private readonly MonoBehaviour _coroutineRunner;
54+
55+
#if UNITY_ANDROID
56+
private void RequestAndroidPermission(PermissionType permissionType, Action onGranted = null,
57+
Action onDenied = null)
58+
{
59+
var androidPermission = PermissionTypeToAndroidPermission(permissionType);
60+
var callbacks = new PermissionCallbacks();
61+
Permission.RequestUserPermission(androidPermission, callbacks);
62+
63+
callbacks.PermissionGranted += permissionName =>
64+
{
65+
if (androidPermission == permissionName)
66+
{
67+
onGranted?.Invoke();
68+
}
69+
};
70+
callbacks.PermissionDenied += permissionName =>
71+
{
72+
if (androidPermission == permissionName)
73+
{
74+
onDenied?.Invoke();
75+
}
76+
};
77+
callbacks.PermissionDeniedAndDontAskAgain += permissionName =>
78+
{
79+
if (androidPermission == permissionName)
80+
{
81+
onDenied?.Invoke();
82+
}
83+
};
84+
}
85+
86+
private string PermissionTypeToAndroidPermission(PermissionType type)
87+
{
88+
switch (type)
89+
{
90+
case PermissionType.Camera: return Permission.Camera;
91+
case PermissionType.Microphone: return Permission.Microphone;
92+
default:
93+
throw new ArgumentOutOfRangeException(nameof(type), type, null);
94+
}
95+
}
96+
#endif
97+
98+
#if UNITY_IOS
99+
private IEnumerator RequestIOSPermissionCoroutine(PermissionType permissionType, Action onGranted = null,
100+
Action onDenied = null)
101+
{
102+
var iosPermission = PermissionTypeToIOSPermission(permissionType);
103+
yield return Application.RequestUserAuthorization(iosPermission);
104+
105+
if (Application.HasUserAuthorization(iosPermission))
106+
{
107+
onGranted?.Invoke();
108+
}
109+
else
110+
{
111+
onDenied?.Invoke();
112+
}
113+
}
114+
115+
UserAuthorization PermissionTypeToIOSPermission(PermissionType type)
116+
{
117+
switch (type)
118+
{
119+
case PermissionType.Camera: return UserAuthorization.WebCam;
120+
case PermissionType.Microphone: return UserAuthorization.Microphone;
121+
default:
122+
throw new ArgumentOutOfRangeException(nameof(type), type, null);
123+
}
124+
}
125+
#endif
126+
}
127+
}

Assets/Samples/Stream Video & Audio Chat SDK/0.7.0/Video & Audio Chat Example Project/Scripts/UI/PermissionsManager.cs.meta

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

Assets/Samples/Stream Video & Audio Chat SDK/0.7.0/Video & Audio Chat Example Project/Scripts/UI/UIManager.cs

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
using StreamVideo.Core.StatefulModels;
77
using StreamVideo.Libs.Utils;
88
using UnityEngine;
9-
#if UNITY_ANDROID
10-
using UnityEngine.Android;
11-
#endif
129

1310
namespace StreamVideo.ExampleProject.UI
1411
{
@@ -19,76 +16,10 @@ public class UIManager : MonoBehaviour
1916
public VideoResolution SenderVideoResolution => new VideoResolution(_senderVideoWidth, _senderVideoHeight);
2017
public int SenderVideoFps => _senderVideoFps;
2118

22-
public void RequestCameraPermissions(Action onGranted = null, Action onDenied = null)
23-
{
24-
#if UNITY_ANDROID
25-
var callbacks = new PermissionCallbacks();
26-
Permission.RequestUserPermission(Permission.Camera, callbacks);
27-
28-
callbacks.PermissionGranted += _ => { onGranted?.Invoke(); };
29-
callbacks.PermissionDenied += permissionName =>
30-
{
31-
onDenied?.Invoke();
32-
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
33-
};
34-
callbacks.PermissionDeniedAndDontAskAgain += (permissionName) =>
35-
{
36-
onDenied?.Invoke();
37-
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
38-
};
39-
#elif UNITY_IOS
40-
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
41-
#endif
42-
}
43-
44-
public bool HasUserAuthorizedCameraPermission()
45-
{
46-
#if UNITY_STANDALONE
47-
return true; //StreamTodo: check if this is true for all platforms
48-
#elif UNITY_ANDROID
49-
return Permission.HasUserAuthorizedPermission(Permission.Camera);
50-
#else
51-
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
52-
#endif
53-
}
54-
55-
public void RequestMicrophonePermissions(Action onGranted = null, Action onDenied = null)
56-
{
57-
#if UNITY_ANDROID
58-
var callbacks = new PermissionCallbacks();
59-
60-
callbacks.PermissionGranted += _ => { onGranted?.Invoke(); };
61-
callbacks.PermissionDenied += permissionName =>
62-
{
63-
onDenied?.Invoke();
64-
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
65-
};
66-
callbacks.PermissionDeniedAndDontAskAgain += (permissionName) =>
67-
{
68-
onDenied?.Invoke();
69-
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
70-
};
71-
72-
Permission.RequestUserPermission(Permission.Microphone, callbacks);
73-
74-
#else
75-
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
76-
#endif
77-
}
78-
79-
public bool HasUserAuthorizedMicrophonePermission()
80-
{
81-
#if UNITY_STANDALONE
82-
return true; //StreamTodo: check if this is true for all platforms
83-
#elif UNITY_ANDROID
84-
return Permission.HasUserAuthorizedPermission(Permission.Microphone);
85-
#else
86-
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
87-
#endif
88-
}
89-
9019
protected void Awake()
9120
{
21+
_permissionsManager = new PermissionsManager(this);
22+
9223
_videoManager.Init();
9324

9425
_videoManager.CallStarted += OnCallStarted;
@@ -100,9 +31,10 @@ protected void Awake()
10031
_portraitModeUIScreensSet.Init(_videoManager, uiManager: this);
10132
_landscapeModeUIScreensSet.Init(_videoManager, uiManager: this);
10233

103-
if (!HasUserAuthorizedCameraPermission())
34+
if (!_permissionsManager.HasPermission(PermissionsManager.PermissionType.Camera))
10435
{
105-
RequestCameraPermissions(onGranted: () => { SelectFirstWorkingCameraOrDefaultAsync().LogIfFailed(); },
36+
_permissionsManager.RequestPermission(PermissionsManager.PermissionType.Camera,
37+
onGranted: () => { SelectFirstWorkingCameraOrDefaultAsync().LogIfFailed(); },
10638
onDenied: ()
10739
=> Debug.LogError("Camera permission was not granted. Video capturing will not work."));
10840
}
@@ -111,9 +43,10 @@ protected void Awake()
11143
SelectFirstWorkingCameraOrDefaultAsync().LogIfFailed();
11244
}
11345

114-
if (!HasUserAuthorizedMicrophonePermission())
46+
if (!_permissionsManager.HasPermission(PermissionsManager.PermissionType.Microphone))
11547
{
116-
RequestMicrophonePermissions(onGranted: SelectFirstMicrophone,
48+
_permissionsManager.RequestPermission(PermissionsManager.PermissionType.Microphone,
49+
onGranted: SelectFirstMicrophone,
11750
onDenied: ()
11851
=> Debug.LogError("Microphone permission was not granted. Audio capturing will not work."));
11952
}
@@ -151,13 +84,15 @@ protected void OnDestroy()
15184

15285
[SerializeField]
15386
private UIScreensSet _landscapeModeUIScreensSet;
154-
87+
15588
[SerializeField]
15689
private UIScreensSet _portraitModeUIScreensSet;
157-
90+
15891
[SerializeField]
15992
private bool _forceTestPortraitMode;
16093

94+
private PermissionsManager _permissionsManager;
95+
16196
private void OnCallStarted(IStreamCall call) => ShowCallScreen(call);
16297

16398
private void OnCallEnded() => ShowMainScreen();
@@ -242,10 +177,10 @@ private void SelectFirstMicrophone()
242177
private UIScreensSet GetCurrentScreenSet()
243178
{
244179
var isPortraitMode = IsPotraitMode();
245-
180+
246181
_portraitModeUIScreensSet.gameObject.SetActive(isPortraitMode);
247182
_landscapeModeUIScreensSet.gameObject.SetActive(!isPortraitMode);
248-
183+
249184
return isPortraitMode ? _portraitModeUIScreensSet : _landscapeModeUIScreensSet;
250185
}
251186

Packages/StreamVideo/Runtime/Core/DeviceManagers/DeviceManagerBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public Task<bool> TestDeviceAsync(TDeviceInfo device, float timeout = 1f)
7979
return OnTestDeviceAsync(device, (int)(timeout * 1000));
8080
}
8181

82+
// StreamTODO: add filter option. E.g. so we can easily consider only front cameras on ios/android
8283
public async Task<TDeviceInfo?> TryFindFirstWorkingDeviceAsync(float testTimeoutPerDevice = 1f)
8384
{
8485
foreach (var device in EnumerateDevices())

0 commit comments

Comments
 (0)