This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from mintware-de/merge_2.x_in_3.x
Merge changes of 2.x into 3.x
- Loading branch information
Showing
12 changed files
with
230 additions
and
129 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
54 changes: 48 additions & 6 deletions
54
android/src/main/kotlin/de/mintware/barcode_scan/ActivityHelper.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 |
---|---|---|
@@ -1,41 +1,83 @@ | ||
package de.mintware.barcode_scan | ||
|
||
import android.Manifest | ||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.util.Log | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
import io.flutter.plugin.common.EventChannel | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.PluginRegistry | ||
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener | ||
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener | ||
|
||
class ActivityHelper(private var applicationContext: Context?, | ||
var activity: Activity? = null | ||
) : PluginRegistry.ActivityResultListener { | ||
) : ActivityResultListener, RequestPermissionsResultListener { | ||
|
||
companion object { | ||
const val TAG = "BarcodeScanPlugin" | ||
const val REQ_START_SCAN = 100 | ||
const val REQ_CAMERA_PERMISSION = 200 | ||
} | ||
|
||
private val resultMap: HashMap<Int, ActivityResultHandler> = linkedMapOf() | ||
private val activityResultMap: HashMap<Int, ActivityResultListener> = linkedMapOf() | ||
private val permissionResultMap: HashMap<Int, RequestPermissionsResultListener> = linkedMapOf() | ||
|
||
fun showScannerActivity(result: MethodChannel.Result, config: Protos.Configuration) { | ||
if (activity == null) { | ||
Log.d(TAG, "Could not launch BarcodeScannerActivity because the plugin is not attached to any activity") | ||
return | ||
} | ||
|
||
resultMap[REQ_START_SCAN] = ScanResultHandler(result) | ||
activityResultMap[REQ_START_SCAN] = ScanResultHandler(result) | ||
|
||
val intent = Intent(applicationContext, BarcodeScannerActivity::class.java) | ||
intent.putExtra(BarcodeScannerActivity.EXTRA_CONFIG, config.toByteArray()) | ||
activity!!.startActivityForResult(intent, REQ_START_SCAN) | ||
} | ||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean { | ||
if (!resultMap.containsKey(requestCode)) { | ||
if (!activityResultMap.containsKey(requestCode)) { | ||
return false | ||
} | ||
|
||
return resultMap.getValue(requestCode).handle(requestCode, resultCode, data) | ||
return activityResultMap | ||
.getValue(requestCode) | ||
.onActivityResult(requestCode, resultCode, data) | ||
} | ||
|
||
fun requestCameraAccessIfNecessary(sink: EventChannel.EventSink? | ||
): Boolean { | ||
if (activity == null) { | ||
Log.d(TAG, "Could not launch BarcodeScannerActivity because the plugin is not attached to any activity") | ||
return false | ||
} | ||
|
||
permissionResultMap[REQ_CAMERA_PERMISSION] = RequestCameraPermissionHandler(sink) | ||
|
||
val array = arrayOf(Manifest.permission.CAMERA) | ||
if (ContextCompat.checkSelfPermission(activity!!, Manifest.permission.CAMERA) | ||
== PackageManager.PERMISSION_GRANTED) { | ||
return false | ||
} | ||
|
||
ActivityCompat.requestPermissions(activity!!, array, REQ_CAMERA_PERMISSION) | ||
return true | ||
} | ||
|
||
override fun onRequestPermissionsResult(requestCode: Int, | ||
permissions: Array<out String>?, | ||
grantResults: IntArray? | ||
): Boolean { | ||
if (!permissionResultMap.containsKey(requestCode)) { | ||
return false | ||
} | ||
|
||
return permissionResultMap | ||
.getValue(requestCode) | ||
.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
android/src/main/kotlin/de/mintware/barcode_scan/ActivityResultHandler.kt
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.