You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExternalAPI.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright @ 2017-present 8x8, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ExternalAPI.h"
  17. #import "JitsiMeetView+Private.h"
  18. // Events
  19. static NSString * const hangUpAction = @"org.jitsi.meet.HANG_UP";
  20. static NSString * const setAudioMutedAction = @"org.jitsi.meet.SET_AUDIO_MUTED";
  21. static NSString * const sendEndpointTextMessageAction = @"org.jitsi.meet.SEND_ENDPOINT_TEXT_MESSAGE";
  22. static NSString * const toggleScreenShareAction = @"org.jitsi.meet.TOGGLE_SCREEN_SHARE";
  23. @implementation ExternalAPI
  24. RCT_EXPORT_MODULE();
  25. - (NSDictionary *)constantsToExport {
  26. return @{
  27. @"HANG_UP": hangUpAction,
  28. @"SET_AUDIO_MUTED" : setAudioMutedAction,
  29. @"SEND_ENDPOINT_TEXT_MESSAGE": sendEndpointTextMessageAction,
  30. @"TOGGLE_SCREEN_SHARE": toggleScreenShareAction
  31. };
  32. };
  33. /**
  34. * Make sure all methods in this module are invoked on the main/UI thread.
  35. */
  36. - (dispatch_queue_t)methodQueue {
  37. return dispatch_get_main_queue();
  38. }
  39. + (BOOL)requiresMainQueueSetup {
  40. return NO;
  41. }
  42. - (NSArray<NSString *> *)supportedEvents {
  43. return @[ hangUpAction, setAudioMutedAction, sendEndpointTextMessageAction, toggleScreenShareAction ];
  44. }
  45. /**
  46. * Dispatches an event that occurred on JavaScript to the view's delegate.
  47. *
  48. * @param name The name of the event.
  49. * @param data The details/specifics of the event to send determined
  50. * by/associated with the specified `name`.
  51. * @param scope
  52. */
  53. RCT_EXPORT_METHOD(sendEvent:(NSString *)name
  54. data:(NSDictionary *)data
  55. scope:(NSString *)scope) {
  56. // The JavaScript App needs to provide uniquely identifying information to
  57. // the native ExternalAPI module so that the latter may match the former
  58. // to the native JitsiMeetView which hosts it.
  59. JitsiMeetView *view = [JitsiMeetView viewForExternalAPIScope:scope];
  60. if (!view) {
  61. return;
  62. }
  63. id delegate = view.delegate;
  64. if (!delegate) {
  65. return;
  66. }
  67. SEL sel = NSSelectorFromString([self methodNameFromEventName:name]);
  68. if (sel && [delegate respondsToSelector:sel]) {
  69. [delegate performSelector:sel withObject:data];
  70. }
  71. }
  72. /**
  73. * Converts a specific event name i.e. redux action type description to a
  74. * method name.
  75. *
  76. * @param eventName The event name to convert to a method name.
  77. * @return A method name constructed from the specified `eventName`.
  78. */
  79. - (NSString *)methodNameFromEventName:(NSString *)eventName {
  80. NSMutableString *methodName
  81. = [NSMutableString stringWithCapacity:eventName.length];
  82. for (NSString *c in [eventName componentsSeparatedByString:@"_"]) {
  83. if (c.length) {
  84. [methodName appendString:
  85. methodName.length ? c.capitalizedString : c.lowercaseString];
  86. }
  87. }
  88. [methodName appendString:@":"];
  89. return methodName;
  90. }
  91. - (void)sendHangUp {
  92. [self sendEventWithName:hangUpAction body:nil];
  93. }
  94. - (void)sendSetAudioMuted:(BOOL)muted {
  95. NSDictionary *data = @{ @"muted": [NSNumber numberWithBool:muted]};
  96. [self sendEventWithName:setAudioMutedAction body:data];
  97. }
  98. - (void)sendEndpointTextMessage:(NSString*)to :(NSString*)message {
  99. NSDictionary *data = @{
  100. @"to": to,
  101. @"message": message
  102. };
  103. [self sendEventWithName:sendEndpointTextMessageAction body:data];
  104. }
  105. - (void)toggleScreenShare {
  106. [self sendEventWithName:toggleScreenShareAction body:nil];
  107. }
  108. @end