Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flutter app crashes after setting desktop wallpaper on android 14 phone #148286

Open
chenxiuhui318 opened this issue May 14, 2024 · 7 comments
Open
Labels
in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds

Comments

@chenxiuhui318
Copy link

chenxiuhui318 commented May 14, 2024

Steps to reproduce

1.Online picture download
2.flutter calls native Settings wallpaper

Expected results

The wallpaper is set successfully and the application does not crash

Actual results

Setting the wallpaper succeeded, but the application crashed

Code sample

dart:

static Future<String> lockScreen(
      {String imageName = "myimage",
        double width = 0,
        double height = 0,
        RequestSizeOptions options = RequestSizeOptions.RESIZE_EXACT,
        DownloadLocation location = DownloadLocation.TEMPORARY_DIRECTORY}) async {
    final String resultvar = await _channel.invokeMethod('LockScreen', {
      'maxWidth': width,
      'maxHeight': height,
      'RequestSizeOptions': options.index,
      'location': location.index,
      'imageName': imageName
    });
    return resultvar;
  }

android native:

public String setHomeScreen() {
        try {
            wallpaperManager = WallpaperManager.getInstance(this);
            File file = GetFile(location, this, imageName);
            if (file != null && file.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                int result = wallpaperManager.setBitmap(bitmap, null, true, FLAG_SYSTEM);
                if (result > 0) {
                    return "success";
                } else {
                    return "failure";
                }
            } else {
                return "failure";
            }

        } catch (Exception exception) {
            return exception.toString();
        }
    }

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

2024-05-17 13:29:27.450 32182-32182 WallpaperManager        com.vision.haokan                    D  Wallpaper set completion.
2024-05-17 13:29:27.451 32182-32182 ANR_LOG                 com.vision.haokan                    E  Entry{target='android.os.Handler', callback='DartMessenger$$ExternalSyntheticLambda0', messageName='android.os.Handler', wall=2300', mWhen=152423101, mArg1=0, mArg2=0, mWhat=0, mCount=0
2024-05-17 13:29:27.841 32182-32182 VRI[MainActivity]       com.vision.haokan                    D  setWindowStopped stopped:true
2024-05-17 13:29:27.841 32182-375   flutter                 com.vision.haokan                    I  应用处于后台-----
2024-05-17 13:29:27.852 32182-32182 BLASTBufferQueue        com.vision.haokan                    D  [SurfaceView[com.vision.haokan/com.vision.haokan.host.MainActivity]#5](f:0,a:3) destructor()
2024-05-17 13:29:27.852 32182-32182 BufferQueueConsumer     com.vision.haokan                    D  [SurfaceView[com.vision.haokan/com.vision.haokan.host.MainActivity]#5(BLAST Consumer)5](id:7db600000005,api:0,p:-1,c:32182) disconnect
2024-05-17 13:29:27.867 32182-32182 FLUTTER OTA             com.vision.haokan                    D  onDetachedFromActivityForConfigChanges
2024-05-17 13:29:27.871 32182-32182 FLUTTER OTA             com.vision.haokan                    D  onDetachedFromEngine

Flutter Doctor output

[✓] Flutter (Channel stable, 3.19.4, on macOS 14.2.1 23C71 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.19.4 on channel stable at /Users/haokan-cxh/Desktop/soft/flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 68bfaea224 (8 weeks ago), 2024-03-20 15:36:31 -0700
    • Engine revision a5c24f538d
    • Dart version 3.3.2
    • DevTools version 2.31.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/haokan-cxh/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    ! CocoaPods 1.12.1 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
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • 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
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.86.2)
    • VS Code at /Users/haokan-cxh/Downloads/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (5 available)
    • RMX3710 (mobile)           • PJGICQI7NF4LYDKF          • android-arm64  • Android 14 (API 34)
    • macOS (desktop)            • macos                     • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
    • Chrome (web)               • chrome                    • web-javascript • Google Chrome 124.0.6367.158
    ! Error: Browsing on the local area network for Haokan 的iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 14, 2024
@darshankawar
Copy link
Member

@chenxiuhui318
In order to properly address the issue, please provide us complete reproducible code sample that we can directly copy paste and use to verify this further.
Also, since you mentioned android 14, is there a way for you to check if the reported behavior occurs on < 14 versions as well or not ?
Also, check if there's any Android 14 setting or config required related to setting desktop wallpaper in Android documentation if this is specific to OS 14.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 14, 2024
@chenxiuhui318
Copy link
Author

I have tested Android 11-13 without problems, only on two Android 14 should crash, see the phenomenon is the application restart

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 15, 2024
@chenxiuhui318
Copy link
Author

This is the dart code:

Stream progressString = imageDownloadProgress(url ?? '');
progressString.listen((data) {
print("downloadImageAndSetWallpaper DataReceived: $data");
}, onDone: () async {

  String isSuccess = '';
  if (type == 1) {
    isSuccess = await lockScreen(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        options: RequestSizeOptions.RESIZE_CENTRE_CROP);
  } else {
    isSuccess = await homeScreen(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        options: RequestSizeOptions.RESIZE_CENTRE_CROP);
  }

  EasyLoading.dismiss();
}, onError: (error) {
  print("Some Error");
});


 static Stream<String> imageDownloadProgress(String url,
  {String imageName = 'myimage',
    DownloadLocation location =
        DownloadLocation.TEMPORARY_DIRECTORY}) async* {
StreamController<String> streamController = StreamController();
try {
  var dir;
  switch (location) {
    case DownloadLocation.APPLICATION_DIRECTORY:
      dir = await getApplicationSupportDirectory();
      break;
    case DownloadLocation.EXTERNAL_DIRECTORY:
      dir = await getExternalStorageDirectory();
      break;
    case DownloadLocation.TEMPORARY_DIRECTORY:
    default:
      dir = await getTemporaryDirectory();
      break;
  }
  Dio dio = Dio();
  dio.download(url, "${dir.path}/" + imageName + ".jpeg",
    onReceiveProgress: (int received, int total) {
      streamController
          .add(((received / total) * 100).toStringAsFixed(0) + "%");
    },
  )
      .then((Response response) {})
      .catchError((ex) {
    streamController.add(ex.toString());
  })
      .whenComplete(() {
    streamController.close();
  });
  yield* streamController.stream;
} catch (ex) {
  print(ex.toString());
}

}

@chenxiuhui318
Copy link
Author

This is Android native code:

@OverRide
public void configureFlutterEngine(@nonnull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
Log.e("MainActivity", "configureFlutterEngine ");
new Handler(getMainLooper()).post(new Runnable() {
@OverRide
public void run() {
MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL);
channel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@OverRide
public void onMethodCall(@nonnull MethodCall call, @nonnull MethodChannel.Result result) {

                    switch (call.method) {
                        case "HomeScreen":
                        case "LockScreen":
                        case "Both":
                            maxWidth = call.argument("maxWidth");
                            maxHeight = call.argument("maxHeight");
                            options = call.argument("RequestSizeOptions");
                            location = call.argument("location");
                            imageName = call.argument("imageName");
                            imageName = imageName == null ? "myimage":imageName;

                            switch (call.method) {
                                case "HomeScreen":
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                        result.success(setHomeScreen());
                                    }
                                    break;
                                case "LockScreen":
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                        result.success(setLockScreen());
                                    }
                                    break;
                                default:
                                    result.notImplemented();
                                    break;
                            }
                            break;
                        default:
                            result.notImplemented();
                    }
                }
            });
        }
    });
}

@RequiresApi(api = Build.VERSION_CODES.N)
public String setHomeScreen() {
    try {
        wallpaperManager = WallpaperManager.getInstance(this);
        File file = new File(getCacheDir(), imageName + ".jpeg")
        if (file != null && file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            int result = wallpaperManager.setBitmap(bitmap, null, true, FLAG_SYSTEM);
            if (result > 0) {
                return "success";
            } else {
                return "failure";
            }
        } else {
            return "failure";
        }

    } catch (Exception exception) {
        return exception.toString();
    }
}

@RequiresApi(api = Build.VERSION_CODES.N)
public String setLockScreen() {
    try {
        wallpaperManager = WallpaperManager.getInstance(this);
        File file = new File(getCacheDir(), imageName + ".jpeg")
        if (file != null && file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            int result = wallpaperManager.setBitmap(bitmap, null, true, FLAG_LOCK);
            if (result > 0) {
                return "success";
            } else {
                return "failure";
            }
        } else {
            return "failure";
        }

    } catch (Exception exception) {
        return exception.toString();
    }
}

@darshankawar
Copy link
Member

If this occurs only on Android 14, then it could be related to the changes made by Android and may not be specific to Flutter. See a related / similar issue with underlying comments from team and check if it helps in your case or not.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 16, 2024
@chenxiuhui318
Copy link
Author

But the same native Android code, in the pure native Android project to set the wallpaper is no problem

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 16, 2024
@darshankawar
Copy link
Member

@chenxiuhui318
I tried using the code sample you provided, but I am unable to replicate or run it properly, that's why we requested you to provide us a running repo that w can clone and verify directly at our end.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds
Projects
None yet
Development

No branches or pull requests

2 participants