This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camerax] Add system services to plugin #6986
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e168785
Add base code from proof of concept
camsim99 b923bc2
Add pigeon types
camsim99 ec1b618
Make corrections, add Dart tests
camsim99 e552ef4
Add test files
camsim99 b1e00b1
Add java tests
camsim99 15914af
Add docs
camsim99 077a044
Add stop, docs, fix analyze
camsim99 1710f7a
Merge remote-tracking branch 'upstream/main' into camx_s2
camsim99 0c47f13
Fix comment
camsim99 7a9f1f1
Add comment and remove literals
camsim99 26a7924
Merge remote-tracking branch 'upstream/main' into camx_s2
camsim99 2b08668
Formatting
camsim99 02a939a
Remove merge conflict reside
camsim99 fe7a6ca
Fix test
camsim99 7811444
Fix bad pasting job
camsim99 934b5fb
Merge remote-tracking branch 'upstream/main' into camx_s2
camsim99 855a6c5
Merge remote-tracking branch 'upstream/main' into camx_s2
camsim99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/camera/camera_android_camerax/android/src/main/AndroidManifest.xml
This file contains hidden or 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,3 +1,8 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="io.flutter.plugins.camerax"> | ||
<uses-feature android:name="android.hardware.camera.any" /> | ||
<uses-permission android:name="android.permission.CAMERA" /> | ||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" | ||
android:maxSdkVersion="28" /> | ||
</manifest> |
This file contains hidden or 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
120 changes: 120 additions & 0 deletions
120
...id_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraPermissionsManager.java
This file contains hidden or 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,120 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camerax; | ||
|
||
import android.Manifest; | ||
import android.Manifest.permission; | ||
import android.app.Activity; | ||
import android.content.pm.PackageManager; | ||
import androidx.annotation.VisibleForTesting; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
|
||
final class CameraPermissionsManager { | ||
interface PermissionsRegistry { | ||
@SuppressWarnings("deprecation") | ||
void addListener( | ||
io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener handler); | ||
} | ||
|
||
interface ResultCallback { | ||
void onResult(String errorCode, String errorDescription); | ||
} | ||
|
||
/** | ||
* Camera access permission errors handled when camera is created. See {@code MethodChannelCamera} | ||
* in {@code camera/camera_platform_interface} for details. | ||
*/ | ||
private static final String CAMERA_PERMISSIONS_REQUEST_ONGOING = | ||
"CameraPermissionsRequestOngoing"; | ||
|
||
private static final String CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE = | ||
"Another request is ongoing and multiple requests cannot be handled at once."; | ||
private static final String CAMERA_ACCESS_DENIED = "CameraAccessDenied"; | ||
private static final String CAMERA_ACCESS_DENIED_MESSAGE = "Camera access permission was denied."; | ||
private static final String AUDIO_ACCESS_DENIED = "AudioAccessDenied"; | ||
private static final String AUDIO_ACCESS_DENIED_MESSAGE = "Audio access permission was denied."; | ||
|
||
private static final int CAMERA_REQUEST_ID = 9796; | ||
@VisibleForTesting boolean ongoing = false; | ||
|
||
void requestPermissions( | ||
Activity activity, | ||
PermissionsRegistry permissionsRegistry, | ||
boolean enableAudio, | ||
ResultCallback callback) { | ||
if (ongoing) { | ||
callback.onResult( | ||
CAMERA_PERMISSIONS_REQUEST_ONGOING, CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE); | ||
return; | ||
} | ||
if (!hasCameraPermission(activity) || (enableAudio && !hasAudioPermission(activity))) { | ||
permissionsRegistry.addListener( | ||
new CameraRequestPermissionsListener( | ||
(String errorCode, String errorDescription) -> { | ||
ongoing = false; | ||
callback.onResult(errorCode, errorDescription); | ||
})); | ||
ongoing = true; | ||
ActivityCompat.requestPermissions( | ||
activity, | ||
enableAudio | ||
? new String[] {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO} | ||
: new String[] {Manifest.permission.CAMERA}, | ||
CAMERA_REQUEST_ID); | ||
} else { | ||
// Permissions already exist. Call the callback with success. | ||
callback.onResult(null, null); | ||
} | ||
} | ||
|
||
private boolean hasCameraPermission(Activity activity) { | ||
return ContextCompat.checkSelfPermission(activity, permission.CAMERA) | ||
== PackageManager.PERMISSION_GRANTED; | ||
} | ||
|
||
private boolean hasAudioPermission(Activity activity) { | ||
return ContextCompat.checkSelfPermission(activity, permission.RECORD_AUDIO) | ||
== PackageManager.PERMISSION_GRANTED; | ||
} | ||
|
||
@VisibleForTesting | ||
@SuppressWarnings("deprecation") | ||
static final class CameraRequestPermissionsListener | ||
implements io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener { | ||
|
||
// There's no way to unregister permission listeners in the v1 embedding, so we'll be called | ||
// duplicate times in cases where the user denies and then grants a permission. Keep track of if | ||
// we've responded before and bail out of handling the callback manually if this is a repeat | ||
// call. | ||
boolean alreadyCalled = false; | ||
|
||
final ResultCallback callback; | ||
|
||
@VisibleForTesting | ||
CameraRequestPermissionsListener(ResultCallback callback) { | ||
this.callback = callback; | ||
} | ||
|
||
@Override | ||
public boolean onRequestPermissionsResult(int id, String[] permissions, int[] grantResults) { | ||
if (alreadyCalled || id != CAMERA_REQUEST_ID) { | ||
return false; | ||
} | ||
|
||
alreadyCalled = true; | ||
// grantResults could be empty if the permissions request with the user is interrupted | ||
// https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[]) | ||
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) { | ||
callback.onResult(CAMERA_ACCESS_DENIED, CAMERA_ACCESS_DENIED_MESSAGE); | ||
} else if (grantResults.length > 1 && grantResults[1] != PackageManager.PERMISSION_GRANTED) { | ||
callback.onResult(AUDIO_ACCESS_DENIED, AUDIO_ACCESS_DENIED_MESSAGE); | ||
} else { | ||
callback.onResult(null, null); | ||
} | ||
return true; | ||
} | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as CameraPermissions.java in the old plugin.