Navaron Bracke
Committed by GitHub

Merge pull request #880 from navaronbracke/fix_android_permission

fix: Fix android permission bug
@@ -45,7 +45,7 @@ class MobileScannerPermissions { @@ -45,7 +45,7 @@ class MobileScannerPermissions {
45 return if (hasPermission) { 45 return if (hasPermission) {
46 1 46 1
47 } else { 47 } else {
48 - 0 48 + 2
49 } 49 }
50 } 50 }
51 51
@@ -70,6 +70,7 @@ class MobileScannerPermissions { @@ -70,6 +70,7 @@ class MobileScannerPermissions {
70 object: ResultCallback { 70 object: ResultCallback {
71 override fun onResult(errorCode: String?, errorDescription: String?) { 71 override fun onResult(errorCode: String?, errorDescription: String?) {
72 ongoing = false 72 ongoing = false
  73 + listener = null
73 callback.onResult(errorCode, errorDescription) 74 callback.onResult(errorCode, errorDescription)
74 } 75 }
75 } 76 }
@@ -189,8 +189,9 @@ class MobileScannerController { @@ -189,8 +189,9 @@ class MobileScannerController {
189 final MobileScannerState state; 189 final MobileScannerState state;
190 190
191 try { 191 try {
192 - state = MobileScannerState  
193 - .values[await _methodChannel.invokeMethod('state') as int? ?? 0]; 192 + state = MobileScannerState.fromRawValue(
  193 + await _methodChannel.invokeMethod('state') as int? ?? 0,
  194 + );
194 } on PlatformException catch (error) { 195 } on PlatformException catch (error) {
195 isStarting = false; 196 isStarting = false;
196 197
@@ -205,32 +206,33 @@ class MobileScannerController { @@ -205,32 +206,33 @@ class MobileScannerController {
205 } 206 }
206 207
207 switch (state) { 208 switch (state) {
  209 + // Android does not have an undetermined permission state.
  210 + // So if the permission state is denied, just request it now.
208 case MobileScannerState.undetermined: 211 case MobileScannerState.undetermined:
209 - bool result = false;  
210 - 212 + case MobileScannerState.denied:
211 try { 213 try {
212 - result = 214 + final bool granted =
213 await _methodChannel.invokeMethod('request') as bool? ?? false; 215 await _methodChannel.invokeMethod('request') as bool? ?? false;
214 - } catch (error) { 216 +
  217 + if (!granted) {
215 isStarting = false; 218 isStarting = false;
216 throw const MobileScannerException( 219 throw const MobileScannerException(
217 - errorCode: MobileScannerErrorCode.genericError, 220 + errorCode: MobileScannerErrorCode.permissionDenied,
218 ); 221 );
219 } 222 }
220 -  
221 - if (!result) { 223 + } on PlatformException catch (error) {
222 isStarting = false; 224 isStarting = false;
223 - throw const MobileScannerException(  
224 - errorCode: MobileScannerErrorCode.permissionDenied, 225 + throw MobileScannerException(
  226 + errorCode: MobileScannerErrorCode.genericError,
  227 + errorDetails: MobileScannerErrorDetails(
  228 + code: error.code,
  229 + details: error.details as Object?,
  230 + message: error.message,
  231 + ),
225 ); 232 );
226 } 233 }
227 234
228 break; 235 break;
229 - case MobileScannerState.denied:  
230 - isStarting = false;  
231 - throw const MobileScannerException(  
232 - errorCode: MobileScannerErrorCode.permissionDenied,  
233 - );  
234 case MobileScannerState.authorized: 236 case MobileScannerState.authorized:
235 break; 237 break;
236 } 238 }