Skip to content

Commit

Permalink
fix android permission not working
Browse files Browse the repository at this point in the history
  • Loading branch information
VNAPNIC committed Mar 16, 2020
1 parent 32aa8c8 commit 12c433f
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.5

* fix Android permission not working

## 0.1.4

* fix userName vs Password is null object reference
Expand Down
2 changes: 0 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.namit.flutter_wowza">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<uses-permission android:name="android.permission.INTERNET" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ import io.flutter.plugin.platform.PlatformView
class FlutterWOWZCameraView internal
constructor(private val context: Context?, private val registrar: PluginRegistry.Registrar,
private val methodChannel: MethodChannel, id: Int?, params: Map<String, Any>?) :
PlatformView, MethodChannel.MethodCallHandler, WOWZBroadcastStatusCallback, WOWZStatusCallback, PluginRegistry.RequestPermissionsResultListener {

//define callback interface
interface PermissionCallbackInterface {
fun onPermissionResult(result: Boolean)
}
PlatformView, MethodChannel.MethodCallHandler,
WOWZBroadcastStatusCallback, WOWZStatusCallback,
PluginRegistry.RequestPermissionsResultListener {

private var mPermissionsGranted = false
private var hasRequestedPermissions = false
var videoIsInitialized = false
var audioIsInitialized = false

private val PERMISSIONS_REQUEST_CODE = 0x1

Expand All @@ -49,8 +48,6 @@ constructor(private val context: Context?, private val registrar: PluginRegistry
// Manifest.permission.READ_PHONE_STATE
)

private var callbackFunction: PermissionCallbackInterface? = null

private val goCoderCameraView: WOWZCameraView = WOWZCameraView(context)
// The top-level GoCoder API interface
private var goCoder: WowzaGoCoder? = null
Expand All @@ -62,6 +59,8 @@ constructor(private val context: Context?, private val registrar: PluginRegistry
private var goCoderBroadcastConfig: WOWZBroadcastConfig? = null

init {
registrar.addRequestPermissionsResultListener(this)

methodChannel.setMethodCallHandler(this)
// Create a broadcaster instance
goCoderBroadcaster = WOWZBroadcast()
Expand Down Expand Up @@ -129,51 +128,18 @@ constructor(private val context: Context?, private val registrar: PluginRegistry
goCoderCameraView.scaleMode = scale
}

"init_go_coder" -> {
if (requestPermissionToAccess())
onPermissionResult(true)
}
"start_preview" -> {
if (goCoder != null) {
var videoIsInitialized = false
var audioIsInitialized = false
this.hasDevicePermissionToAccess(object : PermissionCallbackInterface {
override fun onPermissionResult(result: Boolean) {

Log.i("FlutterWOWZCameraView", "onPermissionResult $result")

if (result) {
// Initialize the camera preview
if (hasDevicePermissionToAccess(Manifest.permission.CAMERA)) {
val availableCameras = goCoderCameraView.cameras
// Ensure we can access to at least one camera
if (availableCameras.isNotEmpty()) {
// Set the video broadcaster in the broadcast config
goCoderBroadcastConfig?.videoBroadcaster = goCoderCameraView
videoIsInitialized = true
Log.i("FlutterWOWZCameraView", "*** getOriginalFrameSizes - Get original frame size : ")
} else {
Log.i("FlutterWOWZCameraView", "Could not detect or gain access to any cameras on this device")
goCoderBroadcastConfig?.isVideoEnabled = false
}
}

if (hasDevicePermissionToAccess(Manifest.permission.RECORD_AUDIO)) {
// Create an audio device instance for capturing and broadcasting audio
goCoderAudioDevice = WOWZAudioDevice()
// Set the audio broadcaster in the broadcast config
goCoderBroadcastConfig?.audioBroadcaster = goCoderAudioDevice
audioIsInitialized = true
}

if (videoIsInitialized && audioIsInitialized) {
Log.i("FlutterWOWZCameraView", "startPreview")
if (!goCoderCameraView.isPreviewing)
goCoderCameraView.startPreview()
}
}
}
})

if (requestPermissionToAccess()) {
if (videoIsInitialized && audioIsInitialized && !goCoderCameraView.isPreviewing )
goCoderCameraView.startPreview()
else
onPermissionResult(true)
}
}

"pause_preview" -> activeCamera?.pausePreview()

"continue_preview" -> activeCamera.continuePreview()
Expand Down Expand Up @@ -254,29 +220,37 @@ constructor(private val context: Context?, private val registrar: PluginRegistry
}
}

private fun hasDevicePermissionToAccess(callback: PermissionCallbackInterface) {
this.callbackFunction = callback
var result = false

if (goCoderBroadcaster != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
result = if (mRequiredPermissions.isNotEmpty()) hasDevicePermissionToAccess(mRequiredPermissions) else true

if (!result && !hasRequestedPermissions) {
ActivityCompat.requestPermissions(registrar.activity(),
mRequiredPermissions,
PERMISSIONS_REQUEST_CODE)
hasRequestedPermissions = true
}
private fun requestPermissionToAccess(): Boolean {
var result = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
result = if (mRequiredPermissions.isNotEmpty()) checkPermissionToAccess(mRequiredPermissions) else true

if (!result && !hasRequestedPermissions) {
ActivityCompat.requestPermissions(registrar.activity(),
mRequiredPermissions,
PERMISSIONS_REQUEST_CODE)
hasRequestedPermissions = true
}
}

this.callbackFunction?.onPermissionResult(result)
return result
}

private fun hasDevicePermissionToAccess(permissions: Array<String>): Boolean {
private fun requestPermissionToAccess(source: String): Boolean {
var result = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
result = if (mRequiredPermissions.isNotEmpty()) checkPermissionToAccess(source) else true
if (!result && !hasRequestedPermissions) {
ActivityCompat.requestPermissions(registrar.activity(),
mRequiredPermissions,
PERMISSIONS_REQUEST_CODE)
hasRequestedPermissions = true
}
}
return result
}

private fun checkPermissionToAccess(permissions: Array<String>): Boolean {
var result = true
if (goCoderBroadcaster != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (permission in permissions) {
Expand All @@ -285,19 +259,23 @@ constructor(private val context: Context?, private val registrar: PluginRegistry
}
}
}
} else {
Log.e("FlutterWOWZCameraView", "goCoderBroadcaster is null!")
result = false
}

Log.i("FlutterWOWZCameraView", "hasDevicePermissionToAccess $result")
return result
}

private fun hasDevicePermissionToAccess(source: String): Boolean {
private fun checkPermissionToAccess(source: String): Boolean {
if (goCoderBroadcaster != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(registrar.activity(), source) == PackageManager.PERMISSION_DENIED) {
return false
}
}
} else {
Log.e("FlutterWOWZCameraView", "goCoderBroadcaster is null!")
return false
}
return true
}
Expand All @@ -312,9 +290,56 @@ constructor(private val context: Context?, private val registrar: PluginRegistry
}
}
hasRequestedPermissions = false
onPermissionResult(mPermissionsGranted)
}
}
this.callbackFunction?.onPermissionResult(mPermissionsGranted)

Log.i("FlutterWOWZCameraView", "onRequestPermissionsResult has requested: $hasRequestedPermissions")
return true
}

private fun onPermissionResult(mPermissionsGranted: Boolean) {
if (goCoder != null) {
if (mPermissionsGranted) {
// Initialize the camera preview
if (requestPermissionToAccess(Manifest.permission.CAMERA)) {
if(!videoIsInitialized) {
val availableCameras = goCoderCameraView.cameras
// Ensure we can access to at least one camera
if (availableCameras.isNotEmpty()) {
// Set the video broadcaster in the broadcast config
goCoderBroadcastConfig?.videoBroadcaster = goCoderCameraView
videoIsInitialized = true
Log.i("FlutterWOWZCameraView", "*** getOriginalFrameSizes - Get original frame size : ")
} else {
Log.e("FlutterWOWZCameraView", "Could not detect or gain access to any cameras on this device")
goCoderBroadcastConfig?.isVideoEnabled = false
}
}
} else {
Log.e("FlutterWOWZCameraView", "Exception Fail to connect to camera service. I checked camera permission in Settings")
}

if (requestPermissionToAccess(Manifest.permission.RECORD_AUDIO)) {
if(!audioIsInitialized) {
// Create an audio device instance for capturing and broadcasting audio
goCoderAudioDevice = WOWZAudioDevice()
// Set the audio broadcaster in the broadcast config
goCoderBroadcastConfig?.audioBroadcaster = goCoderAudioDevice
audioIsInitialized = true
}
} else {
Log.e("FlutterWOWZCameraView", "Exception Fail to connect to record audio service. I checked camera permission in Settings")
}

if (videoIsInitialized && audioIsInitialized) {
Log.i("FlutterWOWZCameraView", "startPreview")
if (!goCoderCameraView.isPreviewing)
goCoderCameraView.startPreview()
}
}
} else {
Log.e("FlutterWOWZCameraView", "goCoder is null!, Please check the license key GoCoder SDK, maybe your license key GoCoder SDK is wrong!")
}
}
}
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class _MyAppState extends State<MyApp> {
width: 1280,
child: WOWZCameraView(
apiLicenseKey: (defaultTargetPlatform == TargetPlatform.android)
? "GOSK-xxxx-xxxx-xxxx-xxxx-xxxx"
: "GOSK-xxxx-xxxx-xxxx-xxxx-xxxx",
? "GOSK-9C47-010C-2895-D225-9FEF"
: "GOSK-9C47-010C-A9B9-EB78-3FBD",
controller: controller,
hostAddress: "xxx.xxx.xxx.xxx",
portNumber: 1935,
Expand Down
3 changes: 2 additions & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "0.1.4"
image:
dependency: transitive
description:
Expand Down Expand Up @@ -200,3 +200,4 @@ packages:
version: "3.5.0"
sdks:
dart: ">=2.4.0 <3.0.0"
flutter: ">=1.10.0 <2.0.0"
8 changes: 4 additions & 4 deletions ios/Classes/FlutterWOWZCameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ public class FlutterWOWZCameraView : NSObject, FlutterPlatformView,WOWZBroadcast
self.goCoder = goCoder
print("gocoder is init!")
WowzaGoCoder.requestPermission(for: .camera, response: { (permission) in
print("Camera permission is: \(permission == .authorized ? "authorized" : "denied")")
print("Camera permission is: \(permission == .authorized ? "authorized" : "denied")")
})

WowzaGoCoder.requestPermission(for: .microphone, response: { (permission) in
print("Microphone permission is: \(permission == .authorized ? "authorized" : "denied")")
print("Microphone permission is: \(permission == .authorized ? "authorized" : "denied")")
})
self.goCoder?.config = self.goCoderConfig
self.goCoder?.cameraView = self.uiView
Expand All @@ -146,11 +146,11 @@ public class FlutterWOWZCameraView : NSObject, FlutterPlatformView,WOWZBroadcast
break
case "start_preview":
WowzaGoCoder.requestPermission(for: .camera, response: { (permission) in
print("Camera permission is: \(permission == .authorized ? "authorized" : "denied")")
print("Camera permission is: \(permission == .authorized ? "authorized" : "denied")")
})

WowzaGoCoder.requestPermission(for: .microphone, response: { (permission) in
print("Microphone permission is: \(permission == .authorized ? "authorized" : "denied")")
print("Microphone permission is: \(permission == .authorized ? "authorized" : "denied")")
})
self.cameraView?.start()
break;
Expand Down
21 changes: 13 additions & 8 deletions lib/gocoder/src/wowz_camera_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,20 @@ class _WOWZCameraViewState extends State<WOWZCameraView> {
widget.controller?._setChannel(_channel);

// license key gocoder sdk
_channel.invokeMethod(_apiLicenseKey, widget.apiLicenseKey);
_channel.invokeMethod(_apiLicenseKey, widget.apiLicenseKey);
// Set the connection properties for the target Wowza Streaming Engine server or Wowza Streaming Cloud live stream
_channel.invokeMethod(_hostAddress, widget.hostAddress);
_channel.invokeMethod(_portNumber, widget.portNumber);
_channel.invokeMethod(_applicationName, widget.applicationName);
_channel.invokeMethod(_streamName, widget.streamName);
if (widget.hostAddress != null && widget.hostAddress.isNotEmpty) {
_channel.invokeMethod(_hostAddress, widget.hostAddress);
}
if (widget.portNumber != null) {
_channel.invokeMethod(_portNumber, widget.portNumber);
}
if (widget.applicationName != null && widget.applicationName.isNotEmpty) {
_channel.invokeMethod(_applicationName, widget.applicationName);
}
if (widget.streamName != null && widget.streamName.isNotEmpty) {
_channel.invokeMethod(_streamName, widget.streamName);
}
//authentication
if (widget.username != null) {
_channel.invokeMethod(_username, widget.username);
Expand All @@ -219,15 +227,12 @@ class _WOWZCameraViewState extends State<WOWZCameraView> {
if (widget.scaleMode != null) {
_channel.invokeMethod(_scaleMode, widget.scaleMode.toString());
}

if(widget.fps!=null){
_channel.invokeListMethod(_fps,widget.fps);
}

if(widget.bps !=null){
_channel.invokeListMethod(_bps,widget.bps);
}

if(widget.khz !=null){
_channel.invokeListMethod(_bps,widget.bps);
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,4 @@ packages:
version: "3.5.0"
sdks:
dart: ">=2.4.0 <3.0.0"
flutter: ">=1.10.0 <2.0.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_wowza
description: Flutter WOWZA plugin for iOS/Android. The project is based on Wowza GoCoder SDK
homepage: https://github.com/VNAPNIC/flutter-wowza
version: 0.1.4
version: 0.1.5
author: vn.apnic@gmail.comn

environment:
Expand Down

0 comments on commit 12c433f

Please sign in to comment.