-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add PowerModeModule and optimized thermal status event reading
- Loading branch information
1 parent
1d474a1
commit b6b7e9b
Showing
9 changed files
with
246 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ct-native/dogfood/android/app/src/main/java/io/getstream/rnvideosample/PowerModeModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.getstream.rnvideosample | ||
|
||
import android.provider.Settings | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReactMethod | ||
|
||
class PowerModeModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { | ||
|
||
override fun getName(): String = "PowerModeModule" | ||
|
||
@ReactMethod | ||
fun isLowPowerModeEnabled(promise: Promise) { | ||
try { | ||
val lowPowerMode = Settings.Global.getInt( | ||
reactContext.contentResolver, | ||
"low_power" | ||
) | ||
promise.resolve(lowPowerMode == 1) | ||
} catch (e: Settings.SettingNotFoundException) { | ||
promise.reject("ERROR", e.message) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...t-native/dogfood/android/app/src/main/java/io/getstream/rnvideosample/PowerModePackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.getstream.rnvideosample | ||
|
||
import com.facebook.react.ReactPackage | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.uimanager.ViewManager | ||
|
||
class PowerModePackage : ReactPackage { | ||
override fun createViewManagers(reactContext: ReactApplicationContext) = emptyList<ViewManager<*, *>>() | ||
|
||
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { | ||
return listOf(PowerModeModule(reactContext)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NativeModules } from 'react-native'; | ||
|
||
const { PowerModeModule } = NativeModules; | ||
|
||
export const checkLowPowerMode = (): Promise<boolean> => { | ||
return PowerModeModule.isLowPowerModeEnabled(); | ||
}; |
42 changes: 28 additions & 14 deletions
42
sample-apps/react-native/dogfood/src/components/ThermalInfo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
sample-apps/react-native/dogfood/src/components/ThermalState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NativeEventEmitter, NativeModules } from 'react-native'; | ||
|
||
export type ThermalState = | ||
| 'unknown' | ||
| 'nominal' | ||
| 'fair' | ||
| 'serious' | ||
| 'critical'; | ||
|
||
const { ThermalStateModule } = NativeModules; | ||
const eventEmitter = new NativeEventEmitter(ThermalStateModule); | ||
|
||
export class ThermalStateManager { | ||
static getCurrentState(): Promise<{ state: ThermalState }> { | ||
return Promise.resolve({ state: 'nominal' }); | ||
} | ||
|
||
static addListener(callback: (response: { state: ThermalState }) => void) { | ||
return eventEmitter.addListener('ThermalStateChange', callback); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
sample-apps/react-native/dogfood/src/components/ThermalStatus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { View, Text, StyleSheet } from 'react-native'; | ||
import { ThermalState, ThermalStateManager } from './ThermalState'; | ||
|
||
export const ThermalStatus = () => { | ||
const [thermalState, setThermalState] = useState<ThermalState>('unknown'); | ||
|
||
useEffect(() => { | ||
// Get initial state | ||
ThermalStateManager.getCurrentState().then((response) => { | ||
setThermalState(response.state); | ||
}); | ||
|
||
// Listen for changes | ||
const subscription = ThermalStateManager.addListener((response) => { | ||
setThermalState(response.state); | ||
}); | ||
|
||
return () => subscription.remove(); | ||
}, []); | ||
|
||
const getStatusColor = (state: ThermalState): string => { | ||
switch (state) { | ||
case 'nominal': | ||
return '#4CAF50'; // Green | ||
case 'fair': | ||
return '#FFC107'; // Yellow | ||
case 'serious': | ||
return '#FF9800'; // Orange | ||
case 'critical': | ||
return '#F44336'; // Red | ||
default: | ||
return '#9E9E9E'; // Grey | ||
} | ||
}; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<View | ||
style={[ | ||
styles.indicator, | ||
{ backgroundColor: getStatusColor(thermalState) }, | ||
]} | ||
/> | ||
<Text style={styles.text}> | ||
Device Temperature: <Text style={styles.state}>{thermalState}</Text> | ||
</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
padding: 10, | ||
}, | ||
indicator: { | ||
width: 12, | ||
height: 12, | ||
borderRadius: 6, | ||
marginRight: 8, | ||
}, | ||
text: { | ||
fontSize: 16, | ||
}, | ||
state: { | ||
textTransform: 'capitalize', | ||
fontWeight: 'bold', | ||
}, | ||
}); |