flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
163.09k stars 26.83k forks source link

Add to App fragment with SurfaceView does not apply in Android even after setSecure to true #147960

Open CaptainIanOnDuty opened 1 month ago

CaptainIanOnDuty commented 1 month ago

Steps to reproduce

  1. Create a sample Android app and add the FLAG_SECURE in onCreate() method under MainActivity
  2. Extends the FlutterFragment and override onFlutterSurfaceViewCreated which setSecure to true
  3. Following https://docs.flutter.dev/add-to-app/android/add-flutter-fragment?tab=add-fragment-java to add Flutter screen using the FlutterFragment created in step 2
  4. Start the screen sharing
  5. Launch the app
  6. Launch the Flutter screen

Expected results

Flutter screen not shared and displayed black screen

Actual results

Can see the Flutter screen which is not guarded by FLAG_SECURE or setSecure(). Native Android screen is guarded as expected. If I use TextureView instead of SurfaceView, this issue will not appear. The guess is that setSecure should be set before attach, but at the point onFlutterSurfaceViewCreated is executed, the view is already attached.

Code sample

Code sample Create following class ```dart public class OurFlutterFragment extends FlutterFragment { @Override public void onFlutterSurfaceViewCreated(@NonNull FlutterSurfaceView flutterSurfaceView) { flutterSurfaceView.setSecure(true); } } ``` Following this to add Flutter screen into Android app https://docs.flutter.dev/add-to-app/android/add-flutter-fragment?tab=add-fragment-java#add-a-flutterfragment-to-an-activity-with-a-new-flutterengine But replace ```dart flutterFragment = FlutterFragment.createDefault(); ``` with ```dart flutterFragment = OurFlutterFragment.createDefault(); ```

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-arm64, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [!] Xcode - develop for iOS and macOS (Xcode 15.3) ! CocoaPods 1.11.3 out of date (1.13.0 is recommended). CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side. Without CocoaPods, plugins will not work on iOS or macOS. For more info, see https://flutter.dev/platform-plugins To upgrade see https://guides.cocoapods.org/using/getting-started.html#updating-cocoapods for instructions. [✓] Chrome - develop for the web [✓] Android Studio (version 2021.2) [✓] IntelliJ IDEA Ultimate Edition (version 2022.3) [✓] VS Code (version 1.72.2) [✓] Connected device (2 available) [✓] Network resources

! Doctor found issues in 1 category.

huycozy commented 1 month ago

Thanks for the report. I can reproduce this issue using a complete sample code here: repro_147960.

When applying flutterSurfaceView.setSecure(true) to FlutterFragment, it will display a back screen only. Without enabling secure, flutter fragment displays normally.

Maybe related: https://github.com/flutter/flutter/issues/32082

flutter doctor -v (stable and master) ```bash [✓] Flutter (Channel stable, 3.19.6, on macOS 14.1 23B74 darwin-x64, locale en-VN) • Flutter version 3.19.6 on channel stable at /Users/huynq/Documents/GitHub/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 54e66469a9 (31 hours ago), 2024-04-17 13:08:03 -0700 • Engine revision c4cd48e186 • Dart version 3.3.4 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode15.3.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app/ • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) [✓] VS Code (version 1.88.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.86.0 [✓] Connected device (3 available) • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 11 (API 30) • macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.124 [✓] Network resources • All expected network resources are available. • No issues found! ``` ```bash [!] Flutter (Channel master, 3.22.0-25.0.pre.5, on macOS 14.1 23B74 darwin-x64, locale en-VN) • Flutter version 3.22.0-25.0.pre.5 on channel master at /Users/huynq/Documents/GitHub/flutter_master ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 3a837bd584 (72 minutes ago), 2024-05-06 23:03:25 -0400 • Engine revision b7bfd94af7 • Dart version 3.5.0 (build 3.5.0-131.0.dev) • DevTools version 2.35.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode15.3.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app/ • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) [✓] VS Code (version 1.88.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.86.0 [✓] Connected device (3 available) • iPhone (mobile) • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios • iOS 15.8 19H370 • macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.119 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ```
Liangwenb commented 1 month ago
You can try adding a transparent view to the upper layer.   
public void setSecure(boolean isSecure) {
        if (activity == null) {
            Log.d("FlutterForbidshotPlugin", "activity ==null");
            return;
        }
        if (isSecure) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        } else {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
        }
        ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
        View flag_secure = viewGroup.findViewWithTag("FLAG_SECURE");
        try {
            if (isSecure) {
                if (flag_secure == null) {
                    flag_secure = new View(activity);
                    flag_secure.setTag("FLAG_SECURE");
                    viewGroup.addView(flag_secure, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
                }
            } else {
                viewGroup.removeView(flag_secure);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }