Skip to content

Commit aef94b7

Browse files
authored
Reland "Updated background execution implementation for Android" (flutter#5954)
* Reland "Updated background execution implementation for Android" w/ JNI fixes for merge breakages This reverts commit 5442c0a.
1 parent 3421bca commit aef94b7

14 files changed

+333
-115
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/platform/Platfor
497497
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
498498
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java
499499
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java
500+
FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterCallbackInformation.java
501+
FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterRunArguments.java
500502
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterCallbackCache.h
501503
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h
502504
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache.mm

shell/platform/android/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ java_library("flutter_shell_java") {
133133
"io/flutter/util/PathUtils.java",
134134
"io/flutter/util/Preconditions.java",
135135
"io/flutter/view/AccessibilityBridge.java",
136+
"io/flutter/view/FlutterCallbackInformation.java",
136137
"io/flutter/view/FlutterMain.java",
137138
"io/flutter/view/FlutterNativeView.java",
139+
"io/flutter/view/FlutterRunArguments.java",
138140
"io/flutter/view/FlutterView.java",
139141
"io/flutter/view/ResourceCleaner.java",
140142
"io/flutter/view/ResourceExtractor.java",

shell/platform/android/android_shell_holder.cc

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,51 @@ namespace shell {
2323

2424
AndroidShellHolder::AndroidShellHolder(
2525
blink::Settings settings,
26-
fml::jni::JavaObjectWeakGlobalRef java_object)
26+
fml::jni::JavaObjectWeakGlobalRef java_object,
27+
bool is_background_view)
2728
: settings_(std::move(settings)), java_object_(java_object) {
2829
static size_t shell_count = 1;
2930
auto thread_label = std::to_string(shell_count++);
3031

3132
FML_CHECK(pthread_key_create(&thread_destruct_key_, ThreadDestructCallback) ==
3233
0);
3334

34-
thread_host_ = {thread_label, ThreadHost::Type::UI | ThreadHost::Type::GPU |
35-
ThreadHost::Type::IO};
35+
if (is_background_view) {
36+
thread_host_ = {thread_label, ThreadHost::Type::UI};
37+
} else {
38+
thread_host_ = {thread_label, ThreadHost::Type::UI | ThreadHost::Type::GPU |
39+
ThreadHost::Type::IO};
40+
}
3641

3742
// Detach from JNI when the UI and GPU threads exit.
3843
auto jni_exit_task([key = thread_destruct_key_]() {
3944
FML_CHECK(pthread_setspecific(key, reinterpret_cast<void*>(1)) == 0);
4045
});
4146
thread_host_.ui_thread->GetTaskRunner()->PostTask(jni_exit_task);
42-
thread_host_.gpu_thread->GetTaskRunner()->PostTask(jni_exit_task);
47+
if (!is_background_view) {
48+
thread_host_.gpu_thread->GetTaskRunner()->PostTask(jni_exit_task);
49+
}
4350

4451
fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
4552
Shell::CreateCallback<PlatformView> on_create_platform_view =
46-
[java_object, &weak_platform_view](Shell& shell) {
47-
auto platform_view_android = std::make_unique<PlatformViewAndroid>(
48-
shell, // delegate
49-
shell.GetTaskRunners(), // task runners
50-
java_object, // java object handle for JNI interop
51-
shell.GetSettings()
52-
.enable_software_rendering // use software rendering
53-
);
53+
[is_background_view, java_object, &weak_platform_view](Shell& shell) {
54+
std::unique_ptr<PlatformViewAndroid> platform_view_android;
55+
if (is_background_view) {
56+
platform_view_android = std::make_unique<PlatformViewAndroid>(
57+
shell, // delegate
58+
shell.GetTaskRunners(), // task runners
59+
java_object // java object handle for JNI interop
60+
);
61+
62+
} else {
63+
platform_view_android = std::make_unique<PlatformViewAndroid>(
64+
shell, // delegate
65+
shell.GetTaskRunners(), // task runners
66+
java_object, // java object handle for JNI interop
67+
shell.GetSettings()
68+
.enable_software_rendering // use software rendering
69+
);
70+
}
5471
weak_platform_view = platform_view_android->GetWeakPtr();
5572
return platform_view_android;
5673
};
@@ -62,13 +79,26 @@ AndroidShellHolder::AndroidShellHolder(
6279
// The current thread will be used as the platform thread. Ensure that the
6380
// message loop is initialized.
6481
fml::MessageLoop::EnsureInitializedForCurrentThread();
65-
66-
blink::TaskRunners task_runners(
67-
thread_label, // label
68-
fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform
69-
thread_host_.gpu_thread->GetTaskRunner(), // gpu
70-
thread_host_.ui_thread->GetTaskRunner(), // ui
71-
thread_host_.io_thread->GetTaskRunner() // io
82+
fml::RefPtr<fml::TaskRunner> gpu_runner;
83+
fml::RefPtr<fml::TaskRunner> ui_runner;
84+
fml::RefPtr<fml::TaskRunner> io_runner;
85+
fml::RefPtr<fml::TaskRunner> platform_runner =
86+
fml::MessageLoop::GetCurrent().GetTaskRunner();
87+
if (is_background_view) {
88+
auto single_task_runner = thread_host_.ui_thread->GetTaskRunner();
89+
gpu_runner = single_task_runner;
90+
ui_runner = single_task_runner;
91+
io_runner = single_task_runner;
92+
} else {
93+
gpu_runner = thread_host_.gpu_thread->GetTaskRunner();
94+
ui_runner = thread_host_.ui_thread->GetTaskRunner();
95+
io_runner = thread_host_.io_thread->GetTaskRunner();
96+
}
97+
blink::TaskRunners task_runners(thread_label, // label
98+
platform_runner, // platform
99+
gpu_runner, // gpu
100+
ui_runner, // ui
101+
io_runner // io
72102
);
73103

74104
shell_ =
@@ -131,10 +161,12 @@ void AndroidShellHolder::Launch(RunConfiguration config) {
131161
fml::MakeCopyable([engine = shell_->GetEngine(), //
132162
config = std::move(config) //
133163
]() mutable {
134-
if (engine) {
135-
if (!engine->Run(std::move(config))) {
136-
FML_LOG(ERROR) << "Could not launch engine in configuration.";
137-
}
164+
FML_LOG(INFO) << "Attempting to launch engine configuration...";
165+
if (!engine || !engine->Run(std::move(config))) {
166+
FML_LOG(ERROR) << "Could not launch engine in configuration.";
167+
} else {
168+
FML_LOG(INFO) << "Isolate for engine configuration successfully "
169+
"started and run.";
138170
}
139171
}));
140172
}

shell/platform/android/android_shell_holder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace shell {
2121
class AndroidShellHolder {
2222
public:
2323
AndroidShellHolder(blink::Settings settings,
24-
fml::jni::JavaObjectWeakGlobalRef java_object);
24+
fml::jni::JavaObjectWeakGlobalRef java_object,
25+
bool is_background_view);
2526

2627
~AndroidShellHolder();
2728

shell/platform/android/io/flutter/app/FlutterActivityDelegate.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.flutter.util.Preconditions;
3131
import io.flutter.view.FlutterMain;
3232
import io.flutter.view.FlutterNativeView;
33+
import io.flutter.view.FlutterRunArguments;
3334
import io.flutter.view.FlutterView;
3435

3536
import java.util.ArrayList;
@@ -162,19 +163,16 @@ public void onCreate(Bundle savedInstanceState) {
162163
}
163164
}
164165

165-
// When an activity is created for the first time, we direct the
166-
// FlutterView to re-use a pre-existing Isolate rather than create a new
167-
// one. This is so that an Isolate coming in from the ViewFactory is
168-
// used.
169-
final boolean reuseIsolate = true;
170-
171-
if (loadIntent(activity.getIntent(), reuseIsolate)) {
166+
if (loadIntent(activity.getIntent())) {
172167
return;
173168
}
174169
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
175170
String appBundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
176171
if (appBundlePath != null) {
177-
flutterView.runFromBundle(appBundlePath, null, "main", reuseIsolate);
172+
FlutterRunArguments arguments = new FlutterRunArguments();
173+
arguments.bundlePath = appBundlePath;
174+
arguments.entrypoint = "main";
175+
flutterView.runFromBundle(arguments);
178176
}
179177
}
180178
}
@@ -325,11 +323,6 @@ private static String[] getArgsFromIntent(Intent intent) {
325323
}
326324

327325
private boolean loadIntent(Intent intent) {
328-
final boolean reuseIsolate = false;
329-
return loadIntent(intent, reuseIsolate);
330-
}
331-
332-
private boolean loadIntent(Intent intent, boolean reuseIsolate) {
333326
String action = intent.getAction();
334327
if (Intent.ACTION_RUN.equals(action)) {
335328
String route = intent.getStringExtra("route");
@@ -343,7 +336,10 @@ private boolean loadIntent(Intent intent, boolean reuseIsolate) {
343336
flutterView.setInitialRoute(route);
344337
}
345338
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
346-
flutterView.runFromBundle(appBundlePath, null, "main", reuseIsolate);
339+
FlutterRunArguments args = new FlutterRunArguments();
340+
args.bundlePath = appBundlePath;
341+
args.entrypoint = "main";
342+
flutterView.runFromBundle(args);
347343
}
348344
return true;
349345
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.view;
6+
7+
/**
8+
* A class representing information for a callback registered using
9+
* `PluginUtilities` from `dart:ui`.
10+
*/
11+
public final class FlutterCallbackInformation {
12+
final public String callbackName;
13+
final public String callbackClassName;
14+
final public String callbackLibraryPath;
15+
16+
/**
17+
* Get callback information for a given handle.
18+
* @param handle the handle for the callback, generated by
19+
* `PluginUtilities.getCallbackHandle` in `dart:ui`.
20+
* @return an instance of FlutterCallbackInformation for the provided handle.
21+
*/
22+
public static FlutterCallbackInformation lookupCallbackInformation(long handle) {
23+
return nativeLookupCallbackInformation(handle);
24+
}
25+
26+
private FlutterCallbackInformation(String callbackName,
27+
String callbackClassName, String callbackLibraryPath) {
28+
this.callbackName = callbackName;
29+
this.callbackClassName = callbackClassName;
30+
this.callbackLibraryPath = callbackLibraryPath;
31+
}
32+
33+
private static native FlutterCallbackInformation nativeLookupCallbackInformation(long handle);
34+
}

0 commit comments

Comments
 (0)