Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Platform resolved locale and Android localization refactor #18645

Merged
merged 54 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e53a7c2
Initial implementation
GaryQian May 28, 2020
22eaecd
Slight cleanup
GaryQian May 28, 2020
765223d
Begin JNI + synchronous impl
GaryQian May 30, 2020
08ef1bb
Dart binding
GaryQian May 30, 2020
354880c
Dart->native impl
GaryQian Jun 1, 2020
3dd7d22
Compiles
GaryQian Jun 2, 2020
7d2f6d9
Initial compiling impl of full android path
GaryQian Jun 2, 2020
3245d00
Working Dart-native interop
GaryQian Jun 3, 2020
42d0717
Working android codepath
GaryQian Jun 4, 2020
674fe7b
Initial iOS impl
GaryQian Jun 4, 2020
e1c2a37
support scriptcode and incomplete locales/resolutions
GaryQian Jun 5, 2020
bd0c590
Working android code path
GaryQian Jun 5, 2020
22817b4
Roughly working iOS implementation
GaryQian Jun 6, 2020
917c74a
Improved iOS locale handling
GaryQian Jun 9, 2020
4329b6a
CLeanup, renaming, comments
GaryQian Jun 9, 2020
87e0b1e
Basic test, additional cleanup
GaryQian Jun 9, 2020
16d6e23
Improved android JNI, analyzer
GaryQian Jun 9, 2020
244869f
Analyzer
GaryQian Jun 9, 2020
75d68d3
Java formatting
GaryQian Jun 9, 2020
4eb0445
More formatting, temp remove test
GaryQian Jun 10, 2020
d8bc98b
Version gate locale bulder
GaryQian Jun 10, 2020
3640a87
More version gating, formatting, licenses
GaryQian Jun 10, 2020
9719562
un-static compute locale method
GaryQian Jun 10, 2020
31f9df1
Formatting
GaryQian Jun 10, 2020
dd25156
Fix ios test compilation, fix NPE
GaryQian Jun 10, 2020
746db4a
Fix Fuchsia tests
GaryQian Jun 10, 2020
2ca3b1f
Fix import
GaryQian Jun 10, 2020
8e75734
Fix formatting
GaryQian Jun 10, 2020
8dc8980
Fix tests
GaryQian Jun 10, 2020
a7e7124
Fix tests
GaryQian Jun 10, 2020
cfb0ec1
Compiles for iOS
GaryQian Jun 10, 2020
7c4679f
New test
GaryQian Jun 11, 2020
74eb79e
Remove extra imports
GaryQian Jun 11, 2020
27220ed
Formatting
GaryQian Jun 11, 2020
b454f09
Formatting
GaryQian Jun 11, 2020
4c896de
Basic request path test, fix infinite loop
GaryQian Jun 11, 2020
6db66f7
Kick tests
GaryQian Jun 11, 2020
15e38d2
API gate test, commends:
GaryQian Jun 11, 2020
99c8627
Imports
GaryQian Jun 11, 2020
6d5deb8
add new tests
GaryQian Jun 11, 2020
d707d13
More tests
GaryQian Jun 11, 2020
be46c42
Cleanup etra code
GaryQian Jun 11, 2020
131be16
Typo
GaryQian Jun 11, 2020
5d7eefd
Merge with JNI refactor
GaryQian Jun 12, 2020
04861c0
Formatting
GaryQian Jun 12, 2020
2011c7e
More formatting
GaryQian Jun 12, 2020
8faaa4c
Merge, address comments
GaryQian Jun 15, 2020
4418471
FlutterEngine constructs plugin
GaryQian Jun 15, 2020
f0859fd
Fix null analyzer:
GaryQian Jun 15, 2020
8eb352c
Fix formatting
GaryQian Jun 15, 2020
5711cf8
Fix test mocking
GaryQian Jun 15, 2020
c0e78de
Fix test
GaryQian Jun 15, 2020
12c2bf0
Typo
GaryQian Jun 15, 2020
3135b0c
Merge with upstream
GaryQian Jun 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/StringCod
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/editing/FlutterTextUtils.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/localization/LocalizationPlugin.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/mouse/MouseCursorPlugin.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/platform/AccessibilityEventsDelegate.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java
Expand Down
28 changes: 28 additions & 0 deletions lib/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,34 @@ class Window {
Locale? get platformResolvedLocale => _platformResolvedLocale;
Locale? _platformResolvedLocale;

/// Performs the platform-native locale resolution.
///
/// Each platform may return different results.
///
/// If the platform fails to resolve a locale, then this will return null.
///
/// This method returns synchronously and is a direct call to
/// platform specific APIs without invoking method channels.
Locale? computePlatformResolvedLocale(List<Locale> supportedLocales) {
final List<String?> supportedLocalesData = <String?>[];
for (Locale locale in supportedLocales) {
supportedLocalesData.add(locale.languageCode);
supportedLocalesData.add(locale.countryCode);
supportedLocalesData.add(locale.scriptCode);
}

final List<String> result = _computePlatformResolvedLocale(supportedLocalesData);

if (result.isNotEmpty) {
return Locale.fromSubtags(
languageCode: result[0],
countryCode: result[1] == '' ? null : result[1],
scriptCode: result[2] == '' ? null : result[2]);
}
return null;
}
List<String> _computePlatformResolvedLocale(List<String?> supportedLocalesData) native 'Window_computePlatformResolvedLocale';

/// A callback that is invoked whenever [locale] changes value.
///
/// The framework invokes this callback in the same zone in which the
Expand Down
23 changes: 23 additions & 0 deletions lib/ui/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,27 @@ void Window::CompletePlatformMessageResponse(int response_id,
response->Complete(std::make_unique<fml::DataMapping>(std::move(data)));
}

Dart_Handle ComputePlatformResolvedLocale(Dart_Handle supportedLocalesHandle) {
std::vector<std::string> supportedLocales =
tonic::DartConverter<std::vector<std::string>>::FromDart(
supportedLocalesHandle);

std::vector<std::string> results =
*UIDartState::Current()
->window()
->client()
->ComputePlatformResolvedLocale(supportedLocales);

return tonic::DartConverter<std::vector<std::string>>::ToDart(results);
}

static void _ComputePlatformResolvedLocale(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle result =
ComputePlatformResolvedLocale(Dart_GetNativeArgument(args, 1));
Dart_SetReturnValue(args, result);
}

void Window::RegisterNatives(tonic::DartLibraryNatives* natives) {
natives->Register({
{"Window_defaultRouteName", DefaultRouteName, 1, true},
Expand All @@ -437,6 +458,8 @@ void Window::RegisterNatives(tonic::DartLibraryNatives* natives) {
{"Window_reportUnhandledException", ReportUnhandledException, 2, true},
{"Window_setNeedsReportTimings", SetNeedsReportTimings, 2, true},
{"Window_getPersistentIsolateData", GetPersistentIsolateData, 1, true},
{"Window_computePlatformResolvedLocale", _ComputePlatformResolvedLocale,
2, true},
});
}

Expand Down
3 changes: 3 additions & 0 deletions lib/ui/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class WindowClient {
int64_t isolate_port) = 0;
virtual void SetNeedsReportTimings(bool value) = 0;
virtual std::shared_ptr<const fml::Mapping> GetPersistentIsolateData() = 0;
virtual std::unique_ptr<std::vector<std::string>>
ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) = 0;

protected:
virtual ~WindowClient();
Expand Down
13 changes: 13 additions & 0 deletions lib/web_ui/lib/src/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,19 @@ abstract class Window {
/// See [locales], which is the list of locales the user/device prefers.
Locale/*?*/ get platformResolvedLocale;

/// Performs the platform-native locale resolution.
///
/// Each platform may return different results.
///
/// If the platform fails to resolve a locale, then this will return null.
///
/// This method returns synchronously and is a direct call to
/// platform specific APIs without invoking method channels.
Locale computePlatformResolvedLocale(List<Locale> supportedLocales) {
// TODO(garyq): Implement on web.
return null;
}

/// A callback that is invoked whenever [locale] changes value.
///
/// The framework invokes this callback in the same zone in which the
Expand Down
7 changes: 7 additions & 0 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ RuntimeController::GetPersistentIsolateData() {
return persistent_isolate_data_;
}

// |WindowClient|
std::unique_ptr<std::vector<std::string>>
RuntimeController::ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) {
return client_.ComputePlatformResolvedLocale(supported_locale_data);
}

Dart_Port RuntimeController::GetMainPort() {
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
return root_isolate ? root_isolate->main_port() : ILLEGAL_PORT;
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ class RuntimeController final : public WindowClient {
// |WindowClient|
std::shared_ptr<const fml::Mapping> GetPersistentIsolateData() override;

// |WindowClient|
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) override;

FML_DISALLOW_COPY_AND_ASSIGN(RuntimeController);
};

Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class RuntimeDelegate {

virtual void SetNeedsReportTimings(bool value) = 0;

virtual std::unique_ptr<std::vector<std::string>>
ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) = 0;

protected:
virtual ~RuntimeDelegate();
};
Expand Down
5 changes: 5 additions & 0 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,11 @@ void Engine::UpdateIsolateDescription(const std::string isolate_name,
delegate_.UpdateIsolateDescription(isolate_name, isolate_port);
}

std::unique_ptr<std::vector<std::string>> Engine::ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) {
return delegate_.ComputePlatformResolvedLocale(supported_locale_data);
}

void Engine::SetNeedsReportTimings(bool needs_reporting) {
delegate_.SetNeedsReportTimings(needs_reporting);
}
Expand Down
23 changes: 23 additions & 0 deletions shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
/// collected and send back to Dart.
///
virtual void SetNeedsReportTimings(bool needs_reporting) = 0;

//--------------------------------------------------------------------------
/// @brief Directly invokes platform-specific APIs to compute the
/// locale the platform would have natively resolved to.
///
/// @param[in] supported_locale_data The vector of strings that represents
/// the locales supported by the app.
/// Each locale consists of three
/// strings: languageCode, countryCode,
/// and scriptCode in that order.
///
/// @return A vector of 3 strings languageCode, countryCode, and
/// scriptCode that represents the locale selected by the
/// platform. Empty strings mean the value was unassigned. Empty
/// vector represents a null locale.
///
virtual std::unique_ptr<std::vector<std::string>>
ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) = 0;
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -765,6 +784,10 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
void UpdateIsolateDescription(const std::string isolate_name,
int64_t isolate_port) override;

// |RuntimeDelegate|
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) override;

void SetNeedsReportTimings(bool value) override;

void StopAnimator();
Expand Down
8 changes: 8 additions & 0 deletions shell/common/platform_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,12 @@ void PlatformView::SetNextFrameCallback(const fml::closure& closure) {
delegate_.OnPlatformViewSetNextFrameCallback(closure);
}

std::unique_ptr<std::vector<std::string>>
PlatformView::ComputePlatformResolvedLocales(
const std::vector<std::string>& supported_locale_data) {
std::unique_ptr<std::vector<std::string>> out =
std::make_unique<std::vector<std::string>>();
return out;
}

} // namespace flutter
38 changes: 38 additions & 0 deletions shell/common/platform_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ class PlatformView {
///
virtual void OnPlatformViewMarkTextureFrameAvailable(
int64_t texture_id) = 0;

//--------------------------------------------------------------------------
/// @brief Directly invokes platform-specific APIs to compute the
/// locale the platform would have natively resolved to.
///
/// @param[in] supported_locale_data The vector of strings that represents
/// the locales supported by the app.
/// Each locale consists of three
/// strings: languageCode, countryCode,
/// and scriptCode in that order.
///
/// @return A vector of 3 strings languageCode, countryCode, and
/// scriptCode that represents the locale selected by the
/// platform. Empty strings mean the value was unassigned. Empty
/// vector represents a null locale.
///
virtual std::unique_ptr<std::vector<std::string>>
ComputePlatformViewResolvedLocale(
const std::vector<std::string>& supported_locale_data) = 0;
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -543,6 +562,25 @@ class PlatformView {
///
void MarkTextureFrameAvailable(int64_t texture_id);

//--------------------------------------------------------------------------
/// @brief Directly invokes platform-specific APIs to compute the
/// locale the platform would have natively resolved to.
///
/// @param[in] supported_locale_data The vector of strings that represents
/// the locales supported by the app.
/// Each locale consists of three
/// strings: languageCode, countryCode,
/// and scriptCode in that order.
///
/// @return A vector of 3 strings languageCode, countryCode, and
/// scriptCode that represents the locale selected by the
/// platform. Empty strings mean the value was unassigned. Empty
/// vector represents a null locale.
///
virtual std::unique_ptr<std::vector<std::string>>
ComputePlatformResolvedLocales(
const std::vector<std::string>& supported_locale_data);

protected:
PlatformView::Delegate& delegate_;
const TaskRunners task_runners_;
Expand Down
13 changes: 13 additions & 0 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,19 @@ void Shell::SetNeedsReportTimings(bool value) {
needs_report_timings_ = value;
}

// |Engine::Delegate|
std::unique_ptr<std::vector<std::string>> Shell::ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) {
return ComputePlatformViewResolvedLocale(supported_locale_data);
}

// |PlatformView::Delegate|
std::unique_ptr<std::vector<std::string>>
Shell::ComputePlatformViewResolvedLocale(
const std::vector<std::string>& supported_locale_data) {
return platform_view_->ComputePlatformResolvedLocales(supported_locale_data);
}

void Shell::ReportTimings() {
FML_DCHECK(is_setup_);
FML_DCHECK(task_runners_.GetRasterTaskRunner()->RunsTasksOnCurrentThread());
Expand Down
8 changes: 8 additions & 0 deletions shell/common/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ class Shell final : public PlatformView::Delegate,
// |PlatformView::Delegate|
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override;

// |PlatformView::Delegate|
std::unique_ptr<std::vector<std::string>> ComputePlatformViewResolvedLocale(
const std::vector<std::string>& supported_locale_data) override;

// |Animator::Delegate|
void OnAnimatorBeginFrame(fml::TimePoint frame_target_time) override;

Expand Down Expand Up @@ -517,6 +521,10 @@ class Shell final : public PlatformView::Delegate,
// |Engine::Delegate|
void SetNeedsReportTimings(bool value) override;

// |Engine::Delegate|
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale(
const std::vector<std::string>& supported_locale_data) override;

// |Rasterizer::Delegate|
void OnFrameRasterized(const FrameTiming&) override;

Expand Down
1 change: 1 addition & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ android_java_sources = [
"io/flutter/plugin/editing/FlutterTextUtils.java",
"io/flutter/plugin/editing/InputConnectionAdaptor.java",
"io/flutter/plugin/editing/TextInputPlugin.java",
"io/flutter/plugin/localization/LocalizationPlugin.java",
"io/flutter/plugin/mouse/MouseCursorPlugin.java",
"io/flutter/plugin/platform/AccessibilityEventsDelegate.java",
"io/flutter/plugin/platform/PlatformPlugin.java",
Expand Down
Loading