Skip to content

Commit

Permalink
fix review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian-mkd committed Dec 18, 2024
1 parent c36a1f7 commit 5056b05
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 144 deletions.
118 changes: 56 additions & 62 deletions packages/client/src/client-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,14 @@ import {
Sdk,
SdkType,
} from './gen/video/sfu/models/models';
import { SendStatsRequest } from './gen/video/sfu/signal_rpc/signal';
import { isReactNative } from './helpers/platforms';
import { UAParser } from 'ua-parser-js';

type WebRTCInfoType = {
version: string;
};

type DeviceState =
| {
oneofKind: 'android';
android: AndroidState;
}
| {
oneofKind: 'apple';
apple: AppleState;
}
| {
oneofKind: undefined;
};

const version = process.env.PKG_VERSION || '0.0.0';
const [major, minor, patch] = version.split('.');

Expand All @@ -42,7 +30,7 @@ let sdkInfo: Sdk | undefined = {
let osInfo: OS | undefined;
let deviceInfo: Device | undefined;
let webRtcInfo: WebRTCInfoType | undefined;
let deviceState: DeviceState;
let deviceState: SendStatsRequest['deviceState'];

export const setSdkInfo = (info: Sdk) => {
sdkInfo = info;
Expand Down Expand Up @@ -80,69 +68,75 @@ export type LocalClientDetailsType = ClientDetails & {
webRTCInfo?: WebRTCInfoType;
};

const getAndroidThermalState = (state: string) => {
switch (state) {
case 'UNKNOWN':
return AndroidThermalState.UNSPECIFIED;
case 'NONE':
return AndroidThermalState.NONE;
case 'LIGHT':
return AndroidThermalState.LIGHT;
case 'MODERATE':
return AndroidThermalState.MODERATE;
case 'SEVERE':
return AndroidThermalState.SEVERE;
case 'CRITICAL':
return AndroidThermalState.CRITICAL;
case 'EMERGENCY':
return AndroidThermalState.EMERGENCY;
case 'SHUTDOWN':
return AndroidThermalState.SHUTDOWN;
default:
return AndroidThermalState.UNSPECIFIED;
export const setThermalState = (state: string) => {
if (!osInfo) {
deviceState = { oneofKind: undefined };
return;
}

if (osInfo.name === 'android') {
const thermalState =
AndroidThermalState[state as keyof typeof AndroidThermalState] ||
AndroidThermalState.UNSPECIFIED;

deviceState = {
oneofKind: 'android',
android: {
thermalState,
isPowerSaverMode:
deviceState?.oneofKind === 'android' &&
deviceState.android.isPowerSaverMode,
},
};
}
};

const getAppleThermalState = (state: string) => {
switch (state.toString()) {
case '0':
return AppleThermalState.UNSPECIFIED;
case '1':
return AppleThermalState.NOMINAL;
case '2':
return AppleThermalState.FAIR;
case '3':
return AppleThermalState.SERIOUS;
case '4':
return AppleThermalState.CRITICAL;
default:
return AppleThermalState.UNSPECIFIED;
if (osInfo.name === 'iOS') {
const thermalState =
AppleThermalState[state as keyof typeof AppleThermalState] ||
AppleThermalState.UNSPECIFIED;

deviceState = {
oneofKind: 'apple',
apple: {
thermalState,
isLowPowerModeEnabled:
deviceState?.oneofKind === 'apple' &&
deviceState.apple.isLowPowerModeEnabled,
},
};
}
};

export const setDeviceState = (state: {
os: string;
thermal: string;
isLowPowerMode: boolean;
}) => {
if (state.os === 'android') {
export const setPowerState = (powerMode: boolean) => {
if (!osInfo) {
deviceState = { oneofKind: undefined };
return;
}

if (osInfo.name === 'android') {
deviceState = {
oneofKind: 'android',
android: {
thermalState: getAndroidThermalState(state.thermal),
isPowerSaverMode: state.isLowPowerMode,
thermalState:
deviceState?.oneofKind === 'android'
? deviceState.android.thermalState
: AndroidThermalState.UNSPECIFIED,
isPowerSaverMode: powerMode,
},
};
} else if (state.os === 'ios') {
}

if (osInfo.name === 'iOS') {
deviceState = {
oneofKind: 'apple',
apple: {
thermalState: getAppleThermalState(state.thermal),
isLowPowerModeEnabled: state.isLowPowerMode,
thermalState:
deviceState?.oneofKind === 'apple'
? deviceState.apple.thermalState
: AppleThermalState.UNSPECIFIED,
isLowPowerModeEnabled: powerMode,
},
};
} else {
deviceState = { oneofKind: undefined };
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,6 @@ class StreamVideoReactNativeModule(reactContext: ReactApplicationContext) : Reac
}
}

override fun getConstants(): Map<String, Any> {
return mapOf(
"POWER_MODE_EVENT" to "isLowPowerModeEnabled",
"THERMAL_EVENT" to "thermalStateDidChange"
)
}

private fun hasPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && reactApplicationContext.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
val appOps =
Expand Down
105 changes: 30 additions & 75 deletions packages/react-native-sdk/src/providers/StreamCall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import {
useCall,
useCallStateHooks,
} from '@stream-io/video-react-bindings';
import React, {
PropsWithChildren,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import { Call, CallingState, setDeviceState } from '@stream-io/video-client';
import React, { PropsWithChildren, useEffect, useRef } from 'react';
import {
Call,
CallingState,
setThermalState,
setPowerState,
} from '@stream-io/video-client';
import { useIosCallkeepWithCallingStateEffect } from '../hooks/push/useIosCallkeepWithCallingStateEffect';
import {
canAddPushWSSubscriptionsRef,
Expand Down Expand Up @@ -166,73 +165,35 @@ const DeviceStats = () => {
const { useCallCallingState } = useCallStateHooks();
const callingState = useCallCallingState();

const [lowPowerMode, setLowPowerMode] = useState<boolean | null>(null);
const [thermalState, setThermalState] = useState<string | null>(null);

const handleLowPowerMode = useCallback(
(isLowPowerMode: boolean, operatingSystem: string) => {
setLowPowerMode(isLowPowerMode);
setDeviceState({
os: operatingSystem,
thermal: thermalState || 'UNKNOWN',
isLowPowerMode,
});
},
[thermalState]
);

const handleThermalState = useCallback(
(state: string, operatingSystem: string) => {
setThermalState(state);
setDeviceState({
os: operatingSystem,
thermal: state,
isLowPowerMode: lowPowerMode || false,
});
},
[lowPowerMode]
);

useEffect(() => {
let powerModeSubscription: EmitterSubscription;
let thermalStateSubscription: EmitterSubscription;

if (callingState === CallingState.JOINED) {
if (lowPowerMode === null) {
NativeModules?.StreamVideoReactNative.isLowPowerModeEnabled().then(
(isLowPowerMode: boolean) =>
handleLowPowerMode(isLowPowerMode, Platform.OS)
);
}
if (callingState !== CallingState.JOINED) {
return;
}

powerModeSubscription = eventEmitter?.addListener(
'isLowPowerModeEnabled',
(isLowPowerMode: boolean) =>
handleLowPowerMode(isLowPowerMode, Platform.OS)
);

if (thermalState === null) {
NativeModules?.StreamVideoReactNative.currentThermalState().then(
(initialState: string) =>
handleThermalState(initialState, Platform.OS)
);
}
NativeModules?.StreamVideoReactNative.isLowPowerModeEnabled().then(
(initialPowerMode: boolean) => setPowerState(initialPowerMode)
);

thermalStateSubscription = eventEmitter?.addListener(
'thermalStateDidChange',
(status: string) => handleThermalState(status, Platform.OS)
);
powerModeSubscription = eventEmitter?.addListener(
'isLowPowerModeEnabled',
(isLowPowerMode: boolean) => setPowerState(isLowPowerMode)
);

// on android we need to explicitly start and stop the thermal status updates
if (Platform.OS === 'android') {
NativeModules?.StreamVideoReactNative.startThermalStatusUpdates();
}
} else {
eventEmitter?.removeAllListeners('isLowPowerModeEnabled');
eventEmitter?.removeAllListeners('thermalStateDidChange');
if (Platform.OS === 'android') {
NativeModules?.StreamVideoReactNative.stopThermalStatusUpdates();
}
NativeModules?.StreamVideoReactNative.currentThermalState().then(
(initialState: string) => setThermalState(initialState)
);

thermalStateSubscription = eventEmitter?.addListener(
'thermalStateDidChange',
(thermalState: string) => setThermalState(thermalState)
);

// on android we need to explicitly start and stop the thermal status updates
if (Platform.OS === 'android') {
NativeModules?.StreamVideoReactNative.startThermalStatusUpdates();
}

return () => {
Expand All @@ -242,13 +203,7 @@ const DeviceStats = () => {
NativeModules?.StreamVideoReactNative.stopThermalStatusUpdates();
}
};
}, [
thermalState,
callingState,
lowPowerMode,
handleLowPowerMode,
handleThermalState,
]);
}, [callingState]);

return null;
};

0 comments on commit 5056b05

Please sign in to comment.